Game Manager
From EsWiki
The ElectroServer 4 Manual gives a good overview of the function of the Game Manager. This tutorial explains how to use the Game Manager in an extension.
Contents |
Extension design
Each of your games should be a separate plug-in of the same extension. This makes things much easier when you want to add another game. Plan to put each game in a separate package, with an extra package for the GMSInitializer.
Game plugins
Each of your games will be a separate plugin, possibly multiple plugins. Usually it is easiest if you simply have your class extend BasePlugin. Override any of the methods that you need to customize.
You will also need a separate server level plugin that registers each of your games with GameManager. This is usually named GMSInitializer.
GMSInitializer
You may name your GMSInitializer class anything you wish. For this article we will assume that it is named GMSInitializer. Either extend BasePlugin, or implement Plugin and PluginLifeCycle. Here's an example of what the Java code might resemble:
import com.electrotank.electroserver4.extensions.Plugin; import com.electrotank.electroserver4.extensions.PluginLifeCycle; import com.electrotank.electroserver4.extensions.api.PluginApi; import com.electrotank.electroserver4.extensions.api.value.EsObjectRO; import com.electrotank.electroserver4.extensions.api.value.EsObject; import com.electrotank.electroserver4.extensions.api.value.ExtensionComponentConfiguration; import com.electrotank.electroserver4.extensions.api.value.RoomConfiguration; import com.electrotank.electroserver4.extensions.api.value.GameConfiguration; public class GMSInitializer implements Plugin, PluginLifeCycle { private PluginApi api; /** * Initializes each minigame's plugin, initial game details, and registers it with * the GameManager. * * @param ignored : could contain the XML parameters from the web admin interface, * but in this case is just ignored */ public void init(EsObjectRO ignored) { // use the name of the extension that will contain all the game plugins String extensionName = "MyGamesExtension"; // invoke the initialization method for each of your games initGame1(extensionName); initGame2(extensionName); } private void initGame1(String extensionName) { ExtensionComponentConfiguration gamePlugin = new ExtensionComponentConfiguration(); gamePlugin.setExtensionName(extensionName); // the handle is the name by which the plugin can be addressed // when instantiated in the room gamePlugin.setHandle("Game1HandlePlugin");//Name by which the plugin can be addressed when instantiated in the room // This needs to be the name of the plugin in the Extension.xml file // Usually it is less confusing to just use the same name as the handle gamePlugin.setName("Game1HandlePlugin");//Name of the plugin in the Extension.xml file // Create the room configuration RoomConfiguration roomConfig = new RoomConfiguration(); roomConfig.setCapacity(-1); roomConfig.setDescription("some description of the game goes here"); //add the game plugin(s) roomConfig.addPlugin(gamePlugin); // Create the game configuration // When a user joins a room there are many events that user can potentially receive. // The default subscriptions for a user joining the game are defined here. GameConfiguration gameRoomConfig = new GameConfiguration(); gameRoomConfig.setReceivingRoomListUpdates(false); gameRoomConfig.setReceivingRoomVariableUpdates(false); gameRoomConfig.setReceivingUserListUpdates(true); gameRoomConfig.setReceivingUserVariableUpdates(true); gameRoomConfig.setReceivingVideoEvents(false); gameRoomConfig.setRoomConfiguration(roomConfig); //Create the default GameDetails object // When a game is created it has a game details EsObject associated with it. // This object is publicly seen in the game list, and can be accessed and modified // by the game itself. EsObject esob = new EsObject(); esob.setInteger("n", 1); esob.setInteger("m", 2); esob.setInteger("c", 10); esob.setBoolean("k", true); esob.setEsObject("d", new EsObject()); gameRoomConfig.setInitialGameDetails(esob); // Register the game // Once the game has been registered, users can create a new instance of this game // using the integrated game manager. // Plugins can also create a new game and put users into it. getApi().registerGameConfiguration("Game1", gameRoomConfig); System.out.println("Game1 plugin initialized."); } // Create a method for each game, similar to initGame1 above private void initGame2(String extensionName) { } /** * Use this to shut down any threads you created manually, and tell * other plugins that need to know you are shutting down. In this case, * the method does nothing. */ public void destroy() { // do nothing } public void setApi(PluginApi api) { this.api = api; } public PluginApi getApi() { return api; } }
Deploy the Extension
Extensions gives details on how basic extensions are structured and placed in the ES4InstallationFolder/server/extensions folder. Deployment is discussed in Extension Structure and Deployment.
After placing the needed files into the extensions folder, reboot the ElectroServer, then go to the web admin's Extensions tab. You should see MyGamesExtension listed. We need this to show GMSInitializerPlugin as a Server Level Plugin. Click the New Server-level Component button, and give GMSInitializerPlugin as both the name of the plugin and the handle. Save, then reboot the server again. Check the server console to see the line of text output that you placed at the bottom of your initGame1 method, to verify that your game was initialized. You are now ready to test!
Adding GMSInitializerPlugin at the server level only needs to be done once. Each time you add another game, edit GMSInitializerPlugin then recompile everything and copy the classes to the extensions folder. Edit the Extension.xml file to include the new plugin(s), then reboot the server.
