Source code: org/dma/ihm/game/league/std/StdLeague.java
1 package org.dma.ihm.game.league.std;
2
3 import java.util.*;
4
5 import org.dma.ihm.controller.*;
6 import org.dma.ihm.controller.data.*;
7 import org.dma.ihm.game.calendar.*;
8 import org.dma.ihm.game.calendar.events.*;
9 import org.dma.ihm.game.league.*;
10 import org.dma.ihm.game.league.helper.*;
11 import org.dma.ihm.game.league.std.events.*;
12 import org.dma.ihm.game.team.*;
13
14 /**
15 * StdLeague Implementation of a standard League after the model of the lovely
16 * country called Switzerland ;-) <p>
17 *
18 * - Stdleague may have n teams.<p>
19 *
20 * - The teams will play n1 times against each other (once at home, once away)
21 * called regular season.<p>
22 *
23 * - After the regular season the top teams play Playoffs, the bottom teams
24 * Playouts.<p>
25 *
26 * - The playoff/outs work with n2 "levels" (quarter, semi- and final or more)
27 * <p>
28 *
29 * - If 0 is passed by the constructor as starting level of playoffs/outs none
30 * are played.
31 *
32 * @author Bernhard von Gunten
33 * @created December 29, 2001
34 */
35 public class StdLeague extends League {
36
37 /** Game calendar helper to place matches in the calendar */
38 private GameCalendarHelper gameCalendarHelper = null;
39 /** Game calendar helper to place Playoff matches in the calendar */
40 private GameCalendarHelper gameCalendarPlayoffHelper = null;
41 /** Game calendar helper to place Playouts matches in the calendar */
42 private GameCalendarHelper gameCalendarPlayoutHelper = null;
43
44 /** Gamedays while regular season */
45 private final static String regularSeasonGameDays = "TU FR SA";
46
47 /** Gamedays while playoff/outs */
48 private final static String playoffGameDays = "TU TH SA";
49
50 /** Regular season */
51 private StdRegularSeason regularSeason = null;
52 /** Playoffs */
53 private StdPlayoffs[] playoffs = null;
54 /** Playouts */
55 private StdPlayoffs[] playouts = null;
56 /** Other Elements (i.E. LeaguePlayoffs) are grouped in the playouts group ! */
57 private Vector otherLeagueElements = null;
58
59 /** Regular Seasons group */
60 private LeagueElementGroup regularSeasonGroup = null;
61 /** Playoffs group */
62 private LeagueElementGroup playoffsGroup = null;
63 /** Playouts group */
64 private LeagueElementGroup playoutsGroup = null;
65
66 /** Playofflevels */
67 private int playoffLevels;
68 /** Playoff are bestOf n */
69 private int playoffsBestOf;
70
71 /** Playoutlevels */
72 private int playoutLevels;
73 /** Playouts are bestOf n */
74 private int playoutsBestOf;
75
76 /** How many times play teams against each other (1 = 1 home and 1 away game) */
77 private int multiRounds;
78
79 /** Helper to mark finished playoffs */
80 private boolean playoffsFinished = false;
81 /** Helper to mark finished playouts */
82 private boolean playoutsFinished = false;
83
84
85 /**
86 * Constructor with leaguename, rank in the owner, teams, multirounds,
87 * playoffLevels, playoffbestOf, playoutLevels, playoutBestOf. Doesn't create
88 * new matches ! See reset function.
89 *
90 * @param leagueName The name of this league
91 * @param ranking The rank of this league (in the leagueOwner)
92 * @param teams The teams of this league
93 * @param multiRounds The nr of rounds played in the regular season
94 * @param playoffLevels The nr of levels played in the whole playoffs
95 * @param playoffsBestOf Best of n playOffs
96 * @param playoutLevels The nr of levels played in the whole playouts
97 * @param playoutsBestOf Best of n playOuts
98 */
99 public StdLeague(String leagueName, int ranking, Team[] teams, int multiRounds, int playoffLevels, int playoffsBestOf, int playoutLevels, int playoutsBestOf) {
100 super(leagueName, ranking, teams);
101 this.otherLeagueElements = new Vector();
102
103 this.playoffLevels = playoffLevels;
104 this.playoffsBestOf = playoffsBestOf;
105
106 this.playoutLevels = playoutLevels;
107 this.playoutsBestOf = playoutsBestOf;
108
109 this.playoffs = new StdPlayoffs[playoffLevels];
110 this.playouts = new StdPlayoffs[playoutLevels];
111 this.multiRounds = multiRounds;
112 }
113
114
115 /**
116 * Gets the multiRounds attribute of the StdLeague object
117 *
118 * @return The multiRounds value
119 */
120 public int getMultiRounds() {
121 return multiRounds;
122 }
123
124
125 /**
126 * Gets the playoffLevels attribute of the StdLeague object
127 *
128 * @return The playoffLevels value
129 */
130 public int getPlayoffLevels() {
131 return playoffLevels;
132 }
133
134
135 /**
136 * Gets the playoffsBestOf attribute of the StdLeague object
137 *
138 * @return The playoffsBestOf value
139 */
140 public int getPlayoffsBestOf() {
141 return playoffsBestOf;
142 }
143
144
145 /**
146 * Gets the playoutLevels attribute of the StdLeague object
147 *
148 * @return The playoutLevels value
149 */
150 public int getPlayoutLevels() {
151 return playoutLevels;
152 }
153
154
155 /**
156 * Gets the playoutsBestOf attribute of the StdLeague object
157 *
158 * @return The playoutsBestOf value
159 */
160 public int getPlayoutsBestOf() {
161 return playoutsBestOf;
162 }
163
164
165
166 /** Resets league, and generates the regular Season */
167 public void newSeason() {
168 this.playoffsFinished = false;
169 this.playoutsFinished = false;
170 this.gameCalendarHelper = Controller.getScenario().getGameCalendar().getGameCalendarHelper();
171 this.gameCalendarHelper.setGameDays(regularSeasonGameDays);
172
173 // Create regular season, playoffs and playouts
174 regularSeason = new StdRegularSeason(0, Controller.getTranslation("stdleague.regularSeason"), this);
175
176 for (int i = 0; i < this.playoffs.length; i++) {
177 this.playoffs[i] = new StdPlayoffs(this.playoffs.length - i - 1, Controller.getTranslation("stdleague.playOffs") + " " + StdPlayoffs.getTitle(i), this, playoffsBestOf);
178 }
179 for (int i = 0; i < this.playouts.length; i++) {
180 this.playouts[i] = new StdPlayoffs(this.playouts.length - i - 1, Controller.getTranslation("stdleague.playOuts") + " " + StdPlayoffs.getTitle(i), this, playoutsBestOf);
181 }
182
183 this.otherLeagueElements = new Vector();
184
185 // Reset all groups
186 regularSeasonGroup = new LeagueElementGroup(this, Controller.getTranslation("stdleague.regularSeason"));
187 playoffsGroup = new LeagueElementGroup(this, Controller.getTranslation("stdleague.playOffs"));
188 playoutsGroup = new LeagueElementGroup(this, Controller.getTranslation("stdleague.playOuts"));
189
190 // Add leagueElement to leagueElement groups
191 regularSeasonGroup.addLeagueElement(regularSeason);
192
193 for (int i = 0; i < this.playoffs.length; i++) {
194 playoffsGroup.addLeagueElement(this.playoffs[i]);
195 }
196 for (int i = 0; i < this.playouts.length; i++) {
197 playoutsGroup.addLeagueElement(this.playouts[i]);
198 }
199 for (int i = 0; i < this.otherLeagueElements.size(); i++) {
200 playoutsGroup.addLeagueElement((LeagueElement) this.otherLeagueElements.get(i));
201 }
202
203 // And now create the regular Season ...
204 this.generateRegularSeason();
205
206 }
207
208
209 /**
210 * Returns the owner of this league
211 *
212 * @return The stdLeagueOwner value
213 */
214 private StdLeagueOwner getStdLeagueOwner() {
215 return (StdLeagueOwner) leagueOwner;
216 }
217
218
219 /**
220 * Creates the matches of the regular season, and places the matches in the
221 * game calendar. Important: Also places a event in the calendar, which will
222 * create playoff/outs (if they are wanted, else directly the end of them).
223 */
224 public void generateRegularSeason() {
225 regularSeason.init(teams, multiRounds, gameCalendarHelper);
226
227 // Create two new calendar helper, so playoff & playout games can be on the same day
228 Calendar day = gameCalendarHelper.getNextFreeGameDay();
229
230 gameCalendarPlayoffHelper = Controller.getScenario().getGameCalendar().getGameCalendarHelperByDay(day);
231 this.gameCalendarPlayoffHelper.setGameDays(playoffGameDays);
232
233 gameCalendarPlayoutHelper = Controller.getScenario().getGameCalendar().getGameCalendarHelperByDay(day);
234 this.gameCalendarPlayoutHelper.setGameDays(playoffGameDays);
235
236 // Now add the first playoffs or the end of the playoffs;
237 if (playoffLevels > 0) {
238 Controller.getScenario().getGameCalendar().addEvent(new StdGameCalendarNewPlayoffLevelEvent(this, gameCalendarPlayoffHelper.getNextFreeGameDay(), playoffLevels - 1, this));
239 } else {
240 Controller.getScenario().getGameCalendar().addEvent(new StdGameCalendarPlayoffFinishedEvent(this, gameCalendarPlayoffHelper.getNextFreeGameDay(), this));
241 }
242
243 // Now add the first playouts or the end of the playouts;
244 if (playoutLevels > 0) {
245 Controller.getScenario().getGameCalendar().addEvent(new StdGameCalendarNewPlayoutLevelEvent(this, gameCalendarPlayoutHelper.getNextFreeGameDay(), playoutLevels - 1, this));
246 } else {
247 Controller.getScenario().getGameCalendar().addEvent(new StdGameCalendarPlayoutFinishedEvent(this, gameCalendarPlayoutHelper.getNextFreeGameDay(), this));
248 }
249
250 }
251
252
253 /**
254 * Creates the level'th round of the playoffs <p>
255 *
256 * - If there is only one round to play (final) or the it's the first round
257 * it takes the teams of the league directly, else out of the winners of the
258 * last playoff round.
259 *
260 * @param level Level of playoffs
261 */
262 public void generatePlayoffsLevel(int level) {
263 int teamsCount = (int) Math.pow(2, level + 1);
264
265 if (level == playoffs.length - 1) {
266 // Take teams from regular Season
267 Team[] tmp = new Team[teamsCount];
268 for (int i = 0; i < teamsCount; i++) {
269 tmp[i] = regularSeason.getLeagueStandingsTeams()[i];
270 }
271 playoffs[level].init(tmp, gameCalendarPlayoffHelper);
272 } else {
273 // Take them of the last playoff round
274 playoffs[level].init(playoffs[level + 1].getWinners(), gameCalendarPlayoffHelper);
275 }
276
277 // Now add the next playoffs or the end of the playoffs;
278 if (level > 0) {
279 Controller.getScenario().getGameCalendar().addEvent(new StdGameCalendarNewPlayoffLevelEvent(this, gameCalendarPlayoffHelper.getNextFreeGameDay(), level - 1, this));
280 } else {
281 Controller.getScenario().getGameCalendar().addEvent(new StdGameCalendarPlayoffFinishedEvent(this, gameCalendarPlayoffHelper.getNextFreeGameDay(), this));
282 }
283 }
284
285
286 /**
287 * Like playoffs, but different ;-))
288 *
289 * @param level The level of playouts
290 */
291 public void generatePlayoutsLevel(int level) {
292 int teamsCount = (int) Math.pow(2, level + 1);
293
294 if (level == playouts.length - 1) {
295 // Take teams from regular Season
296 Team[] tmp = new Team[teamsCount];
297 for (int i = teams.length - teamsCount; i < teams.length; i++) {
298 tmp[i - (teams.length - teamsCount)] = regularSeason.getLeagueStandingsTeams()[i];
299 }
300 playouts[level].init(tmp, gameCalendarPlayoutHelper);
301 } else {
302 // Take them of the last playout round
303 playouts[level].init(playouts[level + 1].getLosers(), gameCalendarPlayoutHelper);
304 }
305
306 // Now add the next playouts or the end of the playouts;
307 if (level > 0) {
308 Controller.getScenario().getGameCalendar().addEvent(new StdGameCalendarNewPlayoutLevelEvent(this, gameCalendarPlayoutHelper.getNextFreeGameDay(), level - 1, this));
309 } else {
310 Controller.getScenario().getGameCalendar().addEvent(new StdGameCalendarPlayoutFinishedEvent(this, gameCalendarPlayoutHelper.getNextFreeGameDay(), this));
311 }
312 }
313
314
315 /**
316 * Returns the winner of this league (called by leagueOwner to i.E. start the
317 * leaguePlayofffs
318 *
319 * @return The leagueFirstTeam value
320 */
321 public Team getLeagueFirstTeam() {
322 if (playoffLevels > 0) {
323 Team[] winner = playoffs[0].getWinners();
324 return winner[0];
325 } else {
326 return regularSeason.getLeagueStandingsTeams()[0];
327 }
328 }
329
330
331 /**
332 * Returns the loser of this league (called by leagueOwner to i.E. start the
333 * leaguePlayofffs
334 *
335 * @return The leagueLastTeam value
336 */
337 public Team getLeagueLastTeam() {
338 if (playoutLevels > 0) {
339 Team[] loser = playouts[0].getLosers();
340 return loser[0];
341 } else {
342 return regularSeason.getLeagueStandingsTeams()[teams.length - 1];
343 }
344 }
345
346
347 /**
348 * Checks if first team (winner) is known (so we could start the
349 * leaguePlayoffs
350 *
351 * @return The firstTeamKnown value
352 */
353 public boolean isFirstTeamKnown() {
354 return playoffsFinished;
355 }
356
357
358 /**
359 * Checks if last team (loser) is known (so we could start the leaguePlayoffs
360 *
361 * @return The lastTeamKnown value
362 */
363 public boolean isLastTeamKnown() {
364 return playoutsFinished;
365 }
366
367
368 /** Sets playoutsFinished flag to true and informes the owner */
369 public void leaguePlayoutsFinished() {
370 this.playoutsFinished = true;
371 getStdLeagueOwner().leaguePartFinished();
372 }
373
374
375 /** Sets playoffFinished flag to true and informes the owner */
376 public void leaguePlayoffsFinished() {
377 this.playoffsFinished = true;
378 getStdLeagueOwner().leaguePartFinished();
379 }
380
381
382 /**
383 * Add playoffs (League Playoffs) to this league
384 *
385 * @param leagueElement The feature to be added to the LeaguePlayoffElement
386 * attribute
387 */
388 public void addLeaguePlayoffElement(LeagueElement leagueElement) {
389 this.otherLeagueElements.add(leagueElement);
390 playoutsGroup.addLeagueElement(leagueElement);
391 }
392
393
394 /**
395 * Returns all leagueElements
396 *
397 * @return The leagueElements value
398 */
399 public Vector getLeagueElements() {
400 Vector result = new Vector();
401 // Regular Season
402 result.add(regularSeason);
403 // Playoffs
404 for (int i = 0; i < playoffs.length; i++) {
405 result.add(playoffs[i]);
406 }
407 // Playouts
408 for (int i = 0; i < playouts.length; i++) {
409 result.add(playouts[i]);
410 }
411 for (int i = 0; i < otherLeagueElements.size(); i++) {
412 result.add(otherLeagueElements.get(i));
413 }
414
415 java.util.Collections.sort(result);
416
417 return result;
418 }
419
420
421 /**
422 * Returns the LeagueElementGroups of this League
423 *
424 * @return The leagueElementGroups value
425 */
426 public Vector getLeagueElementGroups() {
427 Vector result = new Vector();
428 if (regularSeasonGroup.getLeagueElementCount() > 0) {
429 result.add(regularSeasonGroup);
430 }
431 if (playoffsGroup.getLeagueElementCount() > 0) {
432 result.add(playoffsGroup);
433 }
434 if (playoutsGroup.getLeagueElementCount() > 0) {
435 result.add(playoutsGroup);
436 }
437 return result;
438 }
439
440 }