001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2001-2003 Emmanuel Bourg
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019
020 package net.jetrix.winlist;
021
022 import java.util.*;
023
024 import net.jetrix.*;
025
026 /**
027 * The result of a game.
028 *
029 * @author Emmanuel Bourg
030 * @version $Revision: 816 $, $Date: 2010-01-19 18:05:31 +0100 (mar., 19 janv. 2010) $
031 */
032 public class GameResult
033 {
034 private Date startTime;
035 private Date endTime;
036 private List<GamePlayer> gamePlayers;
037 private Channel channel;
038
039 /**
040 * Return the date of the beginning of the game.
041 */
042 public Date getStartTime()
043 {
044 return startTime;
045 }
046
047 public void setStartTime(Date startTime)
048 {
049 this.startTime = startTime;
050 }
051
052 /**
053 * Return the date of the end of the game.
054 */
055 public Date getEndTime()
056 {
057 return endTime;
058 }
059
060 public void setEndTime(Date endTime)
061 {
062 this.endTime = endTime;
063 }
064
065 /**
066 * Return the channel associated to this result. It may be used to send a
067 * message in the channel reporting the new scores of the players.
068 */
069 public Channel getChannel()
070 {
071 return channel;
072 }
073
074 public void setChannel(Channel channel)
075 {
076 this.channel = channel;
077 }
078
079 /**
080 * Return the list of players involved in the game. The collection contains
081 * instances of GamePlayer.
082 */
083 public Collection<GamePlayer> getGamePlayers()
084 {
085 return gamePlayers;
086 }
087
088 private void addGamePlayer(GamePlayer gamePlayer)
089 {
090 if (gamePlayers == null)
091 {
092 gamePlayers = new ArrayList<GamePlayer>();
093 }
094 gamePlayers.add(gamePlayer);
095 }
096
097 /**
098 * Update the result of the game by indicating if the specified user won or not.
099 */
100 public void update(User user, boolean isWinner)
101 {
102 GamePlayer player = new GamePlayer();
103 player.setName(user.getName());
104 player.setTeamName(user.getTeam());
105 player.setWinner(isWinner);
106 player.setEndTime(new Date());
107 addGamePlayer(player);
108 }
109
110 /**
111 * Return the players that finished the game at the specified rank.
112 */
113 public Collection<GamePlayer> getPlayersAtRank(int rank)
114 {
115 Collection<GamePlayer> players = new ArrayList<GamePlayer>();
116
117 if (rank == 1)
118 {
119 // look for the winners
120 for (GamePlayer player : gamePlayers)
121 {
122 if (player.isWinner())
123 {
124 players.add(player);
125 }
126 }
127 }
128 else
129 {
130 // sort by date (reverse order)
131 Collections.sort(gamePlayers, new Comparator<GamePlayer>()
132 {
133 public int compare(GamePlayer player1, GamePlayer player2)
134 {
135 return player2.getEndTime().compareTo(player1.getEndTime());
136 }
137 });
138
139 // find the player at the specified rank
140 int i = 1;
141 Iterator<GamePlayer> it = gamePlayers.iterator();
142 while (it.hasNext() && i < rank)
143 {
144 GamePlayer player = it.next();
145 if (!player.isWinner())
146 {
147 i++;
148 if (i == rank)
149 {
150 players.add(player);
151 }
152 }
153 }
154 }
155
156 return players;
157 }
158
159 /**
160 * Return the number of teams in this game.
161 */
162 public int getTeamCount()
163 {
164 Map<String, String> teams = new HashMap<String, String>();
165
166 int teamCount = 0;
167
168 for (GamePlayer player : gamePlayers)
169 {
170 String team = player.getTeamName();
171
172 if (team == null)
173 {
174 teamCount++;
175 }
176 else
177 {
178 teams.put(team, team);
179 }
180 }
181
182 return teamCount + teams.size();
183 }
184
185 }