These days I am trying to design architecture of a new MMORPG mobile game for my company. This game is similar to Mafia Wars, iMobsters, or RISK. Basic idea is to prepare an army to battle your opponents (online users).
Although I have previously worked on multiple mobile apps but this is something new to me. After a lot of struggle, I have come up with an architecture which is illustrated with the help of a high-level flow diagram:
We have decided to go with client-server model. There will be a centralized database on server. Each client will have its own local database which will remain in sync with server. This database acts as a cache for storing things that do not change frequently e.g. maps, products, inventory etc.
With this model in place, I am not sure how to tackle following issues:
- What would be the best way of synchronizing server and client databases?
- Should an event get saved to local DB before updating it to server? What if app terminates for some reason before saving changes to centralized DB?
- Will simple HTTP requests serve the purpose of synchronization?
- How to know which users are currently logged in? (One way could be to have client keep on sending a request to server after every x minutes to notify that it is active. Otherwise consider a client inactive).
- Are client side validations enough? If not, how to revert an action if server does not validate something?
I am not sure if this is an efficient solution and how it will scale. I would really appreciate if people who have already worked on such apps can share their experiences which might help me to come up with something better. Thanks in advance.
Additional Info:
Client-side is implemented in C++ game engine called marmalade. This is a cross platform game engine which means you can run your app on all major mobile OS. We certainly can achieve threading and which is also illustrated in my flow diagram. I am planning to use MySQL for server and SQLite for client.
This is not a turn based game so there is not much interaction with other players. Server will provide a list of online players and you can battle them by clicking battle button and after some animation, result will be announced.
For database synchronization I have two solutions in mind:
- Store timestamp for each record. Also keep track of when local DB was last updated. When synchronizing, only select those rows that have a greater timestamp and send to local DB. Keep a isDeleted flag for deleted rows so every deletion simply behaves as an update. But I have serious doubts about performance as for every sync request we would have to scan the complete DB and look for updated rows.
- Another technique might be to keep a log of each insertion or update that takes place against a user. When the client app asks for sync, go to this table and find out which rows of which table have been updated or inserted. Once these rows are successfully transferred to client remove this log. But then I think of what happens if a user uses another device. According to logs table all updates have been transferred for that user but actually that was done on another device. So we might have to keep track of device also. Implementing this technique is more time consuming but not sure if it out performs the first one.
Answer
If it's not a "real time" game in the sense that players don't need to see the immediate result of another player's actions on a game scene then you should be fine with HTTP requests. But Keep in mind the overhead of HTTP.
That said using HTTP will not save you from designing your communication protocol with care. But if you are in charge of both the server and the client side you are lucky as you can adjust the protocol when you need it.
To synchronize between the Master DB and the Client DB you can use whatever transport protocol you have access to, HTTP or others. The important part is the logics behind the sync. For a straightforward approach, simply pool from the server all the latest changes needed by the client since the last timestamp in the client DB. Apply It to the client DB and go with that. If you have more changes on client side upload it if it's still relevant, otherwise discard.
Some games don't even use a local DB, they simply keep track of the status by pooling the relevant info from the server when needed.
If loosing local events is not acceptable then yes, you should have local storage and save to that storage as often as possible. You can try doing that before each network send.
To check for active users we used to ping with HTTP every 20 seconds on a successful game... This value went up immediately as the servers have been overloaded :( The server team did not think about success. So I would tell you to add a message or some sort of special header in your communication protocol which will let you reconfigure your clients (for ping frequency load balancing and other communication related values).
Client side validations are enough if you don't mind cheaters, hackers and other automated scripts attacking your game. The server can simply refuse your action and send an error message with some details. You handle that error message by either rolling back the local changes or by not applying them to your local storage if you can wait until the server responds to actually save the changes.
We used the command pattern in our game to allow for simple and efficient rollback of failed or invalid user actions. Commands were played locally sent to the peers or translated to the server message and then applied on the peers or checked on the server, in case of a problem the commands were unplayed and the game scene went back to the initial state with a notification.
Using HTTP is not a bad idea as, later, it will allow you to integrate very easily with Flash or HTML5 clients, it's flexible, you can use any type of server scripting language and with basic load balancing techniques add more servers later without much effort to scale your backend.
You have a lot to do but it's a fun job... So enjoy!
No comments:
Post a Comment