Package org.swtpra1
Class DoubleRoundRobinTournament
- java.lang.Object
-
- org.swtpra1.DoubleRoundRobinTournament
-
- All Implemented Interfaces:
java.io.Serializable
public class DoubleRoundRobinTournament extends java.lang.Object implements java.io.Serializable
Represents one instance of a tournament and all its players at any given moment. Contains all the games that have and will be played and keeps track of the availability of the players. Saves instances of all players and games centrally and only uses their unique ids otherwise to prevent object equality confusion when working with deserialized data.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_COLUMN_SIZE
static int
DEFAULT_ROW_SIZE
-
Constructor Summary
Constructors Constructor Description DoubleRoundRobinTournament(java.util.List<Player> players, long maxTurnTime)
Constructs a new tournament using the current time as a filename.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
allPlayersFree(Game game)
Checks whether all participating players of a game are free to play right now.void
generateGames()
Constructs a list of matches in the DoubleRoundRobin format, which includes a dummy player if there is an odd number of players.java.util.List<Game>
getAllGames()
java.util.Set<java.lang.Integer>
getFinishedGames()
java.util.Set<java.lang.Integer>
getFreePlayers()
java.util.Map<Player,java.lang.Integer>
getLeaderboard()
Gets a mapping Player -> #wins sorted in descending order.long
getMaxTurnTime()
Player
getPlayerById(int id)
Return the locally saved player object with the given absolute id if it exists.java.util.List<Player>
getPlayers()
java.beans.PropertyChangeSupport
getPropertyChangeSupport()
Getter for the PropertyChangeSupport in order to register Listeners.java.util.Set<java.lang.Integer>
getRunningGames()
java.util.Set<java.lang.Integer>
getUnstartedGames()
boolean
isFinished()
Returns whether every match of the tournament has been completed.Game
nextGame()
Returns the next game that can be started.boolean
startGame(Game game)
Will start a given game and log its players as busy if they were previously available.int
updateGame(int gameId, Game newState)
Updates the state of a game with given Id, sorting it into the appropriate list.
-
-
-
Field Detail
-
DEFAULT_ROW_SIZE
public static final int DEFAULT_ROW_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_COLUMN_SIZE
public static final int DEFAULT_COLUMN_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DoubleRoundRobinTournament
public DoubleRoundRobinTournament(java.util.List<Player> players, long maxTurnTime)
Constructs a new tournament using the current time as a filename.- Parameters:
players
- the participating playersmaxTurnTime
- the time allowed for each turn
-
-
Method Detail
-
generateGames
public void generateGames()
Constructs a list of matches in the DoubleRoundRobin format, which includes a dummy player if there is an odd number of players. The dummy player and his matches are not included in the resulting list. The list is saved in this.allGames. Implements the "circle method" as described on https://de.wikipedia.org/wiki/Jeder-gegen-jeden-Turnier#Rutschsystem
-
nextGame
public Game nextGame()
Returns the next game that can be started.- Returns:
- the next game; null if there are no possible games
-
isFinished
public boolean isFinished()
Returns whether every match of the tournament has been completed.- Returns:
- boolean
-
getPlayers
public java.util.List<Player> getPlayers()
-
getAllGames
public java.util.List<Game> getAllGames()
-
getFinishedGames
public java.util.Set<java.lang.Integer> getFinishedGames()
-
getRunningGames
public java.util.Set<java.lang.Integer> getRunningGames()
-
getUnstartedGames
public java.util.Set<java.lang.Integer> getUnstartedGames()
-
getFreePlayers
public java.util.Set<java.lang.Integer> getFreePlayers()
-
getMaxTurnTime
public long getMaxTurnTime()
-
updateGame
public int updateGame(int gameId, Game newState)
Updates the state of a game with given Id, sorting it into the appropriate list. If it switches into the "finished games" list, an propertyChangeEvent will be broadcast- Parameters:
gameId
- the Id of the game that (might have) changednewState
- the updated state of said game- Returns:
- the id of the winner, -1 if no winner, -2 if no game found
-
getLeaderboard
public java.util.Map<Player,java.lang.Integer> getLeaderboard()
Gets a mapping Player -> #wins sorted in descending order.- Returns:
- Map <Player, Integer>
-
startGame
public boolean startGame(Game game)
Will start a given game and log its players as busy if they were previously available.- Parameters:
game
- the game that shall be started- Returns:
- true if successful, false if game has already finished or players are busy
-
getPropertyChangeSupport
public java.beans.PropertyChangeSupport getPropertyChangeSupport()
Getter for the PropertyChangeSupport in order to register Listeners.- Returns:
- instance of PropertyChangeSupport
-
allPlayersFree
public boolean allPlayersFree(Game game)
Checks whether all participating players of a game are free to play right now.- Parameters:
game
- the game to be played- Returns:
- true if all are free; false otherwise
-
getPlayerById
public Player getPlayerById(int id)
Return the locally saved player object with the given absolute id if it exists.- Parameters:
id
- the id of the player- Returns:
- Player object; null if player not found
-
-