0
\$\begingroup\$

I'm a young beginner in PHP and all. I'm just trying to make a fun game while learning by myself so you will easily spot errors.

The code is working, yet it need some serious improvements as this is the worst part of my site. I will list my problems with the matching part of the code.

Optimization over my attack code

Here's the code, I'll detail it after:

    if (isset($_POST['fdd_attackbas2']) AND ($infomonstre["fightm_cdbas2"] < 1)) {
        $query = $db->prepare('UPDATE users SET vieac=(vieac - :degats ) WHERE username=:username');
        $query->bindValue(':username', $username, PDO::PARAM_INT);
        $query->bindValue(':degats', $degatreduiteffet, PDO::PARAM_INT);
        $query->execute();
        $query->CloseCursor();
        $query = $db->prepare('UPDATE fightmonster SET 
        fightm_life=(fightm_life - :atqb),
        fightm_dmgperso=:atqb,  
        fightm_dmgenemy=:degats, 
        fightm_cdfuite=GREATEST(0, fightm_cdfuite - 1), 
        fightm_cdbas2=(fightm_cdbas2 + 1), 
        fightm_cdbas1=GREATEST(0, fightm_cdbas1 - 1), 
        fightm_cdbas3=GREATEST(0, fightm_cdbas3 - 1), 
        fightm_cdrare1=GREATEST(0, fightm_cdrare1 - 1), 
        fightm_cdrare2=GREATEST(0, fightm_cdrare2 - 1), 
        fightm_cdultime=GREATEST(0, fightm_cdultime - 1)
        WHERE player_id =:id');
        $query->bindParam(':id', $donnees['id'], PDO::PARAM_INT);
        $query->bindValue(':degats', $degatreduiteffet, PDO::PARAM_INT);
        $query->bindValue(':atqb', $atkboosteffet, PDO::PARAM_INT);
        $query->execute();
        $query->CloseCursor();
        header("Refresh:0");
        exit();
}
if (isset($_POST['fdd_attackbas3']) AND ($infomonstre["fightm_cdbas3"] < 1)) {
        $query = $db->prepare('UPDATE users SET vieac=(vieac - :degats ) WHERE username=:username');
        $query->bindValue(':username', $username, PDO::PARAM_INT);
        $query->bindValue(':degats', $degatreduiteffet, PDO::PARAM_INT);
        $query->execute();
        $query->CloseCursor();
        $query = $db->prepare('UPDATE fightmonster SET 
        fightm_life=(fightm_life - :atqb),
        fightm_dmgperso=:atqb,  
        fightm_dmgenemy=:degats, 
        fightm_cdfuite=GREATEST(0, fightm_cdfuite - 1), 
        fightm_cdbas3=(fightm_cdbas3 + 1), 
        fightm_cdbas2=GREATEST(0, fightm_cdbas2 - 1), 
        fightm_cdbas1=GREATEST(0, fightm_cdbas1 - 1), 
        fightm_cdrare1=GREATEST(0, fightm_cdrare1 - 1), 
        fightm_cdrare2=GREATEST(0, fightm_cdrare2 - 1), 
        fightm_cdultime=GREATEST(0, fightm_cdultime - 1)
        WHERE player_id =:id');
        $query->bindParam(':id', $donnees['id'], PDO::PARAM_INT);
        $query->bindValue(':degats', $degatreduiteffet, PDO::PARAM_INT);
        $query->bindValue(':atqb', $atkboosteffet, PDO::PARAM_INT);
        $query->execute();
        $query->CloseCursor();
        header("Refresh:0");
        exit();
}

Here's what the code is doing:

  • If the user chose "Attack 2" and the CD of "Attack 2" is lower than 1:
    • The user take damages from the monster
    • The monster in "fightmonster" take damage and stock the damage done by both sides
  • Lower the cooldown of every other attack except the attack the user did
  • Refresh the page to let the user know.
  • And so on, the code repeat except for the CD if user chose "Attack 3"

What I would like to do with this part

What I would like is an array consisting of the attacks (fdd_attackbas1, fdd_attackbas2, fdd_attackbas3, fdd_attackrare1, fdd_attackrare2, fdd_attackult) and a foreach to stop repeating the code over and over for each attack.

$attacks = array("fdd_attackbas1", "fdd_attackbas2", "fdd_attackbas3", "fdd_attackrare1, "fdd_attackrare2", "fdd_attackultime");
foreach ($attacks as $attack) {

        // MULTIPLICATOR OF THE ATTACK
        if isset($_POST['fdd_attackrare1']) {
        $atkboosteffet = $atkboosteffet * $infofdd["fdd_rare1mult"]; }
        if isset($_POST['fdd_attackrare2']) {
        $atkboosteffet = $atkboosteffet * $infofdd["fdd_rare2mult"]; }
        if isset($_POST['fdd_attackultime']) {
        $atkboosteffet = $atkboosteffet * $infofdd["fdd_ultmult"]; }

        $query = $db->prepare('UPDATE users SET vieac=(vieac - :degats ) WHERE username=:username');
        $query->bindValue(':username', $username, PDO::PARAM_INT);
        $query->bindValue(':degats', $degatreduiteffet, PDO::PARAM_INT);
        $query->execute();
        $query->CloseCursor();
        $query = $db->prepare('UPDATE fightmonster SET 
        fightm_life=(fightm_life - :atqb),
        fightm_dmgperso=:atqb,  
        fightm_dmgenemy=:degats, 
        // COOLDOWN //
        fightm_cdfuite=GREATEST(0, fightm_cdfuite - 1), 
        fightm_cdbas1=GREATEST(0, fightm_cdbas1 - 1), 
        fightm_cdbas2=GREATEST(0, fightm_cdbas2 - 1), 
        fightm_cdbas3=GREATEST(0, fightm_cdbas3 - 1), 
        fightm_cdrare1=GREATEST(0, fightm_cdrare1 - 1), 
        fightm_cdrare2=GREATEST(0, fightm_cdrare2 - 1), 
        fightm_cdultime=GREATEST(0, fightm_cdultime - 1),
        // ++ CD TO THE ATTACK I CHOSE //
        WHERE player_id =:id');
        $query->bindParam(':id', $donnees['id'], PDO::PARAM_INT);
        $query->bindValue(':degats', $degatreduiteffet, PDO::PARAM_INT);
        $query->bindValue(':atqb', $atkboosteffet, PDO::PARAM_INT);
        $query->execute();
        $query->CloseCursor();
        header("Refresh:0");
        exit();
    endforeach; }

Multiplication of the attack

If the user chose the attack Rare1, Rare2 or Ultime, the code will check in the table "fdd" the value of "fdd_ultmult" if this is the "Ultime" attack and so on. Then, it will be multiplication by our attack. For example, if my attack is 3 and I attack with the attack "Rare2" which got a x3 multiplicator, i will do 9 damage.

Cooldown

The cooldown is made in a way that it will reduce by 1 the cooldown of every attack by 1 (without going under 0) BUT I need a way to know which attack was used to add the cooldown to the attack. By the way, basics attacks have 1 turn CD, Rares have 3 and Ultimate got 8.

\$\endgroup\$
  • \$\begingroup\$ You have a mismatched double-quote in the first line of your second code block. Please verify that your code works, and that you have posted the code as intended. \$\endgroup\$ – 200_success Oct 1 '17 at 6:21
  • \$\begingroup\$ so when iterating over the values in $attacks, as soon as one of the matching values in $_POST is set and there is a corresponding value in $infomonstre["fightm_cdbasY"] (where Y is the integer at the end of the array element in $attacks) less than 1 it should run the code in the loop (i.e. run two db queries) and then exit/return/redirect? \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Oct 1 '17 at 20:39
1
\$\begingroup\$

Almost any time you use variables like fdd_attackbas1, fdd_attackbas2, fdd_attackbas3, and fightm_cdbas1, fightm_cdbas2, fightm_cdbas3, you really want to use arrays and loops.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.