My Leveling System

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

My Leveling System

Post by MikeD »

Well, need more help..Again :P.

Trying to create my leveling system, and it's just not working.

The basics of it are, if you are level 1-10 it costs 10 exp to level for a skill, 11-20 costs 20 exp, 21-30 costs 30 exp etc.

To keep it user friendly, I tried doing the leveling on the same page. Not a success.

It keeps coming up with "undefined index level" and "undefined variables of $statname and $mod"
Also in the link to level up, I have a GET for the Cost, i'd like to change that as a post, because there could be cheating with it. Problem is idk how to change that to a POST.

Here is my code. (it's long). But you can ignore most of it.

Code: Select all

/////////////////////////////////////////////Speed Level Costs

if ($speed <= 10)
{
$scost=10;
}
if ($speed >= 11)
{
$scost=20;
}
if ($speed >= 21)
{
$scost=30;
}
if ($speed >= 31)
{
$scost=40;
}
if ($speed >= 41)
{
$scost=50;
}
if ($speed >= 51)
{
$scost=60;
}
if ($speed >= 61)
{
$scost=70;
}
if ($speed >= 71)
{
$scost=80;
}
if ($speed >= 81)
{
$scost=90;
}
if ($speed >= 91)
{
$scost=100;
}
if ($speed >= 101)
{
$scost=150;
}

/////////////////////////////////////Power Level Costs
if ($power <= 10)
{
$pcost=10;
}
if ($power >= 11)
{
$pcost=20;
}
if ($power >= 21)
{
$pcost=30;
}
if ($power >= 31)
{
$pcost=40;
}
if ($power >= 41)
{
$pcost=50;
}
if ($power >= 51)
{
$pcost=60;
}
if ($power >= 61)
{
$pcost=70;
}
if ($power >= 71)
{
$pcost=80;
}
if ($power >= 81)
{
$pcost=90;
}
if ($power >= 91)
{
$pcost=100;
}
if ($power >= 101)
{
$pcost=150;
}
////////////////////////////////////////Melee Level Costs
if ($melee <= 10)
{
$mcost=10;
}
if ($melee >= 11)
{
$mcost=20;
}
if ($melee >= 21)
{
$mcost=30;
}
if ($melee >= 31)
{
$mcost=40;
}
if ($melee >= 41)
{
$mcost=50;
}
if ($melee >= 51)
{
$mcost=60;
}
if ($melee >= 61)
{
$mcost=70;
}
if ($melee >= 71)
{
$mcost=80;
}
if ($melee >= 81)
{
$mcost=90;
}
if ($melee >= 91)
{
$mcost=100;
}
if ($melee >= 101)
{
$mcost=150;
}
///////////////////////////////////////////Magic Level Costs
if ($mage <= 10)
{
$magecost=10;
}
if ($mage >= 11)
{
$magecost=20;
}
if ($mage >= 21)
{
$magecost=30;
}
if ($mage >= 31)
{
$magecost=40;
}
if ($mage >= 41)
{
$magecost=50;
}
if ($mage >= 51)
{
$magecost=60;
}
if ($mage >= 61)
{
$magecost=70;
}
if ($mage >= 71)
{
$magecost=80;
}
if ($mage >= 81)
{
$magecost=90;
}
if ($mage >= 91)
{
$magecost=100;
}
if ($mage >= 101)
{
$magecost=150;
}
///////////////////////////////////////////Archery Level Costs
if ($arch <= 10)
{
$archcost=10;
}
if ($arch >= 11)
{
$archcost=20;
}
if ($arch >= 21)
{
$archcost=30;
}
if ($arch >= 31)
{
$archcost=40;
}
if ($arch >= 41)
{
$archcost=50;
}
if ($arch >= 51)
{
$archcost=60;
}
if ($arch >= 61)
{
$archcost=70;
}
if ($arch >= 71)
{
$archcost=80;
}
if ($arch >= 81)
{
$archcost=90;
}
if ($arch >= 91)
{
$archcost=100;
}
if ($arch >= 101)
{
$archcost=150;
}
///////////////////////////////////////////Melee Defense Level Costs
if ($meldef <= 10)
{
$meldefcost=10;
}
if ($meldef >= 11)
{
$meldefcost=20;
}
if ($meldef >= 21)
{
$meldefcost=30;
}
if ($meldef >= 31)
{
$meldefcost=40;
}
if ($meldef >= 41)
{
$meldefcost=50;
}
if ($meldef >= 51)
{
$meldefcost=60;
}
if ($meldef >= 61)
{
$meldefcost=70;
}
if ($meldef >= 71)
{
$meldefcost=80;
}
if ($meldef >= 81)
{
$meldefcost=90;
}
if ($meldef >= 91)
{
$meldefcost=100;
}
if ($meldef >= 101)
{
$meldefcost=150;
}
///////////////////////////////////////////Magic Defense Level Costs
if ($magedef <= 10)
{
$magedefcost=10;
}
if ($magedef >= 11)
{
$magedefcost=20;
}
if ($magedef >= 21)
{
$magedefcost=30;
}
if ($magedef >= 31)
{
$magedefcost=40;
}
if ($magedef >= 41)
{
$magedefcost=50;
}
if ($magedef >= 51)
{
$magedefcost=60;
}
if ($magedef >= 61)
{
$magedefcost=70;
}
if ($magedef >= 71)
{
$magedefcost=80;
}
if ($magedef >= 81)
{
$magedefcost=90;
}
if ($magedef >= 91)
{
$magedefcost=100;
}
if ($magedef >= 101)
{
$magedefcost=150;
}
///////////////////////////////////////////Archery Def Level Costs
if ($archdef <= 10)
{
$archdefcost=10;
}
if ($archdef >= 11)
{
$archdefcost=20;
}
if ($archdef >= 21)
{
$archdefcost=30;
}
if ($archdef >= 31)
{
$archdefcost=40;
}
if ($archdef >= 41)
{
$archdefcost=50;
}
if ($archdef >= 51)
{
$archdefcost=60;
}
if ($archdef >= 61)
{
$archdefcost=70;
}
if ($archdef >= 71)
{
$archdefcost=80;
}
if ($archdef >= 81)
{
$archdefcost=90;
}
if ($archdef >= 91)
{
$archdefcost=100;
}
if ($archdef >= 101)
{
$archdefcost=150;
}
///////////////////////////////////////////Health Level Costs
if ($health <= 10)
{
$healthcost=10;
}
if ($health >= 11)
{
$healthcost=20;
}
if ($health >= 21)
{
$healthcost=30;
}
if ($health >= 31)
{
$healthcost=40;
}
if ($health >= 41)
{
$healthcost=50;
}
if ($health >= 51)
{
$healthcost=60;
}
if ($health >= 61)
{
$healthcost=70;
}
if ($health >= 71)
{
$healthcost=80;
}
if ($health >= 81)
{
$healthcost=90;
}
if ($health >= 91)
{
$healthcost=100;
}
if ($health >= 101)
{
$healthcost=150;
}
///////////////////////////////////////////Agility Level Costs
if ($agility <= 10)
{
$agilitycost=10;
}
if ($agility >= 11)
{
$agilitycost=20;
}
if ($agility >= 21)
{
$agilitycost=30;
}
if ($agility >= 31)
{
$agilitycost=40;
}
if ($agility >= 41)
{
$agilitycost=50;
}
if ($agility >= 51)
{
$agilitycost=60;
}
if ($agility >= 61)
{
$agilitycost=70;
}
if ($agility >= 71)
{
$agilitycost=80;
}
if ($agility >= 81)
{
$agilitycost=90;
}
if ($agility >= 91)
{
$agilitycost=100;
}
if ($agility >= 101)
{
$agilitycost=150;
}

////////////////////////////////Leveling Up

$stat = $_GET['level'];

switch($stat)
 {
 case 'speed':
 $statname = 'speed';
 $mod = 1;
 break;
 case 'power':
 $statname = 'power';
 $mod = 1;
 break;
 case 'melee':
 $statname = 'melee';
 $mod = 1;
 break;
 case 'mage':
 $statname = 'magic';
 $mod = 1;
 break;
 case 'arch':
 $statname = 'archery';
 $mod = 1;
 break;
 case 'meldef':
 $statname = 'meldef';
 $mod = 1;
 break;
 case 'magedef':
 $statname = 'magedef';
 $mod = 1;
 break;
 case 'archdef':
 $statname = 'archdef';
 $mod = 1;
 break;
  case 'health':
 $statname = 'health';
 $mod = 1;
 break;
  case 'agility':
 $statname = 'agility';
 $mod = 1;
 break;
 }
$cost=$_GET['cost'];
if ($cost > $exp)
{
echo "Not enough Experience to Level Up!";
}
else
{
$expused = $cost;
$totalxpused = $xpused + $expused;

$newexp = $exp-$cost;
$newlevel = $total+1/10;
$newlevel = (int)$newlevel;


$updatecharacter="UPDATE `characters` SET `$statname`=`$statname`+'$mod',`level`='$newlevel',`exper`='$newexp',`experused`='$totalxpused',`maxmana`=`magic`*10,`maxhpoints`=`health`*10 WHERE `cid`='$cid'";
mysql_query($updatecharacter) or die("Could not update character");
  print "<p>You have leveled up your Speed!<p>";
}
}

Code: Select all

<table border="3" cellpadding="3" cellspacing="0">
 <tr>
  <th align="center">Skill</th>
  <th align="center">Level</th>
  <th align="center">Level Cost</th>
  <th align="center">Level Up!</th>
 </tr>
 <tr>
  <td align="center"><?php echo "Speed" ?> </td>
  <td align="center"><?php echo " $speed " ?> </td>
  <td align="center"><?php echo " $scost " ?> </td>
<?php
if ($scost > $exp)
{
?>
  <td align="center"><?php echo "Not Enough Exp!" ?> </td>
<?php
}
else
{
?>
  <td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$speed&cost=$scost' />Level Up Speed</a>" ?> </td>
<?php
}
?>
</tr>
<tr>
  <td align="center"><?php echo "Power" ?> </td>
  <td align="center"><?php echo " $power " ?> </td>
  <td align="center"><?php echo " $pcost " ?> </td>
<?php
if ($pcost > $exp)
{
?>
  <td align="center"><?php echo "Not Enough Exp!" ?> </td>
<?php
}
else 
{
?>
  <td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$power&cost=$pcost' />Level Up Power</a>" ?> </td>
<?php
}
?>
</tr>
<tr>
  <td align="center"><?php echo "Melee" ?> </td>
  <td align="center"><?php echo " $melee " ?> </td>
  <td align="center"><?php echo " $mcost " ?> </td>
<?php
if ($mcost > $exp)
{
?>
  <td align="center"><?php echo "Not Enough Exp!" ?> </td>
<?php
}
else
{
?>
  <td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$melee&cost=$mcost' />Level Up Melee</a>" ?> </td>
<?php
}
?>
</tr>
<tr>
  <td align="center"><?php echo "Magic" ?> </td>
  <td align="center"><?php echo " $mage " ?> </td>
  <td align="center"><?php echo " $magecost " ?> </td>
<?php
if ($magecost > $exp)
{
?>
  <td align="center"><?php echo "Not Enough Exp!" ?> </td>
<?php
}
else
{
?>
  <td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$mage&cost=$magecost' />Level Up Mage</a>" ?> </td>
<?php
}
?>
</tr>
<tr>
  <td align="center"><?php echo "Archery" ?> </td>
  <td align="center"><?php echo " $arch " ?> </td>
  <td align="center"><?php echo " $archcost " ?> </td>
<?php
if ($archcost > $exp)
{
?>
  <td align="center"><?php echo "Not Enough Exp!" ?> </td>
<?php
}
else
{
?>
  <td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$arch&cost=$archcost' />Level Up Archery</a>" ?> </td>
<?php
}
?>
</tr>
<tr>
  <td align="center"><?php echo "Melee Defense" ?> </td>
  <td align="center"><?php echo " $meldef " ?> </td>
  <td align="center"><?php echo " $meldefcost " ?> </td>
<?php
if ($meldefcost > $exp)
{
?>
  <td align="center"><?php echo "Not Enough Exp!" ?> </td>
<?php
}
else 
{
?>
  <td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$meldef&cost=$meldefcost' />Level Up Melee Def</a>" ?> </td>
<?php
}
?>
</tr>
<tr>
  <td align="center"><?php echo "Magic Defense" ?> </td>
  <td align="center"><?php echo " $magedef " ?> </td>
  <td align="center"><?php echo " $magedefcost " ?> </td>
<?php
if ($magedefcost > $exp)
{
?>
  <td align="center"><?php echo "Not Enough Exp!" ?> </td>
<?php
}
else
{
?>
  <td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$magedef&cost=$magedefcost' />Level Up Mage Def</a>" ?> </td>
<?php
}
?>
</tr>
<tr>
  <td align="center"><?php echo "Archery Defense" ?> </td>
  <td align="center"><?php echo " $archdef " ?> </td>
  <td align="center"><?php echo " $archdefcost " ?> </td>
<?php
if ($archdefcost > $exp)
{
?>
  <td align="center"><?php echo "Not Enough Exp!" ?> </td>
<?php
}
else 
{
?>
  <td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$archdef&cost=$archdefcost' />Level Up Archery Def</a>" ?> </td>
<?php
}
?>
</tr>
<tr>
  <td align="center"><?php echo "Health" ?> </td>
  <td align="center"><?php echo " $health " ?> </td>
  <td align="center"><?php echo " $healthcost " ?> </td>
<?php
if ($healthcost > $exp)
{
?>
  <td align="center"><?php echo "Not Enough Exp!" ?> </td>
<?php
}
else
{
?>
  <td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$health&cost=$healthcost' />Level Up Health</a>" ?> </td>
<?php
}
?>
</tr>
<tr>
  <td align="center"><?php echo "Agility" ?> </td>
  <td align="center"><?php echo " $agility " ?> </td>
  <td align="center"><?php echo " $agilitycost " ?> </td>
<?php
if ($agilitycost > $exp)
{
?>
  <td align="center"><?php echo "Not Enough Exp!" ?> </td>
<?php
}
else
{
?>
  <td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$agility&cost=$agilitycost' />Level Up Agility</a>" ?> </td>
<?php
}
?>
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: My Leveling System

Post by Chris »

Wow that's a lot of ifs. A switch would have been handier there.. And this definitely isn't something a little maths can't do.

First what you want to do is divide the level by 10, then floor it and multiply it by 10, and add 10.

Code: Select all

function calcXpNeeded($level)
{
    return ((10*floor($level/10))+10);
} 
Another thing. POST will never make you invulnerable to intrusion.. I see POST as a playground to database hacking and injection on most websites. HTML can be easily edited to modify whatever you want..
Fighting for peace is declaring war on war. If you want peace be peaceful.
Loopy

Re: My Leveling System

Post by Loopy »

Mike, I didn't look through all your code, but the two thing stood out to me... too many ifs, and then I saw this line too:

<td align="center"><?php echo "<a href='playwarrior.php?id=$cid&level=$speed&cost=$scost' />Level Up Speed</a>" ?> </td>

If you pass the cost of the level up in the URL, there's nothing stopping a user from defining their own value for the cost. For instance, once parsed, that URL is going to look something like this:

playwarrior.php?id=1&level=32&cost=40

A user can just change the value of cost to whatever, and pay a different amount for the upgrade than you intended.
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: My Leveling System

Post by Chris »

Loopy I thought it was funny you found that the worst part. I found the fact that the character ID is passed through the URL worse. LOL.. just change another players stats...

It's nearly like saying It's worse to be injured than die.

You should be storing the character id in a session.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: My Leveling System

Post by MikeD »

Yeah this whole thing was a mess today.

I realized the cost could be manipulated when I posted it, I'll figure out a different way to that right.

@Chris, thanks for that function, it will work perfectly :D. I already had a check to make sure the character you're trying to use is your own. In my game, you have an account (Which is stored in a session), that you can make characters on and use the characters. Being very new to programming, I am not sure if I can store a character in a session along with the player. I will look into it, because that would be much better.
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: My Leveling System[Solved]

Post by MikeD »

Completely working now, those functions are awesome, thanks again Chris!
Post Reply

Return to “Beginner Help and Support”