1 package net.sourceforge.simplegamenet.specs.to; 2 3 import java.io.Serializable; 4 import net.sourceforge.simplegamenet.specs.gui.GameSettingsPanel; 5 import net.sourceforge.simplegamenet.specs.model.Engine; 6 import net.sourceforge.simplegamenet.specs.tools.MultiPlayerGameSettings; 7 import net.sourceforge.simplegamenet.specs.tools.StandardPlayerSettings; 8 9 /*** 10 * An abstract superclass which represents all game settings that determine how a game is played. 11 * There is one single <code>GameSettings</code> object during a server session and every connected 12 * client holds a copy. A server session spawns over several game sessions, which start and stop 13 * with game start and end events. 14 * <p/> 15 * If these settings are updated the game server and every game player client receive a {@link 16 * GameServer#gameSettingsUpdated(GameSettings, GameSettings)} event. 17 * <p/> 18 * These game settings should only be changed by the host (using the "Game Settings" tab and a 19 * {@link net.sourceforge.simplegamenet.specs.gui.GameSettingsPanel} created with the {@link 20 * #createGameSettingsPanel()} method) or by the game server (using custom methods). The host is not 21 * allowed to change these game settings if the game is playing. The game server can change these 22 * game settings at any time but should call {@link net.sourceforge.simplegamenet.framework.model.ServerEngineImpl#refreshGameSettings()} 23 * so all clients update their copy. 24 * 25 * @author Geoffrey De Smet 26 * @version 1.0, 2003-06-18 27 * @see net.sourceforge.simplegamenet.framework.model.EngineImpl#getGameSettings() 28 * @see net.sourceforge.simplegamenet.specs.gui.GameSettingsPanel 29 */ 30 public abstract class GameSettings implements Serializable { 31 32 /*** 33 * The <code>EngineImpl</code> of the current application. This variable allows the methods of 34 * these game settings to know the player settings of each player, the game state, etc. 35 * <p/> 36 * Initially this variable is <code>null</code>, but when an engine is created it is set by the 37 * SimpleGameNet framework. During construction this variable is <code>null</code> and during 38 * the methods {@link #createGameSettingsPanel()} and {@link #createChangedGameSettings(net.sourceforge.simplegamenet.specs.gui.GameSettingsPanel)} 39 * it can be <code>null</code>. 40 */ 41 protected transient Engine engine = null; 42 43 /*** 44 * Constructs new game settings. The SimpleGameNet framework asks the <code>GameFactory</code> 45 * to create game settings for him with {@link net.sourceforge.simplegamenet.specs.model.GameFactory#createDefaultGameSettings()}. 46 * <p/> 47 * This class <code>GameSettings</code> can be extended directly to use custom settings or one 48 * of the configurable game settings from the <code>net.sourceforge.simplegamenet.specs.tools</code> 49 * package can be used, such as {@link MultiPlayerGameSettings} or {@link 50 * TwoPlayerGameSettings}. 51 * <p/> 52 * During the construction the engine variable is <code>null</code>. 53 */ 54 protected GameSettings() { 55 } 56 57 /*** 58 * Creates a new <code>GameSettingsPanel</code> based on these game settings. This new 59 * <code>GameSettingsPanel</code> is shown in the "Connection wizard" dialog or the "Game 60 * settings" tab. That panel is enabled with the host which allows him to change the game 61 * settings, but it is disabled with all other users, who can just see the game settings. 62 * <p/> 63 * During this method the engine variable can be <code>null</code>. 64 * 65 * @return a new <code>GameSettingsPanel</code> 66 */ 67 public abstract GameSettingsPanel createGameSettingsPanel(); 68 69 /*** 70 * Creates a new <code>GameSettings</code> based on the <code>GameSettingsPanel</code>. This 71 * method fetches all the data a host can edit with custom methods from the 72 * <code>GameSettingsPanel</code> and copies any non editable data from these game settings into 73 * the new <code>GameSettings</code>. 74 * <p/> 75 * This method will never be called when the game is playing because the host is not allowed to 76 * change the game settings during the game. 77 * <p/> 78 * During this method the engine variable can be <code>null</code>. 79 * 80 * @param gameSettingsPanel a <code>GameSettingsPanel</code> changed by the host 81 * @return a new <code>GameSettings</code> 82 */ 83 public abstract GameSettings createChangedGameSettings(GameSettingsPanel gameSettingsPanel); 84 85 /*** 86 * Returns <code>true</code> if the current game state allows these game settings to be updated 87 * to the changed game settings. This method is called after {@link 88 * #createChangedGameSettings(GameSettingsPanel)} has been called. 89 * <p/> 90 * If a subclass does not overwrite this method it always returns <code>true</code>. A subclass 91 * should override this method if it wants to prevent the host to change the game settings to 92 * certain settings sometimes. 93 * 94 * @param changedGameSettings the changed game settings created by {@link #createChangedGameSettings(net.sourceforge.simplegamenet.specs.gui.GameSettingsPanel)} 95 * @return <code>true</code> if the game settings are allowed to change 96 */ 97 public boolean isChangeGameSettingsAllowed(GameSettings changedGameSettings) { 98 return true; 99 } 100 101 /*** 102 * Returns <code>true</code> if the current game state and these game settings allow the new 103 * player to connect. If <code>true</code> the {@link #createDefaultPlayerSettings(Integer, int, 104 * String)} is called next, which will decide whether the new player becomes a participant or 105 * observer. 106 * <p/> 107 * If a subclass does not overwrite this method it always returns <code>true</code>. A subclass 108 * should overwrite this method if it wants to block new players sometimes. 109 * 110 * @param playerType the new player's playerType, as defined in {@link net.sourceforge.simplegamenet.specs.to.PlayerSettings} 111 * @return <code>true</code> if the player is allowed to connect 112 */ 113 public boolean isCreateDefaultPlayerSettingsAllowed(int playerType) { 114 return true; 115 } 116 117 /*** 118 * Creates a new <code>PlayerSettings</code> based on the current game state. Subclasses that 119 * overwrite this method should not make any changes to the playerID, playerType or nickname and 120 * pass it to the constructor of a subclass of <code>PlayerSettings</code>, together with the 121 * engine variable. 122 * <p/> 123 * If a subclass does not overwrite this method it creates a new <code>StandardPlayerSettings</code>, 124 * a participating if the game is not playing and observing if the game is playing. A subclass 125 * should overwrite this method if it wants to use custom <code>PlayerSettings</code>. 126 * 127 * @param playerID the new player's unique identification 128 * @param playerType the new player's playerType, as defined in {@link net.sourceforge.simplegamenet.specs.to.PlayerSettings} 129 * @param nickname the new player's preferred nickname filtered from bad language if bad 130 * language is filtered by the server. 131 * @return default player settings for the new player 132 */ 133 public PlayerSettings createDefaultPlayerSettings(Integer playerID, int playerType, 134 String nickname) { 135 int playingState = engine.isGamePlaying() ? PlayerSettings.OBSERVING 136 : PlayerSettings.PARTICIPATING; 137 return new StandardPlayerSettings(engine, playerID, playerType, nickname, 138 playingState); 139 } 140 141 /*** 142 * Return <code>true</code> if the current game state and these game settings allow a player to 143 * change his player settings. 144 * <p/> 145 * If <code>true</code> the {@link PlayerSettings#isChangePlayerSettingsAllowed(PlayerSettings)} 146 * is called next, which checks if the old player settings allow the change. 147 * <p/> 148 * If a subclass does not overwrite this method it always returns <code>true</code>. A subclass 149 * should overwrite this method if it wants to block players changing their player settings 150 * sometimes. 151 * 152 * @param currentPlayerSettings the current player settings 153 * @param changedPlayerSettings the changed player settings 154 * @return <code>true</code> if player is allowed to change his player settings 155 */ 156 public boolean isChangePlayerSettingsAllowed(PlayerSettings currentPlayerSettings, 157 PlayerSettings changedPlayerSettings) { 158 return true; 159 } 160 161 /*** 162 * Returns <code>true</code> if the current game state allows a game to be started. 163 * <p/> 164 * If a subclass does not overwrite this method it always returns <code>true</code>. A subclass 165 * should overwrite this method if it wants to disallow a game to be started, for example when 166 * there aren't enough participants. 167 * 168 * @return <code>true</code> if a game is allowed to start 169 */ 170 public boolean isStartGameAllowed() { 171 return true; 172 } 173 174 /*** 175 * Sets the Engine for these game settings. This method should only be used by the SGN 176 * framework. 177 * 178 * @param engine 179 */ 180 public final void setEngine(Engine engine) { 181 this.engine = engine; 182 } 183 184 }