View Javadoc
1   /******************************************************************************
2    * Player.java - Describe one FIBS player
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright´┐Ż 2008 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.6  2011/01/04 17:42:02  dick
10   * Mixing ready and !isPlaying() is a bad idea.
11   *
12   * Revision 1.5  2011/01/01 00:17:39  dick
13   * Convienence constructor to set the player's name.
14   *
15   * Revision 1.4  2010/12/30 04:09:43  dick
16   * He's really only ready if he is not playing.
17   *
18   * Revision 1.3  2010/12/24 02:59:07  dick
19   * WinLoss becomes a light object instead of a String.
20   *
21   * Revision 1.2  2010/02/04 09:17:13  inim
22   * Made player parsing a little more robust
23   *
24   * Revision 1.1  2010/02/04 05:57:53  inim
25   * Mavenized project folder layout
26   *
27   * Revision 1.13  2010/01/23 06:17:02  dick
28   * Add the invited flag.
29   *
30   * Revision 1.12  2009/03/04 18:53:59  dick
31   * Add the winLoss attribute.
32   *
33   * Revision 1.11  2009/02/24 06:58:06  dick
34   * Add isPlaying().
35   *
36   * Revision 1.10  2009/02/20 10:26:26  dick
37   * Add id, which is a database key.
38   *
39   * Revision 1.9  2009/02/14 15:43:01  dick
40   * BuckoFIBS is released under the GNU license.
41   * Javadoc.
42   *
43   * Revision 1.8  2009/02/11 09:09:13  dick
44   * setName(s) for when we get an invite from an unknown player.
45   *
46   * Revision 1.7  2009/02/01 21:14:23  dick
47   * Add the MissManners attribute.
48   *
49   * Revision 1.6  2009/02/01 09:00:08  dick
50   * Add a saved match status string.
51   *
52   * Revision 1.5  2009/01/28 22:10:57  dick
53   * Wacky parsing error?  How ss[1] be null?
54   *
55   * Revision 1.4  2009/01/28 02:52:31  dick
56   * Javadoc.
57   *
58   * Revision 1.3  2009/01/27 19:15:24  dick
59   * Add bfStatus stuff to handle player warnings.
60   *
61   * Revision 1.2  2008/12/11 10:01:28  dick
62   * Parse opponent named "-" as empty (no opponent).
63   *
64   * Revision 1.1  2008/03/31 07:10:56  dick
65   * Describe one FIBS player.
66   */
67  
68  /* 
69   * This program is free software: you can redistribute it and/or modify
70   * it under the terms of the GNU General Public License as published by
71   * the Free Software Foundation, either version 3 of the License, or
72   * (at your option) any later version.
73   *
74   * This program is distributed in the hope that it will be useful,
75   * but WITHOUT ANY WARRANTY; without even the implied warranty of
76   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
77   * GNU General Public License for more details.
78   *
79   * You should have received a copy of the GNU General Public License
80   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
81   *
82   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
83   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
84   * 
85   */
86  package com.buckosoft.fibs.domain;
87  
88  import org.slf4j.Logger;
89  import org.slf4j.LoggerFactory;
90  
91  
92  /** Describe one FIBS player. <br>
93   * Most of these fields don't have setters because this object is mostly filled out from FIBS.
94   * See {@link #parsePlayer(String)}.
95   * @author Dick Balaska
96   * @since 2008/03/31
97   * @version $Revision$ <br> $Date$
98   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/domain/Player.java">cvs Player.java</a>
99   */
100 public class Player implements Comparable<Player> {
101 	private	final static boolean DEBUG = false;
102 	private Logger logger = LoggerFactory.getLogger(getClass());
103 
104 	private	int		id = 0;
105 	private	String	name = "???";
106 	private	String	opponent = "";
107 	private	String	watching = "";
108 	private	boolean	ready = false;
109 	private	boolean	away = false;
110 	private	double	rating = 0.0;
111 	private	int		experience = 0;
112 	private	int		idleTime = 0;
113 	private	long	loginTime = 0;
114 	private	String	hostName = "";
115 	private	String	client = "";
116 	private	String	email = "";
117 	private	WinLoss	winLoss = null;
118 
119 	private	int		bfFlag = 0;
120 	private	String	bfStatus;
121 
122 	private	boolean	invited = false;
123 	
124 //	private	boolean	hasSavedGame = false;
125 	private	String	savedMatch;
126 	private	String	missManners;
127 
128 	/** Create a default/empty Player
129 	 */
130 	public Player() {}
131 	
132 	/** Convienence constructor to set the player's name.
133 	 * @param playerName The name of this new player.
134 	 */
135 	public Player(String playerName) {
136 		this.name = playerName;
137 	}
138 
139 	/** Get the local player id.
140 	 * This is a key to our local database, and is not a FIBS number at all.
141 	 * @return the id
142 	 */
143 	public int getId() {
144 		return id;
145 	}
146 
147 	/** Set the local player id.
148 	 * This is a key to our local database, and is not a FIBS number at all.
149 	 * @param id the id to set
150 	 */
151 	public void setId(int id) {
152 		this.id = id;
153 	}
154 
155 	/** Get the username of this player
156 	 * @return The username
157 	 */
158 	public String	getName() {
159 		return(name);
160 	}
161 
162 	/** Set the username of this player.
163 	 * This is useful when we get an invite from an unknown player.
164 	 * @param name The player's name
165 	 */
166 	public void setName(String name) {
167 		this.name = name;
168 	}
169 
170 	/** Return true if this player is playing someone.
171 	 * This field is derived from the opponent field.
172 	 * @return true if playing.
173 	 */
174 	public boolean isPlaying() {
175 		if (opponent.length() > 0)
176 			return(true);
177 		return(false);
178 	}
179 
180 	/** If this player is playing someone, then return the opponent's name.
181 	 * @return The opponent's name or the empty string if not playing anyone.
182 	 */
183 	public	String getOpponent() {
184 		return(opponent);
185 	}
186 
187 	/** If this player is watching someone, then return the name of the player we are watching.
188 	 * @return The player being watched or the empty string if not watching anyone.
189 	 */
190 	public	String	getWatching() {
191 		return(watching);
192 	}
193 
194 	/** Is this player ready to play?
195 	 * @return true if ready to play.
196 	 */
197 	public	boolean isReady() {
198 		return(ready);
199 	}
200 
201 	/** Has this player marked himself as away?
202 	 * @return true = yes
203 	 */
204 	public	boolean	isAway() {
205 		return(away);
206 	}
207 
208 	/** Get the rating for this player
209 	 * @return The rating
210 	 */
211 	public	double	getRating() {
212 		return(rating);
213 	}
214 
215 	/** Get the number of games this player has played
216 	 * @return The Experience
217 	 */
218 	public int getExperience() {
219 		return(experience);
220 	}
221 
222 	/** How long has this player been idle?
223 	 * @return The number of seconds he has not done anything.
224 	 */
225 	public int getIdleTime() {
226 		return(idleTime);
227 	}
228 
229 	/** When did this player log in?
230 	 * @return The number of seconds since Jan. 1, 1970
231 	 */
232 	public long getLoginTime() {
233 		return(loginTime);
234 	}
235 
236 	/** Get the hostname of the machine this player is playing on.
237 	 * @return The hostname
238 	 */
239 	public String getHostName() {
240 		return(hostName);
241 	}
242 	
243 	/** What client is this player using?
244 	 * @return The name of the client.
245 	 */
246 	public String getClient() {
247 		return(client);
248 	}
249 
250 	/** Set the name of the client that this player is using.
251 	 * @param client
252 	 */
253 	public void setClient(String client) {
254 		this.client = client;
255 	}
256 
257 	/** What is this player's email address?
258 	 * @return The email address that the player gave to FIBS (probably not a real email address).
259 	 */
260 	public String getEmail() {
261 		return(email);
262 	}
263 
264 	/** Return a string of your won/loss record against this player.
265 	 * @return the winLoss
266 	 */
267 	public WinLoss getWinLoss() {
268 		return(winLoss);
269 //		if (winLoss == null)
270 //			return("");
271 //		return winLoss.toString();
272 	}
273 
274 	/** Set the string of your won/loss record against this player.
275 	 * @param wins The number of times you have beaten this opponent.
276 	 * @param losses The number of times you have lost to this opponent.
277 	 */
278 	public void setWinLoss(int wins, int losses) {
279 		this.winLoss = new WinLoss(wins, losses);
280 	}
281 
282 	/** Get the BuckoFIBS flag (not used yet)
283 	 * @return the bfFlag
284 	 */
285 	public int getBfFlag() {
286 		return bfFlag;
287 	}
288 
289 	/** Set the BuckoFIBS flag (not used yet)
290 	 * @param bfFlag the bfFlag to set
291 	 */
292 	public void setBfFlag(int bfFlag) {
293 		this.bfFlag = bfFlag;
294 	}
295 
296 	/** If this player has a special status string, like an invite warning, it goes here.
297 	 * @return the bfStatus The status string.
298 	 */
299 	public String getBfStatus() {
300 		return bfStatus;
301 	}
302 
303 	/** If this player has a special status string, like an invite warning, it goes here.
304 	 * @param bfStatus the bfStatus to set
305 	 */
306 	public void setBfStatus(String bfStatus) {
307 		this.bfStatus = bfStatus;
308 	}
309 
310 	/** Have we invited this player?
311 	 * @return the invited
312 	 */
313 	public boolean isInvited() {
314 		return invited;
315 	}
316 
317 	/**
318 	 * @param invited the invited to set
319 	 */
320 	public void setInvited(boolean invited) {
321 		this.invited = invited;
322 	}
323 
324 	/** If you have a saved match with this player, then this string describes it. 
325 	 * @return the savedMatch or null if none
326 	 */
327 	public String getSavedMatch() {
328 		return savedMatch;
329 	}
330 
331 	/** Set the savedMatch (tooltip) text
332 	 * @param savedMatch the savedMatch to set
333 	 */
334 	public void setSavedMatch(String savedMatch) {
335 		this.savedMatch = savedMatch;
336 	}
337 
338 	/** If this player has a MissManners warning, this is it.
339 	 * @return the missManners warning, or null if this player has none.
340 	 */
341 	public String getMissManners() {
342 		return missManners;
343 	}
344 
345 	/** Set the MissManners warning issued about this player
346 	 * @param missManners the missManners to set
347 	 */
348 	public void setMissManners(String missManners) {
349 		this.missManners = missManners;
350 	}
351 
352 	/** Parse this player from a FIBS string.
353 	 * Fills in most of this object's fields from the FIBS 
354 	 * <a href="http://www.fibs.com/fibs_interface.html#clip_who_info">who info</a> line.
355 	 * @param s The who info line from FIBS
356 	 * @return success
357 	 */
358 	public boolean parsePlayer(String s) {
359 		String[] ss = s.split(" ");
360 		if (ss.length != 13) {
361 			if (DEBUG) {
362 				logger.info("Error parsing player");
363 				logger.info(s);
364 			}
365 			return(false);
366 		}
367 		name = ss[1];
368 		opponent = ss[2];
369 		if ("-".equals(ss[2]) || opponent == null)
370 			opponent = "";
371 		watching = ss[3];
372 		if ("-".equals(watching))
373 			watching = "";
374 		ready = "1".equals(ss[4]);
375 		away = "1".equals(ss[5]);
376 		rating = Double.parseDouble(ss[6]);
377 		experience = Integer.parseInt(ss[7]);
378 		idleTime = Integer.parseInt(ss[8]);
379 		loginTime = Long.parseLong(ss[9]);
380 		hostName = ss[10];
381 		client = ss[11];
382 		email = ss[12];
383 		return(true);
384 	}
385 
386 	/** String compare the name of this player with that one.
387 	 * @param o The other player to compare
388 	 */
389 	@Override
390 	public int compareTo(Player o) {
391 		return(this.name.compareToIgnoreCase(o.getName()));
392 	}
393 	
394 }