php mysql游戏levelup脚本问题

时间:2009-08-08 09:22:56

标签: php mysql game-development

这是我的问题。在我的游戏中,我正在尝试实施这种调平系统,一旦你达到一定的经验,你就会升级到更高的水平。我使用的是PHP 5.0和最新的MYSQL。我的问题是我无法让我的脚本正确执行。

所以这里有一些虚拟统计数据,然后是我的代码任何有助于我解决问题的信息都将不胜感激

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

当我从13到14的levelup时,没有任何统计数据更新为新值和更高级别dosnt ...是的所有我从connect.php到数据库服务器的连接都正常工作,是的我是在我的代码中使用正确的表名

 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";
        }

问题是它没有显示任何错误消息。

它不会正确更新数据,关卡不会更新。

5 个答案:

答案 0 :(得分:3)

您是否测试过查询实际上是否与您提供的值一起使用?即,使用phpMyAdmin或类似的手动尝试查询?

我遇到过我认为我的PHP不正确但我的SQL中出现一个小错误的问题。

您可能还想将PHP的错误模式设置为E_ALL。你可以在php.ini中或通过代码

来实现
ini_set("display_errors","2"); ERROR_REPORTING(E_ALL);

在脚本开头。它应该[希望]给你一个识别问题的错误。


修改

我刚注意到你的情况可能有问题......

你有:

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

我宁愿怀疑if block永远不会运行而且不会调用levelUPSTATS()。

当你真的想要如果$ playerLEVEL不等于13 时,你要求如果NOT $ playerLEVEL等于13 ,这将导致条件:

if($playerLEVEL != 13)

注意(NOT)的位置。

顺便说一下,你有一个不平等的情况,某些边缘情况意味着玩家是两个级别。

例如,对于2级,您需要介于100和200 EXP之间。但是3级需要200和400 EXP。如果你的播放器有200EXP,他在技术上是2级和3级。当代码运行时,它将匹配代码的2级部分中的200而不是3级部分......

您的代码:

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

对我来说这似乎是错的 - 虽然它可能是你的意图,但我不知道。如果不打算将其更改为:

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

请注意使用&lt; [LESS THAN]而不是&lt; = [少于或等于]。

我希望这能解决你的问题:)

答案 1 :(得分:0)

Bozo和Warrior未在第一个代码段中用引号括起来

答案 2 :(得分:0)

查看代码的语法突出显示,14级代码周围的问题显然是错误的。看起来您无缘无故地添加了2条额外的行:

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

您应该尽量确保代码不会被复制粘贴,并且如果您尚未使用语法高亮显示,也可以使用编辑器。

答案 3 :(得分:0)

我很抱歉,但您的代码完全不可读,这就是您找不到问题的原因。

尝试实施这些更改:

  • 您不需要为每个集合操作运行不同的MySQL查询。你的整个'战士'部分应该是这样的:
    $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");
  • 使用switch语句而不是所有这些IF ELSE语句:

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

$ playerEXPERIENCE IF ELSE阻止

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

然后,您可以打印出查询并使用MySQL客户端正确检查它们。

答案 4 :(得分:0)

你可以试试这个。 $ x =用户当前exp $ userlevel =“SELECT * FROM level其中exp =(SELECT MAX(exp)FROM level其中exp&lt; = $ x)”; $ userlevel1 =的mysql_query($用户级); $ userlevelmax = mysql_fetch_array($ userlevel1);

然后创建一个表格,用于存储每个级别所需的体验。

所以你不必重复这么多if if循环而是使用它。用您所制作的表格中的值替换您的数字。

if($ playerstats3 ['c_exp']&gt; = $ userlevelmax2 ['exp'])         {             if($ playerstats ['level']!= $ userlevelmax ['level']){             $ newlevel = $ userlevelmax [ '电平'];

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


        }