Search code examples
mysqlactionscript-3pollingmultiplayer

Will polling a MySQL Database suffice for a turn based Flash Game?


I've wanted to expand my game development into the realm of multiplayer, however I don't have the funds for a proper server that has sockets or the time to develop an understanding of how they work and how to use them properly and securely.

What I am however fully capable of is sending and receiving data between Flash and MySQL via PHP in various formats.

If I were to create a turn based game, would polling a MySQL database in this fashion (via PHP) be suitable? At the moment I am thinking along these lines:

  1. Two players find each other by a means such as global listing of open games (pretty standard approach).
  2. An encounter or similar row is created in the database.
  3. The row will store information about who's turn it is and the current state of the game in JSON or XML format. The row will also contain a timestamp from each player representing the last time they had polled the database for game state information. When players complete their turn, the "turn" field will switch over.
  4. Each player polls the database to see if it is their turn yet. If it is, the game state information will be compared to the game state information before they ended their last turn and actions will take place on-screen to reflect the difference. Their turn then begins and the process repeats.
  5. While polling the database, if the opponent hasn't polled the database for their turn within 120 (arbitrary) seconds, the player will receive a notice that the other player has left / timed out / whatever.

Will this be far too slow or unreliable? Or are there other issues that can be foreseen such as the result of both players polling the database at the same time (not sure if that matters)?


Solution

  • Instead of polling, I'd suggest that you look into a technology allowing to push data from the server to the Flash client. The two most common options are either socket connections, or using the Real Time Messaging Protocol, see this question for more details. That's the default approach to building real time games using Flash.

    For PHP, there are a number of open source products or frameworks with support for the RTMP based AMF protocol:

    1. WebORB for PHP
    2. Zend Framework AMF Adobe Systems has contributed support for their open, binary Action Message Format (AMF) protocol to Zend Framework. Very useful if you use Zend Framework already.
    3. amfPHP
    4. sabreamf

    I've personally used Zend Framework AMF with good results, heard good things about WebORB as well. I have not used the other AMF libraries.

    Regarding your specific questions: I'd not store the information in a database as JSON or XML, since you want to query the data. It's far more efficient to store the data as native SQL data.

    Here is a short example showing you how to use Zend Framework AMF to give you an idea how it would work.