View Javadoc

1   /*
2    * Copyright (C) 2010 Unreal Visualizer Authors
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package nl.tudelft.goal.ut2004.visualizer.data;
18  
19  import java.util.Collection;
20  import java.util.Map;
21  
22  import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
23  import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectId;
24  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.FlagInfo;
25  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameInfo;
26  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.TeamScore;
27  import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
28  
29  /**
30   * 
31   * {@link AbstractData} object that keeps track of the objects that refer to the status of the game. More precisely stores the latest {@link GameStatus} and
32   * {@link FlagInfo} objects.
33   * 
34   * @author Lennard de Rijk
35   * 
36   */
37  public class GameStatusData extends AbstractData {
38  
39  	/**
40  	 * Total number of teams we can store {@link FlagInfo} for.
41  	 */
42  	private static final int NUMBER_OF_TEAMS = 2;
43  
44  	/**
45  	 * Last {@link GameStatus} we have received
46  	 */
47  	private GameInfo gameInfo;
48  
49  	/**
50  	 * Contains for each team the last {@link FlagInfo} object we have received
51  	 */
52  	private Collection<FlagInfo> flagInfoCollection;
53  
54  	private Collection<TeamScore> teamScoreCollection;
55  
56  	public GameStatusData() {
57  		super();
58  	}
59  
60  	/**
61  	 * Listener for the server.
62  	 * 
63  	 * May be called from another thread.
64  	 */
65  	@Override
66  	public synchronized void serverChanged(IUT2004Server server) {
67  		if (server != null) {
68  			updateGameInfo(server.getWorldView());
69  			updateFlagInfo(server.getWorldView());
70  			updateTeamScore(server.getWorldView());
71  		} else {
72  			clearGameInfo();
73  			clearFlagInfo();
74  			clearTeamScore();
75  		}
76  
77  		notifyListeners();
78  	}
79  
80  	private void clearTeamScore() {
81  		teamScoreCollection = null;
82  	}
83  
84  	private void updateTeamScore(IWorldView view) {
85  		Class<TeamScore> teamScoreClass = TeamScore.class;
86  		teamScoreCollection = view.getAll(teamScoreClass).values();
87  	}
88  
89  	/**
90  	 * Clears the {@link FlagInfo} from the game status.
91  	 */
92  	private void clearFlagInfo() {
93  		flagInfoCollection = null;
94  	}
95  
96  	/**
97  	 * Clears the {@link GameInfo} from the game status.
98  	 */
99  	private void clearGameInfo() {
100 		gameInfo = null;
101 	}
102 
103 	/**
104 	 * Updates the current game status.
105 	 * 
106 	 * @param server
107 	 *            the latest {@link GameStatus} object
108 	 */
109 	private void updateGameInfo(IWorldView view) {
110 		this.gameInfo = view.getSingle(GameInfo.class);
111 	}
112 
113 	/**
114 	 * Updates the current information about a flag.
115 	 * 
116 	 * @param flagInfo
117 	 *            the {@link FlagInfo} object to store
118 	 */
119 	private void updateFlagInfo(IWorldView view) {
120 		Map<WorldObjectId, FlagInfo> flags = view.getAll(FlagInfo.class);
121 		flagInfoCollection = flags.values();
122 	}
123 
124 	/**
125 	 * 
126 	 * @return The most recent {@link GameStatus}
127 	 */
128 	public synchronized GameInfo getGameInfo() {
129 		return gameInfo;
130 	}
131 
132 	/**
133 	 * @return the flagInfoCollection
134 	 */
135 	public synchronized Collection<FlagInfo> getFlagInfoCollection() {
136 		return flagInfoCollection;
137 	}
138 
139 	/**
140 	 * 
141 	 * @return the teamScoreCollection
142 	 */
143 	public Collection<TeamScore> getTeamScoreCollection() {
144 		return teamScoreCollection;
145 	}
146 
147 }