Jump to content

PHP - Live Timer to Show Completion - Strategy Based


oliverj777

Recommended Posts

Hello,

 

I'm working on a very simple strategy based game site (mainly to expand my knowledge).

 

What I basically want it to do is, you send a solider to an area which takes, say 20 seconds. After 20 seconds, he battles for 30 seconds, and then arrived back home which takes another 20 seconds.

 

All the user is going to be seeing is a 'status' echo'ed text saying "Arriving in 00:??", "Battle Over in 00:??", and "Home in 00:??".

 

So its just using a time counter. But what makes it more complicated, is that I want the time progress to be saved (datebase), so say I have to wait 50 seconds, I can sign off, go on another computer, sign in and still see my timer going down according to the time that has just passed.

 

So, what I've done so far is, have 4 variables:

 

$realTime - Holds the actual time in seconds.

$waitingTime - Holds how long I need to wait. (this is stored in my DB)

$differenceTime - Difference between real time and my waiting time. (loaded from my DB)

$soliderProgress - Progress of the solider, so: Arriving, Battle ... (stored on DB)

 

 

$realTime = date("s");

//soliderProgress is 'Starting' by default 
if(soliderProgress == "Starting"){
   //solider is moving to area
   $differenceTime = $realTime + $waitingTime;
   $SQL = ("UPDATE users SET differenceTime='$differenceTime' WHERE username='$username'");
   soliderProgress = "Arriving";
   $SQL = ("UPDATE users SET soliderProgress='$soliderProgress' WHERE username='$username'");
}

if(soliderProgress == "Arriving"){
   echo "Arriving to area";
   if($realTime >= $differenceTime){
      //timer is complete
      soliderProgress = "Battle";
      $SQL = ("UPDATE users SET soliderProgress='$soliderProgress' WHERE username='$username'");
   }
}

 

This kind of works, but sometimes my $differenceTime is higher than 60 which messes it up, and also the timer isn't live ... so I have to keep refreshing the page

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.