First off in each file I include my time.php script near the start, just under where I check a player is logged in.
Code: Select all
$checktime=1; include 'time.php';
In time.php itself it starts off like this
Code: Select all
<?php
include_once 'connect.php';
if (!isset($_SESSION['time_15wait'])) $_SESSION['time_15wait']=0;
if (isset($checktime) && time()>$_SESSION['time_15wait']) // only check if another page has given the go ahead AND 15 minutes has passed. Saves on DB access calls
{
Code: Select all
$worldstatsinfo="SELECT time FROM world_stats where id=1";
$worldstatsinfo2=mysql_query($worldstatsinfo) or die("could not get world stats! #1");
$worldstatsinfo3=mysql_fetch_array($worldstatsinfo2);
$dbpriortime = $worldstatsinfo3['time'];
Code: Select all
if ($dbpriortime==0) // wow, the first initialisation if statement - repeated forever, pointlessly! >:) Comment this out if statement out if you want to, after the first run
{
$dbpriortime=time();
$updateworldstats="update world_stats set time='$dbpriortime' where id=1 limit 1";
mysql_query($updateworldstats) or die("Could not update world stats #1");
}
Code: Select all
$now=time();
$dbprior15=floor($dbpriortime/900);
$now15=floor(time()/900);
$_SESSION['time_15wait']=$now15+900;
$passing15=$now15-$dbprior15;
Code: Select all
if ($passing15>0)
{
$playerinfo = "SELECT id,name,energy FROM players";
$playerinfo2 = mysql_query($playerinfo);
while($playerinfo3 = mysql_fetch_array($playerinfo2))
{ // --------------------------------------------------------------------------------------------------
$bulkid=$playerinfo3['id'];
$bulkname=$playerinfo3['name'];
// add to stats, multiplying by $passing15 to include however many number of 15 minute increments have passed
// energy
$bulkenergy+=1*$passing15;
if ($bulkenergy>100) $bulkenergy=100;
// end of energy increase
$updateplayer="update players set energy='$bulkenergy' where id='$bulkid' limit 1";
mysql_query($updateplayer) or die("Could not update player stats");
}
$updateworldstats="update world_stats set time='$now' where id=1 limit 1";
mysql_query($updateworldstats) or die("Could not update world stats #2");
}
And that's pretty much it. Daily additions are similar, having their own while statement. Other than that, that's pretty much it - feel free to ask questions and no doubt those with greater coding skill will shame me (showing code is a bit like one of them dreams you have where you give a speach, but naked...)