View Javadoc
1   /*******************************************************************************
2    * jhunters: Pool League
3    * Copyright 2015 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Player Performance Analysis.
26   */
27  public class PlayerAnalysis
28          implements Comparable<PlayerAnalysis> {
29      /**
30       * The player.
31       */
32      private final ObjectProperty<Player> thePlayer;
33  
34      /**
35       * Matches played.
36       */
37      private final ObjectProperty<Integer> theMatchesPlayed;
38  
39      /**
40       * Matches won.
41       */
42      private final ObjectProperty<Integer> theMatchesWon;
43  
44      /**
45       * Matches lost.
46       */
47      private final ObjectProperty<Integer> theMatchesLost;
48  
49      /**
50       * Frames won.
51       */
52      private final ObjectProperty<Integer> theFramesFor;
53  
54      /**
55       * Frames lost.
56       */
57      private final ObjectProperty<Integer> theFramesAgainst;
58  
59      /**
60       * Frames difference.
61       */
62      private final ObjectProperty<Integer> theFramesDifference;
63  
64      /**
65       * Points.
66       */
67      private final ObjectProperty<Integer> thePoints;
68  
69      /**
70       * The tie breaker.
71       */
72      private final ObjectProperty<Integer> theTieBreaker;
73  
74      /**
75       * Constructor.
76       * @param pPlayer the player
77       */
78      protected PlayerAnalysis(final Player pPlayer) {
79          /* Allocate properties */
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          /* initialise values */
91          reset();
92      }
93  
94      /**
95       * Reset.
96       */
97      protected void reset() {
98          /* reset properties */
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      * Obtain the player property.
111      * @return the player property.
112      */
113     public ObjectProperty<Player> playerProperty() {
114         return thePlayer;
115     }
116 
117     /**
118      * Obtain the matches played property.
119      * @return the matches played property.
120      */
121     public ObjectProperty<Integer> playedProperty() {
122         return theMatchesPlayed;
123     }
124 
125     /**
126      * Obtain the matches won property.
127      * @return the matches won property.
128      */
129     public ObjectProperty<Integer> wonProperty() {
130         return theMatchesWon;
131     }
132 
133     /**
134      * Obtain the matches lost property.
135      * @return the matches lost property.
136      */
137     public ObjectProperty<Integer> lostProperty() {
138         return theMatchesLost;
139     }
140 
141     /**
142      * Obtain the frames for property.
143      * @return the frames for property.
144      */
145     public ObjectProperty<Integer> forProperty() {
146         return theFramesFor;
147     }
148 
149     /**
150      * Obtain the frames against property.
151      * @return the frames against property.
152      */
153     public ObjectProperty<Integer> againstProperty() {
154         return theFramesAgainst;
155     }
156 
157     /**
158      * Obtain the frames difference property.
159      * @return the frames difference property.
160      */
161     public ObjectProperty<Integer> differenceProperty() {
162         return theFramesDifference;
163     }
164 
165     /**
166      * Obtain the points property.
167      * @return the points property.
168      */
169     public ObjectProperty<Integer> pointsProperty() {
170         return thePoints;
171     }
172 
173     /**
174      * Obtain the tie-breaker property.
175      * @return the tie-breaker property.
176      */
177     public ObjectProperty<Integer> tieBreakerProperty() {
178         return theTieBreaker;
179     }
180 
181     /**
182      * Get Player.
183      * @return the player
184      */
185     public Player getPlayer() {
186         return thePlayer.getValue();
187     }
188 
189     /**
190      * Get Points.
191      * @return the points
192      */
193     public Integer getPoints() {
194         return thePoints.getValue();
195     }
196 
197     /**
198      * Get Tie-Breaker.
199      * @return the tie-breaker
200      */
201     protected Integer getTieBreaker() {
202         return theTieBreaker.getValue();
203     }
204 
205     /**
206      * Get Frames Difference.
207      * @return the frame difference
208      */
209     protected Integer getFramesDifference() {
210         return theFramesDifference.getValue();
211     }
212 
213     /**
214      * Set Tie-Breaker value.
215      * @param pValue the value
216      */
217     protected void setTieBreaker(final Integer pValue) {
218         theTieBreaker.setValue(pValue);
219     }
220 
221     /**
222      * Is the player active?
223      * @return true/false
224      */
225     protected boolean isActive() {
226         return theMatchesPlayed.getValue() > 0;
227     }
228 
229     /**
230      * Analysis Home fixture.
231      * @param pFixture the fixture
232      */
233     protected void analyseHomeFixture(final Fixture pFixture) {
234         /* Obtain result */
235         Result myResult = pFixture.getResult();
236 
237         /* If the match has been played */
238         if (myResult != null) {
239             /* Analyse the result */
240             analyseResult(myResult);
241         }
242     }
243 
244     /**
245      * Analysis Away fixture.
246      * @param pFixture the fixture
247      */
248     protected void analyseAwayFixture(final Fixture pFixture) {
249         /* Obtain result */
250         Result myResult = pFixture.getResult();
251 
252         /* If the match has been played */
253         if (myResult != null) {
254             /* Analyse the result */
255             analyseResult(myResult.reverse());
256         }
257     }
258 
259     /**
260      * Add value.
261      * @param pProperty the property
262      * @param pValue the value to add
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      * Analysis result.
272      * @param pResult the result
273      */
274     protected void analyseResult(final Result pResult) {
275         /* Increment matches played */
276         addValue(theMatchesPlayed, 1);
277 
278         /* Adjust win/loss */
279         if (pResult.isWin()) {
280             addValue(theMatchesWon, 1);
281         } else {
282             addValue(theMatchesLost, 1);
283         }
284 
285         /* Adjust frames for/against */
286         addValue(theFramesFor, pResult.getFramesFor());
287         addValue(theFramesAgainst, pResult.getFramesAgainst());
288         addValue(theFramesDifference, pResult.getFramesDifference());
289 
290         /* Adjust points */
291         addValue(thePoints, pResult.getPoints());
292     }
293 
294     @Override
295     public boolean equals(final Object pThat) {
296         /* Handle trivial cases */
297         if (pThat == this) {
298             return true;
299         }
300         if (pThat == null) {
301             return false;
302         }
303 
304         /* Check for class */
305         if (!(pThat instanceof PlayerAnalysis)) {
306             return false;
307         }
308 
309         /* Access as PlayerAnalysis */
310         PlayerAnalysis myThat = (PlayerAnalysis) pThat;
311 
312         /* Check fields */
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         /* Compare points */
324         int myResult = tieBreakCompareTo(pThat);
325         if (myResult != 0) {
326             return myResult;
327         }
328 
329         /* Just compare players */
330         return getPlayer().compareTo(pThat.getPlayer());
331     }
332 
333     /**
334      * Compare for tieBreaks.
335      * @param pThat the object to compare to
336      * @return -1,0,1 as this object is less than, equal or greater than the passed object
337      */
338     protected int tieBreakCompareTo(final PlayerAnalysis pThat) {
339         /* Compare points */
340         int myResult = pThat.getPoints() - getPoints();
341         if (myResult != 0) {
342             return myResult;
343         }
344 
345         /* Compare tie-breaker */
346         myResult = getTieBreaker() - pThat.getTieBreaker();
347         if (myResult != 0) {
348             return myResult;
349         }
350 
351         /* Compare frames difference */
352         return pThat.getFramesDifference() - getFramesDifference();
353     }
354 
355     /**
356      * Comparator class.
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      * Analysis fields.
369      */
370     public enum AnalysisField {
371         /**
372          * Name.
373          */
374         NAME("Name"),
375 
376         /**
377          * Played.
378          */
379         PLAYED("P"),
380 
381         /**
382          * Won.
383          */
384         WON("W"),
385 
386         /**
387          * Lost.
388          */
389         LOST("L"),
390 
391         /**
392          * For.
393          */
394         FOR("F"),
395 
396         /**
397          * Lost.
398          */
399         AGAINST("A"),
400 
401         /**
402          * Difference.
403          */
404         DIFFERENCE("D"),
405 
406         /**
407          * TieBreak.
408          */
409         TIEBREAK("T"),
410 
411         /**
412          * Points.
413          */
414         POINTS("Pts");
415 
416         /**
417          * The Short name.
418          */
419         private final String theShortName;
420 
421         /**
422          * The String name.
423          */
424         private String theName;
425 
426         /**
427          * Constructor.
428          * @param pShort the short name
429          */
430         private AnalysisField(final String pShort) {
431             theShortName = pShort;
432         }
433 
434         /**
435          * Obtain the short name.
436          * @return the short name
437          */
438         public String getShortName() {
439             return theShortName;
440         }
441 
442         @Override
443         public String toString() {
444             /* If we have not yet loaded the name */
445             if (theName == null) {
446                 /* Load the name */
447                 theName = DataResource.getKeyForAnalysisField(this).getValue();
448             }
449 
450             /* return the name */
451             return theName;
452         }
453     }
454 }