13 4 / 2011
The Refactoring

So, this week I took a step back to evaluate all of the code that has been written so far and look at how it could be improved to make the game as stable as possible, scalable and importantly - more secure against potential cheating.
The Singleton Design Pattern
At this point the code for Animash was based around singletons, a class for the interface, game, controls, networking, player, hud and more all living in the global namespace and all exposing public methods in-order to interact with each other.
Up until now this has made life easy as any class can reference another - and boy, did they! For example, the network code was hooked into the interface, game and controls. This lead to a tangle of functionality and references and all the inherent problems of cluttering up the global namespace.
Cheating
Unfortunately this approach also makes cheating a breeze; simply open your javascript console in any decent browser - watch the events going back and forth between the exposed methods and insert your own to give yourself more speed, kills or any number of other possible tweaks to ensure you win ;-). For example one of the methods you could call was as simple as:
Game.squashAllPlayers();
Handy, huh?!
The Module Pattern
The solution to all of these issues has been to implement the module design pattern, utilising requirejs to load and manage the requirements of each module. Although initial this meant a lot of going backwards to go forwards (completely breaking the game!) - this now results in no exposed methods that can be easily tampered with and a huge reduction in global variables, although there will always be some…