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.time.LocalDate;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import javafx.collections.FXCollections;
27  import javafx.collections.ObservableList;
28  import net.sourceforge.jhunters.pool.data.PlayerAnalysis.AnalysisComparator;
29  
30  /**
31   * Competition Table.
32   */
33  public class CompetitionStandings {
34      /**
35       * The competition.
36       */
37      private final Competition theCompetition;
38  
39      /**
40       * The competition Table.
41       */
42      private final AnalysisTable theTable;
43  
44      /**
45       * Map of TieBreak tables.
46       */
47      private final Map<Integer, AnalysisTable> theTieBreakers;
48  
49      /**
50       * The comparator.
51       */
52      private final Comparator<PlayerAnalysis> theComparator;
53  
54      /**
55       * Constructor.
56       * @param pCompetition the competition
57       */
58      public CompetitionStandings(final Competition pCompetition) {
59          theCompetition = pCompetition;
60          theTable = new AnalysisTable();
61          theTieBreakers = new HashMap<Integer, AnalysisTable>();
62          theComparator = new AnalysisComparator();
63      }
64  
65      /**
66       * Obtain the table.
67       * @return the table
68       */
69      public ObservableList<PlayerAnalysis> getTable() {
70          return theTable.getTable();
71      }
72  
73      /**
74       * Process fixtures.
75       * @param pFixtures the fixtures
76       */
77      protected void processFixtures(final FixtureList pFixtures) {
78          /* Clear the active status */
79          theCompetition.setActive(Boolean.FALSE);
80  
81          /* Reset table and map */
82          theTable.reset();
83          theTieBreakers.clear();
84  
85          /* Loop through the fixture sets */
86          Iterator<FixtureSet> myIterator = pFixtures.fixtureIterator();
87          while (myIterator.hasNext()) {
88              FixtureSet mySet = myIterator.next();
89  
90              /* Process fixture set */
91              processFixtureSet(mySet);
92          }
93  
94          /* Re-Loop through the fixture sets */
95          myIterator = pFixtures.fixtureIterator();
96          while (myIterator.hasNext()) {
97              FixtureSet mySet = myIterator.next();
98  
99              /* Process fixture for tieBreakers */
100             processTieBreakers(mySet);
101         }
102 
103         /* propagate the tieBreaks */
104         propagateTieBreaks();
105 
106         /* Sort the table */
107         sortTable();
108     }
109 
110     /**
111      * Sort table.
112      */
113     protected void sortTable() {
114         /* Resort the list */
115         FXCollections.sort(theTable.getTable(), theComparator);
116     }
117 
118     /**
119      * Process fixtureSet.
120      * @param pSet the fixture Set
121      */
122     private void processFixtureSet(final FixtureSet pSet) {
123         /* Access competition startDate */
124         LocalDate myDate = theCompetition.getStartDate();
125 
126         /* Loop through the fixtures */
127         Iterator<Fixture> myIterator = pSet.fixtureIterator();
128         while (myIterator.hasNext()) {
129             Fixture myFixture = myIterator.next();
130 
131             /* Ignore byes */
132             if (myFixture.isBye()) {
133                 continue;
134             }
135 
136             /* Process for home player */
137             Player myPlayer = myFixture.getHomePlayer();
138             PlayerAnalysis myAnalysis = theTable.getAnalysis(myPlayer);
139             myAnalysis.analyseHomeFixture(myFixture);
140 
141             /* Process for away player */
142             myPlayer = myFixture.getAwayPlayer();
143             myAnalysis = theTable.getAnalysis(myPlayer);
144             myAnalysis.analyseAwayFixture(myFixture);
145 
146             /* If the result is non-null */
147             if (myFixture.getResult() != null) {
148                 /* Competition is active */
149                 theCompetition.setActive(Boolean.TRUE);
150             }
151 
152             /* If we don't have a startDate */
153             if (myDate == null) {
154                 /* Set start date from fixture */
155                 myDate = myFixture.getDate();
156                 theCompetition.setStartDate(myDate);
157             }
158         }
159     }
160 
161     /**
162      * Process fixtureSet tieBreakers.
163      * @param pSet the fixture Set
164      */
165     private void processTieBreakers(final FixtureSet pSet) {
166         /* Loop through the fixtures */
167         Iterator<Fixture> myIterator = pSet.fixtureIterator();
168         while (myIterator.hasNext()) {
169             Fixture myFixture = myIterator.next();
170 
171             /* Ignore byes */
172             if (myFixture.isBye()) {
173                 continue;
174             }
175 
176             /* Access relevant analyses */
177             Player myHomePlayer = myFixture.getHomePlayer();
178             PlayerAnalysis myHome = theTable.getAnalysis(myHomePlayer);
179             Player myAwayPlayer = myFixture.getAwayPlayer();
180             PlayerAnalysis myAway = theTable.getAnalysis(myAwayPlayer);
181 
182             /* If we need a tieBreak */
183             if (myHome.getPoints().equals(myAway.getPoints())) {
184                 /* Access tieBreak Table */
185                 AnalysisTable myTieBreak = getTieBreak(myHome.getPoints());
186 
187                 /* Analyse for tieBreaks */
188                 myHome = myTieBreak.getAnalysis(myHomePlayer);
189                 myHome.analyseHomeFixture(myFixture);
190                 myAway = myTieBreak.getAnalysis(myAwayPlayer);
191                 myAway.analyseAwayFixture(myFixture);
192             }
193         }
194     }
195 
196     /**
197      * Obtain tieBreak table.
198      * @param pPoints the points.
199      * @return the table
200      */
201     private AnalysisTable getTieBreak(final Integer pPoints) {
202         /* Access existing entry */
203         AnalysisTable myTable = theTieBreakers.get(pPoints);
204 
205         /* If we need a new entry */
206         if (myTable == null) {
207             /* Create new analysis and store in the map */
208             myTable = new AnalysisTable();
209             theTieBreakers.put(pPoints, myTable);
210         }
211 
212         /* Return the table */
213         return myTable;
214     }
215 
216     /**
217      * propagate TieBreaker.
218      */
219     private void propagateTieBreaks() {
220         /* Loop through the entries */
221         Iterator<AnalysisTable> myIterator = theTieBreakers.values().iterator();
222         while (myIterator.hasNext()) {
223             AnalysisTable myCurr = myIterator.next();
224 
225             /* Sort the table */
226             Collections.sort(myCurr.getTable(), theComparator);
227 
228             /* propagate tieBreaks */
229             myCurr.updateTieBreaker(theTable);
230         }
231     }
232 
233     /**
234      * Obtain tie-Break table.
235      * @param pPoints the points for the table
236      * @return the tie-break table
237      */
238     public ObservableList<PlayerAnalysis> getTieBreakTable(final Integer pPoints) {
239         /* Access the table */
240         AnalysisTable myTable = theTieBreakers.get(pPoints);
241 
242         /* If we have a table that is inactive */
243         if ((myTable != null)
244             && !myTable.isActive()) {
245             /* Discard the table */
246             myTable = null;
247         }
248 
249         /* return the table */
250         return myTable == null
251                                ? null
252                                : myTable.getTable();
253     }
254 }