project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/NetRoomState.java
changeset 7582 714310efad8f
parent 7508 763d3961400b
child 7584 7831c84cc644
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/NetRoomState.java	Mon Aug 20 20:16:37 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/NetRoomState.java	Mon Aug 20 20:19:35 2012 +0200
@@ -1,202 +1,130 @@
 package org.hedgewars.hedgeroid.netplay;
 
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_GAMESTYLE;
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP;
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_DRAWDATA;
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_GENERATOR;
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_NAME;
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_SEED;
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_TEMPLATE;
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_THEME;
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAZE_SIZE;
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_SCHEME;
-import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_WEAPONSET;
+import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.*;
+import static org.hedgewars.hedgeroid.util.ObjectUtils.equal;
 
 import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.Collections;
+import java.util.Map;
+import java.util.TreeMap;
 
-import org.hedgewars.hedgeroid.RoomStateManager;
+import org.hedgewars.hedgeroid.BasicRoomState;
 import org.hedgewars.hedgeroid.Datastructures.GameConfig;
 import org.hedgewars.hedgeroid.Datastructures.MapRecipe;
 import org.hedgewars.hedgeroid.Datastructures.Scheme;
+import org.hedgewars.hedgeroid.Datastructures.Team;
+import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
+import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes;
 import org.hedgewars.hedgeroid.Datastructures.Weaponset;
 import org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType;
 
 /**
  * This class manages the room state in a network game.
  */
-class NetRoomState implements RoomStateManager {
-	private List<RoomStateManager.Observer> observers = new LinkedList<RoomStateManager.Observer>();
+class NetRoomState extends BasicRoomState {
+	final Map<String, TeamInGame> requestedTeams = new TreeMap<String, TeamInGame>();
 	private Netplay netplay;
 	
-	boolean chief;
-	String gameStyle;
-	Scheme scheme;
-	MapRecipe map;
-	Weaponset weaponset;
-	
 	public NetRoomState(Netplay netplay) {
 		this.netplay = netplay;
-		this.map = MapRecipe.makeRandomMap(0, "seed", GameConfig.DEFAULT_THEME);
-	}
-
-	public MapRecipe getMapRecipe() {
-		return map;
-	}
-
-	public boolean getChiefStatus() {
-		return chief;
-	}
-
-	public Scheme getScheme() {
-		return scheme;
-	}
-
-	public String getGameStyle() {
-		return gameStyle;
-	}
-
-	public Weaponset getWeaponset() {
-		return weaponset;
+		initRoomState(false);
 	}
 
 	public void changeWeaponset(Weaponset weaponset) {
-		if(chief && !weaponset.equals(this.weaponset)) {
+		if(getChiefStatus() && !equal(weaponset, getWeaponset())) {
 			sendToNet(MSG_SEND_WEAPONSET, weaponset);
 			setWeaponset(weaponset);
 		}
 	}
 	
 	public void changeMapRecipe(MapRecipe mapRecipe) {
-		if(chief && !mapRecipe.equals(this.map)) {
+		if(getChiefStatus() && !equal(mapRecipe, getMapRecipe())) {
 			sendToNet(MSG_SEND_MAP, mapRecipe);
 			setMapRecipe(mapRecipe);
 		}
 	}
 	
 	public void changeMapNameAndGenerator(String mapName) {
-		if(chief && !mapName.equals(this.map.name)) {
+		if(getChiefStatus() && !equal(mapName, getMapRecipe().name)) {
 			int newGenerator = MapRecipe.generatorForMapname(mapName);
-			if(newGenerator != this.map.mapgen) {
+			if(newGenerator != getMapRecipe().mapgen) {
 				sendToNet(MSG_SEND_MAP_GENERATOR, newGenerator, null);
 			}
 			sendToNet(MSG_SEND_MAP_NAME, mapName);
-			setMapRecipe(map.withName(mapName).withMapgen(newGenerator));
+			setMapRecipe(getMapRecipe().withName(mapName).withMapgen(newGenerator));
 		}
 	}
 	
 	public void changeMapTemplate(int template) {
-		if(chief && template != this.map.templateFilter) {
+		if(getChiefStatus() && template != getMapRecipe().templateFilter) {
 			sendToNet(MSG_SEND_MAP_TEMPLATE, template, null);
-			setMapRecipe(map.withTemplateFilter(template));
+			setMapRecipe(getMapRecipe().withTemplateFilter(template));
 		}
 	}
 	
 	public void changeMazeSize(int mazeSize) {
-		if(chief && mazeSize != this.map.mazeSize) {
+		if(getChiefStatus() && mazeSize != getMapRecipe().mazeSize) {
 			sendToNet(MSG_SEND_MAZE_SIZE, mazeSize, 0);
-			setMapRecipe(map.withMazeSize(mazeSize));
+			setMapRecipe(getMapRecipe().withMazeSize(mazeSize));
 		}
 	}
 	
 	public void changeMapSeed(String seed) {
-		if(chief && !seed.equals(this.map.seed)) {
+		if(getChiefStatus() && !equal(seed, getMapRecipe().seed)) {
 			sendToNet(MSG_SEND_MAP_SEED, seed);
-			setMapRecipe(map.withSeed(seed));
+			setMapRecipe(getMapRecipe().withSeed(seed));
 		}
 	}
 	
 	public void changeMapTheme(String theme) {
-		if(chief && !theme.equals(this.map.theme)) {
+		if(getChiefStatus() && !equal(theme, getMapRecipe().theme)) {
 			sendToNet(MSG_SEND_MAP_THEME, theme);
-			setMapRecipe(map.withTheme(theme));
+			setMapRecipe(getMapRecipe().withTheme(theme));
 		}
 	}
 	
 	public void changeMapDrawdata(byte[] drawdata) {
-		if(chief && !Arrays.equals(drawdata, this.map.getDrawData())) {
+		if(getChiefStatus() && !Arrays.equals(drawdata, getMapRecipe().getDrawData())) {
 			sendToNet(MSG_SEND_MAP_DRAWDATA, drawdata);
-			setMapRecipe(map.withDrawData(drawdata));
+			setMapRecipe(getMapRecipe().withDrawData(drawdata));
 		}
 	}
 	
 	public void changeGameStyle(String gameStyle) {
-		if(chief && !gameStyle.equals(this.gameStyle)) {
+		if(getChiefStatus() && !equal(gameStyle, getGameStyle())) {
 			sendToNet(MSG_SEND_GAMESTYLE, gameStyle);
 			setGameStyle(gameStyle);
 		}
 	}
 	
 	public void changeScheme(Scheme scheme) {
-		if(chief && !scheme.equals(this.scheme)) {
+		if(getChiefStatus() && !equal(scheme, getScheme())) {
 			sendToNet(MSG_SEND_SCHEME, scheme);
 			setScheme(scheme);
 		}
 	}
 	
-	void setWeaponset(Weaponset weaponset) {
-		if(!weaponset.equals(this.weaponset)) {
-			this.weaponset = weaponset;
-			for(RoomStateManager.Observer observer : observers) {
-				observer.onWeaponsetChanged(weaponset);
-			}
-		}
-	}
-	
-	void setMapRecipe(MapRecipe map) {
-		if(!map.equals(this.map)) { 
-			this.map = map;
-			for(RoomStateManager.Observer observer : observers) {
-				observer.onMapChanged(map);
-			}
-		}
-	}
-	
-	void setGameStyle(String gameStyle) {
-		if(!gameStyle.equals(this.gameStyle)) {
-			this.gameStyle = gameStyle;
-			for(RoomStateManager.Observer observer : observers) {
-				observer.onGameStyleChanged(gameStyle);
-			}
-		}
-	}
-	
-	void setScheme(Scheme scheme) {
-		if(!scheme.equals(this.scheme)) {
-			this.scheme = scheme;
-			for(RoomStateManager.Observer observer : observers) {
-				observer.onSchemeChanged(scheme);
-			}
-		}
-	}
-	
-	void setChief(boolean chief) {
-		if(chief != this.chief) {
-			this.chief = chief;
-			for(RoomStateManager.Observer observer : observers) {
-				observer.onChiefStatusChanged(chief);
-			}
-		}
+	void initRoomState(boolean chief) {
+		setTeams(Collections.<String, TeamInGame>emptyMap());
+		requestedTeams.clear();
+		
+		setChief(chief);
+		setGameStyle(GameConfig.DEFAULT_STYLE);
+		setMapRecipe(MapRecipe.makeRandomMap(0, "seed", GameConfig.DEFAULT_THEME));
+		setScheme(netplay.defaultScheme);
+		setWeaponset(netplay.defaultWeaponset);
+		sendFullConfig();
 	}
 	
 	void sendFullConfig() {
-		if(chief) {
-			sendToNet(MSG_SEND_GAMESTYLE, gameStyle);
-			sendToNet(MSG_SEND_SCHEME, scheme);
-			sendToNet(MSG_SEND_WEAPONSET, weaponset);
-			sendToNet(MSG_SEND_MAP, map);
+		if(getChiefStatus()) {
+			sendToNet(MSG_SEND_GAMESTYLE, getGameStyle());
+			sendToNet(MSG_SEND_SCHEME, getScheme());
+			sendToNet(MSG_SEND_WEAPONSET, getWeaponset());
+			sendToNet(MSG_SEND_MAP, getMapRecipe());
 		}
 	}
 	
-	public void registerObserver(Observer observer) {
-		observers.add(observer);
-	}
-
-	public void unregisterObserver(Observer observer) {
-		observers.remove(observer);
-	}
-	
 	private boolean sendToNet(ToNetMsgType what, Object obj) {
 		return netplay.sendToNet(what, 0, obj);
 	}
@@ -204,4 +132,35 @@
 	private boolean sendToNet(ToNetMsgType what, int arg1, Object obj) {
 		return netplay.sendToNet(what, arg1, obj);
 	}
+
+	public void requestAddTeam(Team team, int colorIndex) {
+		TeamIngameAttributes tia = new TeamIngameAttributes(netplay.getPlayerName(), colorIndex, TeamIngameAttributes.DEFAULT_HOG_COUNT, false);
+		TeamInGame newTeamInGame = new TeamInGame(team, tia);
+		requestedTeams.put(team.name, newTeamInGame);
+		sendToNet(MSG_SEND_ADD_TEAM, newTeamInGame);
+	}
+
+	public void requestRemoveTeam(String teamname) {
+		sendToNet(MSG_SEND_REMOVE_TEAM, teamname);
+	}
+
+	public void changeTeamColorIndex(String teamname, int colorIndex) {
+		if(getChiefStatus()) {
+			TeamInGame team = getTeams().get(teamname);
+			if(team.ingameAttribs.colorIndex != colorIndex) {
+				sendToNet(MSG_SEND_TEAM_COLOR_INDEX, colorIndex, teamname);
+				putTeam(team.withAttribs(team.ingameAttribs.withColorIndex(colorIndex)));
+			}
+		}
+	}
+
+	public void changeTeamHogCount(String teamname, int hogcount) {
+		if(getChiefStatus()) {
+			TeamInGame team = getTeams().get(teamname);
+			if(team.ingameAttribs.hogCount != hogcount) {
+				sendToNet(MSG_SEND_TEAM_HOG_COUNT, hogcount, teamname);
+				putTeam(team.withAttribs(team.ingameAttribs.withHogCount(hogcount)));
+			}
+		}
+	}
 }