Calc not working out correctly - Bad code - just needs to wo

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Calc not working out correctly - Bad code - just needs to wo

Post by gaogier »

Problem -

http://runehints.com/calculators/mining ... calculator

So far all is correct, once you click calculate the problems start.

If I do a little math,

level 67 is the target, with 548028 xp, minus the current xp of 533469 that = 14559

14559 / 50 xp per coal = x amount of coal which is 291.18 coal.

Now, my calculator is saying, 33173 is the answer. Which it clearly is not the case.

The URL I get, http://runehints.com/calculators/mining ... vl=2192114

if you change the last part (2192114) to 548028 it nearly works out to be right, as it shows 291 coal.

http://runehints.com/calculators/mining ... lvl=548028

I plan to get this script rewritten over the next couple of months, is it worth fixing it? I mean can you spot the error?

Code: Select all

<?php
preg_match('~calculators\/([a-zA-Z]+)~', $_SERVER['REQUEST_URI'], $which);
    $which = $which[1];
?>
                      <div id="contenttitle"><?php echo ucfirst($which); ?> Calculator</div>
                        <div class="news">
<?php
preg_match('~(agility|attack|combat|cooking|crafting|defence|farming|firemaking|fishing|fletching|herblore|hitpoints|magic|mining|prayer|ranged|runecrafting|slayer|strength|summoning|thieving|woodcutting)~', $_SERVER['REQUEST_URI'], $url);
    $uri = $url[1];
if (empty($uri)) {
    echo '<table class="guide">
            <tr class="trtitle">
                <th class="main">Skill Name</th>
            <tr>
            <tr>
                <td><a href="/calculators/agility">Agility</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/attack">Attack</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/cooking">Cooking</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/crafting">Crafting</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/defence">Defence</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/farming">Farming</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/fishing">Fishing</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/fletching">Fletching</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/herblore">Herblore</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/hitpoints">Hitpoints</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/magic">Magic</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/mining">Mining</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/prayer">Prayer</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/ranged">Ranged</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/runecrafting">Runecrafting</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/slayer">Slayer</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/strength">Strength</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/summoning">Summoning</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/thieving">Thieving</a></td>
            <tr>
            <tr>
                <td><a href="/calculators/woodcutting">Woodcutting</a></td>
            <tr>
    </table>';
} else {
# All Calculator - calculate.php
# Orignally created on 28 December 2004 remade using RS Hiscores on 9 June 2005
# Created by Joseph Jeffery of DSIBaN Designs
//This page functions as every calculator
//set page headers
$lvl = 0;
    preg_match('~rsname=(.*)&action~', $_SERVER['REQUEST_URI'], $rsnam);
    $rsn = $rsnam[1];

/**
 * Return current xp for specific skill
 */
function return_exp($name, $skill) {
   $url = "http://hiscore.runescape.com/index_lite.ws?player=" . strtolower(str_replace(' ', '_', $name));
   if (($page = @file_get_contents($url)) === false) { echo "User $name was not found in the highscores for $skill"; return false; }
   if(strpos($page, "<html>")) { trigger_error('Unable to parse RuneScape highscore page', E_USER_WARNING); echo "<h1>DUMP</h1>"; }
   $catagories = split('
', $page);

   //Find proper index for skill
   switch($skill) {
        case "attack":
         $ind = 1;
	 break;
	case "defence":
	 $ind = 2;
	 break;
	case "strength":
	 $ind = 3;
	 break;
	case "constitution":
	 $ind = 4;
	 break;
	case "ranged":
	 $ind = 5;
	 break;
	case "prayer":
	 $ind = 6;
	 break;
	case "magic":
	 $ind = 7;
	 break;
	case "cooking":
	 $ind = 8;
	 break;
	case "woodcutting":
	 $ind = 9;
	 break;
	case "fletching":
	 $ind = 10;
	 break;
	case "fishing":
	 $ind = 11;
	 break;
	case "firemaking":
	 $ind = 12;
	 break;
	case "crafting":
	 $ind = 13;
	 break;
	case "smithing":
	 $ind = 14;
	 break;
	case "mining":
	 $ind = 15;
	 break;
	case "herblore":
	 $ind = 16;
	 break;
	case "agility":
	 $ind = 16;
	 break;
	case "thieving":
	 $ind = 17;
	 break;
	case "slayer":
	 $ind = 18;
	 break;
	case "farming":
	 $ind = 20;
	 break;
	case "runecrafting":
	 $ind = 21;
	 break;
	case "hunter":
	 $ind = 22;
	 break;
	case "construction":
	 $ind = 23;
	 break;
	case "summoning":
	 $ind = 24;
	 break;
	case "dungeoneering":
	 $ind = 25;
	 break;
   }

   
   $skill_info = split(",", $catagories[$ind]);
   
   $skill_exp = $skill_info[2];
		
   return $skill_exp;

   
}

function enter_name(){
	$skill = $_GET['skill'];
	$mem = $_GET['members'];
    $_GET['action'] = 'calculator';
    preg_match('~rsname=(.*)&action~', $_SERVER['REQUEST_URI'], $rsnam);
    $rsn = $rsnam[1];
    preg_match('~&action=(.*)~', $_SERVER['REQUEST_URI'], $action);
    $_GET['action'] = $action[1];
    
	echo '<form action="" method="get">
	<p>Runescape name:&nbsp; <input type="text" name="rsname" class="text" value="' . $rsn  . '" />
    <input type="hidden" name="action" value="calculator" /></p>
	<input type="submit" class="text" value="Grab XP" onMouseOver="this.style.cursor=\'hand\';"/>
	</form><br />
	';

}


function calculator(){
    preg_match('~rsname=(.*)&action~', $_SERVER['REQUEST_URI'], $rsnam);
    $rsn = $rsnam[1];
    preg_match('~calculators\/(.*)\?rsname~', $_SERVER['REQUEST_URI'], $skill);
    $skill = $skill[1];
    preg_match('~&action=(.*)~', $_SERVER['REQUEST_URI'], $action);
    $_GET['action'] = $action[1];
if($rsn != "") { $xp = return_exp($rsn, $skill); }
	
	$mem = $_GET['members'];
    if ($xp > 13034430){
        echo 'You\'re already level 99!';
    }
    ?>
  	<form action="" method="get">
		<table border="0" class="calcinput">
		<input type="hidden" name="rsname" value="<?php echo $rsn; ?>">
        <input type="hidden" name="action" value="calculate" />
		<tr><td>Current Xp:</td><td> <input type="text" name="cxp" class="text" value="<?php if ($xp != "0") { echo $xp; } ?>" /></td></tr> 
		<tr><td>Current Level:</td><td><input type="text" class="text" value="<?php echo getLevel($xp); ?>" /></td></tr> 
		<tr><td>Desired Level:</td><td> <SELECT name="lvl" class="text"><?php genDropDown($xp); ?> </select>		<?php
			if ($mem == "yes"){
			echo '<tr><td>Show members items?:</td><td> <input name="showmembers" type="checkbox" disabled="true" value="yes" checked></td></tr> ';
			}else{
			echo '<tr><td>Hide members items?:</td><td> <input name="showmembers" type="checkbox" value="no"></td></tr> ';
			}		
		?>		
		<tr><td><input type="submit" class="text" value="Calculate" onMouseOver="this.style.cursor='hand';"/></td><td>
		<input type="reset" class="text" value="Reset" onMouseOver="this.style.cursor='hand';"/></td></tr> 
		</table>
		<?php 
}

/*Calculate level based on experience
function getLevel($xp) {
   $a = 0;
   for($i = 0; $i < 100; $i++) {
      if($a / 4 > $xp) { return $i; }
      $a += floor($i + 300 * pow(2, ($i / 7)));
   }
} */

function getLevel($exp){
   $levels = array(1 => 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018, 5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833, 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721, 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466, 247866, 273742, 302288, 333804, 368599, 407015, 449428, 496254, 547953, 605032, 668051, 737627, 814445, 899256, 992895, 1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1968068, 2192818, 2421087, 2673114, 2951373, 3258594, 3597729, 3972294, 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7994614, 8771558, 9684577, 10692629, 11805606, 13034431);
   $l = count($levels);
   while ($levels[$l] > $exp) $l--;
   return $l;
} 

//Generate dropdowns based on xp
function genDropDown($xp) {
   //Gather experience
   $a = 0;
   
   for($i = 0; $i < 100; $i++) {
         if($a / 4 > $xp) { 
            if($lvl == 0) { $lvl = $i; }
            echo "<option value=\"$a\">Level $i - " . floor($a / 4) . " xp</option>"; 
         }
      $a += floor($i + 300 * pow(2, ($i / 7)));  
   }
}
function calculate(){
    preg_match('~&lvl=(.*)~', $_SERVER['REQUEST_URI'], $lvl);
    $lvl = $lvl[1];
    preg_match('~&cxp=(.*)&lvl~', $_SERVER['REQUEST_URI'], $cxp);
    $cxp = $cxp[1];
    preg_match('~rsname=(.*)&action~', $_SERVER['REQUEST_URI'], $rsnam);
    $rsn = $rsnam[1];
    preg_match('~calculators\/(.*)\?rsname~', $_SERVER['REQUEST_URI'], $skills);
    $skills = $skills[1];
    preg_match('~&action=(.*)~', $_SERVER['REQUEST_URI'], $action);
    $_GET['action'] = $action[1];

	$lev = $_POST['lev'];
	$xpn = $lvl - $cxp;  
	if ($xpn == ""){
		$xpn = $_GET['xpn'];
	}
    if ($lev == ""){ 
		$lev = $_GET['lev'];
    }
	$show = $_GET['showmembers'];
	if ($show == "0"){
	$show = $_POST['showmembers'];
	}
    
	switch($skills) {

		case 'firemaking':
			$skill = "fire";
			$item = "Log";
			break;
		case 'runecrafting':
			$skill = "rune";
			$item = "Rune";
			break;
		case 'ranging':
			$skill = "range";
			$item = "Monster";
			break;
		case 'herblore':
			$skill = "herb";
			$item = "Herb/Potion";
			break;
		case 'fishing':
			$skill = "fish";
			$item = "Fish";
			break;
		case 'cooking':
			$skill = "cook";
			$item = "Food";
			break;
	   case 'woodcutting':
			$skill = "wood";
			$item = "Log";
			break;
		case 'agility':
			$skill = "agility";
			$item = "Course";
			break;
		case 'slayer':
			$skill = "slayer";
			$item = "monster";
			break;
		case 'farming':
			$skill = "farming";
			$item = "Seed/Action";
			break;
		case 'fletching':
			$skill = "fletching";
			$item = "Item";
			break;
		case 'magic':
			$skill = "magic";
			$item = "Spell";
			break;
		case 'mining':
			$skill = "mining";
			$item = "Ore";
			break;
		case 'smithing':
			$skill = "smithing";
			$item = "Bar";
			break;
		case 'thieving':
			$skill = "thieving";
			$item = "Chest/Stall/Pickpocket";
			break;
		case 'fighting':
			$skill = "fighting";
			$item = "Monster";
			break;
		case 'crafting':
			$skill = "crafting";
			$item = "Item";
			break;
		case 'prayer':
			$skill = "prayer";
			$item = "Bones";
			break;	
		default:
		   $skill = $skill[1];
		   $item = "Item";
	}
	$mem = $_GET['members'];	
	echo'
		<table border="0.5" cellspacing="1" cellpadding="2" align="center" bgcolor="#857553">
		<tr><td class="tableheader"><a style="color: #EED8B4" href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=level&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers='.$show.'">Level needed</a></td><td class="tableheader"><a style="color: #EED8B4" href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=item&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers='.$show.'">'.$item.'</a></td><td class="tableheader"><a style="color: #EED8B4" href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=xp&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers='.$show.'">Experience given</a></td><td class="tableheader"><a style="color: #EED8B4" href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=xp&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers='.$show.'">Number of times</a></td><td class="tableheader"><a style="color: #EED8B4" href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=members&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers='.$show.'">Members?</a></td>';		
		require_once ('includes/connect.php');//connect to db		
		$by = $_GET['orderby'];
		if ($mem == "no"){
		if ($show == "no"){
		$mess = '<a href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=level&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers=no">Click here to show members items</a>';
		$n_query = 'SELECT * FROM `'.$skill.'calc` WHERE `members`="0" ORDER BY `'.$by.'` ASC';
		}else{
		$mess = '<a href="'.$_SERVER['PHP_SELF'].'?action=calculate&skill='.$skill.'&orderby=level&xpn='.$xpn.'&lev='.$lev.'&members='.$mem.'&showmembers=yes">Click here to hide members items</a>';
		$n_query = 'SELECT * FROM `'.$skill.'calc` ORDER BY `'.$by.'` ASC';
		}		
		}else{
		$n_query = 'SELECT * FROM `'.$skill.'calc` ORDER BY `'.$by.'` ASC';
		}
		 	$n_result = mysql_query ($n_query);
			while ($n_row = mysql_fetch_assoc($n_result)) {
			$level = $n_row['level'];  
			$course = $n_row['item'];
			$xpg = $n_row['xp'];
			
			$tot1 = $xpn / $xpg;
			$tot = sprintf('%.0f', $tot1);
			$members1 = $n_row['members'];
			if ($members1 == "1"){
			$members = "Yes";
			$bg2 = "#808080";
			}else{
			$members = "No";
			$bg2 = "#FF8C00";
			}
			if ($lev < $level){
			$bg = "#049f2c";
			}else{
			$bg = "#006600";
			}		
		echo '<tr><td class="calc" bgcolor="'.$bg.'"onMouseOver="this.style.backgroundColor=\'#71828A\'" onMouseOut=this.style.backgroundColor="'.$bg.'">'.$level.'</td><td>'.$course.'</td><td>'.$xpg.'</td><td>'.$tot.'</td><td class="calc" bgcolor="'.$bg2.'" onMouseOver="this.style.backgroundColor=\'#71828A\'" onMouseOut=this.style.backgroundColor="'.$bg2.'">'.$members.'</td>';
		}
		echo'
		</table><br />
		';
        echo $lev;

}
    preg_match('~rsname=(.*)&action~', $_SERVER['REQUEST_URI'], $rsnam);
    $rsn = $rsnam[1];
    preg_match('~calculators\/(.*)\?rsname~', $_SERVER['REQUEST_URI'], $skill);
    $skill = $skill[1];
    preg_match('~&action=([a-zA-Z0-9]+)~', $_SERVER['REQUEST_URI'], $action);
    $_GET['action'] = $action[1];
    
switch($_GET['action']) {
    case 'calculate':
    	enter_name();
        calculate();
        break;
    case 'calculator':
        enter_name();
        calculator();
        break;
   case 'enter_name':
        enter_name();
	        calculator();
        break;
    default:
        
       enter_name();
}
}
?>
</div>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Calc not working out correctly - Bad code - just needs t

Post by McInfo »

gaogier wrote:if you change the last part (2192114) to 548028 it nearly works out to be right, as it shows 291 coal.
That problem can be traced back to this line,

Code: Select all

echo "<option value=\"$a\">Level $i - " . floor($a / 4) . " xp</option>";
which outputs HTML such as this.

Code: Select all

<option value="2192114">Level 67 - 548028 xp</option>
gaogier wrote:is it worth fixing it?
Personally, I would start over. The code is quite a mess, even after sending what you have posted through html_entity_decode().

Some things to improve are:
  • The way regular expressions are used on the REQUEST_URI to get user-submitted values is unusual.
  • The script should make better use of arrays.
  • HTML should be separated from logic as much as possible.
Post Reply