How do Real Time Strategy games work in PHP?

前端 未结 5 1695
遥遥无期
遥遥无期 2021-02-01 23:55

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 r

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 00:34

    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.

提交回复
热议问题