Jump to content


Photo

PHP Help?


  • Please log in to reply
8 replies to this topic

#1 entity

entity
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationEngland, UK

Posted 14 May 2006 - 10:42 AM

Hi, I've only just joined; but I've been a part of the PHP phenomena for quite a while now. Well I've been scripting a code for an Online RPG (Adventure Quest); this code allows you to work out stats. Anyway, I've been working quite hard on this code but I have finally come to a full stop on it. My problem is that stats can only be in 5's so 5 all the way up to 175 (unless your a staff member you can get anything as long as it is a multiple of five), my problem is I don't know how to make a validation code that will only allow multiples of five. Any help on this would be greatly appriciated as this is the first code I've written without copying parts here and there.
Thank you very much.

- entity

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 14 May 2006 - 11:05 AM

i dont know your approach on this (how your numbers is sendt) but here is something for you to work on.
It wouldn't surprice me if there is an easier way though...
In this i've assumed that numbers sendt can be any sum of numbers

Test it with page.php?n=105 or whatever
<?

$number = $_GET['n'];
settype($number,"integer");
$max = "175";

if(!empty($number))
{
if($number > $max)
{
  print "The number is higher than $max";
}
else
{
if(substr($number, -1, 1)== "0" OR substr($number, -1, 1)== "5")
{
  print "Approved";
}
else
{
  print "Declined";
}
}
}
else
{
  print "No number is present"; 
  // note that a single 0 in $number will be seen as no number in this case
}
?>


#3 entity

entity
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationEngland, UK

Posted 14 May 2006 - 11:40 AM

Well that is pretty cool, would it help if I posted the code up? I'll do it anyways:

<?php
//
// Stats Calculator
// entity@entitys-arena.co.uk
// MWA HA HA
// Version 1.0
// Melee Attack Equation:
// (DEX / 16) + (STR / 16) + (LUK / 20)
// Ranged Attack Equation:
// (DEX / 8) + (LUK / 20)
// Magic Attack Equation:
// (DEX / 16) + (INT / 16) + (LUK / 20)
// Melee Attacks: STR / 8
// Ranged Attacks: (STR / 16) + (LUK / 20)
// Magic Attacks: INT / 8
//
$MStats = $str + $dex + $int + $end + $cha + $luck;
if(($str == "")||($dex == "")||($int == "")||($end == "")||($cha == "")||($luck == "")||($level == "")) {
echo("Please fill in all sections even if its just a zero ^_^\n");
}
elseif($level > "125") {
echo("You cannot go over the level of 125 at this time.\n");
}
elseif($account == "agx") {
    $account_type = "Adventurer/Guardian/X-Guardian";
    if(($str > "175")||($dex > "175")||($int > "175")||($end > "175")||($cha > "175")||($luck > "175")) {
    echo("Your stats do not go above 175, please go back and correct this.\n");
    }
    elseif($MStats > "625") {
    echo("You will not gain more than 625 stat points unless you are a staff member ¬_¬.\n");
    }
    else{
    // This section calculates your BtH for each weapon type
    // It will also work out BtH for pets ^_^
    // BtH - Str - Melee
    $SBtH = round(($dex / 16) + ($str / 16) + ($luck / 20));
    // BtH - Dex - Ranged
    $DBtH = round(($dex / 8) + ($luck / 20));
    // BtH - Int - Magic
    $IBtH = round(($dex / 16) + ($int / 16) + ($luck / 20));
    // BtH - Cha - Pets
    $CBtH = round($cha / 15 + 5);
    // This section calculates extra random damage gained for MRM & Pets
    // Xtra - Str - Melee
    $XStr = round($str / 8);
    // Xtra - Dex - Ranged
    $XDex = round($dex / 16 + $str / 16);
    // Xtra - Int - Magic
    $XInt = round($int / 8);
    // Xtra - Cha - Pets
    $XCha = round($cha / 15);
    // This part works out the chance you have to block with your current stats
    $CtB = round($dex / 10 + $luck / 20);
    // Now to work out the treasure chest you'll be getting!
    $WTcT = ($level + $luck / 5);
    if($WTcT < 30) {
    $TcT = "Small Treasure Chest - 125 Gold\n";
    }
    elseif(($WTcT >= 30)&&($WTcT <= 49)) {
    $TcT = "Treasure Chest - 275 Gold\n";
    }
    elseif(($WTcT >= 50)&&($WTcT <= 64)) {
    $TcT = "Big Treasure Chest - 425 Gold\n";
    }
    elseif(($WTcT >= 65)&&($WTcT <= 84)) {
    $TcT = "Huge Treasure Chest - 650 Gold\n";
    }
    elseif($WTcT >= 85) {
    $TcT = "Treasure Hoard - 800 Gold\n";
    }
    // Working out HP & MP
    $HP = round(($level * 5 + 100) * (1+($end / 100)));
    $MP = round(($level * 5 + 100) * (1+($int / 100)));
    // Working out weapon average
    $average = round(($base + $max) / 2);
    // Working out weapon random
    $random = $max - $base;
    echo("<table>");
    echo("<tr>");
    echo("<td colspan=\"2\">Account Details</td>");
    echo("</tr>");
    echo("<tr>");
    echo("<td><b>Account Type:</b></td>");
    echo("<td>$account_type</td>");
    echo("</tr>");
    echo("</table>");
    echo("<br /><br />");
    echo("<!-- BtH Start -->");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">Bonus to Hit</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Melee BtH</b></td>\n");
    echo("<td>$SBtH%</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Ranged BtH</b></td>\n");
    echo("<td>$DBtH%</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Magic BtH</b></td>\n");
    echo("<td>$IBtH%</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Pet BtH</b></td>\n");
    echo("<td>$CBtH%</td>\n");
    echo("</tr>\n");
    echo("</table>\n");
    echo("<!-- BtH End -->\n");
    echo("<!-- Added Damage Section Start -->\n");
    echo("<br /><br />\n");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">Added Damage</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Melee Damage</b></td>\n");
    echo("<td>+$XStr</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Ranged Damage</b></td>\n");
    echo("<td>+$XDex</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Magic Damage</b></td>\n");
    echo("<td>+$XInt</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Pet Damage</b></td>\n");
    echo("<td>+$XCha</td>\n");
    echo("</tr>\n");
    echo("</table>\n");
    echo("<!-- Added Damage Section End -->\n");
    echo("<!-- Random Section Start -->\n");
    echo("<br /><br />\n");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">Chance to block</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Defence:</b></td>\n");
    echo("<td>$CtB%</td>\n");
    echo("</tr>\n");
    echo("</table>\n");
    echo("<br /><br />\n");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">Treasure Chests</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>L Value:</b></td>\n");
    echo("<td>$level</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Treasure Chest:</b></td>\n");
    echo("<td>$TcT</td>\n");
    echo("</tr>\n");
    echo("</table>\n");
    echo("<br /><br />\n");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">Weapon Averages and Random Damage</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Weapon Average:</b></td>\n");
    echo("<td>$max + $base / 2 = $average</td>\n");
    echo("</tr\n>");
    echo("<tr>\n");
    echo("<td><b>Weapon Random Damage:</b></td>\n");
    echo("<td>$max - $base = $random</td>\n");
    echo("</tr>\n");
    echo("</table>\n");
    echo("<br /><br />\n");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">HP & MP</td>\n");
    echo("</tr>\n");
    echo("<tr>");
    echo("<td><b>HP:</b></td>\n");
    echo("<td>$HP</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>MP:</b></td>\n");
    echo("<td>$MP</td>\n");
    echo("</tr>\n");
    echo("<!-- Random Section End -->\n");
    }
}
elseif($account == "staff") {
    $account_type = "Staff";
    // This section calculates your BtH for each weapon type
    // It will also work out BtH for pets ^_^
    // BtH - Str - Melee
    $SBtH = round(($dex / 16) + ($str / 16) + ($luck / 20));
    // BtH - Dex - Ranged
    $DBtH = round(($dex / 8) + ($luck / 20));
    // BtH - Int - Magic
    $IBtH = round(($dex / 16) + ($int / 16) + ($luck / 20));
    // BtH - Cha - Pets
    $CBtH = round($cha / 15 + 5);
    // This section calculates extra random damage gained for MRM & Pets
    // Xtra - Str - Melee
    $XStr = round($str / 8);
    // Xtra - Dex - Ranged
    $XDex = round($dex / 16 + $str / 16);
    // Xtra - Int - Magic
    $XInt = round($int / 8);
    // Xtra - Cha - Pets
    $XCha = round($cha / 15);
    // This part works out the chance you have to block with your current stats
    $CtB = round($dex / 10 + $luck / 20);
    // Now to work out the treasure chest you'll be getting!
    $WTcT = ($level + $luck / 5);
    if($WTcT < 30) {
    $TcT = "Small Treasure Chest - 125 Gold\n";
    }
    elseif(($WTcT >= 30)&&($WTcT <= 49)) {
    $TcT = "Treasure Chest - 275 Gold\n";
    }
    elseif(($WTcT >= 50)&&($WTcT <= 64)) {
    $TcT = "Big Treasure Chest - 425 Gold\n";
    }
    elseif(($WTcT >= 65)&&($WTcT <= 84)) {
    $TcT = "Huge Treasure Chest - 650 Gold\n";
    }
    elseif($WTcT >= 85) {
    $TcT = "Treasure Hoard - 800 Gold\n";
    }
    // Working out HP & MP
    $HP = round(($level * 5 + 100) * (1+($end / 100)));
    $MP = round(($level * 5 + 100) * (1+($int / 100)));
    // Working out weapon average
    $average = round(($base + $max) / 2);
    // Working out weapon random
    $random = $max - $base;
    echo("<table>");
    echo("<tr>");
    echo("<td colspan=\"2\">Account Details</td>");
    echo("</tr>");
    echo("<tr>");
    echo("<td><b>Account Type:</b></td>");
    echo("<td>$account_type</td>");
    echo("</tr>");
    echo("</table>");
    echo("<br /><br />");
    echo("<!-- BtH Start -->");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">Bonus to Hit</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Melee BtH</b></td>\n");
    echo("<td>$SBtH%</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Ranged BtH</b></td>\n");
    echo("<td>$DBtH%</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Magic BtH</b></td>\n");
    echo("<td>$IBtH%</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Pet BtH</b></td>\n");
    echo("<td>$CBtH%</td>\n");
    echo("</tr>\n");
    echo("</table>\n");
    echo("<!-- BtH End -->\n");
    echo("<!-- Added Damage Section Start -->\n");
    echo("<br /><br />\n");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">Added Damage</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Melee Damage</b></td>\n");
    echo("<td>+$XStr</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Ranged Damage</b></td>\n");
    echo("<td>+$XDex</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Magic Damage</b></td>\n");
    echo("<td>+$XInt</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Pet Damage</b></td>\n");
    echo("<td>+$XCha</td>\n");
    echo("</tr>\n");
    echo("</table>\n");
    echo("<!-- Added Damage Section End -->\n");
    echo("<!-- Random Section Start -->\n");
    echo("<br /><br />\n");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">Chance to block</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Defence:</b></td>\n");
    echo("<td>$CtB%</td>\n");
    echo("</tr>\n");
    echo("</table>\n");
    echo("<br /><br />\n");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">Treasure Chests</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>L Value:</b></td>\n");
    echo("<td>$level</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Treasure Chest:</b></td>\n");
    echo("<td>$TcT</td>\n");
    echo("</tr>\n");
    echo("</table>\n");
    echo("<br /><br />\n");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">Weapon Averages and Random Damage</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>Weapon Average:</b></td>\n");
    echo("<td>$max + $base / 2 = $average</td>\n");
    echo("</tr\n>");
    echo("<tr>\n");
    echo("<td><b>Weapon Random Damage:</b></td>\n");
    echo("<td>$max - $base = $random</td>\n");
    echo("</tr>\n");
    echo("</table>\n");
    echo("<br /><br />\n");
    echo("<table>\n");
    echo("<tr>\n");
    echo("<td colspan=\"2\">HP & MP</td>\n");
    echo("</tr>\n");
    echo("<tr>");
    echo("<td><b>HP:</b></td>\n");
    echo("<td>$HP</td>\n");
    echo("</tr>\n");
    echo("<tr>\n");
    echo("<td><b>MP:</b></td>\n");
    echo("<td>$MP</td>\n");
    echo("</tr>\n");
    echo("<!-- Random Section End -->\n");
}
?>

It's pretty simple (and admittedly quite chunky) but it works well; I just need a little help with the finishing touches! ^_^

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 May 2006 - 01:31 PM

[!--quoteo(post=373697:date=May 14 2006, 05:42 AM:name=entity)--][div class=\'quotetop\']QUOTE(entity @ May 14 2006, 05:42 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi, I've only just joined; but I've been a part of the PHP phenomena for quite a while now. Well I've been scripting a code for an Online RPG (Adventure Quest); this code allows you to work out stats. Anyway, I've been working quite hard on this code but I have finally come to a full stop on it. My problem is that stats can only be in 5's so 5 all the way up to 175 (unless your a staff member you can get anything as long as it is a multiple of five), my problem is I don't know how to make a validation code that will only allow multiples of five. Any help on this would be greatly appriciated as this is the first code I've written without copying parts here and there.
Thank you very much.

- entity
[/quote]
Hey! I've played your game before. Pretty cool! So basically what you want to do is only want to have 5,10,15,20,25,30,35....175 ? how about this:

<?php
   if ($_POST['blah']) {
      if (is_numeric($_POST['blah'])){
         echo $_POST['blah'];  
           if (($_POST['blah'] % 5) == 0) {
            echo " is divisible by 5.";
           } else {
              echo " is NOT divisible by 5.";
         }
      } else {
         echo "Please enter in a valid number";
      }
   } else {
        echo "Please enter in a number";
     }    

   echo "<br><br>";
   echo "<form action='$PHP_SELF' method ='post'>";
   echo "<input type = 'text' name='blah' size ='5'>";
   echo "<input type = 'submit' value='check num'>";
   echo "</form>";
?>

now it's not really that long. it's just an example of it in action. the only line that you really need to pay attention to is this:

if (($_POST['blah'] % 5) == 0) {

this takes the number in question ($_POST['blah]) and divides it by 5 and returns the remainder. if the remainder is zero, then it echos 'this number is divisible by 5. otherwise, it is not divisible by 5.

Now, as far is only MAKING it divisible by 5, well you can just round up to the next divisible number. for example:

$num = 213;
$blah = ($num % 5);
if ($blah != 0) {
   $num = $num + $blah;
}

I think you are probably php saavy enough to figure out how to make use of this spiffy little operator [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 entity

entity
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationEngland, UK

Posted 14 May 2006 - 03:29 PM

Lol, it isn't my game, I'm just making a fan based web site and this going to be one of the codes that I use Well I'll try this out! &_&

#6 entity

entity
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationEngland, UK

Posted 14 May 2006 - 08:53 PM

Sorry about the double post guys, but thing is I've just noticed something about my script and I've been working on this for aggggges; well I'm doing quite a few calculations in there, but quite a few seem to come out wrong,
$SBtH = round($dex / 16 + $str / 16 + $luck / 20);
Well what is supposed to happen there
is this:
$dex, $str and $luck are all brought from a form, well their numbers are
$dex = 100
$str = 100
$luck = 20
so what happens is:
100 divided by 16 = 6.25
+
100 divided by 16 = 6.25
+
20 divided by 20 = 1

once added altogether this should equal
8.50 rounded up to 9 but what I get is 17 which is wrong but so is the nine... what it is supposed to be is 14... what is going on here? Can anyone give me a few hints please?

#7 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 May 2006 - 09:06 PM

$dex = 100; // normally retrieved from the $_POST array
$str = 100;
$luck = 20;
$SBtH = round($dex / 16 + $str / 16 + $luck / 20);
echo $SBtH;

That produces the correct answer (14). If your code doesn't then there's either something tragically wrong with your code or maybe you're looking at an older version or it's not uploaded to the server or something.

If it really doesn't work - can we see some of the code that processes the form data
Legend has it that reading the manual never killed anyone.
My site

#8 entity

entity
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationEngland, UK

Posted 14 May 2006 - 10:34 PM

Well thanks for the help I've got that working now; I've just gotta get Crayon Violent's thing working now, hopefully!

#9 entity

entity
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationEngland, UK

Posted 15 May 2006 - 06:25 AM

Well, I'm still hving problems, I can't seem to find a way to implement Crayon Violent's idea, I'll post the code and could you try and see how it could be fixed? Cheers ^_^

PS: Sorry for all the double posting by the way!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users