Doin' Time
Posted: Thu Aug 16, 2012 1:00 am
Shar asked how I did time in my game. I thought I'd give a brief run down as I don't use crons. First off the 15 increment system, where numbers might increase once every 15 minutes. BTW, the following code is untested and more an example (also maybe there are more efficient ways of doing it*shrug*):
First off in each file I include my time.php script near the start, just under where I check a player is logged in.
$checktime is just there to ensure time.php can only be run from scripts which call it.
In time.php itself it starts off like this
Since were checking on each page, I set it to only check once every 15 minutes, to save on database calls.
Here is where we get the recorded time value from world_stats, or wherever you want to keep it. And we keep a record of what was the last/prior time recorded in the database, storing it in $dbpriortime.
As you can see in the comments, yeah, this if statement becomes redundant after the first run as it sets the prior time to the last 15 minute increment. Maybe there is a better way of doing this, but I prefer to have robust code that handles it rather than relying on having to prime the 'time' value in the database manually.
Here's where we grab the current 15 minute increment. 900 being the number of seconds in 15 minutes. $passing15 is where we figure if one or more increments of 15 minutes have passed.
And so this while loop goes through all players and you can give them whatever they get every fifteen minutes. Here I've put in some 'energy' code to show energy that increases by one every fifteen minutes, up to 100 points worth.
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...)
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...)