0
\$\begingroup\$

I'm attempting to develop a multi-platform turn-based game that [I suspect] follows the general client-server pattern. For this question, let's assume I only care about communications inside of a "battle" right now.

Here are the relevant mechanics of a battle: there are some number of "soldiers" in each battle. Each soldier is under the control of a player and a player may be in control of multiple soldiers in a given battle. Each turn only 1 soldier makes an action, the server will contain logic to determine which soldier that is. There are no time limits on turns. The player in control of that soldier select an action for that soldier which is sent to the server. The server will then determine the result of that action and send that information to every client.

This diagram can hopefully show my intended flow of information:

muh diagram

With each communication to client I was expecting to send a json of a ClientBattle object that holds all the data a client needs. On communications to the server, I was expecting to send a json of an Action object that contains all the info the server needs to calculate the outcome and advance the battle.

Unfortunately, I'm not quite sure how I would go about implementing this.

An approach that I would know how to implement that mimics this behavior would be to have each client HTTP GET a turn number from the server at a regular interval of ~3 seconds. If the turn number matches the client's data it does nothing until asking the server again in 3 seconds. If the number doesn't match, then the client HTTP GETs new battle data from the server and uses that to update its display. On client to server communication I'd send a HTTP POST request.

How would I clean this up - or - how could I implement it as in the diagram?

\$\endgroup\$
  • \$\begingroup\$ Any reason why you cannot leverage a technology that lets the server push the data to you? \$\endgroup\$ – Steven Dec 30 '15 at 22:50
  • \$\begingroup\$ On thinking about this, other then a turn #, don't know how else you would implement your pattern if polling is your only choice. However, once it is your turn you no longer need to poll until you submit your action. Then you have to start polling again. \$\endgroup\$ – Steven Dec 30 '15 at 23:26
  • \$\begingroup\$ "Any reason why you cannot leverage a technology that lets the server push the data to you?" @Steven - A solution like this would be great! Unfortunately, I don't know how to do that (or what technology I could use to make the server push to clients). Unfortunately networking is a bit of a dark spot in my knowledge. Can you link me to some reading material or hit me with some keywords so I can figure it out? \$\endgroup\$ – ZackDeRose Dec 31 '15 at 2:08
0
\$\begingroup\$

Using simple AJAX polling I think you've come up with the best solution, with the exception that a client doesn't need to be in a polling state while it is their turn. This reduces some of the polling. I also think you can reduce the frequency of polling to 5 or even 10 seconds since someone waiting for their turn won't know when exactly the other player finished their turn. You could also perform an incremental system where you poll ever 3 seconds until 15 seconds have passed, and then slow down, under the assumption that you are not in a rapid pace state of the game, so neither player will care for or require really responsive turn-taking.

Otherwise you want to investigate some kind of push notification. Here are some links to other questions:

https://stackoverflow.com/questions/16122911/efficient-reloading-data-pushing-data-from-server-to-client

https://stackoverflow.com/questions/5753674/php-best-way-to-push-data-from-server-to-clients

And some other information:

http://www.abrandao.com/2013/06/websockets-html5-php/

\$\endgroup\$
  • \$\begingroup\$ Thanks!! Read up some here Oracle's WebSocket API on the Java WebSockets API. Ended up finding this repository: TooTallNate's Java-WebSocket and was able to successfully push messages from server to client! Will post a new answer with code once I'm able to replicate the diagram above. \$\endgroup\$ – ZackDeRose Dec 31 '15 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.