0

So as you know in some browser-games such as Travian, Tribalwars and etcetera, you can build up a building, it takes X amount of time and it finishes. So I'm curious how that is done?

Is it a cron-job running every second or what? How are they then doing with troops, they can't have a cron-job running ever millisecond, that wouldn't be resource usage friendly, right?

So I'm really curious about this and I have no idea so I can't really say I have tried. I have however searched around but never found anything helpful.

Thanks.

prk
  • 3,781
  • 6
  • 17
  • 27
  • 1
    a solution would be: time when building starts can be saved in the database, then time is calculated when the user opens the game with respect to the time saved in the database (subtracting 2 dates), then count down continues client side – CodeBird Mar 10 '14 at 21:37
  • event-based, [persistent processes](http://stackoverflow.com/questions/2036654/run-php-script-as-daemon-process): run a PHP script forever (while loop?) to check incoming and process outgoing data. i wouldn't use PHP for this to be honest; you need a daemon. – zamnuts Mar 10 '14 at 21:39

3 Answers3

2

The easiest way to implement something like this is simple timestamps. The request on the front end generates a timestamp based on the constraints given by the details of the request (what type of building you are building, what level you are, if you have bought the upgrade). Then a timestamp is inserted into the database for when the completion occurs. Then, if you want the browser to refresh when the job is up, you make a script on the js that makes a request for all timestamps in queue and reloads when they come up.

  • Mmh, but without an JS for it, how would you make it be able to finish it on it's own? (so you don't have to be sitting on the page forever) – prk Mar 10 '14 at 21:39
  • 2
    Anytime there is a request against the city/village/town from an outside village, a simple request, or a page view for the viewer, then calculation would complete. You don't have to cron it, because it never has to finish unless someone is asking about it. – Devyn Cunningham Mar 10 '14 at 21:44
0

One way: You let the client handle the timer. So the timer will sit on the browser side counting down using javascript. When the time is up it will contact server to see if it's valid (never trust client side code). Server looks up the building and see if it would have been finished by then. Server side doesn't need to keep any timers it just answers requests. Timers are UI side.

Ryan Knopp
  • 582
  • 1
  • 4
  • 12
0

Well, PHP is one of the worst things you could use to build a game... In games, everything is controlled by the main loop that controls the game. So basically, in a game, everything is running inside an infinite loop, albeit one that allows for user input without freezeing the computer, obviously. So that loop takes care of the timing as well, and the way to compute timing will depends on the language on which the game is developed. For web-based games, Java, JavaScript and Flash are usual choices.

  • Java is in no way better than PHP for you to build a game for web. You can have a php service running in the background just as easily as you can a java service. Just because you don't appreciate a language doesn't mean it is wrong. – Devyn Cunningham Mar 10 '14 at 21:45
  • Well, I have experience only with Visual Basic 6 and PHP. How many times did I hear about a game made in PHP? Uhm... never. And just because I said that PHP is not suitable for games, doesn't mean that I don't appreciate it. I appreciate every language for what it is useful. –  Mar 10 '14 at 21:48
  • One of the games he uses in his list as references uses PHP (TribalWars). How have you never heard of one? There are php versions of Chess, and Tic Tac Toe, and Othello. Not all games are graphic intensive looped games. – Devyn Cunningham Mar 10 '14 at 21:51