Thursday, February 27, 2014

Rest API Based Game

Most multiplayer games rely on an active and constant network connection between all players. While this is nearly mandatory for most types of games, where quick responses and active play are crucial, not all games have such requirements, however. Games such as Civilization and other turn based games often have the ability to play over email and other such methods. This isn't always optimal, however, and there are other ways to facilitate turn based play. I explored the usage of a REST API to store and inform clients of the game state. I used a WAMP stack server and Unity for the actual game.

The first step in my exploration was how to keep track of each player. Normally with a multiplayer game, the client is connected to the server for the duration of the game, meaning that the server can assign an identifier to that client connection. With a non-continuous connection, I needed to create a way to identify a player between sessions. I decided that creating a simple account and login system would be the best.

I won't go into the details of the login system, as that is not the focus of the project.

Once a player is logged in the client polls the REST API for the current game state with a GET, which is output in XML format.
while($row = mysqli_fetch_array($result))
{
   //XML format info about player
   echo "
   <playerpos>
      <char>".$row['name']."</char>
      <x>".$row['x']."</x>
      <y>".$row['y']."</y>
      <class>".$row['class']."</class>
      <health>".$row['health']."</health>
   </playerpos>";
}

With the game state, the client assembles the game world and displays it to the player. Part of the game state information is also the current players turn. When the player makes their move, the client sends a POST to the server with their move, which is dealt with server side to determine the new game state.

This method doesn't rely on players being connected simultaneously and allows for larger scalability of a game session, since the server isn't managing a large number of simultaneous connections.

You can check out the simple game here.

No comments:

Post a Comment