15

Some MMO Real Time Strategy games such as Travian or oGame are coded in PHP.

Could you briefly explain how such a game works behind the scenes ? How does the game make real time DB updates without player requests ?

Also, what kind of server load / bandwidth would one have to expect when running a RTS game such as Travian with 1000 active players ?

conradkleinespel
  • 6,560
  • 10
  • 51
  • 87
  • 8
    I would not code this in PHP, even if it can be done. I would code this in NodeJS. – AlienWebguy Feb 27 '12 at 18:06
  • 2
    Well, that's what I thought, but Travian and oGame are quite "famous" and they are PHP. I'm not saying it's the right way, but it seems to be doable. – conradkleinespel Feb 27 '12 at 18:12
  • 3
    OGame was written years ago by an average private developer as a hobby project (and trust me, you do not want to see any of the really old OGame code which has hopefully been replaced by now). At this point, node.js did not even exist and python was not really widespread for web applications. So unless you wanted to use Perl, Java or ASP, PHP was pretty much the only solution. – ThiefMaster Feb 27 '12 at 18:16
  • @AlienWebguy Can you please tell me why you prefer `NodeJS` over `PHP` if you are going to do something like Travian ? – Accountant م Aug 02 '18 at 07:04

5 Answers5

11

Even though this topic is rather old, I do think I still have a 'better' (if I may say so myself) answer to your question then the vague "the updates are done by cronjobs" answer.

Travian i.e. gives you the illusion of it being real-time through the use of javascript. What actually happens in the back is the following:

Player A sends an attack to player B. In the MySQL database this is recorded with a timestamp of arrival. Every time player A changes or refreshes a page a script gets launched (by using includes) that checks for any activity in regards to this player (reinforcements arriving, attacks arriving at targets etc.). The script obviously checks the current time and looks at all activities with a timestamp that is less than the current one. This means the action should have taken place. Right at that moment the action actually gets processed.

This also means that if neither player A nor player B ever log in again that the attack will never be calculated, unless someone else also attacks player B - then all activities for player B and the attacking player will be processed.

njs
  • 330
  • 3
  • 11
  • Thanks. It's what I had in mind after thinking about it a bit more :) – conradkleinespel Jan 08 '13 at 01:27
  • 1
    Only problem is, when an attack influences the highscore or as in ogame will create some debris around the battlefield. So the server has to process those battles even when both player didn't log in (in case of the highscore this might be done every hour.. for the debris this perhaps more often). Also it would be good to use idle cpu-time whenever possible to calculate those updates.. so imagine at the evening everybody does his actions and logs off.. and in the morning the server needs to calculate for everyone :) – balrok May 04 '13 at 12:41
  • 1
    So how they calculate that rice, wood, etc. are increasing in the storage? Very interesting. There are any documentation about it? – Lazaro Fernandes Lima Suleiman Jan 26 '16 at 00:24
  • what you describe is "update data lazily' but this does not seem to work like that in modern ogame where for example a Combat result will be computed at impact time, even if both players are logged out. It could still be computed lazily, for example, only when a player open the galaxy menu or open the statistics. But this might get out of hands and we have no proof they use this. – Antoine Weber May 08 '22 at 21:31
11

Usually there are two parts: The web interface and a background daemon (often called the "event handler").

The webinterface does all the readonly stuff and harmless things where race conditions are not a problem at all - password changes, renaming things, etc.

More important things such as building units or fighting other players are submitted to the event handler where they'll be checked, validated and then stored until the execution time has been reached. Performing checks at this place instead of in the webinterface has the advantage that you completely remove the risk of race conditions (such as launching ships containing all units on a planet while at the same time building something expensive which would basically result in duplicating the available units of the player) as long as you ensure only one action/event runs at a given time (e.g. no multithreading, multiprocessing etc.).

If yours is not fully realtime but uses "ticks" (e.g. actions only happen every x minutes), you can of course use a cronjob instead of a background daemon - but then you need to use some other way to avoid race conditions.


In my own game I have a background daemon which has a RPC-like interface so in the webinterface I simply call a function syncCall('someFunction', ....); which will then connect to the background daemon via a socket and execute the given function, returning whatever that function returns.

However, if I wrote a new game nowadays, I'd certainly go with an asynchronous solution such as node.js or one of the async python frameworks. It removes the need of having two different parts - but for some parts you'll have to take care about locking since whenever you return from one of your functions called by node itself, a callback from another event might be executed.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 1
    So the event handler is not written in PHP then, or most likely not, right ? – conradkleinespel Feb 27 '12 at 18:15
  • 2
    It's usually easy if you use the same technique for everything so in my case it is written in PHP. This has the advantages that I can include the same function libraries and do not have to write the same things in two languages. – ThiefMaster Feb 27 '12 at 18:17
  • Thank you very much for this great answer. Is [this](https://stackoverflow.com/questions/2036654/run-php-script-as-daemon-process) what you mean by *"I have a background daemon"* ? – Accountant م Feb 28 '18 at 14:50
  • 1
    @Accountantم: I was running it within a `screen` session (so technically not a daemon); nowadays I would probably use a docker container or a systemd service to run it. Most important, I would certainly not use PHP for it anymore ;) – ThiefMaster Feb 28 '18 at 14:52
4

The updates are done by cron jobs most likely or another possibility is that they do it at login/any page change. Bandwidth may vary a lot, based on how active users are, how much possibilities there are, etc. I think you should measure it on localhost/test hist with example requests because it depends very much on the project.

Also, if there'll be considerable amount of players, etc., I'd think of not coding it in PHP+MySQL but in Python&PostgreSQL, maybe even Java, or another systems.

axiomer
  • 2,088
  • 1
  • 17
  • 26
  • Cron jobs ? Would that mean that they have to do a cron every second ? Because it's actually real time. Even if you're not online, other players see the results of other players actions live. – conradkleinespel Feb 27 '12 at 18:14
  • 1
    I think these are half-realtime, perhaps they do it every minute or so. – axiomer Feb 27 '12 at 18:15
  • Well, as soon as attacks from you against others "hit", you can see the result, so it's got to be stored in the database somewhere, and immediately, right ? And people can send troops from one account to another, right before an attack, so it's gotta be real time, or at least their backend has to check for changes every single second, or maybe even more. Am I right ? – conradkleinespel Feb 27 '12 at 18:18
  • 1
    I've never played these games, but I'm sure that only timed actions (attack, deploy, etc.) are using cron/event handler (mentioned by ThiefMaster), others (starting attack, deploy) are done by the click itself. And to the timed, I don't know which is better, you'll have to decide that does it have to be 100% realtime or not. – axiomer Feb 27 '12 at 18:22
  • 2
    Why Python/PostgreSQL over PHP/MySql ? Speed ? – conradkleinespel Feb 27 '12 at 21:19
  • Faster, the language is cleaner and if later want to use Java (also faster) then it is easier to connect. Negative is that PHP has better frameworks and way more developers - and if it's a big project (and this seems), it doesn't matter how good you're you might develop it but bugfixes etc. can't be handled by one person in a reasonable time – axiomer Feb 28 '12 at 08:28
1

Well as user1842120 said: Travian realtime is a illusion..

Ill simply explain how 'realtime' works in Web Games...

Imagine there are 3 players, 2 of them are online.., P1 attacks P3, When reloading the page or changing page the script will be activated and P3 will be attacked..

P3 is offline, He can't see the attack but it's happening., When P3 comes online P3 sees that his/her village is being attacked, Simply:

You only 1 need one client (Online user / Browser on page) to run the game....

-3

I think that I would ouse iframes and javascript to update them. So like 1 info iFrame that's hidden and qui/output iFrames who's url refers to php and which are updating all the time.

doory
  • 47
  • 7