project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/NetRoomState.java
changeset 7582 714310efad8f
parent 7508 763d3961400b
child 7584 7831c84cc644
equal deleted inserted replaced
7580:c92596feac0d 7582:714310efad8f
     1 package org.hedgewars.hedgeroid.netplay;
     1 package org.hedgewars.hedgeroid.netplay;
     2 
     2 
     3 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_GAMESTYLE;
     3 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.*;
     4 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP;
     4 import static org.hedgewars.hedgeroid.util.ObjectUtils.equal;
     5 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_DRAWDATA;
       
     6 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_GENERATOR;
       
     7 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_NAME;
       
     8 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_SEED;
       
     9 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_TEMPLATE;
       
    10 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAP_THEME;
       
    11 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_MAZE_SIZE;
       
    12 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_SCHEME;
       
    13 import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.MSG_SEND_WEAPONSET;
       
    14 
     5 
    15 import java.util.Arrays;
     6 import java.util.Arrays;
    16 import java.util.LinkedList;
     7 import java.util.Collections;
    17 import java.util.List;
     8 import java.util.Map;
       
     9 import java.util.TreeMap;
    18 
    10 
    19 import org.hedgewars.hedgeroid.RoomStateManager;
    11 import org.hedgewars.hedgeroid.BasicRoomState;
    20 import org.hedgewars.hedgeroid.Datastructures.GameConfig;
    12 import org.hedgewars.hedgeroid.Datastructures.GameConfig;
    21 import org.hedgewars.hedgeroid.Datastructures.MapRecipe;
    13 import org.hedgewars.hedgeroid.Datastructures.MapRecipe;
    22 import org.hedgewars.hedgeroid.Datastructures.Scheme;
    14 import org.hedgewars.hedgeroid.Datastructures.Scheme;
       
    15 import org.hedgewars.hedgeroid.Datastructures.Team;
       
    16 import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
       
    17 import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes;
    23 import org.hedgewars.hedgeroid.Datastructures.Weaponset;
    18 import org.hedgewars.hedgeroid.Datastructures.Weaponset;
    24 import org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType;
    19 import org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType;
    25 
    20 
    26 /**
    21 /**
    27  * This class manages the room state in a network game.
    22  * This class manages the room state in a network game.
    28  */
    23  */
    29 class NetRoomState implements RoomStateManager {
    24 class NetRoomState extends BasicRoomState {
    30 	private List<RoomStateManager.Observer> observers = new LinkedList<RoomStateManager.Observer>();
    25 	final Map<String, TeamInGame> requestedTeams = new TreeMap<String, TeamInGame>();
    31 	private Netplay netplay;
    26 	private Netplay netplay;
    32 	
       
    33 	boolean chief;
       
    34 	String gameStyle;
       
    35 	Scheme scheme;
       
    36 	MapRecipe map;
       
    37 	Weaponset weaponset;
       
    38 	
    27 	
    39 	public NetRoomState(Netplay netplay) {
    28 	public NetRoomState(Netplay netplay) {
    40 		this.netplay = netplay;
    29 		this.netplay = netplay;
    41 		this.map = MapRecipe.makeRandomMap(0, "seed", GameConfig.DEFAULT_THEME);
    30 		initRoomState(false);
    42 	}
       
    43 
       
    44 	public MapRecipe getMapRecipe() {
       
    45 		return map;
       
    46 	}
       
    47 
       
    48 	public boolean getChiefStatus() {
       
    49 		return chief;
       
    50 	}
       
    51 
       
    52 	public Scheme getScheme() {
       
    53 		return scheme;
       
    54 	}
       
    55 
       
    56 	public String getGameStyle() {
       
    57 		return gameStyle;
       
    58 	}
       
    59 
       
    60 	public Weaponset getWeaponset() {
       
    61 		return weaponset;
       
    62 	}
    31 	}
    63 
    32 
    64 	public void changeWeaponset(Weaponset weaponset) {
    33 	public void changeWeaponset(Weaponset weaponset) {
    65 		if(chief && !weaponset.equals(this.weaponset)) {
    34 		if(getChiefStatus() && !equal(weaponset, getWeaponset())) {
    66 			sendToNet(MSG_SEND_WEAPONSET, weaponset);
    35 			sendToNet(MSG_SEND_WEAPONSET, weaponset);
    67 			setWeaponset(weaponset);
    36 			setWeaponset(weaponset);
    68 		}
    37 		}
    69 	}
    38 	}
    70 	
    39 	
    71 	public void changeMapRecipe(MapRecipe mapRecipe) {
    40 	public void changeMapRecipe(MapRecipe mapRecipe) {
    72 		if(chief && !mapRecipe.equals(this.map)) {
    41 		if(getChiefStatus() && !equal(mapRecipe, getMapRecipe())) {
    73 			sendToNet(MSG_SEND_MAP, mapRecipe);
    42 			sendToNet(MSG_SEND_MAP, mapRecipe);
    74 			setMapRecipe(mapRecipe);
    43 			setMapRecipe(mapRecipe);
    75 		}
    44 		}
    76 	}
    45 	}
    77 	
    46 	
    78 	public void changeMapNameAndGenerator(String mapName) {
    47 	public void changeMapNameAndGenerator(String mapName) {
    79 		if(chief && !mapName.equals(this.map.name)) {
    48 		if(getChiefStatus() && !equal(mapName, getMapRecipe().name)) {
    80 			int newGenerator = MapRecipe.generatorForMapname(mapName);
    49 			int newGenerator = MapRecipe.generatorForMapname(mapName);
    81 			if(newGenerator != this.map.mapgen) {
    50 			if(newGenerator != getMapRecipe().mapgen) {
    82 				sendToNet(MSG_SEND_MAP_GENERATOR, newGenerator, null);
    51 				sendToNet(MSG_SEND_MAP_GENERATOR, newGenerator, null);
    83 			}
    52 			}
    84 			sendToNet(MSG_SEND_MAP_NAME, mapName);
    53 			sendToNet(MSG_SEND_MAP_NAME, mapName);
    85 			setMapRecipe(map.withName(mapName).withMapgen(newGenerator));
    54 			setMapRecipe(getMapRecipe().withName(mapName).withMapgen(newGenerator));
    86 		}
    55 		}
    87 	}
    56 	}
    88 	
    57 	
    89 	public void changeMapTemplate(int template) {
    58 	public void changeMapTemplate(int template) {
    90 		if(chief && template != this.map.templateFilter) {
    59 		if(getChiefStatus() && template != getMapRecipe().templateFilter) {
    91 			sendToNet(MSG_SEND_MAP_TEMPLATE, template, null);
    60 			sendToNet(MSG_SEND_MAP_TEMPLATE, template, null);
    92 			setMapRecipe(map.withTemplateFilter(template));
    61 			setMapRecipe(getMapRecipe().withTemplateFilter(template));
    93 		}
    62 		}
    94 	}
    63 	}
    95 	
    64 	
    96 	public void changeMazeSize(int mazeSize) {
    65 	public void changeMazeSize(int mazeSize) {
    97 		if(chief && mazeSize != this.map.mazeSize) {
    66 		if(getChiefStatus() && mazeSize != getMapRecipe().mazeSize) {
    98 			sendToNet(MSG_SEND_MAZE_SIZE, mazeSize, 0);
    67 			sendToNet(MSG_SEND_MAZE_SIZE, mazeSize, 0);
    99 			setMapRecipe(map.withMazeSize(mazeSize));
    68 			setMapRecipe(getMapRecipe().withMazeSize(mazeSize));
   100 		}
    69 		}
   101 	}
    70 	}
   102 	
    71 	
   103 	public void changeMapSeed(String seed) {
    72 	public void changeMapSeed(String seed) {
   104 		if(chief && !seed.equals(this.map.seed)) {
    73 		if(getChiefStatus() && !equal(seed, getMapRecipe().seed)) {
   105 			sendToNet(MSG_SEND_MAP_SEED, seed);
    74 			sendToNet(MSG_SEND_MAP_SEED, seed);
   106 			setMapRecipe(map.withSeed(seed));
    75 			setMapRecipe(getMapRecipe().withSeed(seed));
   107 		}
    76 		}
   108 	}
    77 	}
   109 	
    78 	
   110 	public void changeMapTheme(String theme) {
    79 	public void changeMapTheme(String theme) {
   111 		if(chief && !theme.equals(this.map.theme)) {
    80 		if(getChiefStatus() && !equal(theme, getMapRecipe().theme)) {
   112 			sendToNet(MSG_SEND_MAP_THEME, theme);
    81 			sendToNet(MSG_SEND_MAP_THEME, theme);
   113 			setMapRecipe(map.withTheme(theme));
    82 			setMapRecipe(getMapRecipe().withTheme(theme));
   114 		}
    83 		}
   115 	}
    84 	}
   116 	
    85 	
   117 	public void changeMapDrawdata(byte[] drawdata) {
    86 	public void changeMapDrawdata(byte[] drawdata) {
   118 		if(chief && !Arrays.equals(drawdata, this.map.getDrawData())) {
    87 		if(getChiefStatus() && !Arrays.equals(drawdata, getMapRecipe().getDrawData())) {
   119 			sendToNet(MSG_SEND_MAP_DRAWDATA, drawdata);
    88 			sendToNet(MSG_SEND_MAP_DRAWDATA, drawdata);
   120 			setMapRecipe(map.withDrawData(drawdata));
    89 			setMapRecipe(getMapRecipe().withDrawData(drawdata));
   121 		}
    90 		}
   122 	}
    91 	}
   123 	
    92 	
   124 	public void changeGameStyle(String gameStyle) {
    93 	public void changeGameStyle(String gameStyle) {
   125 		if(chief && !gameStyle.equals(this.gameStyle)) {
    94 		if(getChiefStatus() && !equal(gameStyle, getGameStyle())) {
   126 			sendToNet(MSG_SEND_GAMESTYLE, gameStyle);
    95 			sendToNet(MSG_SEND_GAMESTYLE, gameStyle);
   127 			setGameStyle(gameStyle);
    96 			setGameStyle(gameStyle);
   128 		}
    97 		}
   129 	}
    98 	}
   130 	
    99 	
   131 	public void changeScheme(Scheme scheme) {
   100 	public void changeScheme(Scheme scheme) {
   132 		if(chief && !scheme.equals(this.scheme)) {
   101 		if(getChiefStatus() && !equal(scheme, getScheme())) {
   133 			sendToNet(MSG_SEND_SCHEME, scheme);
   102 			sendToNet(MSG_SEND_SCHEME, scheme);
   134 			setScheme(scheme);
   103 			setScheme(scheme);
   135 		}
   104 		}
   136 	}
   105 	}
   137 	
   106 	
   138 	void setWeaponset(Weaponset weaponset) {
   107 	void initRoomState(boolean chief) {
   139 		if(!weaponset.equals(this.weaponset)) {
   108 		setTeams(Collections.<String, TeamInGame>emptyMap());
   140 			this.weaponset = weaponset;
   109 		requestedTeams.clear();
   141 			for(RoomStateManager.Observer observer : observers) {
   110 		
   142 				observer.onWeaponsetChanged(weaponset);
   111 		setChief(chief);
   143 			}
   112 		setGameStyle(GameConfig.DEFAULT_STYLE);
   144 		}
   113 		setMapRecipe(MapRecipe.makeRandomMap(0, "seed", GameConfig.DEFAULT_THEME));
   145 	}
   114 		setScheme(netplay.defaultScheme);
   146 	
   115 		setWeaponset(netplay.defaultWeaponset);
   147 	void setMapRecipe(MapRecipe map) {
   116 		sendFullConfig();
   148 		if(!map.equals(this.map)) { 
       
   149 			this.map = map;
       
   150 			for(RoomStateManager.Observer observer : observers) {
       
   151 				observer.onMapChanged(map);
       
   152 			}
       
   153 		}
       
   154 	}
       
   155 	
       
   156 	void setGameStyle(String gameStyle) {
       
   157 		if(!gameStyle.equals(this.gameStyle)) {
       
   158 			this.gameStyle = gameStyle;
       
   159 			for(RoomStateManager.Observer observer : observers) {
       
   160 				observer.onGameStyleChanged(gameStyle);
       
   161 			}
       
   162 		}
       
   163 	}
       
   164 	
       
   165 	void setScheme(Scheme scheme) {
       
   166 		if(!scheme.equals(this.scheme)) {
       
   167 			this.scheme = scheme;
       
   168 			for(RoomStateManager.Observer observer : observers) {
       
   169 				observer.onSchemeChanged(scheme);
       
   170 			}
       
   171 		}
       
   172 	}
       
   173 	
       
   174 	void setChief(boolean chief) {
       
   175 		if(chief != this.chief) {
       
   176 			this.chief = chief;
       
   177 			for(RoomStateManager.Observer observer : observers) {
       
   178 				observer.onChiefStatusChanged(chief);
       
   179 			}
       
   180 		}
       
   181 	}
   117 	}
   182 	
   118 	
   183 	void sendFullConfig() {
   119 	void sendFullConfig() {
   184 		if(chief) {
   120 		if(getChiefStatus()) {
   185 			sendToNet(MSG_SEND_GAMESTYLE, gameStyle);
   121 			sendToNet(MSG_SEND_GAMESTYLE, getGameStyle());
   186 			sendToNet(MSG_SEND_SCHEME, scheme);
   122 			sendToNet(MSG_SEND_SCHEME, getScheme());
   187 			sendToNet(MSG_SEND_WEAPONSET, weaponset);
   123 			sendToNet(MSG_SEND_WEAPONSET, getWeaponset());
   188 			sendToNet(MSG_SEND_MAP, map);
   124 			sendToNet(MSG_SEND_MAP, getMapRecipe());
   189 		}
   125 		}
   190 	}
       
   191 	
       
   192 	public void registerObserver(Observer observer) {
       
   193 		observers.add(observer);
       
   194 	}
       
   195 
       
   196 	public void unregisterObserver(Observer observer) {
       
   197 		observers.remove(observer);
       
   198 	}
   126 	}
   199 	
   127 	
   200 	private boolean sendToNet(ToNetMsgType what, Object obj) {
   128 	private boolean sendToNet(ToNetMsgType what, Object obj) {
   201 		return netplay.sendToNet(what, 0, obj);
   129 		return netplay.sendToNet(what, 0, obj);
   202 	}
   130 	}
   203 	
   131 	
   204 	private boolean sendToNet(ToNetMsgType what, int arg1, Object obj) {
   132 	private boolean sendToNet(ToNetMsgType what, int arg1, Object obj) {
   205 		return netplay.sendToNet(what, arg1, obj);
   133 		return netplay.sendToNet(what, arg1, obj);
   206 	}
   134 	}
       
   135 
       
   136 	public void requestAddTeam(Team team, int colorIndex) {
       
   137 		TeamIngameAttributes tia = new TeamIngameAttributes(netplay.getPlayerName(), colorIndex, TeamIngameAttributes.DEFAULT_HOG_COUNT, false);
       
   138 		TeamInGame newTeamInGame = new TeamInGame(team, tia);
       
   139 		requestedTeams.put(team.name, newTeamInGame);
       
   140 		sendToNet(MSG_SEND_ADD_TEAM, newTeamInGame);
       
   141 	}
       
   142 
       
   143 	public void requestRemoveTeam(String teamname) {
       
   144 		sendToNet(MSG_SEND_REMOVE_TEAM, teamname);
       
   145 	}
       
   146 
       
   147 	public void changeTeamColorIndex(String teamname, int colorIndex) {
       
   148 		if(getChiefStatus()) {
       
   149 			TeamInGame team = getTeams().get(teamname);
       
   150 			if(team.ingameAttribs.colorIndex != colorIndex) {
       
   151 				sendToNet(MSG_SEND_TEAM_COLOR_INDEX, colorIndex, teamname);
       
   152 				putTeam(team.withAttribs(team.ingameAttribs.withColorIndex(colorIndex)));
       
   153 			}
       
   154 		}
       
   155 	}
       
   156 
       
   157 	public void changeTeamHogCount(String teamname, int hogcount) {
       
   158 		if(getChiefStatus()) {
       
   159 			TeamInGame team = getTeams().get(teamname);
       
   160 			if(team.ingameAttribs.hogCount != hogcount) {
       
   161 				sendToNet(MSG_SEND_TEAM_HOG_COUNT, hogcount, teamname);
       
   162 				putTeam(team.withAttribs(team.ingameAttribs.withHogCount(hogcount)));
       
   163 			}
       
   164 		}
       
   165 	}
   207 }
   166 }