1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jhunters.pool.data;
18
19 import java.util.Comparator;
20
21 import javafx.beans.property.ObjectProperty;
22 import javafx.beans.property.SimpleObjectProperty;
23
24
25
26
27 public class PlayerAnalysis
28 implements Comparable<PlayerAnalysis> {
29
30
31
32 private final ObjectProperty<Player> thePlayer;
33
34
35
36
37 private final ObjectProperty<Integer> theMatchesPlayed;
38
39
40
41
42 private final ObjectProperty<Integer> theMatchesWon;
43
44
45
46
47 private final ObjectProperty<Integer> theMatchesLost;
48
49
50
51
52 private final ObjectProperty<Integer> theFramesFor;
53
54
55
56
57 private final ObjectProperty<Integer> theFramesAgainst;
58
59
60
61
62 private final ObjectProperty<Integer> theFramesDifference;
63
64
65
66
67 private final ObjectProperty<Integer> thePoints;
68
69
70
71
72 private final ObjectProperty<Integer> theTieBreaker;
73
74
75
76
77
78 protected PlayerAnalysis(final Player pPlayer) {
79
80 thePlayer = new SimpleObjectProperty<Player>(this, AnalysisField.NAME.toString(), pPlayer);
81 theMatchesPlayed = new SimpleObjectProperty<Integer>(this, AnalysisField.PLAYED.toString());
82 theMatchesWon = new SimpleObjectProperty<Integer>(this, AnalysisField.WON.toString());
83 theMatchesLost = new SimpleObjectProperty<Integer>(this, AnalysisField.LOST.toString());
84 theFramesFor = new SimpleObjectProperty<Integer>(this, AnalysisField.FOR.toString());
85 theFramesAgainst = new SimpleObjectProperty<Integer>(this, AnalysisField.AGAINST.toString());
86 theFramesDifference = new SimpleObjectProperty<Integer>(this, AnalysisField.DIFFERENCE.toString());
87 thePoints = new SimpleObjectProperty<Integer>(this, AnalysisField.POINTS.toString());
88 theTieBreaker = new SimpleObjectProperty<Integer>(this, AnalysisField.TIEBREAK.toString());
89
90
91 reset();
92 }
93
94
95
96
97 protected void reset() {
98
99 theMatchesPlayed.set(0);
100 theMatchesWon.set(0);
101 theMatchesLost.set(0);
102 theFramesFor.set(0);
103 theFramesAgainst.set(0);
104 theFramesDifference.set(0);
105 thePoints.set(0);
106 theTieBreaker.set(0);
107 }
108
109
110
111
112
113 public ObjectProperty<Player> playerProperty() {
114 return thePlayer;
115 }
116
117
118
119
120
121 public ObjectProperty<Integer> playedProperty() {
122 return theMatchesPlayed;
123 }
124
125
126
127
128
129 public ObjectProperty<Integer> wonProperty() {
130 return theMatchesWon;
131 }
132
133
134
135
136
137 public ObjectProperty<Integer> lostProperty() {
138 return theMatchesLost;
139 }
140
141
142
143
144
145 public ObjectProperty<Integer> forProperty() {
146 return theFramesFor;
147 }
148
149
150
151
152
153 public ObjectProperty<Integer> againstProperty() {
154 return theFramesAgainst;
155 }
156
157
158
159
160
161 public ObjectProperty<Integer> differenceProperty() {
162 return theFramesDifference;
163 }
164
165
166
167
168
169 public ObjectProperty<Integer> pointsProperty() {
170 return thePoints;
171 }
172
173
174
175
176
177 public ObjectProperty<Integer> tieBreakerProperty() {
178 return theTieBreaker;
179 }
180
181
182
183
184
185 public Player getPlayer() {
186 return thePlayer.getValue();
187 }
188
189
190
191
192
193 public Integer getPoints() {
194 return thePoints.getValue();
195 }
196
197
198
199
200
201 protected Integer getTieBreaker() {
202 return theTieBreaker.getValue();
203 }
204
205
206
207
208
209 protected Integer getFramesDifference() {
210 return theFramesDifference.getValue();
211 }
212
213
214
215
216
217 protected void setTieBreaker(final Integer pValue) {
218 theTieBreaker.setValue(pValue);
219 }
220
221
222
223
224
225 protected boolean isActive() {
226 return theMatchesPlayed.getValue() > 0;
227 }
228
229
230
231
232
233 protected void analyseHomeFixture(final Fixture pFixture) {
234
235 Result myResult = pFixture.getResult();
236
237
238 if (myResult != null) {
239
240 analyseResult(myResult);
241 }
242 }
243
244
245
246
247
248 protected void analyseAwayFixture(final Fixture pFixture) {
249
250 Result myResult = pFixture.getResult();
251
252
253 if (myResult != null) {
254
255 analyseResult(myResult.reverse());
256 }
257 }
258
259
260
261
262
263
264 private void addValue(final ObjectProperty<Integer> pProperty,
265 final Integer pValue) {
266 Integer myValue = pProperty.getValue();
267 pProperty.set(myValue + pValue);
268 }
269
270
271
272
273
274 protected void analyseResult(final Result pResult) {
275
276 addValue(theMatchesPlayed, 1);
277
278
279 if (pResult.isWin()) {
280 addValue(theMatchesWon, 1);
281 } else {
282 addValue(theMatchesLost, 1);
283 }
284
285
286 addValue(theFramesFor, pResult.getFramesFor());
287 addValue(theFramesAgainst, pResult.getFramesAgainst());
288 addValue(theFramesDifference, pResult.getFramesDifference());
289
290
291 addValue(thePoints, pResult.getPoints());
292 }
293
294 @Override
295 public boolean equals(final Object pThat) {
296
297 if (pThat == this) {
298 return true;
299 }
300 if (pThat == null) {
301 return false;
302 }
303
304
305 if (!(pThat instanceof PlayerAnalysis)) {
306 return false;
307 }
308
309
310 PlayerAnalysis myThat = (PlayerAnalysis) pThat;
311
312
313 return thePlayer.getValue().equals(myThat.thePlayer.getValue());
314 }
315
316 @Override
317 public int hashCode() {
318 return thePlayer.getValue().hashCode();
319 }
320
321 @Override
322 public int compareTo(final PlayerAnalysis pThat) {
323
324 int myResult = tieBreakCompareTo(pThat);
325 if (myResult != 0) {
326 return myResult;
327 }
328
329
330 return getPlayer().compareTo(pThat.getPlayer());
331 }
332
333
334
335
336
337
338 protected int tieBreakCompareTo(final PlayerAnalysis pThat) {
339
340 int myResult = pThat.getPoints() - getPoints();
341 if (myResult != 0) {
342 return myResult;
343 }
344
345
346 myResult = getTieBreaker() - pThat.getTieBreaker();
347 if (myResult != 0) {
348 return myResult;
349 }
350
351
352 return pThat.getFramesDifference() - getFramesDifference();
353 }
354
355
356
357
358 protected static final class AnalysisComparator
359 implements Comparator<PlayerAnalysis> {
360 @Override
361 public int compare(final PlayerAnalysis pFirst,
362 final PlayerAnalysis pSecond) {
363 return pFirst.compareTo(pSecond);
364 }
365 }
366
367
368
369
370 public enum AnalysisField {
371
372
373
374 NAME("Name"),
375
376
377
378
379 PLAYED("P"),
380
381
382
383
384 WON("W"),
385
386
387
388
389 LOST("L"),
390
391
392
393
394 FOR("F"),
395
396
397
398
399 AGAINST("A"),
400
401
402
403
404 DIFFERENCE("D"),
405
406
407
408
409 TIEBREAK("T"),
410
411
412
413
414 POINTS("Pts");
415
416
417
418
419 private final String theShortName;
420
421
422
423
424 private String theName;
425
426
427
428
429
430 private AnalysisField(final String pShort) {
431 theShortName = pShort;
432 }
433
434
435
436
437
438 public String getShortName() {
439 return theShortName;
440 }
441
442 @Override
443 public String toString() {
444
445 if (theName == null) {
446
447 theName = DataResource.getKeyForAnalysisField(this).getValue();
448 }
449
450
451 return theName;
452 }
453 }
454 }