PHP - any easy way to make this math equation?

Viewed 49

I have a game where you can buy defence, but my math doesn't add up and I don't understand how I am suppossed to solve it.

Amount from   amount to       Price pr
0             10 000          $1 500 
10 000        50 000          $2 500
50 000        1 000 000       $3 000
1 000 000     5 000 000       $5 000
5 000 000     inf             $10 000

$fp_from[0] = 0;
$fp_to[0] = 10000;

$fp_from[1] = $fp_to[0];
$fp_to[1] = 50000;

$fp_from[2] = $fp_to[1];
$fp_to[2] = 1000000;

$fp_from[3] = $fp_to[2];
$fp_to[3] = 5000000;

$fp_from[4] = $fp_to[3];
$fp_to[4] = INF;

$price_pr[0] = 1500;
$price_pr[1] = 2500;
$price_pr[2] = 3000;
$price_pr[3] = 5000;
$price_pr[4] = 10000;

For now I just have this for loop to get the price:

for($k = 0; $k < count($fp_from); $k++){
    if($defence >= $fp_from[$k] && $defence <= $fp_to[$k]){
        $my_price_pr = $price_pr[$k];
    }
}

But say I have 1 000 defence, and buy 10 000 I will have 11 000. But the price for the 10 000 is only 1 500 pr, but I want the amount up to 10 000 to be 1 500, and over be 2 500. The price that I get is 15 000 000 ( 10 000 * 1 500 ), but the price I want is 16 000 000 ( 13 500 000 + 2 500 000 ).

This is my whole buying script

if(isset($_POST['post'])){
    if(isset($_POST['amount'])){
        $amount = remove_space($_POST['amount']);
        
        $defence = $AS_session_row['AS_def'];
        $my_price_pr = 0;
        $money = 100000000; // 100 000 000

        for($k = 0; $k < count($fp_from); $k++){
            if($defence >= $fp_from[$k] && $defence <= $fp_to[$k]){
                $my_price_pr = $price_pr[$k];
            }
        }
        
        if(is_numeric($amount) && $amount > 0){
            if($my_price_pr * $amount <= $money){
                $total_price = $my_price_pr * $amount;
                
                give_fp($amount, $_SESSION['ID'], $pdo);
                take_money($_SESSION['ID'], $total_price, $pdo);
                
                echo success_msg("FP was bought");
            } else {
                echo error_msg("Not enough money");
            }
        } else {
            echo fail_msg("Wrong input");
        }
    }
}
1 Answers

I would suggest in looking into foreach loop https://www.php.net/manual/en/control-structures.foreach.php they are quite useful when dealing with arrays.

EDIT: You need to increment the total_price while looping through prices as well take into consideration defense level before and after it is bought. Afterwards you can use your checks if the person has enough money and so on...

$defence = 0;
$my_price_pr = 0;

$fp_from[0] = 0;
$fp_to[0] = 10000;

$fp_from[1] = $fp_to[0];
$fp_to[1] = 50000;

$fp_from[2] = $fp_to[1];
$fp_to[2] = 1000000;

$fp_from[3] = $fp_to[2];
$fp_to[3] = 5000000;

$fp_from[4] = $fp_to[3];
$fp_to[4] = INF;

$price_pr[0] = 1500;
$price_pr[1] = 2500;
$price_pr[2] = 3000;
$price_pr[3] = 5000;
$price_pr[4] = 10000;


$total_price = 0;

$amount = 11000;

$my_price_pr = 0;



$defenceAfterBuying = $defence + $amount;

foreach ($price_pr as $key => $value) {
    for ($i=$defence; $i <= $defenceAfterBuying; $i++) { 
        if ($i > $fp_from[$key] && $i <= $fp_to[$key]) {
            $total_price += $price_pr[$key];
        }
    }
}
Related