Re: Free 2 - Gym
ive added the script edited it so it would work with lite but it seems the stats dont go up and the rank is completley wrong can some one please help this is the code
gym2.php
<?php
session_start();
require "global_func.php";
if($_SESSION['loggedin']==0) { header("Location: login.php");exit; }
$userid=$_SESSION['userid'];
require "header.php";
$h = new headers;
$h->startheaders();
include "mysql.php";
global $c;
$is=mysql_query("SELECT u.*,us.* FROM users u LEFT JOIN userstats us ON u.userid=us.userid WHERE u.userid=$userid",$c) or die(mysql_error());
$ir=mysql_fetch_array($is);
check_level();
$fm=money_formatter($ir['money']);
$cm=money_formatter($ir['crystals']);
$lv=date('F j, Y, g:i a',$ir['laston']);
$h->userdata($ir,$lv,$fm,$cm);
$h->menuarea();
function mysql_fetch_single( $rs )
{
if (is_array($row = mysql_fetch_row($rs)))
return $row[0];
return false;
}
if ($ir['hospital'])
{
echo "
You cannot access the gym whilst in hospital</p>";
$h->endpage();
exit;
}
$prefs = isset($_SESSION['gymprefs']) ? $_SESSION['gymprefs'] : array('what' => "all", 'expend' => "100%");
$what = $prefs['what'];
$expend = $prefs['expend'];
$message = false;
$sql = sprintf("SELECT us.strength, us.agility, us.guard, us.labour, u.energy, u.will, u.jail FROM users u LEFT JOIN userstats us ON (u.userid = us.userid) WHERE (u.userid = %u)", $userid);
$row = mysql_fetch_row(mysql_query($sql));
$formID = preg_replace("`^([0-9A-F]{8})([0-9A-F]{4})([0-9A-F]{4})([0-9A-F]{4})([0-9A-F]{12})$`ms", "{\\1-\\2-\\3-\\4-\\5}", strtoupper(md5(serialize(array(session_id(), "Gym", null)))));
if (isset($_POST['formID']) && is_string($_POST['formID']) && ($_POST['formID'] === $formID))
{
$what = isset($_POST['what']) && is_string($_POST['what']) && preg_match("`^((all)|(strength)|(agility)|(guard)|(labour))$`ims", $_POST['what']) ? strtolower($_POST['what']) : $what;
$expend = isset($_POST['expend']) && is_string($_POST['expend']) && preg_match("`^((100%)|(75%)|(50%)|(25%)|(value))$`ims", $_POST['expend']) ? strtolower($_POST['expend']) : $expend;
$value = isset($_POST['value']) && is_string($_POST['value']) && preg_match("`^\d+$`ims", $_POST['value']) ? @intval($_POST['value']) : 0;
$sql = sprintf("SELECT energy, will FROM users WHERE (userid = %u)", $userid);
$rs = mysql_query($sql);
$info = mysql_fetch_row($rs);
$energy = $row['energy'];
$will = $row['will'];
switch ($expend)
{
case "100%": $value = $row['energy']; break;
case "75%": $value = $row['energy'] * 0.75; break;
case "50%": $value = $row['energy'] * 0.50; break;
case "25%": $value = $row['energy'] * 0.25; break;
default: break;
}
$value = floor($value);
if ($value > $row['energy'])
$message = "<span style='color:#C00;'>You don't have enough energy</span>";
else
{
$prefs = array('what' => $what, 'expend' => $expend);
$gain = 0;
$will = $row['will'];
for ($i = 0; $i < $value; $i++)
{
$gain += mt_rand(1, 3) / mt_rand(500, 900) * mt_rand(500, 900) * (($will + 20) / 150);
$will = max(0, $will - mt_rand(1, 3));
}
if ($row['jail'])
$gain *= 0.5;
$row['will'] = floor($will);
$row['energy'] -= $value;
switch ($what)
{
case "all":
$message = sprintf("<span style='color:#070;'>You gain %s in each stat</span>", number_format($gain * 0.25, 2));
$row['strength'] += $gain * 0.25;
$row['agility'] += $gain * 0.25;
$row['guard'] += $gain * 0.25;
$row['labour'] += $gain * 0.25;
break;
case "strength":
$message = sprintf("<span style='color:#070;'>You gain %s in strength</span>", number_format($gain, 2));
$row['strength'] += $gain;
break;
case "agility":
$message = sprintf("<span style='color:#070;'>You gain %s in agility</span>", number_format($gain, 2));
$row['agility'] += $gain;
break;
case "guard":
$message = sprintf("<span style='color:#070;'>You gain %s in guard</span>", number_format($gain, 2));
$row['guard'] += $gain;
break;
case "labour":
$message = sprintf("<span style='color:#070;'>You gain %s in labour</span>", number_format($gain, 2));
$row['labour'] += $gain;
break;
}
$sql1 = sprintf("UPDATE userstats SET strength = %.6f, agility = %.6f, guard = %.6f, labour = %.6f WHERE (userid = %u)", $row['strength'], $row['agility'], $row['guard'], $row['labour'], $userid);
$sql2 = sprintf("UPDATE users SET will = %u, energy = %d WHERE (userid = %u)", $row['will'], $row['energy'], $userid);
mysql_query($sql1);
mysql_query($sql2);
}
$_SESSION['gymprefs'] = $prefs;
}
$row['ranks'] = array
(
'strength' => mysql_fetch_single(mysql_query(sprintf("SELECT COUNT(userid) FROM userstats WHERE (strength > %.6f)", $row['strength']))) + 1,
'agility' => mysql_fetch_single(mysql_query(sprintf("SELECT COUNT(userid) FROM userstats WHERE (agility > %.6f)", $row['agility']))) + 1,
'guard' => mysql_fetch_single(mysql_query(sprintf("SELECT COUNT(userid) FROM userstats WHERE (guard > %.6f)", $row['guard']))) + 1,
'labour' => mysql_fetch_single(mysql_query(sprintf("SELECT COUNT(userid) FROM userstats WHERE (labour > %.6f)", $row['labour']))) + 1,
'total' => mysql_fetch_single(mysql_query(sprintf("SELECT COUNT(userid) FROM userstats WHERE (strength + agility + guard + labour > %.6f)", $row['strength'] + $row['agility'] + $row['guard'] + $row['labour']))) + 1,
);
echo "<style type='text/css'>";
echo "#gym { }";
echo "#gym h3, #gym p { }";
echo "#gym table { width:90%;border-collapse:collapse; }";
echo "#gym th { border:solid 1px #333;background:#F6F6F6 url(titlebg.gif);padding:3px;color:#333; }";
echo "#gym td { border:solid 1px #333;padding:3px;color:#333; }";
echo "#gym { }";
echo "</style>";
echo "<div id='gym'>";
echo sprintf("<h3>%sGym</h3>", $ir['jail'] ? "Jail " : "");
if ($message)
echo sprintf("
%s</p>", $message);
echo "<form name='train' id='train' action='gym2.php' method='post' style='padding:0;margin:0;'>";
echo sprintf("<input type='hidden' name='formID' value='%s' />", $formID);
echo "<table cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<th class='sub' style='width:50%;'>Train</th>";
echo "<th class='sub' style='width:50%;'>Expend</th>";
echo "</tr>";
echo "<tr>";
echo "<td style='line-height:200%;'>";
echo sprintf("<input type='radio' name='what' id='what0' value='all' %sstyle='vertical-align:middle;' /><label for='what0' style='cursor:pointer;'>All (25%% energy in each) [Rank #%s]</label>
", $what === "all" ? "checked='checked' " : "", number_format($row['ranks']['total']));
echo sprintf("<input type='radio' name='what' id='what1' value='strength' %sstyle='vertical-align:middle;' /><label for='what1' style='cursor:pointer;'>Strength (%s) [Rank #%s]</label>
", $what === "strength" ? "checked='checked' " : "", number_format($row['strength']), number_format($row['ranks']['strength']));
echo sprintf("<input type='radio' name='what' id='what2' value='agility' %sstyle='vertical-align:middle;' /><label for='what2' style='cursor:pointer;'>Agility (%s) [Rank #%s]</label>
", $what === "agility" ? "checked='checked' " : "", number_format($row['agility']), number_format($row['ranks']['agility']));
echo sprintf("<input type='radio' name='what' id='what3' value='guard' %sstyle='vertical-align:middle;' /><label for='what3' style='cursor:pointer;'>Guard (%s) [Rank #%s]</label>
", $what === "guard" ? "checked='checked' " : "", number_format($row['guard']), number_format($row['ranks']['guard']));
echo sprintf("<input type='radio' name='what' id='what4' value='labour' %sstyle='vertical-align:middle;' /><label for='what4' style='cursor:pointer;'>Labour (%s) [Rank #%s]</label>
", $what === "labour" ? "checked='checked' " : "", number_format($row['labour']), number_format($row['ranks']['labour']));
echo "</td>";
echo "<td style='line-height:200%;'>";
echo sprintf("<input type='radio' name='expend' id='expend0' value='100%%' %sstyle='vertical-align:middle;' /><label for='expend0' style='cursor:pointer;'>100%% Energy</label>
", $expend === "100%" ? "checked='checked' " : "");
echo sprintf("<input type='radio' name='expend' id='expend1' value='75%%' %sstyle='vertical-align:middle;' /><label for='expend1' style='cursor:pointer;'>75%% Energy</label>
", $expend === "75%" ? "checked='checked' " : "");
echo sprintf("<input type='radio' name='expend' id='expend2' value='50%%' %sstyle='vertical-align:middle;' /><label for='expend2' style='cursor:pointer;'>50%% Energy</label>
", $expend === "50%" ? "checked='checked' " : "");
echo sprintf("<input type='radio' name='expend' id='expend3' value='25%%' %sstyle='vertical-align:middle;' /><label for='expend3' style='cursor:pointer;'>25%% Energy</label>
", $expend === "25%" ? "checked='checked' " : "");
echo sprintf("<input type='radio' name='expend' id='expend4' value='value' %sstyle='vertical-align:middle;' onclick='document.train.value.focus();' /><label for='expend4' style='cursor:pointer;'><input type='text' id='value' name='value' value='{$row['energy']}' size='8' onfocus='this.select();' /> Energy</label>
", $expend === "value" ? "checked='checked' " : "");//(C)2008,Nyna
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2' style='text-align:center;padding:6px;'>";
echo "<input type='submit' name='submit' value='Train' />";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "</div>";
echo "<p style='color:#CCC;'>An open-source extension from [url='http://nyna.co.uk/']Nyna[/url]</p>";
$h->endpage();
?>
gym.php
<?php
session_start();
require "global_func.php";
if($_SESSION['loggedin']==0) { header("Location: login.php");exit; }
$userid=$_SESSION['userid'];
require "header.php";
$h = new headers;
$h->startheaders();
include "mysql.php";
global $c;
$is=mysql_query("SELECT u.*,us.* FROM users u LEFT JOIN userstats us ON u.userid=us.userid WHERE u.userid=$userid",$c) or die(mysql_error());
$ir=mysql_fetch_array($is);
check_level();
$fm=money_formatter($ir['money']);
$lv=date('F j, Y, g:i a',$ir['laston']);
$out="";
$_GET['times']= abs((int) $_GET['times']);
if(isset($_GET['train']))
{
if($_GET['train'] != "strength" && $_GET['train'] != "agility" && $_GET['train'] != "guard" && $_GET['train'] != "labour") { $h->userdata($ir,$lv,$fm,$cm);
$h->menuarea(); die("Abusers aren't allowed."); }
$tgain=0;
for($i=1;$i<=$_GET['times'] && $ir['energy'] > 0;$i++)
{
if ($ir['energy'] > 0)
{
$gain=rand(1,3)/rand(800,1000)*rand(800,1000)*(($ir['will']+20)/150);
$tgain+=$gain;
if ($_GET['train']=="IQ") { $gain/=100; }
$ir[$_GET['train']]+=$gain;
$egain=$gain/10;
$ts=$ir[$_GET['train']];
$st=$_GET['train'];
mysql_query("UPDATE userstats SET $st=$st+".$gain." WHERE userid=$userid",$c) or die("UPDATE userstats SET $st=$st+$gain,energy=energy-1,exp=exp+$egain WHERE userid=$userid
".mysql_error());
$wu=(int) (rand(1,3));
if ($ir['will'] >= $wu)
{
$ir['will']-=$wu;
mysql_query("UPDATE users SET energy=energy-1,exp=exp+$egain,will=will-$wu WHERE userid=$userid",$c);
}
else
{
$ir['will']=0;
mysql_query("UPDATE users SET energy=energy-1,exp=exp+$egain,will=0 WHERE userid=$userid",$c);
}
$ir['energy']-=1;
$ir['exp']+=$egain;
}
else
{
$out="You do not have enough energy to train.";
}
}
$stat=$ir[$st];
$i--;
$out="You begin training your $st.
You have gained $tgain $st by training it $i times.
You now have $stat $st and {$ir['energy']} energy left.
";
}
else
{
$out="<h3>Gym: Main Lobby<h3>";
}
$h->userdata($ir,$lv,$fm);
$h->menuarea();
print $out;
print "Enter the amount of times you wish to train and choose the stat to train.
You can train up to {$ir['energy']} times.
<form action='gym.php' method='get'>
<input type='text' name='times' value='1' /><select type='dropdown' name='train'>
<option value='strength'>Strength</option>
<option value='agility'>Agility</option>
<option value='labour'>Labour</option>
<option value='guard'>Guard</option></select>
<input type='submit' value='Train!' /></form>";
$h->endpage();
?>
Thanxs in advance for anyone that helps me