What is the best way to implement a (relatively simple) multi-user online game?
I am trying to create a game for users to play online using their mobile phones, via an application, similar to Words With Friends, or other "* With Friends" games. Since I have no exp开发者_StackOverflow社区erience with implementing such games, I am unsure of the best practices. Do I want to minimize the amount of code that should have to run on the server, or the amount of code that should run on the client?
My intuition is to use JSON files on the server side, which stores the game state for the current game. Then every time a client opens, have it get the latest game state from the server, and prompt the user accordingly. This eliminates the need for any game-related code to run on the server, and allows the server to merely act as a middleman between all participating clients.
But what if I want the server to be aware enough to notify clients B and C when client A has completed its turn, and B and C can now take action? Do I need to then run code on the server to determine this, or can I have the client inform the server that it is/maybe time for the other clients to take a turn?
I realize that the answers to this are slightly relative, or maybe even obvious, but I was hoping to find some general direction/guidance on the matter so I can know in which direction to begin, and start thinking about the structure of my code/game mechanic.
You might want to examine the code of existing open-source social games to get an idea of how they do it.
If it's truly simple and doesn't require real-time (I'm using this in the context of video games, not hard/soft real-time systems) then a simple server-side REST backend is optimal and would let you use JSON as the client-server communication protocol.
Message propagation to say, clients B and C after A has completed an action against an object they ostensibly care about can be easily accomplished with many to one relationships against whatever it is they're participating in together.
Such that: a Game Board (lets assume this is like scrabble or WWF) has many Users, who will receive a notification anytime a write is performed against said game board.
There are more sophisticated ways to do it, but KISS in my opinion.
what if I want the server to be aware enough to notify clients B and C when client A has completed its turn, and B and C can now take action? Do I need to then run code on the server to determine this, or can I have the client inform the server that it is/maybe time for the other clients to take a turn?
I don't think AJAX designs can (officially) support server -> client push yet, and guarantee cross-browser compatibility. So the clients would have to poll the server to check for updates.
But there is a work-around. Check this (old-ish) article for a design pattern that gets around that, called Comet.
Also, check the wikipedia entry on Comet.
You'd have to implement (a minimal amount of) server side code to make this work.
精彩评论