View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2001-2003  Emmanuel Bourg
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package net.jetrix.winlist;
21  
22  import java.util.*;
23  
24  import net.jetrix.*;
25  
26  /***
27   * The result of a game.
28   *
29   * @author Emmanuel Bourg
30   * @version $Revision: 816 $, $Date: 2010-01-19 18:05:31 +0100 (mar., 19 janv. 2010) $
31   */
32  public class GameResult
33  {
34      private Date startTime;
35      private Date endTime;
36      private List<GamePlayer> gamePlayers;
37      private Channel channel;
38  
39      /***
40       * Return the date of the beginning of the game.
41       */
42      public Date getStartTime()
43      {
44          return startTime;
45      }
46  
47      public void setStartTime(Date startTime)
48      {
49          this.startTime = startTime;
50      }
51  
52      /***
53       * Return the date of the end of the game.
54       */
55      public Date getEndTime()
56      {
57          return endTime;
58      }
59  
60      public void setEndTime(Date endTime)
61      {
62          this.endTime = endTime;
63      }
64  
65      /***
66       * Return the channel associated to this result. It may be used to send a
67       * message in the channel reporting the new scores of the players.
68       */
69      public Channel getChannel()
70      {
71          return channel;
72      }
73  
74      public void setChannel(Channel channel)
75      {
76          this.channel = channel;
77      }
78  
79      /***
80       * Return the list of players involved in the game. The collection contains
81       * instances of GamePlayer.
82       */
83      public Collection<GamePlayer> getGamePlayers()
84      {
85          return gamePlayers;
86      }
87  
88      private void addGamePlayer(GamePlayer gamePlayer)
89      {
90          if (gamePlayers == null)
91          {
92              gamePlayers = new ArrayList<GamePlayer>();
93          }
94          gamePlayers.add(gamePlayer);
95      }
96  
97      /***
98       * Update the result of the game by indicating if the specified user won or not.
99       */
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 }