tags:

views:

173

answers:

5

Here is my problem. In my game I am trying to implement this leveling system where once you reached a certain amount of experience that you advance to a higher level. I am using php 5.0 and the Latest MYSQL. My problem is that i cannot get my scripts to execute properly.

SO here are some dummy stats followed by my code Any information that would help me solve my problems would be greatly appreciated

     $playerNAME = "Bozo";
 $playerClASS = "Warrior";
 $playerLEVEL = 13;
 $playerSTR = 5;
 $playerDEF = 2;
 $playerDEX = 3;
 $playerMAG = 2;
     $playerEXPERIENCE = 99999;

when I go to levelup from 13 to 14 none of the stats are updated with a new value and the level dosnt update... Yes all my connections from the connect.php to the DB server are working properly and yes i am using the correct table names in my code

function levelUPSTATS () { global $playerNAME; global $playerClASS; global $playerLEVEL; global $playerSTR; global $playerDEF; global $playerDEX; global $playerMAG;

if ($playerCLASS === "Warrior") {
 $playerSTR = $playerSTR + 3;
 $playerDEF = $playerDEF + 2;
 $playerDEX = $playerDEX + 3;
 $playerMAG = $playerMAG + 2;
 $playerBASE_DAMAGE = ceil($playerSTR*$playerDEX);
 $playerSPELL_BASE_DAMAGE = ceil($playerMAG * $playerDEX);
 $playerMAX_HEALTH_POINTS = ceil($playerSTR * $playerDEF * $playerDEX);
 $playerMAX_MANA_POINTS = ceil($playerMAG * $playerDEF * $playerDEX / $playerSTR);
 $statusplayerSTR = "update players set strength=strength+'$playerSTR' where username='$playerNAME'";
 mysql_query($statusplayerSTR) or die("Could not update player");

 $statusplayerDEF = "update players set defence=defence+'$playerDEF' where username='$playerNAME'";
 mysql_query($statusplayerDEF) or die("Could not update player");

 $statusplayerDEX = "update players set dexterity=dexterity+'$playerDEX' where username='$playerNAME'";
 mysql_query($statusplayerDEX) or die("Could not update player");

 $statusplayerMAG = "update players set magic=magic+'$playerMAG' where username='$playerNAME'";
 mysql_query($statusplayerMAG) or die("Could not update player");

 $statusplayerBASE_DAMAGE = "update players set basedamage=basedamage+'$playerBASE_DAMAGE' where username='$playerNAME'";
 mysql_query($statusplayerBASE_DAMAGE) or die("Could not update player");

 $statusplayerSPELL_BASE_DAMAGE = "update players set spellbasedamage=spellbasedamage+'$playerSPELL_BASE_DAMAGE' where username='$playerNAME'";
 mysql_query($statusplayerSPELL_BASE_DAMAGE) or die("Could not update player");

 $statusplayerHealthPOINTS = "update players set healthpoints='$playerMAX_HEALTH_POINTS' where username='$playerNAME'";
 mysql_query($statusplayerHealthPOINTS) or die("Could not update player");

 $statusplayerMaxHealthPOINTS = "update players set maxhealthpoints='$playerMAX_HEALTH_POINTS' where username='$playerNAME'";
 mysql_query($statusplayerMaxHealthPOINTS) or die("Could not update player");

 $statusplayerManaPOINTS = "update players set manapoints='$playerMAX_MANA_POINTS' where username='$playerNAME'";
 mysql_query($statusplayerManaPOINTS) or die("Could not update player");

 $statusplayerMaxManaPOINTS = "update players set maxmanapoints='$playerMAX_MANA_POINTS' where username='$playerNAME'";
 mysql_query($statusplayerMaxManaPOINTS) or die("Could not update player");
} elseif ($playerCLASS === "Mage") {
 $playerSTR = $playerSTR + 2;
 $playerDEF = $playerDEF + 2;
 $playerDEX = $playerDEX + 3;
 $playerMAG = $playerMAG + 3;
 $playerBASE_DAMAGE = ceil($playerSTR * $playerDEF);
 $playerSPELL_BASE_DAMAGE = ceil($playerMAG * $playerDEX);
 $playerMAX_HEALTH_POINTS = ceil($playerDEF * $playerDEX * $playerMAG);
 $playerMAX_MANA_POINTS = ceil($playerMAG * $playerDEF * $playerDEX / $playerSTR);   
} else {
}

}

 if ($playerEXPERIENCE < 100) {
  if (!$playerLEVEL ==1) {
   $playerLEVEL = 1;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");

  } else {
   $playerNEXT_LEVEL = 100;
  }
 } elseif ($playerEXPERIENCE >= 100 && $playerEXPERIENCE <= 200) {
  if (!$playerLEVEL ==2) {
                            levelUPSTATS ();
   $playerLEVEL = 2;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 200;
  }
 } elseif ($playerEXPERIENCE >= 200 && $playerEXPERIENCE <= 400) {
  if (!$playerLEVEL ==3) {
                            levelUPSTATS ();
   $playerLEVEL = 3;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 400;
  }
 } elseif ($playerEXPERIENCE >= 400 && $playerEXPERIENCE <= 800) {
  if (!$playerLEVEL ==4) {
                            levelUPSTATS ();
   $playerLEVEL = 4;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 800;
  }
 } elseif ($playerEXPERIENCE >= 800 && $playerEXPERIENCE <= 1600) {
  if (!$playerLEVEL ==5) {
                            levelUPSTATS ();
   $playerLEVEL = 5;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 1600;
  }
 } elseif ($playerEXPERIENCE >= 1600 && $playerEXPERIENCE <= 3200) {
  if (!$playerLEVEL ==6) {

                            levelUPSTATS ();
   $playerLEVEL = 6;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 3200;
  }
 } elseif ($playerEXPERIENCE >= 3200 && $playerEXPERIENCE <= 6400) {
  if (!$playerLEVEL ==7) {
                            levelUPSTATS ();
   $playerLEVEL = 7;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 6400;
  }
 } elseif ($playerEXPERIENCE >= 6400 && $playerEXPERIENCE <= 12800) {
  if (!$playerLEVEL ==8) {
                            levelUPSTATS ();
   $playerLEVEL = 8;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 12800;
  }
 } elseif ($playerEXPERIENCE >= 12800 && $playerEXPERIENCE <= 25000) {
  if (!$playerLEVEL ==9) {
                            levelUPSTATS ();
   $playerLEVEL = 9;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 25000;
  }
 } elseif ($playerEXPERIENCE >= 25000 && $playerEXPERIENCE <= 35000) {
  if (!$playerLEVEL ==10) {
                            levelUPSTATS ();
   $playerLEVEL = 10;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");

  } else {
   $playerNEXT_LEVEL = 35000;
  }
 } elseif ($playerEXPERIENCE >= 35000 && $playerEXPERIENCE <= 50000) {
  if (!$playerLEVEL ==11) {
                            levelUPSTATS ();
   $playerLEVEL = 11;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 50000;
  }
 } elseif ($playerEXPERIENCE >= 50000 && $playerEXPERIENCE <= 75000) {
  if (!$playerLEVEL ==12) {
                            levelUPSTATS ();
   $playerLEVEL = 12;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 75000;
  }
 } elseif ($playerEXPERIENCE >= 75000 && $playerEXPERIENCE <= 100000) {
  if (!$playerLEVEL ==13) {
                            levelUPSTATS ();
   $playerLEVEL = 13;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 100000;
  }
 } elseif ($playerEXPERIENCE >= 100000 && $playerEXPERIENCE <= 135000) {
  if (!$playerLEVEL == 14) {
   levelUPSTATS ();
   $playerLEVEL = 14;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 135000;
  }
 } elseif ($playerEXPERIENCE >= 135000 && $playerEXPERIENCE <= 200000) {
  if (!$playerLEVEL ==15) {
                            levelUPSTATS ();
   $playerLEVEL = 15;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 200000;
   }
 } elseif ($playerEXPERIENCE >= 200000 && $playerEXPERIENCE <= 300000) {
  if (!$playerLEVEL ==16) {

                            levelUPSTATS ();
   $playerLEVEL = 16;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 300000;
   }
 } elseif ($playerEXPERIENCE >= 300000 && $playerEXPERIENCE <= 420000) {
  if (!$playerLEVEL ==17) {
                            levelUPSTATS ();
   $playerLEVEL = 17;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 420000;
   }
 } elseif ($playerEXPERIENCE >= 420000 && $playerEXPERIENCE <= 600000) {
  if (!$playerLEVEL ==18) {
                            levelUPSTATS ();
   $playerLEVEL = 18;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 600000;
   }
 } elseif ($playerEXPERIENCE >= 600000 && $playerEXPERIENCE <= 800000) {
  if (!$playerLEVEL ==19) {
                            levelUPSTATS ();
   $playerLEVEL = 19;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 800000;
   }
 } elseif ($playerEXPERIENCE >= 800000 && $playerEXPERIENCE <= 1000000) {
  if (!$playerLEVEL ==20) {
                            levelUPSTATS ();
   $playerLEVEL = 20;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  } else {
   $playerNEXT_LEVEL = 1000000;
   }
 } elseif ($playerEXPERIENCE >= 1000000) {
  if (!$playerLEVEL ==21) {
                            levelUPSTATS ();
   $playerLEVEL = 21;
                            $statusplayerLEVELUPDATE = "update players set level='$playerLEVEL' where username='$playerNAME'";
 mysql_query($statusplayerLEVELUPDATE ) or die("Could not update player");
  }
 } else {
  print "N/A";
 }

problem is that it isn't display any error message.

It wont update the stats properly the level doesn't update.

A: 

Bozo and Warrior aren't enclosed in quotes in the first code snippet

Charlie Somerville
A: 

Looking at the syntax highlighting of your code, it's clear somethings wrong around the level 14 code. Looks like you added 2 extra lines for no reason:

playerLEVEL' where username='$playerNAME'";
mysql_query($statusplayerSTR) or die("Could not update player");

You should really try to make sure the code isn't copy-pasted as much, and also use a editor with syntax highlighting if you aren't already.

danamlund
i manage to remove that in my personal script but it still does not effect the code performs. and i am using the Crimson Editor.
Jeremy
Beware SQL injection
Charlie Somerville
About the SQL injection their is no way for a player to input any data into this, it is all done with php and the information is grabbed from the DB so the player doesn't know the variables or that there is a connection even though we all know their is :D
Jeremy
+2  A: 

Have you tested the queries actually work with the values you are giving them? ie, use phpMyAdmin or similar and try the query manually?

I have had situations where I thought my PHP was incorrect but a small error in my SQL was the problem.

You might also want to set PHP's error mode to E_ALL. You can do that in php.ini or through the code by having

ini_set("display_errors","2"); ERROR_REPORTING(E_ALL);

at the start of your script. It should [hopefully] give you an error that identifies your problem.


EDIT

I just noticed something with your conditions that may be the problem...

You have:

if (!$playerLEVEL ==13) 
{
    levelUPSTATS ();
    $playerLEVEL = 13;
    //etc
}

I rather suspect that if block will never run and there will be no call to levelUPSTATS().

You are asking if NOT $playerLEVEL is EQUAL to 13 when you really want if $playerLEVEL is NOT EQUAL to 13 which would make the condition:

if($playerLEVEL != 13)

Note where the ! (NOT) goes.

As an aside, you have a situation with your inequalities where certain edge cases would mean the player is two levels.

For example, for level 2 you need between 100 and 200 EXP inclusive. But level 3 needs 200 and 400 EXP inclusive. If your player has 200EXP he is technically level 2 and 3. When the code runs it will match the 200 in the Level 2 section of code and not the Level 3 section...

Your code:

if($playerEXPERIENCE >= 100 && $playerEXPERIENCE <= 200)
//code to make them 2
if($playerEXPERIENCE >= 200 $$ $playerEXPERIENCE <= 400)
//code to make them 3

That to me seems wrong - though it might be what you intended, I don't know. If it wasn't intended you should change it to:

if($playerEXPERIENCE >= 100 && $playerEXPERIENCE < 200)

Notice the use of < [LESS THAN] instead of <= [LESS THAN OR EQUAL TO].

I hope this solves your problem :)

Peter Spain
Thank for the reply, I have inf act tested all my sql statements and everything seems to be running perfect. I honestly think it is a php mistake that i am overlooking but cannot see so I was looking for outside supportive eyes to help
Jeremy
I started looking at my code just before you posted and i figure out the !=, lol what a stupid but simple mistake. and thank you for your input on the the level checking, I will most definitely make the proper adjustments. And lastly thank you for the php error display code it allowed me to find some of my variables weren't defined properly.
Jeremy
It is very easy to make little mistakes that go unnoticed! Done a few myself :PWhen you have a large amount of code that does something similar, you should write just a small portion of it and see if it works. Once you know it works, then add the rest. For example you probably should have just started with Levels 1, 2 and 3. Once you know the code works then you can expand it to cover more levels. It is easier to test and there is less code to change if/when there is a mistake ;)
Peter Spain
A: 

I am sorry but your code is completely unreadable and that has to be the reason why you don't find the problem.

Try implementing these changes:

  • You do not need to run a different MySQL query for each set action. Your whole 'warrior' section should look something like:
    $statusplayerSTR = "update players set strength=strength+'$playerSTR',
           defence=defence+'$playerDEF',
           dexterity=dexterity+'$playerDEX',
           magic=magic+'$playerMAG',
           basedamage=basedamage+'$playerBASE_DAMAGE',
           spellbasedamage=spellbasedamage+'$playerSPELL_BASE_DAMAGE',
           healthpoints='$playerMAX_HEALTH_POINTS',
           ......
           WHERE username='$playerNAME'"
    mysql_query($statusplayerSTR) or die("Could not update player");
  • Use a switch statement instead of all these IF ELSE statements:

    switch ($playerCLASS) {
       case "Warrior": 
          ....
          break;
       case "Mage": 
          ....
          break;
       case default:
          ....
    }

the $playerEXPERIENCE IF ELSE block

switch (true) {
   case $playerEXPERIENCE < 100:
      ... 
      break;
   case $playerEXPERIENCE >= 100 && $playerEXPERIENCE <= 200:
      ....
}

You can then print out the queries and check them properly using a MySQL client.

Saggi Malachi
Than you for your input, What you have written I didn't know how to do. I am not familiar with Switch statements aswell I Was unaware that you could attempt to send more then one table worth of data per transaction. In my text editor my code is actually very readable and I can understand how it is not due to your comment/answer I will try out your suggestions and who knows I might just get a little more proper with my code thanks to your advice.so again thank you...
Jeremy
A: 

you can try using this. $x = user current exp $userlevel="SELECT * FROM level where exp=(SELECT MAX(exp) FROM level where exp<= $x)"; $userlevel1=mysql_query($userlevel); $userlevelmax=mysql_fetch_array($userlevel1);

then make a table for level where you store your experience needed for each level.

so you doesnt have to repeat so many if else loop and use this instead. replace your numbers with values from table you made.

if($playerstats3['c_exp'] >= $userlevelmax2['exp']) { if($playerstats['level'] != $userlevelmax['level']){ $newlevel=$userlevelmax['level'];

                        $updateuserlevel="update youruserinfotable set level=$newlevel, allurnewstatsforthatlevel where UID='$playerstats3[UID]'";
                     mysql_query($updateuserlevel) or die("It just died");


  }