Jump to content
MakeWebGames

Time and countdown help


TomW

Recommended Posts

Hello,

I am developing a new PHP game which I have been working on for sometime now. Some of you may have seen the film In Time, time is the new currency and if their time runs out they will die. I want to create something similar which has my own modifications on it.

I am stuck on a problem I have. I want to give people 1 week of time and a count down will be started, they can gain more time by doing jobs and other roles which will be added to the game later on.

I want to know which will be the most efficient way of doing the time. I want a count down in the top of the screen which they can always view. The way I have come up with is I make a time stamp of when the user registers and add a week onto it. Then from that keep adding time when jobs are completed. I will then have a cron job running every second which will find out if the time is over or not, once time is over the person will be killed.

Could someone help me as to how i could achieve this.

Or is there an easier, or more efficient way of doing it.

Thanks, Tom.

Link to comment
Share on other sites

Hi TomW!

Sounds like a very interesting idea. I'd say a timestamp of the integer/float data type is probably the easiest solution, and actually the only one I can come up with that will lead to something. A timestamp is easy enough to convert to basically anything you'd like. With a timestamp it's easy enough to calculate as well. What you also could do is just use an basic integer that holds the time to live. During register this would hold the basic value, with the time they have before they die. (I assume it would be 604800) From here you can count down to 0 and add seconds as they progress to the game? Not that there is such a difference between just using the timestamp of their registration date, it's just a different value you start from actually... I think you're looking in the right direction though.

One last thing, be careful with running that cron job every second... most hosts won't even allow this. You could probably achieve this feature by using javascript on the client side (use a count down timer), and by checking each page request on the server side. Eventually you could combine the both as well.

Link to comment
Share on other sites

I like the method of giving it them in seconds, but how would I get the database to countdown live, cause it will have to countdown by 1 every second.

Octarine I like your method, I do want a countdown on the top of the page so a user can see how long they have left in real time. I will try your code thanks very much!

Edited by TomW
Link to comment
Share on other sites

In case you didn't know, the built-in PHP function time() returns the unix timestamp which is the number of seconds since January 1, 1970. You can use this by adding the number of seconds in a week (604800 as Nickson said) to the current timestamp when they register and put this in the database as their time of death (one week in the future). Then whenever the player does something in the game to extend the time, you just add to that number. If they extend their life time by an hour, you would add 3600 to that number for instance.

Displaying their time left could be as simple as a short PHP script that takes the time of death minus the current time and converts the difference into weeks/days/hours/minutes/seconds and shows it in the header and updates each time a page is loaded. You could have a countdown running on the page too, but I don't have experience doing that.

You don't need to and really shouldn't have a cron job running every second. Google 'PHP time() function' and you'll find what we're talking about.

Edit: Also, checking to see if their life time has ran out would be done by checking to see if their life time number is less than the current timestamp.

Edited by JordanD
Link to comment
Share on other sites

I like the method of giving it them in seconds, but how would I get the database to countdown live, cause it will have to countdown by 1 every second.

Octarine I like your method, I do want a countdown on the top of the page so a user can see how long they have left in real time. I will try your code thanks very much!

Use the last bit of code of Octarine's example. There is no need for the database to count down live. You would just store when they die in the database. If you want a live timer, you can use a javascript countdown function to show users how long they have to live, based on the value in the database.

Link to comment
Share on other sites

Hey TomW. The best way you can go (for me atleast) is a php timestamp that is being translated by javascript, You do not want to have the user check the database to see if a second has passed (especially if 100 users are playing, just for a timer to countdown that is 100 queries a second. The best way is when the page loads, the php timestamp is given to javascript and javascript counts down on its own. Even if the user disables javascript you still have the original times in the database and this would update on the next page load (get it?). Crons probably are not needed in this situation. Just some classic php timestamps, some javascript and maybe a simple ajax call when the timer hits 0 and they are still online.

A cron actually might be necessary to kill the dead users if your entire game is ajax. If its just a regular old click and page reloads kind of game, you shouldnt need any crons.

Important Note: If you take the other advice and do not use javascript, your users would refresh the page over and over to watch the time move. its better just to have a simple 3 or 4 line javascript to countdown your timestamp

Edited by runthis
Link to comment
Share on other sites

$sql = <<<SQL
SELECT *
 FROM users
WHERE id = {$me}
SQL;

$rs   = mysql_query($sql);
$user = mysql_fetch_row($rs, MYSQL_ASSOC);

mysql_free_result($rs);

if ($user['died'] > time())
{
   /**
     * They are still alive...
     **/

}
else
{
   /**
     * Oops, they've died - maybe redirect to a new page
     * and reset all their stats to zero?
     **/
}

 

 

Wouldn't it be more plausible and smaller code where you dont have to perform a fetch if you did it like this

 

$sql=("SELECT id FROM users WHERE userid=$id AND die_time<$time LIMIT 1");
$n=mysql_num_rows($sql);
if($n == 0){ ##do nothing of course 
}else{
       echo "you are dead";
}

 

or even better with using sessions to display and queries to update

update/ add time

runyourquery($timetoadd);
$_SESSION['die_time']=($_SESSION['die_time'] + $timetoadd);

 

update/ remove time

runyourquery($timetoadd);
$_SESSION['die_time']=($_SESSION['die_time'] - $timetoadd);

 

check time / no query

 

if($_SESSION['die_time'] < time()){ echo "You are dead"; }
Edited by runthis
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...