How do Real Time Strategy games work in PHP?
Asked Answered
S

5

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 ?

Selmner answered 27/2, 2012 at 18:3 Comment(4)
I would not code this in PHP, even if it can be done. I would code this in NodeJS.Airwaves
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.Selmner
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.Stines
@AlienWebguy Can you please tell me why you prefer NodeJS over PHP if you are going to do something like Travian ?Hermia
T
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.

Tamer answered 7/1, 2013 at 23:45 Comment(4)
Thanks. It's what I had in mind after thinking about it a bit more :)Selmner
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 :)Minute
So how they calculate that rice, wood, etc. are increasing in the storage? Very interesting. There are any documentation about it?Thimble
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.Mantic
S
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.

Stines answered 27/2, 2012 at 18:10 Comment(4)
So the event handler is not written in PHP then, or most likely not, right ?Selmner
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.Stines
Thank you very much for this great answer. Is this what you mean by "I have a background daemon" ?Hermia
@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 ;)Stines
T
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.

Tamer answered 7/1, 2013 at 23:45 Comment(4)
Thanks. It's what I had in mind after thinking about it a bit more :)Selmner
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 :)Minute
So how they calculate that rice, wood, etc. are increasing in the storage? Very interesting. There are any documentation about it?Thimble
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.Mantic
E
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.

Elvinelvina answered 27/2, 2012 at 18:7 Comment(6)
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.Selmner
I think these are half-realtime, perhaps they do it every minute or so.Elvinelvina
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 ?Selmner
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.Elvinelvina
Why Python/PostgreSQL over PHP/MySql ? Speed ?Selmner
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 timeElvinelvina
M
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....

Mopey answered 8/1, 2014 at 21:27 Comment(0)
D
-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.

Dalury answered 2/12, 2013 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.