View Javadoc

1   /**
2    * This program is free software: you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
13   */
14  
15  package net.curre.prefcount.bean;
16  
17  import java.util.Map;
18  import java.util.TreeMap;
19  import javax.swing.JTextField;
20  
21  import net.curre.prefcount.gui.type.Place;
22  import net.curre.prefcount.util.Utilities;
23  
24  /**
25   * Object of this class represents various
26   * game scores/data for a player.
27   * <p/>
28   * Created date: Apr 6, 2007
29   *
30   * @author Yevgeny Nyden
31   */
32  public class PlayerStatistics {
33  
34    /** Player's name. */
35    private String playerName;
36  
37    /**
38     * Players place on the players statistics map
39     * in the result bean object.
40     */
41    private Place playerPlace;
42  
43    /** Player's mountain. */
44    private Integer mountain;
45  
46    /** Player's pool. */
47    private Integer pool;
48  
49    /** Player's extra mountain points (when "divisible by N"). */
50    private Integer mountFix;
51  
52    /**
53     * Map of whists that this player has for other players
54     * (key = player's place, value = whists).
55     */
56    private Map<Place, Integer> whistsMap;
57  
58    /**
59     * Map that holds whists saldo/balances that this player
60     * has against other players; the keys in this map are the
61     * other players places and the values are the corresponding
62     * whist saldo values;
63     * <br /><br />
64     * total player's whist saldo/balance is stored under the
65     * same (as the player's place) key.
66     */
67    private Map<Place, Integer> whistSaldoMap;
68  
69    /**
70     * Map of whist fixes that this player has against other players
71     * (key = player place, value = whist fix). These values may
72     * be generated when applying the "Divisible By N" option.
73     */
74    private Map<Place, Integer> whistFixesMap;
75  
76    /** Reference to the result bean. */
77    private GameResultBean resultBean;
78  
79    /**
80     * Constructor with a result bean argument.
81     *
82     * @param resultBean  Reference to the result bean.
83     * @param playerPlace Player's place.
84     */
85    public PlayerStatistics(GameResultBean resultBean, Place playerPlace) {
86      this.whistsMap = new TreeMap<Place, Integer>();
87      this.whistSaldoMap = new TreeMap<Place, Integer>();
88      this.whistFixesMap = new TreeMap<Place, Integer>();
89      this.resultBean = resultBean;
90      this.playerPlace = playerPlace;
91    }
92  
93    /**
94     * Getter for the player's name.
95     *
96     * @return Player's name.
97     */
98    public String getPlayerName() {
99      return this.playerName;
100   }
101 
102   /**
103    * Gets player's place.
104    *
105    * @return player's place.
106    */
107   public Place getPlayerPlace() {
108     return playerPlace;
109   }
110 
111   /**
112    * Getter for the first letter of the player's name
113    * (returned capitalized).
114    *
115    * @return Capitalized first letter of the player's name
116    *         or an empty string if player's name is blank.
117    */
118   public String getPlayerNameLetter() {
119     if (this.playerName != null) {
120       return (this.playerName.length() > 0 ?
121               this.playerName.substring(0, 1).toUpperCase() :
122               "");
123     }
124     return "";
125   }
126 
127   /**
128    * Setter for the player's name.
129    *
130    * @param playerName Player's name to set.
131    */
132   public void setPlayerName(String playerName) {
133     this.playerName = playerName;
134   }
135 
136   /**
137    * Getter for the player's mountain.
138    *
139    * @return Player's mountain value.
140    */
141   public Integer getMountain() {
142     return this.mountain;
143   }
144 
145   /**
146    * Setter for the player's mountain.
147    *
148    * @param mountain Player's new mountain value.
149    */
150   public void setMountain(Integer mountain) {
151     this.mountain = mountain;
152   }
153 
154   /**
155    * Returns the new computed mountain value according
156    * to the current maxPool value and the type of pref
157    * (Leningradka or other).
158    *
159    * @return New computed mountain value.
160    */
161   public int getNewMountain() {
162     return this.mountain - (this.resultBean.isLeningradka() ? 2 : 1)
163                            * (this.pool - this.resultBean.getMaxPool());
164   }
165 
166   /**
167    * Returns the "final" computed mountain value in whists (x10),
168    * which is = (averageMountain - (mountain + mountFix - minMountain)) x 10.
169    * If mount divisibility is ignored, mountFix is always 0.
170    *
171    * @return Final computed mountain value in whists (x10).
172    */
173   public int getFinalMountainInWhists() {
174     final int mountFix = this.mountFix == null ? 0 : this.mountFix;
175     return (int) ((this.resultBean.getAverageMountain() -
176                    (getNewMountain() + mountFix - this.resultBean.getMinMountain())) * 10);
177   }
178 
179   /**
180    * Setter for the player's mounatain that is fetched
181    * from the passed <code>JTextField</code> argument.
182    *
183    * @param field <code>JTextField</code> to use to fetch the mountain value.
184    */
185   public void setMountainFromField(JTextField field) {
186     this.mountain = Utilities.parseIntFromTextField(field);
187   }
188 
189   /**
190    * Getter for the player's pool value.
191    *
192    * @return Player's pool value.
193    */
194   public Integer getPool() {
195     return this.pool;
196   }
197 
198   /**
199    * Setter for the player's pool value.
200    *
201    * @param pool Player's new pool value.
202    */
203   public void setPool(Integer pool) {
204     this.pool = pool;
205   }
206 
207   /**
208    * Getter for property 'mountFix'.
209    * Player's extra mountain points (when "divisible by N").
210    *
211    * @return Value for property 'mountFix'.
212    */
213   public Integer getMountFix() {
214     return this.mountFix;
215   }
216 
217   /**
218    * Setter for property 'mountFix'.
219    * Player's extra mountain points (when "divisible by N").
220    *
221    * @param mountFix Value to set for property 'mountFix'.
222    */
223   public void setMountFix(Integer mountFix) {
224     this.mountFix = mountFix;
225   }
226 
227   /**
228    * Setter for the player's pool that is fetched
229    * from the passed <code>JTextField</code> argument.
230    *
231    * @param field <code>JTextField</code> to use to fetch the pool value.
232    */
233   public void setPoolFromField(JTextField field) {
234     this.pool = Utilities.parseIntFromTextField(field);
235   }
236 
237   /**
238    * Setter for the player's whists that this player
239    * has against the player refered by its index argument.
240    *
241    * @param place Other player's place that this player has whists against.
242    * @param field <code>JTextField</code> to use to fetch the whists value.
243    * @return Fetched whists value as an <code>Integer</code> object.
244    */
245   public Integer setWhistsForPlayerFromField(Place place, JTextField field) {
246     int val = Utilities.parseIntFromTextField(field);
247     this.whistsMap.put(place, val);
248     return val;
249   }
250 
251   /**
252    * Returns whist that the current player
253    * has against the given player.
254    *
255    * @param place Player's place to fetch the whists for.
256    * @return whist that this player has agains the other player.
257    */
258   public Integer getWhistsAgainstPlayer(Place place) {
259     return this.whistsMap.get(place);
260   }
261 
262   /**
263    * Returns whists for the given player (refered by its index);
264    * value is returned as a string terminated with a period.
265    *
266    * @param place Player's place to fetch the whists for.
267    * @return whists value that this player has agains the other player.
268    */
269   public String getWhistsStringForPlayer(Place place) {
270     Integer val = getWhistsAgainstPlayer(place);
271     return val == null ? "" : String.valueOf(val) + ".";
272   }
273 
274   /**
275    * Returns computed final score (balance) in whists.
276    *
277    * @return Computed final score (balance) in whists.
278    */
279   public int getFinalScoreInWhists() {
280     Integer wSaldo = this.whistSaldoMap.get(this.playerPlace);
281     return getFinalMountainInWhists() + (wSaldo == null ? 0 : wSaldo.intValue());
282   }
283 
284   /**
285    * Gets the map that holds whist saldo/balances that this player
286    * has against other players; the keys in this map are the
287    * other players places and the values are the corresponding
288    * whist saldo values;
289    * <br /><br />
290    * total player's whist saldo/balance is stored under the
291    * same (as the player's place) key.
292    *
293    * @return Map of whist saldo values that the player has
294    *         against other playes.
295    */
296   public Map<Place, Integer> getWhistSaldoMap() {
297     return this.whistSaldoMap;
298   }
299 
300   /**
301    * Gets whist saldo against the given player.
302    *
303    * @param place other player's place.
304    * @return whist saldo against the given player.
305    */
306   public Integer getWhistSaldoAgainstPlayer(Place place) {
307     return this.whistSaldoMap.get(place);
308   }
309 
310   /**
311    * Gets the map that holds whist fixes that this player has
312    * against other players (key = player place, value = whist fix).
313    * These values may be generated when applying the "Divisible By N"
314    * option.
315    *
316    * @return Map that holds whist fixes that this player has
317    *         against other players.
318    */
319   public Map<Place, Integer> getWhistFixesMap() {
320     return this.whistFixesMap;
321   }
322 
323   /**
324    * Gets whist fix against the given player.
325    *
326    * @param place other player's place.
327    * @return whist fix against the given player.
328    */
329   public Integer getWhistFixAgainstPlayer(Place place) {
330     return this.whistFixesMap.get(place);
331   }
332 
333   /**
334    * Returns the minimum mountain in the game.
335    *
336    * @return the minimum mountain in the game;
337    */
338   public int getMinMountain() {
339     if (this.resultBean != null) {
340       return this.resultBean.getMinMountain();
341     }
342     return 0;
343   }
344 
345   /** Private methods ********************** */
346 
347 }