Source code: org/dma/ihm/game/league/std/StdPlayoffs.java
1 package org.dma.ihm.game.league.std;
2
3 import java.io.*;
4 import java.util.*;
5
6 import org.dma.ihm.controller.*;
7 import org.dma.ihm.controller.data.*;
8 import org.dma.ihm.game.calendar.*;
9 import org.dma.ihm.game.calendar.events.*;
10 import org.dma.ihm.game.league.*;
11 import org.dma.ihm.game.league.helper.*;
12 import org.dma.ihm.game.league.std.events.*;
13 import org.dma.ihm.game.match.*;
14 import org.dma.ihm.game.team.*;
15 import org.dma.ihm.game.player.*;
16
17 /**
18 * StdPlayoffs Implementation of playoffs. - Creates matches & GameDayEvents
19 * and post them in the calendar.
20 *
21 * @author Bernhard von Gunten
22 * @created December 29, 2001
23 */
24 public class StdPlayoffs extends LeagueElement implements Serializable {
25
26 /** Array of Competitions [teams/2] */
27 private PlayoffCompetition[] playoffCompetitions = null;
28 /** Array of possible gameDates for additional matches */
29 private Calendar[] addGameDates;
30 /** Maximum count of matches between two teams */
31 private int bestOf;
32 /** Count of competitions (4 if 8 teams are in playoffs) */
33 private int competitionsCount;
34 /** Teams of this playoffs */
35 private Team[] teams;
36
37
38 /**
39 * Create empty Playoffs
40 *
41 * @param rank Rank of leagueElement
42 * @param name Name of this playoffs
43 * @param league League of this playoffs
44 * @param bestOf Best of n Playoffs
45 */
46 public StdPlayoffs(int rank, String name, League league, int bestOf) {
47 super(rank, name, league);
48 this.bestOf = bestOf;
49 }
50
51
52 /**
53 * Init Playoffs
54 *
55 * @param teams Teams in this playoffs
56 * @param gameCalendarHelper GameCalendarHelper for match announcements
57 */
58 public void init(Team[] teams, GameCalendarHelper gameCalendarHelper) {
59 this.teams = teams;
60 setAnnounced();
61 this.competitionsCount = teams.length / 2;
62 playoffCompetitions = new PlayoffCompetition[competitionsCount];
63 addGameDates = new Calendar[(bestOf - 1) / 2];
64 generateTeamStats();
65 generateMatches();
66 generateCalendarEvents(gameCalendarHelper);
67 }
68
69
70 /**
71 * Returns teams of this leagueElement
72 *
73 * @return The teams value
74 */
75 public Team[] getTeams() {
76 if (teams == null) {
77 return new Team[0];
78 } else {
79 return teams;
80 }
81 }
82
83 public Player[] getPlayers() {
84 if (teams == null) {
85 return new Player[0];
86 } else {
87 Vector tmp = new Vector();
88 Team[] teams = getTeams();
89 for (int i = 0; i < teams.length; i++) {
90 Player[] players = teams[i].getPlayers();
91 for (int n = 0; n < players.length; n++) {
92 tmp.add(players[n]);
93 }
94 }
95
96 return ((Player[]) tmp.toArray(new Player[tmp.size()]) );
97
98 }
99 }
100
101
102 /** Generates schedule for this playoffs */
103 public void generateMatches() {
104 Rules rules = new StdPlayoffRules();
105 for (int i = 0; i < playoffCompetitions.length; i++) {
106 playoffCompetitions[i] = new PlayoffCompetition(teams[i], teams[teams.length - i - 1], bestOf);
107 for (int n = 0; n < bestOf; n++) {
108 if (n % 2 == 0) {
109 playoffCompetitions[i].addMatch(new Match(this, teams[i], teams[teams.length - i - 1], rules), n);
110 } else {
111 playoffCompetitions[i].addMatch(new Match(this, teams[teams.length - i - 1], teams[i], rules), n);
112 }
113 }
114 }
115 }
116
117
118 /**
119 * Generate GameDayEvents (and their StopBeforeEvents) for playoff games and
120 * make "reservations" for more games if needed
121 *
122 * @param gameCalendarHelper GameCalendarHelper for match announcements
123 */
124 public void generateCalendarEvents(GameCalendarHelper gameCalendarHelper) {
125 // Place fix games
126 Calendar day = Controller.getScenario().getGameCalendar().getToday();
127 // Fill with today, just in case
128 for (int n = 0; n < (bestOf + 1) / 2; n++) {
129 day = gameCalendarHelper.getNextFreeGameDay();
130 GameCalendarGameDayEvent event = new GameCalendarGameDayEvent(this, day, new Vector(), this, this.getDescription() + " : " + String.valueOf(n + 1));
131 for (int i = 0; i < playoffCompetitions.length; i++) {
132 event.addMatch(playoffCompetitions[i].getMatch(n));
133 }
134 GameCalendarStopBeforeGamedayEvent tmp = Controller.getScenario().getGameCalendar().getStopBeforeGamedayEvent(day);
135 tmp.addGameCalendarGameDayEvent(event);
136
137 Controller.getScenario().getGameCalendar().addEvent(event);
138 }
139
140 // Add a first PlayoffRoundFinished event after first half of playoff
141 Controller.getScenario().getGameCalendar().addEvent(new StdGameCalendarPlayoffRoundFinishedEvent(this, day, this, (bestOf - 1) / 2));
142
143 // "Reservation" for additional games
144 for (int n = 0; n < (bestOf - 1) / 2; n++) {
145 addGameDates[n] = gameCalendarHelper.getNextFreeGameDay();
146 }
147 }
148
149
150 /**
151 * Playoff round is played. Look if further matches are needed
152 *
153 * @param playedRound The nr of the played round
154 */
155 public void roundPlayed(int playedRound) {
156 boolean postEvent = false;
157 Calendar day = addGameDates[playedRound - ((bestOf - 1) / 2)];
158 GameCalendarGameDayEvent event = new GameCalendarGameDayEvent(this, day, new Vector(), this, this.getDescription() + " : " + String.valueOf(playedRound + 2));
159 for (int i = 0; i < playoffCompetitions.length; i++) {
160 if (!playoffCompetitions[i].isCompetitionWon()) {
161 postEvent = true;
162 event.addMatch(playoffCompetitions[i].getMatch(playedRound + 1));
163 }
164 }
165 if (postEvent) {
166 GameCalendarStopBeforeGamedayEvent tmp = Controller.getScenario().getGameCalendar().getStopBeforeGamedayEvent(day);
167 tmp.addGameCalendarGameDayEvent(event);
168
169 Controller.getScenario().getGameCalendar().addEvent(event);
170 if (playedRound < bestOf - 2) {
171 Controller.getScenario().getGameCalendar().addEvent(new StdGameCalendarPlayoffRoundFinishedEvent(this, day, this, playedRound + 1));
172 }
173 }
174 }
175
176
177 /**
178 * Returns the winners of this playoffs
179 *
180 * @return The winners value
181 */
182 public Team[] getWinners() {
183 return getWinnersOrLosers(true);
184 }
185
186
187 /**
188 * Returns the losers of this playoffs
189 *
190 * @return The losers value
191 */
192 public Team[] getLosers() {
193 return getWinnersOrLosers(false);
194 }
195
196
197 /**
198 * Returns the winners or losers of this playoffs
199 *
200 * @param winners If winners (true) or losers (false) are searched.
201 * @return An array of teams
202 */
203 private Team[] getWinnersOrLosers(boolean winners) {
204 Team[] result = new Team[competitionsCount];
205
206 // Calculate winner
207 for (int i = 0; i < playoffCompetitions.length; i++) {
208 if (winners) {
209 result[i] = playoffCompetitions[i].getWinner();
210 } else {
211 result[i] = playoffCompetitions[i].getLoser();
212 }
213 }
214 return result;
215 }
216
217
218 /**
219 * Returns the title of this Playoffs
220 *
221 * @param level The level of a global playoffs (0 = final, 1 = semiFinal
222 * etc.)
223 * @return The title value
224 */
225 public static String getTitle(int level) {
226 if (level == 0) {
227 return Controller.getTranslation("stdleague.playOffFinal");
228 }
229
230 if (level == 1) {
231 return Controller.getTranslation("stdleague.playOffSemiFinal");
232 }
233
234 if (level == 2) {
235 return Controller.getTranslation("stdleague.playOffQuarterFinal");
236 }
237
238 // Todo ... some formating like 1/8 final ...
239 return String.valueOf(level);
240 }
241
242
243 /**
244 * Returns all competitions
245 *
246 * @return The playoffCompetitions value
247 */
248 public PlayoffCompetition[] getPlayoffCompetitions() {
249 return playoffCompetitions;
250 }
251
252
253 /**
254 * Returns the standings of this playoffs
255 *
256 * @return The leagueStandings value
257 */
258 public LeagueStandings getLeagueStandings() {
259 return new LeagueStandingsPlayoffs(this, getPlayoffCompetitions());
260 }
261
262 }