1 package net.sourceforge.simplegamenet.specs.to;
2
3 import java.io.Serializable;
4 import net.sourceforge.simplegamenet.specs.gui.PlayerSettingsPanel;
5 import net.sourceforge.simplegamenet.specs.model.Engine;
6 import net.sourceforge.simplegamenet.specs.tools.StandardPlayerSettings;
7
8 /***
9 * An abstract superclass which represents all player settings for one player, identified by a
10 * playerID. For each player, there is one single <code>PlayerSettings</code> object during a server
11 * session and every connected client holds a copy. A server session spawns over several game
12 * sessions, which start and stop with game start and end events.
13 * <p/>
14 * If these settings are updated the game server and every game player client receive a {@link
15 * GameServer#playerSettingsUpdated(PlayerSettings, PlayerSettings)} event.
16 * <p/>
17 * These player settings should only be changed by the player (using the "Player Settings" panel in
18 * the "Chat" tab and a {@link net.sourceforge.simplegamenet.specs.gui.PlayerSettingsPanel} created
19 * with the {@link #createPlayerSettingsPanel()} method) or by the game server (using custom
20 * methods). The player is not allowed to change these player settings if the game is playing. The
21 * game server can change these player settings at any time but should call {@link
22 * net.sourceforge.simplegamenet.framework.model.ServerEngineImpl#refreshPlayerSettings(Integer)} so
23 * all clients update their copy.
24 *
25 * @author Geoffrey De Smet
26 * @version 1.0, 2003-06-18
27 * @see net.sourceforge.simplegamenet.specs.to.PlayerSettingsMap
28 * @see net.sourceforge.simplegamenet.specs.gui.PlayerSettingsPanel
29 */
30 public abstract class PlayerSettings implements Serializable, Comparable {
31
32 /***
33 * The maximum length of a player's nickname
34 */
35 public static final int MAXIMUM_NICKNAME_LENGTH = 15;
36
37 /***
38 * The player type of the host.
39 */
40 public static final int HOST = 0;
41 /***
42 * The player type of a user, different from the host.
43 */
44 public static final int USER = 10;
45 /***
46 * The player type of a bot.
47 */
48 public static final int BOT = 20;
49
50 /***
51 * The playing state of a player participating in the game.
52 */
53 public static final int PARTICIPATING = 100;
54 /***
55 * The playing state of a player observing the game.
56 */
57 public static final int OBSERVING = 110;
58
59 /***
60 * The <code>EngineImpl</code> of the current application. This variable allows the methods of
61 * these player settings to know the game settings, the player settings of other players, the
62 * game state, etc.
63 */
64 protected transient Engine engine = null;
65
66 private Integer playerID;
67 private int playerType;
68
69 private String nickname;
70 private int playingState;
71
72 /***
73 * Constructs new players settings. The SimpleGameNet framework asks the
74 * <code>GameSettings</code> to create player settings for a new player with {@link
75 * GameSettings#createDefaultPlayerSettings(Integer, int, String)}.
76 * <p/>
77 * This class <code>PlayerSettings</code> can be extended directly to use custom settings or one
78 * of the configurable player settings from the <code>net.sourceforge.simplegamenet.specs.tools</code>
79 * package can be used, such as {@link StandardPlayerSettings} or {@link
80 * ColoredPlayerSettings}.
81 *
82 * @param engine the engine of the current application
83 * @param playerID the playerID which uniquely identifies the player, which never changes
84 * @param playerType the type of the player ({@link #HOST}, {@link #USER} or {@link #BOT}),
85 * which never changes
86 * @param nickname the nickname of the player
87 * @param playingState the playing state of the player ({@link #PARTICIPATING} or {@link
88 * #OBSERVING})
89 */
90 public PlayerSettings(Engine engine, Integer playerID, int playerType, String nickname,
91 int playingState) {
92 this.engine = engine;
93 this.playerID = playerID;
94 this.playerType = playerType;
95 this.nickname = engine.getPlayerSettingsMap().getUniqueNickname(nickname, playerID);
96 this.playingState = playingState;
97 }
98
99 /***
100 * Creates a new <code>PlayerSettingsPanel</code> based on these player settings. This new
101 * <code>PlayerSettingsPanel</code> is shown in the "Player Settings" panel in the "Chat" tab
102 * only with the player whose player settings it represent.
103 *
104 * @return a new <code>PlayerSettingsPanel</code>
105 */
106 public abstract PlayerSettingsPanel createPlayerSettingsPanel();
107
108 /***
109 * Creates a new <code>PlayerSettings</code> based on the <code>PlayerSettingsPanel</code>. This
110 * method fetches all the data a player can edit with custom methods from the
111 * <code>PlayerSettingsPanel</code> and copies any non editable data from these player settings
112 * into the new <code>PlayerSettings</code>.
113 * <p/>
114 * This method will never be called when the game is playing because the player is not allowed
115 * to change his player settings during the game.
116 *
117 * @param gameSettingsPanel a <code>PlayerSettingsPanel</code> changed by the player
118 * @return a new <code>PlayerSettings</code>
119 */
120 public abstract PlayerSettings createChangedPlayerSettings(
121 PlayerSettingsPanel playerSettingsPanel);
122
123 /***
124 * Returns <code>true</code> if the current game state allows these player settings to be
125 * updated to the changed player settings. This method is called after {@link
126 * #createChangedPlayerSettings(PlayerSettingsPanel)} has been called.
127 * <p/>
128 * If a subclass does not overwrite this method it always returns <code>true</code>. A subclass
129 * should override this method if it wants to prevent the player to change his player settings
130 * to certain settings sometimes.
131 *
132 * @param changedPlayerSettings the changed player settings created by {@link
133 * #createChangedPlayerSettings(PlayerSettingsPanel)}
134 * @return <code>true</code> if the player settings are allowed to change
135 */
136 public boolean isChangePlayerSettingsAllowed(PlayerSettings changedPlayerSettings) {
137 return true;
138 }
139
140 /***
141 * Returns <code>true</code> if the player is allowed to chat while the game is playing.
142 * <p/>
143 * If a subclass does not overwrite this method it always returns <code>true</code>.
144 *
145 * @return <code>true</code> if the player is allowed to chat while the game is playing
146 */
147 public boolean isChattingAllowedDuringGame() {
148 return true;
149 }
150
151
152
153
154
155
156
157 /***
158 * Gets the playerID of the player.
159 *
160 * @return the playerID of the player
161 */
162 public Integer getPlayerID() {
163 return playerID;
164 }
165
166 /***
167 * Gets the player type ({@link #HOST}, {@link #USER} or {@link #BOT}) of the player.
168 *
169 * @return the player type of the player
170 */
171 public int getPlayerType() {
172 return playerType;
173 }
174
175 /***
176 * Get the nickname of the player.
177 *
178 * @return the nickname of the player
179 */
180 public String getNickname() {
181 return nickname;
182 }
183
184 /***
185 * Gets the playing state ({@link #PARTICIPATING} or {@link #OBSERVING}) of the player.
186 *
187 * @return the playing state of the player
188 */
189 public int getPlayingState() {
190 return playingState;
191 }
192
193 /***
194 * Sets the playing state ({@link #PARTICIPATING} or {@link #OBSERVING}) of the player. Afther
195 * this method the game server should call {@link net.sourceforge.simplegamenet.framework.model.ServerEngineImpl#refreshPlayerSettings(Integer)}
196 * with the playerID of these player settings.
197 *
198 * @param playingState the playing state of the player
199 */
200 public void setPlayingState(int playingState) {
201 this.playingState = playingState;
202 }
203
204 /***
205 * Compares two player settings based on their playerIDs.
206 *
207 * @param object the object to compare these player settings against
208 * @return these player settings's playerID compared to those of the argument
209 */
210 public boolean equals(Object object) {
211 if (object == null || !(object instanceof PlayerSettings)) {
212 return false;
213 }
214 PlayerSettings playerSettings = (PlayerSettings) object;
215 return playerID.equals(playerSettings.playerID);
216 }
217
218 /***
219 * Compares two player settings based on their playerIDs.
220 *
221 * @param object the object to compare these player settings against
222 * @return these player settings's playerID compared to those of the argument
223 */
224 public int compareTo(Object object) {
225 if (object == null || !(object instanceof PlayerSettings)) {
226 return -1;
227 }
228 return playerID.compareTo(((PlayerSettings) object).playerID);
229 }
230
231 /***
232 * Sets the engine for these game settings. This method should only be used by the SGN
233 * framework.
234 *
235 * @param engine
236 */
237 public final void setEngine(Engine engine) {
238 this.engine = engine;
239 }
240
241 /***
242 * Sets the nickname for these player settings. This method should only be used by the SGN
243 * framework.
244 *
245 * @param nickname
246 */
247 public void setNickname(String nickname) {
248 this.nickname = engine.getPlayerSettingsMap().getUniqueNickname(nickname, playerID);
249 }
250
251 }