   [web] Cron jobs for AJAX games?
  Hello,I've been looking at games like ikarium and tribalwars and they have resources that pile up for each player by the second.  Those resources pile up regardless of whether the player is online, and can be stolen, consumed, etc.  Are those resources (wood, gold) being incremented via a cron job that runs every second of every day for every player?  Or maybe are they calculated via formula whenever needed based on checkpoints?(X seconds since the lumbermill was constructed) Wouldn't thousands of cron jobs running every second making database updates totally cripple any server?How does that real time reporting work?Thanks   
  I guess it's a combination of request-triggered DB queries (so that only when the information is needed, it is computed) and dead reckoning via &#106avascript.   ;  Thats my best guess, but surely there would be some cron jobs mixed in for scheduled building constructions.   ;  Okay, so when your neighbor shows up to loot yer town, php calculates how much gold you had when you last built something x hours ago, and the rate at which you make gold, gives the attacker the gold, updates the database for your gold amount and production rate with no need for a running total.Thats my best guess, but surely there would be some cron jobs mixed in for scheduled building constructions.  They would also need to take into account popcaps and how/when that effects gold production *in advance*. Somewhat limits complex interactions, but should be good enough I hope.  Making those production projections must be consistent and tricky.EDIT: sry for the double postThanks   ;Quote:Original post by jackobear...but surely there would be some cron jobs mixed in for scheduled building constructions...They would also need to take into account popcaps and how/when that effects gold production *in advance*...No, there isn't any cronjob used for something like that.Why do you need a cronjob for a building queue? If the player is online, he puts his buildings into the queue. In that moment, the start time and the building's identifier gets saved to the database. Now whenever someones needs the actual state of this town (examples: either the player himself after opening the town info, or another player attacking that town), that queue data gets read from the database and the result gets computed by the PHP file (or whatever server side language is used). If the actual time minus start time is greater than the time to build that specific building, that queue record gets deleted in the database, a new record gets inserted which places that building in that town, and there you go. No cronjob needed for that.Now to your popcap for something like gold production. No cronjob used there too. Whenever the player builds something, his actual amount of gold gets saved into the database with an actual timestamp. Now if someone asks the state of the according town (see paragraph above for examples), the PHP file gets that amount and the timestamp from the database, takes the actual time into account, all building which raises the production income and calculates the outcome to save that into the database (erasing the old record from the player itself) and to output the result to the requesting player.And why do you see the amount of gold actualizing in "realtime" in your browser? Because of dead-reckoning with &#106avascript (but on the server side, NOTHING happens during that time!), which Konfusius already said.[Edited by - Anntor on April 15, 2010 2:46:48 AM];  oops, wrong button (second post instead of edit) ;-)Example for production cap:- player opens town info- PHP computes actual amount of gold = 1000.- PHP sends 1000 and cap of 2000 to browser and saves 1000 with actual timestamp 08:00 PM to database- player logs out... 2 hours later, with income of 1000 per hour, player would have 3000 gold, but has a cap of 2000 ...- player logs in at 10:00 PM- PHP gets record from database, sees 1000 at 08:00 PM, calculates with income per hour, makes 3000 now- PHP gets cap from database, cuts 3000 to 2000 and sends that to players browser while saving it to database again... in the meantime, at 09:00 PM ...- player didn't closed his browser, sees town during this hour, &#106avascript precalculates 2000 and stops due to cap, &#106avascript causes NO calculations or triggers on the serverside !(but &#106avascript could trigger something, thats up to the programmer, no cronjob needed here; e.g. for triggering the building queue calculation on the server, when &#106avascript detects that it is time to finish a building from the queue)   ;  Interesting advice, thank you much :)Yeah, I guess it doesn't matter whether you make calculations based on a database table called 'build queue' versus the 'built buildings' table.  Now the concern is to make those login calculations efficient rather than worrying about zillions of cron jobs hammering away.Thanks again   