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.*;
20
21 import net.curre.prefcount.util.Utilities;
22
23 /**
24 * Object of this class represents various
25 * game scores/data for a player.
26 * <p/>
27 * Created date: Apr 6, 2007
28 *
29 * @author Yevgeny Nyden
30 */
31 public class PlayerStatistics {
32
33 /** Player's name. */
34 private String playerName;
35
36 /**
37 * Players index on tha players statistics list
38 * in the result bean object.
39 */
40 private int playerIndex;
41
42 /** Player's mountain. */
43 private Integer mountain;
44
45 /** Player's bullet. */
46 private Integer bullet;
47
48 /**
49 * Map of vists that this player has for other players
50 * (key = player index, value = vists).
51 */
52 private Map<Integer, Integer> vistsMap;
53
54 /**
55 * Map that holds vist saldo/balances that this player
56 * has against other players; the keys in this map are the
57 * other players indexes and the values are the corresponding
58 * vist saldo values;
59 * <p/>
60 * total player's vist saldo/balance is stored under the
61 * same (as the player's index) key.
62 * </p>
63 */
64 private Map<Integer, Integer> vistSaldoMap;
65
66 /** Reference to the result bean. */
67 private GameResultBean resultBean;
68
69 /**
70 * Constructor with a result bean argument.
71 *
72 * @param resultBean Reference to the result bean.
73 * @param playerIndex Player index.
74 */
75 public PlayerStatistics(GameResultBean resultBean, int playerIndex) {
76 this.vistsMap = new TreeMap<Integer, Integer>();
77 this.vistSaldoMap = new TreeMap<Integer, Integer>();
78 this.resultBean = resultBean;
79 this.playerIndex = playerIndex;
80 }
81
82 /**
83 * Getter for the player's name.
84 *
85 * @return Player's name.
86 */
87 public String getPlayerName() {
88 return playerName;
89 }
90
91 /**
92 * Getter for the first letter of the player's name
93 * (returned capitalized).
94 *
95 * @return Capitalized first letter of the player's name
96 * or an empty string if player's name is blank.
97 */
98 public String getPlayerNameLetter() {
99 if (playerName != null) {
100 return (playerName.length() > 0 ?
101 playerName.substring(0, 1).toUpperCase() :
102 "");
103 }
104 return "";
105 }
106
107 /**
108 * Setter for the player's name.
109 *
110 * @param playerName Player's name to set.
111 */
112 public void setPlayerName(String playerName) {
113 this.playerName = playerName;
114 }
115
116 /**
117 * Getter for the player's mountain.
118 *
119 * @return Player's mountain value.
120 */
121 public Integer getMountain() {
122 return mountain;
123 }
124
125 /**
126 * Setter for the player's mountain.
127 *
128 * @param mountain Player's new mountain value.
129 */
130 public void setMountain(Integer mountain) {
131 this.mountain = mountain;
132 }
133
134 /**
135 * Returns the new computed mountain value according
136 * to the current targetBullet value.
137 *
138 * @return New computed mountain value.
139 */
140 public Integer getNewMountain() {
141 return mountain - 2 * (bullet - resultBean.getTargetBullet());
142 }
143
144 /**
145 * Returns the "final" computed mountain value in vists (x10),
146 * which is = (averageMountain - (mountain - minMountain)) x 10.
147 *
148 * @return Final computed mountain value in vists (x10).
149 */
150 public Integer getFinalMountainInVists() {
151 return (int) ((resultBean.getAverageMountain() - getNewMountain() + resultBean.getMinMountain()) * 10);
152 }
153
154 /**
155 * Setter for the player's mounatain that is fetched
156 * from the passed <code>JTextField</code> argument.
157 *
158 * @param field <code>JTextField</code> to use to fetch the mountain value.
159 */
160 public void setMountainFromField(JTextField field) {
161 this.mountain = Utilities.parseIntFromTextField(field);
162 }
163
164 /**
165 * Getter for the player's bullet value.
166 *
167 * @return Player's bullet value.
168 */
169 public Integer getBullet() {
170 return bullet;
171 }
172
173 /**
174 * Setter for the player's bullet value.
175 *
176 * @param bullet Player's new bullet value.
177 */
178 public void setBullet(Integer bullet) {
179 this.bullet = bullet;
180 }
181
182 /**
183 * Setter for the player's bullet that is fetched
184 * from the passed <code>JTextField</code> argument.
185 *
186 * @param field <code>JTextField</code> to use to fetch the bullet value.
187 */
188 public void setBulletFromField(JTextField field) {
189 this.bullet = Utilities.parseIntFromTextField(field);
190 }
191
192 /**
193 * Setter for the player's vists that this player
194 * has against the player refered by its index argument.
195 *
196 * @param playerInd Other player's index that this player has vists against.
197 * @param field <code>JTextField</code> to use to fetch the vists value.
198 * @return Fetched vists value as an <code>Integer</code> object.
199 */
200 public Integer setVistsForPlayerFromField(Integer playerInd, JTextField field) {
201 int val = Utilities.parseIntFromTextField(field);
202 vistsMap.put(playerInd, val);
203 return val;
204 }
205
206 /**
207 * Returns vists for the given player (refered by its index).
208 *
209 * @param playerInd Player index to fetch the vists for.
210 * @return Vists value that this player has agains the other player.
211 */
212 public Integer getVistsForPlayer(Integer playerInd) {
213 return vistsMap.get(playerInd);
214 }
215
216 /**
217 * Returns vists for the given player (refered by its index);
218 * value is returned as a string terminated with a period.
219 *
220 * @param playerInd Player index to fetch the vists for.
221 * @return Vists value that this player has agains the other player.
222 */
223 public String getVistsStringForPlayer(Integer playerInd) {
224 Integer val = vistsMap.get(playerInd);
225 return val == null ? "" : String.valueOf(val) + ".";
226 }
227
228 /**
229 * Returns computed final score (balance) in vists.
230 *
231 * @return Computed final score (balance) in vists.
232 */
233 public Integer getFinalScoreInVists() {
234 return getFinalMountainInVists() + vistSaldoMap.get(playerIndex);
235 }
236
237 /**
238 * Gets the map that holds vist saldo/balances that this player
239 * has against other players; the keys in this map are the
240 * other players indexes and the values are the corresponding
241 * vist saldo values;
242 * <p/>
243 * total player's vist saldo/balance is stored under the
244 * same (as the player's index) key.
245 * </p>
246 *
247 * @return Map of vist saldo values that the player has
248 * against other playes.
249 */
250 public Map<Integer, Integer> getVistSaldoMap() {
251 return vistSaldoMap;
252 }
253
254 /** Private methods ********************** */
255
256 }