PHP Time for a Game and Use

C++, C#, Java, PHP, ect...
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

PHP Time for a Game and Use

Post 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 ;)
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: PHP Time for a Game and Use

Post 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;
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: PHP Time for a Game and Use

Post 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?
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP Time for a Game and Use

Post 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).
The indelible lord of tl;dr
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: PHP Time for a Game and Use

Post 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.
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: PHP Time for a Game and Use

Post 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?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Re: PHP Time for a Game and Use

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


User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: PHP Time for a Game and Use

Post by vitinho444 »

ok i understood, goona test.

So the durations need to be in Seconds?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Re: PHP Time for a Game and Use

Post 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.
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: PHP Time for a Game and Use

Post 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!
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Post Reply

Return to “Coding”