Page 1 of 1

PHP Time for a Game and Use

Posted: Sun Jul 17, 2011 4:03 pm
by vitinho444
Hey guys, i searched a lot of tutorials, i asked at the section of the browser based tutorial and stuff, but no solid answer...

I got the time displayed in my game. It's of course the server side time. Ok till now everything ok.

But now for example i wanna make a button called travel.

It will travel from A to B, and spent 19 minutes.

Ok so the travel.php should be: (concept)

Code: Select all

query player infos
query travel time
$time = server time

Ok now everything ok.. but how can i make it so player needs to wait the 19 minutes to reach the B and travel to A or C....?

Thanks a lot ;)

Re: PHP Time for a Game and Use

Posted: Mon Jul 18, 2011 12:11 am
by Callan S.
You use

Code: Select all

$time=time();
This gives you the time in seconds since some time back in the 70's!

Basically when they start the travel, grab the time and record it in the players entry in the database.

Then just check if X number of seconds have passed since the last page refresh. If so, change their position.

19 minutes would require 1140 seconds to have passed since the last recording of time() to the database;

Re: PHP Time for a Game and Use

Posted: Mon Jul 18, 2011 12:43 am
by 62896dude
In addition to this question, Callan, could this method be incorporated to create a timeout function for users who are inactive for say, 10 minutes? and if so, how?

Re: PHP Time for a Game and Use

Posted: Mon Jul 18, 2011 6:16 am
by Jackolantern
An auto-logout function could be done in PHP through a cron job that would run the database, checking a last activity field for each player, and setting them as logged out if it is over a certain time. To support something like that, you would have to make every activity the player does record into that field (not too hard with the MySQL TIMESTAMP column type), and also make a way for a PHP script to log people out. A script would have no access to log a player out in a session-based login system, so that would have to be done differently.

Alternatively, you can just set an expiration time on player cookies. This would just be a convenience for the players, since this would be shifting the auto-logout responsibility to the browser-side, where it could be tampered with easily. But for a PBBG, pretty much any type of logging out would be a convenience for the player, provided you are not running AJAX polling, since an AFK player is not using any resources (and as far as the server is really concerned, an AFK player basically is logged out since it is stateless).

Re: PHP Time for a Game and Use

Posted: Mon Jul 18, 2011 9:08 am
by Callan S.
Yeah, it's a question of why do you want a log out? As far as I know it doesn't do much? I'm second guessing you maybe want some 'number of players online' count? In that case you could just have it record the time() on each page refresh into a 'online' value in the players entry. Then where you do your number of players online, just count the number of players who's last refresh is smaller than 10 minutes. Anyone who hasn't refereshed in ten minutes is pretty much offline anyway.

Re: PHP Time for a Game and Use

Posted: Mon Jul 18, 2011 2:16 pm
by vitinho444
Hey callan thanks a lot i will try it and feedback later

ps: i was using the $time = HH:MM:SS

but i can use that and then use yours at the background for that system.

Thanks a lot ;)

Ah wait:

If i do:

$time = time(); //this is in seconds like u say
$finish = $time + 1400;

if ($time == $finish)
{
YOU ARE HERE HURRAY!!
}
else
{
You are still walking..
}

Does it works?

Re: PHP Time for a Game and Use

Posted: Mon Jul 18, 2011 2:56 pm
by alexander19
It won't work..because $finish will never be equal to $time in your case, so as Callan said, you must insert the $finish time inside the database and then pull it out whenever you need :

Code: Select all

$playerinfo = mysql_fetch_assoc(mysql_query("SELECT * FROM players WHERE name='$player'"));

//so if you have a row in your players table, called travel_duration,to which you update the required time needed to reach a destination, it would look something like this:

$finish = $playerinfo['travel_duration'] - time();

if($finish <=0)
{
//reached the destination
}
else {
echo "You must wait :".date("h:i:s ",$finish)." to reach your destination.";
}



Re: PHP Time for a Game and Use

Posted: Mon Jul 18, 2011 3:58 pm
by vitinho444
ok i understood, goona test.

So the durations need to be in Seconds?

Re: PHP Time for a Game and Use

Posted: Mon Jul 18, 2011 4:03 pm
by alexander19
Yes, the duration will be in seconds, considering that time() returns the date since 1 January 1970 in seconds..but you can change its aspect by using the date() function.

Re: PHP Time for a Game and Use

Posted: Tue Jul 19, 2011 12:23 am
by Callan S.
vitinho444 wrote:ok i understood, goona test.

So the durations need to be in Seconds?
You can make it larger increments if you want, like

Code: Select all

$minutes=floor(time()/60); 
Will give you the time in minutes. As 60 seconds equals a minute, of course.

In my game I use one that I divide down to days, to determine when a new day has passed in game. That's dividing time() by 86400!