1 package net.sourceforge.simplegamenet.specs.to; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map; 6 import net.sourceforge.simplegamenet.specs.model.Engine; 7 8 /*** 9 * A map which contains all <code>PlayerSettings</code>, one for each player who joined the game 10 * session. 11 * 12 * @author Geoffrey De Smet 13 * @version 1.0, 2003-06-18 14 * @see PlayerSettings 15 * @see net.sourceforge.simplegamenet.framework.model.EngineImpl#getPlayerSettingsMap() 16 */ 17 public class PlayerSettingsMap { 18 19 private int participantsAmount = 0; 20 private Map map; 21 22 public PlayerSettingsMap() { 23 map = new HashMap(8); 24 } 25 26 public PlayerSettingsMap(Engine engine, PlayerSettings[] playerSettingsArray) { 27 map = new HashMap(Math.max(playerSettingsArray.length, 8)); 28 for (int i = 0; i < playerSettingsArray.length; i++) { 29 playerSettingsArray[i].setEngine(engine); 30 map.put(playerSettingsArray[i].getPlayerID(), playerSettingsArray[i]); 31 if (playerSettingsArray[i].getPlayingState() == PlayerSettings.PARTICIPATING) { 32 participantsAmount++; 33 } 34 } 35 } 36 37 /*** 38 * Returns <code>true</code> if this map contains a mapping for the specified playerID. 39 * 40 * @param playerID the playerID whose presence in this map is to be tested 41 * @return <code>true</code> if this map contains a mapping for the specified playerID 42 */ 43 public boolean containsPlayerID(Integer playerID) { 44 return map.containsKey(playerID); 45 } 46 47 /*** 48 * Returns the <code>PlayerSettings</code> to which the specified playerID is mapped in this 49 * map, or <code>null</code> if the map contains no mapping for the playerID. 50 * 51 * @param playerID the playerID whose associated <code>PlayerSettings</code> is to be returned 52 * @return the <code>PlayerSettings</code> to which the specified playerID is mapped in this 53 * map 54 */ 55 public PlayerSettings getPlayerSettings(Integer playerID) { 56 Object object = map.get(playerID); 57 return object == null ? null : (PlayerSettings) object; 58 } 59 60 /*** 61 * Returns the <code>PlayerSettings</code> of the host or <code>null</code> if the map contains 62 * no mapping for the host. 63 * 64 * @return the <code>PlayerSettings</code> of the host 65 */ 66 public PlayerSettings getHostPlayerSettings() { 67 for (Iterator it = map.entrySet().iterator(); it.hasNext();) { 68 PlayerSettings playerSettings = (PlayerSettings) ((Map.Entry) it.next()).getValue(); 69 if (playerSettings.getPlayerType() == PlayerSettings.HOST) { 70 return playerSettings; 71 } 72 } 73 return null; 74 } 75 76 /*** 77 * Returns an array of all the playerIDs that this map contains. 78 * 79 * @return an array of all the playerIDs that this map contains 80 */ 81 public Integer[] getPlayerIDs() { 82 Integer[] playerIDs = new Integer[map.size()]; 83 Iterator iterator = map.keySet().iterator(); 84 for (int i = 0; i < map.size() && iterator.hasNext(); i++) { 85 playerIDs[i] = (Integer) iterator.next(); 86 } 87 return playerIDs; 88 } 89 90 /*** 91 * Returns an array of all the <code>PlayerSettings</code> that this map contains. 92 * 93 * @return an array of all the <code>PlayerSettings</code> that this map contains 94 */ 95 public PlayerSettings[] getPlayerSettingsArray() { 96 PlayerSettings[] playerSettingsArray = new PlayerSettings[map.size()]; 97 Iterator iterator = map.entrySet().iterator(); 98 for (int i = 0; i < map.size() && iterator.hasNext(); i++) { 99 playerSettingsArray[i] = (PlayerSettings) ((Map.Entry) iterator.next() 100 ).getValue(); 101 } 102 return playerSettingsArray; 103 } 104 105 /*** 106 * Returns an array of the playerIDs of all the participants that this map contains. 107 * 108 * @return an array of the playerIDs of all the participants that this map contains 109 */ 110 public Integer[] getParticipantIDs() { 111 Integer[] participantIDs = new Integer[participantsAmount]; 112 Iterator iterator = map.keySet().iterator(); 113 for (int i = 0; i < participantsAmount && iterator.hasNext();) { 114 Integer playerID = (Integer) iterator.next(); 115 if (getPlayerSettings(playerID).getPlayingState() 116 == PlayerSettings.PARTICIPATING) { 117 participantIDs[i] = playerID; 118 i++; 119 } 120 } 121 return participantIDs; 122 } 123 124 /*** 125 * Returns an array of the <code>PlayerSettings</code> of all the participants that this map 126 * contains. 127 * 128 * @return an array of the <code>PlayerSettings</code> of all the participants that this map 129 * contains 130 */ 131 public PlayerSettings[] getParticipantSettingsArray() { 132 PlayerSettings[] participantSettingsArray = new PlayerSettings[participantsAmount]; 133 Iterator iterator = map.entrySet().iterator(); 134 for (int i = 0; i < participantsAmount && iterator.hasNext();) { 135 participantSettingsArray[i] = (PlayerSettings) ((Map.Entry) iterator.next() 136 ).getValue(); 137 if (participantSettingsArray[i].getPlayingState() 138 == PlayerSettings.PARTICIPATING) { 139 i++; 140 } 141 } 142 return participantSettingsArray; 143 } 144 145 /*** 146 * Returns the number of participants. 147 * 148 * @return the number of participants 149 */ 150 public int getParticipantsAmount() { 151 return participantsAmount; 152 } 153 154 /*** 155 * Returns the number of <code>PlayerSettings</code>. 156 * 157 * @return the number of <code>PlayerSettings</code> 158 */ 159 public int getConnectionsAmount() { 160 return map.size(); 161 } 162 163 public boolean containsPlayerSettings(Integer playerID) { 164 return map.containsKey(playerID); 165 } 166 167 /*** 168 * This method should only be used by the SGN framework. 169 * 170 * @param nickname 171 * @param ignorablePlayerID 172 * @return 173 */ 174 public String getUniqueNickname(String nickname, Integer ignorablePlayerID) { 175 nickname = nickname.replaceAll(" ", "_"); 176 if (nickname.length() > PlayerSettings.MAXIMUM_NICKNAME_LENGTH) { 177 nickname = nickname.substring(0, PlayerSettings.MAXIMUM_NICKNAME_LENGTH); 178 } 179 int appendIndex = 0; 180 String returnNickname = nickname; 181 while (!isUniqueNickname(returnNickname, ignorablePlayerID)) { 182 appendIndex++; 183 if (nickname.length() + Integer.toString(appendIndex).length() + 2 184 > PlayerSettings.MAXIMUM_NICKNAME_LENGTH) { 185 returnNickname = nickname.substring(0, PlayerSettings.MAXIMUM_NICKNAME_LENGTH 186 - Integer.toString(appendIndex).length() - 2) 187 + "(" + appendIndex + ")"; 188 } else { 189 returnNickname = nickname + "(" + appendIndex + ")"; 190 } 191 } 192 return returnNickname; 193 } 194 195 private boolean isUniqueNickname(String nickname, Integer ignorablePlayerID) { 196 if (nickname.equals("")) { 197 return false; 198 } 199 for (Iterator it = map.entrySet().iterator(); it.hasNext();) { 200 PlayerSettings playerSettings = 201 (PlayerSettings) ((Map.Entry) it.next()).getValue(); 202 if (!ignorablePlayerID.equals(playerSettings.getPlayerID()) 203 && nickname.equalsIgnoreCase(playerSettings.getNickname())) { 204 return false; 205 } 206 } 207 return true; 208 } 209 210 /*** 211 * This method should only be used by the SGN framework. 212 * 213 * @param playerSettings 214 */ 215 public void addPlayerSettings(PlayerSettings playerSettings) { 216 map.put(playerSettings.getPlayerID(), playerSettings); 217 if (playerSettings.getPlayingState() == PlayerSettings.PARTICIPATING) { 218 participantsAmount++; 219 } 220 } 221 222 /*** 223 * This method should only be used by the SGN framework. 224 * 225 * @param updatedPlayerSettings 226 */ 227 public void updatePlayerSettings(PlayerSettings updatedPlayerSettings) { 228 PlayerSettings oldPlayerSettings = 229 (PlayerSettings) map.remove(updatedPlayerSettings.getPlayerID()); 230 if (oldPlayerSettings.getPlayingState() == PlayerSettings.PARTICIPATING) { 231 participantsAmount--; 232 } 233 if (updatedPlayerSettings.getPlayingState() == PlayerSettings.PARTICIPATING) { 234 participantsAmount++; 235 } 236 map.put(updatedPlayerSettings.getPlayerID(), updatedPlayerSettings); 237 } 238 239 /*** 240 * This method should only be used by the SGN framework. 241 * 242 * @param playerID 243 * @return 244 */ 245 public PlayerSettings removePlayerSettings(Integer playerID) { 246 PlayerSettings playerSettings = (PlayerSettings) map.remove(playerID); 247 if (playerSettings.getPlayingState() == PlayerSettings.PARTICIPATING) { 248 participantsAmount--; 249 } 250 return playerSettings; 251 } 252 253 }