Doin' Time

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Doin' Time

Post by Callan S. »

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.

Code: Select all

$checktime=1; include 'time.php';
$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

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
{
Since were checking on each page, I set it to only check once every 15 minutes, to save on database 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'];
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.

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");
   }
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.

Code: Select all

$now=time();
$dbprior15=floor($dbpriortime/900);
$now15=floor(time()/900);
$_SESSION['time_15wait']=$now15+900;

$passing15=$now15-$dbprior15;
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.

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 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...)
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Doin' Time

Post by Jackolantern »

Very cool! I think this will be very helpful :D
The indelible lord of tl;dr
User avatar
Sharlenwar
Posts: 523
Joined: Mon May 28, 2012 7:14 pm

Re: Doin' Time

Post by Sharlenwar »

Amazing stuff! Exactly what I was hoping for. You went above and beyond. Thank you so much for this as it is going to be really handy and answers exactly what I need. This will work for me, for when you do work and when your energy drops, you'll need to go and rest. Well resting will occur in various areas, such as your residence, or other businesses that might have better energy regen rates. Even from the larger business side where you have a mine, and so you can harvest the resources from it every so often. It will also be based on if anyone is working for you, as that will increase the speed at which you mine, but cost the company money. So tons of ideas are racing in my mind now.

I was hoping it wasn't going to be complicated, and this seems to me to be just right. Halls' tutorials have helped me immensely as well.
Deep within the Void of Quasion, a creation.

**My Corner of the Web**
***NEW***GrindFest - My own PHP/MySQL game!
Sharlenwar's Adventures
Torn-City - Massively multiplayer online text based RPG
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Doin' Time

Post by hallsofvallhalla »

you are a true gentleman
Post Reply

Return to “Coding”