View Javadoc

1   /*
2    * The ReWinder Project
3    * 
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (at your option) any later version.
8   
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13  
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   */
18  package ch.twiddlefinger.inet.rewinder.model.replay;
19  
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Set;
23  
24  import ch.twiddlefinger.inet.rewinder.model.chat.ChatLog;
25  import ch.twiddlefinger.inet.rewinder.model.entities.GameType;
26  import ch.twiddlefinger.inet.rewinder.model.entities.Player;
27  import ch.twiddlefinger.inet.rewinder.model.entities.Team;
28  import ch.twiddlefinger.inet.rewinder.model.entities.WarcraftVersion;
29  
30  /***
31   * A Warcraft Replay
32   */
33  public class WarcraftReplay {
34  	private GameType gameType;
35  	private String gameName;
36  	private long lengthMsec;
37  	private long fileSize;
38  	private long version;
39  	private HashSet players = new HashSet();
40  	private HashSet teams = new HashSet();
41  	private WarcraftVersion warcraftVersion = null;
42  
43  	/***
44  	 * Default Constructor
45  	 */
46  	public WarcraftReplay() {
47  		players = new HashSet();
48  	}
49  
50  	/***
51  	 * Returns the {@link WarcraftVersion} of this replay. o Reign of Chaos o
52  	 * Frozen Throne
53  	 *
54  	 * @return version
55  	 */
56  	public WarcraftVersion getWarcraftVersion() {
57  		return warcraftVersion;
58  	}
59  
60  	/***
61  	 * Sets the {@link WarcraftVersion} of this Replay Is mostly used by
62  	 * parsers
63  	 *
64  	 * @param version the Warcraft Version
65  	 */
66  	public void setWarcraftVersion(String version) {
67  		if (version.equals("W3XP")) {
68  			warcraftVersion = WarcraftVersion.TFT;
69  		} else {
70  			warcraftVersion = WarcraftVersion.ROC;
71  		}
72  	}
73  
74  	/***
75  	 * Returns the build version of this replay e.g. 1.13, 1.06, 1.07, 1.12
76  	 *
77  	 * @return the version tag
78  	 */
79  	public String getVersion() {
80  		return "1." + version;
81  	}
82  
83  	/***
84  	 * Sets the replays version tag (e.g. 1.13)
85  	 *
86  	 * @param version
87  	 */
88  	public void setVersion(long version) {
89  		this.version = version;
90  	}
91  
92  	/***
93  	 * The compressed size of the replay file
94  	 *
95  	 * @return the replay's file size
96  	 */
97  	public long getFileSize() {
98  		return this.fileSize;
99  	}
100 
101 	/***
102 	 * Sets the replay's file size
103 	 *
104 	 * @param size
105 	 */
106 	public void setFileSize(long size) {
107 		this.fileSize = size;
108 	}
109 
110 	/***
111 	 * Returns the length of the replay in milliseconds
112 	 *
113 	 * @return milliseconds
114 	 */
115 	public long getLengthMsec() {
116 		return this.lengthMsec;
117 	}
118 
119 	/***
120 	 * Sets the game length in milliseconds
121 	 *
122 	 * @param msec
123 	 */
124 	public void setLengthMsec(long msec) {
125 		this.lengthMsec = msec;
126 	}
127 
128 	//TODO Chatlog implementieren
129 
130 	/***
131 	 * Returns the ChatLog
132 	 *
133 	 * @return
134 	 */
135 	public ChatLog getChatLog() {
136 		// TODO Auto-generated method stub
137 		return null;
138 	}
139 
140 	/***
141 	 * Adds a player which was playing in this replay See {@link Player}
142 	 *
143 	 * @param p
144 	 */
145 	public void addPlayer(Player p) {
146 		this.players.add(p);
147 	}
148 
149 	/***
150 	 * Removes a player from this replay See {@link player}
151 	 *
152 	 * @param p a player
153 	 */
154 	public void removePlayer(Player p) {
155 		this.players.remove(p);
156 	}
157 
158 	/***
159 	 * Searches for player by it's ID
160 	 *
161 	 * @param id the player's id
162 	 *
163 	 * @return the coresponding player object
164 	 */
165 	public Player getPlayerByID(int id) {
166 		Iterator i = this.players.iterator();
167 
168 		while (i.hasNext()) {
169 			Player p = (Player) i.next();
170 
171 			if (p.getPlayerID() == id) {
172 				return p;
173 			}
174 		}
175 
176 		return null;
177 	}
178 
179 	/***
180 	 * Searches for a player by its name
181 	 * @param name the player name to be searched for
182 	 * @return the player object
183 	 */
184 
185 	public Player getPlayerByName(String name) {
186 		Iterator i = this.players.iterator();
187 
188 		while (i.hasNext()) {
189 			Player p = (Player) i.next();
190 
191 			if (p.getName().equals(name)) {
192 				return p;
193 			}
194 		}
195 		return null;
196 	}
197 
198 	/***
199 	 * Returns a list of the known players in this replay
200 	 *
201 	 * @return a player list
202 	 */
203 	public Set getPlayers() {
204 		return this.players;
205 	}
206 
207 	/***
208 	 * Sets the game name (e.g. BNet, FooBar's local game ...)
209 	 *
210 	 * @param name a game name
211 	 */
212 	public void setGameName(String name) {
213 		this.gameName = name;
214 	}
215 
216 	/***
217 	 * returns the game's name (e.g. BNet, FooBar's local game ...)
218 	 *
219 	 * @return the game name
220 	 */
221 	public String getGameName() {
222 		return this.gameName;
223 	}
224 
225 	/***
226 	 * returns the number of players in this game
227 	 *
228 	 * @return
229 	 */
230 	public int getPlayerCount() {
231 		return this.players.size();
232 	}
233 
234 	/***
235 	 * Sets the game type (e.g. FFA, 1on1, AT, RT etc.)
236 	 *
237 	 * @param type
238 	 */
239 	public void setGameType(GameType type) {
240 		this.gameType = type;
241 	}
242 
243 	/***
244 	 * Returns the game type
245 	 *
246 	 * @return the game's type (1on1, RT, AT, FFA)
247 	 */
248 	public GameType getGameType() {
249 		return this.gameType;
250 	}
251 
252 	/***
253 	 * returns a team by its id, if the team doesnt exist, it will be created
254 	 * @param b
255 	 * @return a team with a distinctive id
256 	 */
257 	public Team getTeamByID(byte id) {
258 		Team t = null;
259 		Iterator i = this.teams.iterator();
260 		while (i.hasNext()) {
261 			Team temp = (Team) i.next();
262 			if (temp.getID() == id) {
263 				t = temp;
264 				break;
265 			}
266 		}
267 
268 		if (t == null) {
269 			t = new Team(id);
270 			this.teams.add(t);
271 		}
272 
273 		return t;
274 	}
275 
276 	/***
277 	 * @return
278 	 */
279 	public int getTeamCount() {
280 		return this.teams.size();
281 	}
282 
283 	/***
284 	 * @return
285 	 */
286 	public Iterator getTeamIterator() {
287 		return this.teams.iterator();
288 	}
289 
290 	/***
291 	 * @return
292 	 */
293 	public Iterator getPlayerIterator() {
294 		return this.players.iterator();
295 	}
296 }