This is a documentation for Board Game Arena: play board games online !
User:SwHawk/Create Modular Code
This page will cover steps you can take as a BGA developer to make your code more modular and better organized. The way the framework is implemented makes the game logic and the interface logic files very monolithic. While this is not a problem in itself, even simple to moderately complex game may require this files to be over 5k lines of code. Thus you can quickly become lost in all the functions you need to declare, where they are declared, and where they are called.
But there are steps that you can follow to take advantage of PHP's Object Oriented Code and the dojo toolkit's AMD loader to organize your code in separate files. Let's see what's available:
On the PHP side
Game logic methods
The way the framework is implemented, your game logic is gathered inside a class (`YourProjectName`) extending the `Table` class. You define your methods there so that they may be called from other parts of the framework (as long as they're public methods obviously).
But you can take advantage of PHP Traits. In a nutshell, Traits are a collection of methods that can be reused in different classes, to avoid code duplication. Here we're not going to reuse them anywhere else than in the game class, but this little trick allows us to put methods definitions in other files, and in so doing to avoid monolithic code.
Basically you create a Trait in the `modules` directory, for instance:
[[modules\UtiltityFunctionsTrait.php]] <?php namespace YourProjectNamespace\Modules; trait UtilityFunctionsTrait { public function myFirstUtilityFunction($args) { //Your code here } }
Then you can include it in you project's `game.php` file like so:
[[X_game.php]] <?php /** *------ * BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com> * ProgressEvolutionTechnologySwH implementation : © <Your name here> <Your email address here> * * This code has been produced on the BGA studio platform for use on http://boardgamearena.com. * See http://en.boardgamearena.com/#!doc/Studio for more information. * ----- * * progressevolutiontechnologyswh.game.php * * This is the main file for your game logic. * * In this PHP file, you are going to defines the rules of the game. * */ require_once 'modules/UtilityFunctionsTrait.php'; //Here we require the trait to be loaded when the class is loaded use YourProjectNamespace\Modules\UtilityFunctionsTrait; //We import the trait to be used directly as UtilityFunctionsTraits, otherwise we would have to use the Fully qualified class name (YourProjectNamespace\Modules\UtilityFunctionsTrait) class YourProjectName extends Table { //... ////////////////////////////////////////////////////////////////////////////// //////////// Utility functions //////////// /* In this space, you can put any utility methods useful for your game logic */ use UtilityFunctionsTrait; //Here we actually include the trait inside the class, making all the trait's methods available as class methods, keeping their visibility scope (so a private method is still private) }