Archive for February, 2007

Browser Games and Software Engineering

Thursday, February 15th, 2007

I recently started writing (yet another) browser based massively multiplayer online game (MMOG) engine. Nothing to fancy, very basic and simple stuff. And I’m of course nowhere near finished. However I spent a considerable amount of time thinking about the fundamental design from the standpoint of a software engineer. This article describes what I came up with.

The very basic design pattern
As all well coded modern applications implement the model-view-controller design pattern I tried to adapt this princible. The pattern facilitates splitting the application into three layers: the model, the view and the controller.

The view (or user interface) represents the data in a convenient way for the user and enables him to trigger certain actions, request data and so on.

The controller (or in my case the game logic) embodies the actual game logic. It is capable of processing and responding to user requests or events in general. A ticker object (a piece of code that updates the game regularly) would also fit into this layer. Additionally it contains a data-link, to connect the controller and the model layer.

Finally the model layer. It represents the game data. In browser games this is often a database of some sort. In my case it’s a shiny PostgreSQL 8.2 database.

The advantage of this pattern is obvious: by decoupling data presentation, business logic and data access/storage the layers may be altered independently. It also simplifies development by reducing the complexity of the engine. Additionally it allows developers to work on different problems, e.g. say a web designer develops the user interface whereas the PHP developer creates the game logic. The developers simply have to define the interfaces between their layers. That’s all they need to know of each other.

Decoupling of the presentation layer nowadays is often achieved by using some sort of template engine. A really cool thing! A detailed description of the mvc design pattern can be found on here.

Controller and Model Design
So far I have designed the controller and the model layers. The following pseudo class-diagram visualizes the concept:

The view layer is not yet implemented. However I plan to allow the user interface to send REST requests to the controller layer. There the received data is validated and the user is authenticated. Then the business logic processes the event, requests some data from the database via the data link, compiles the data together and triggers an output class (XML) to return the data to the user interface. Very straight forward. The user interface hopefully knows what to do with this data and represents it accordingly.

Implementation Consideration
I have already implemented the classes in the diagram shown above. I had to make several choices:

Error handling is a difficult and vital point of the design. I followed one basic principle: only the main class (the business logic class) is allowed to handle errors. Errors occurring in other modules are passed through to the business logic class. This is, in my opinion, vital to maintaining a clearly arranged engine. To return errors I used simple function return values and exceptions. PHP supports a very cool and straight forward exception system (with cool try and catch blocks). Exceptions are also a great way to report errors that occur in a class constructor. However I did not stick with exceptions exclusively because sometimes return values are much easier and faster to implement.

Data validation is a two layer task: first I check if the user tries to foist bogus data on the engine. This can be done with regular expressions and preg or ereg. The second layer ensures that the user does not try to do anything he is not allowed to, like reading someone else’s messages or building more soldiers than he can pay for.

Free Stuff
In case you are interested, the images are created with the ingenious 0.45 version of Inkspace. It’s totally free and really cool. A ZIP file containing the source of the images can be downloaded here. The gorgeous icons are taken from the Tango Desktop Project. A larger version of the second image can be found here. As soon as I finish parts of the game I will put the source code online.