project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/Netplay.java
changeset 7568 75ba91f14ed5
parent 7508 763d3961400b
child 7582 714310efad8f
equal deleted inserted replaced
7566:57d343ee382f 7568:75ba91f14ed5
    78 	public final ObservableTreeMap<String, PlayerInRoom> roomPlayerlist = new ObservableTreeMap<String, PlayerInRoom>();
    78 	public final ObservableTreeMap<String, PlayerInRoom> roomPlayerlist = new ObservableTreeMap<String, PlayerInRoom>();
    79 	public final Roomlist roomList = new Roomlist();
    79 	public final Roomlist roomList = new Roomlist();
    80 	public final MessageLog lobbyChatlog;
    80 	public final MessageLog lobbyChatlog;
    81 	public final MessageLog roomChatlog;
    81 	public final MessageLog roomChatlog;
    82 	public final ObservableTreeMap<String, TeamInGame> roomTeamlist = new ObservableTreeMap<String, TeamInGame>();
    82 	public final ObservableTreeMap<String, TeamInGame> roomTeamlist = new ObservableTreeMap<String, TeamInGame>();
    83 	private final Map<String, Team> roomRequestedTeams = new TreeMap<String, Team>();
    83 	private final Map<String, TeamInGame> roomRequestedTeams = new TreeMap<String, TeamInGame>();
    84 	
    84 	
    85 	private final List<GameMessageListener> gameMessageListeners = new LinkedList<GameMessageListener>();
    85 	private final List<GameMessageListener> gameMessageListeners = new LinkedList<GameMessageListener>();
    86 	private final List<RunGameListener> runGameListeners = new LinkedList<RunGameListener>();
    86 	private final List<RunGameListener> runGameListeners = new LinkedList<RunGameListener>();
    87 	
    87 	
    88 	public Netplay(Context appContext) {
    88 	public Netplay(Context appContext) {
   174 	public void sendFollowPlayer(String nick) { sendToNet(MSG_SEND_FOLLOW_PLAYER, nick); }
   174 	public void sendFollowPlayer(String nick) { sendToNet(MSG_SEND_FOLLOW_PLAYER, nick); }
   175 	public void sendJoinRoom(String name) { sendToNet(MSG_SEND_JOIN_ROOM, name); }
   175 	public void sendJoinRoom(String name) { sendToNet(MSG_SEND_JOIN_ROOM, name); }
   176 	public void sendCreateRoom(String name) { sendToNet(MSG_SEND_CREATE_ROOM, name); }
   176 	public void sendCreateRoom(String name) { sendToNet(MSG_SEND_CREATE_ROOM, name); }
   177 	public void sendLeaveRoom(String message) { sendToNet(MSG_SEND_LEAVE_ROOM, message); }
   177 	public void sendLeaveRoom(String message) { sendToNet(MSG_SEND_LEAVE_ROOM, message); }
   178 	public void sendKick(String player) { sendToNet(MSG_SEND_KICK, player); }
   178 	public void sendKick(String player) { sendToNet(MSG_SEND_KICK, player); }
   179 	public void sendAddTeam(Team newTeam) {
   179 	public void sendAddTeam(Team newTeam, int colorIndex) {
   180 		roomRequestedTeams.put(newTeam.name, newTeam);
   180 		TeamIngameAttributes tia = new TeamIngameAttributes(playerName, colorIndex, TeamIngameAttributes.DEFAULT_HOG_COUNT, false);
   181 		sendToNet(MSG_SEND_ADD_TEAM, newTeam);
   181 		TeamInGame newTeamInGame = new TeamInGame(newTeam, tia);
       
   182 		roomRequestedTeams.put(newTeam.name, newTeamInGame);
       
   183 		sendToNet(MSG_SEND_ADD_TEAM, newTeamInGame);
   182 	}
   184 	}
   183 	public void sendRemoveTeam(String teamName) { sendToNet(MSG_SEND_REMOVE_TEAM, teamName); }
   185 	public void sendRemoveTeam(String teamName) { sendToNet(MSG_SEND_REMOVE_TEAM, teamName); }
   184 	public void sendTeamColorIndex(String teamName, int colorIndex) { sendToNet(MSG_SEND_TEAM_COLOR_INDEX, colorIndex, teamName); }
   186 	public void changeTeamColorIndex(String teamName, int colorIndex) {
   185 	public void sendTeamHogCount(String teamName, int hogCount) { sendToNet(MSG_SEND_TEAM_HOG_COUNT, hogCount, teamName); }
   187 		if(isChief()) {
       
   188 			sendToNet(MSG_SEND_TEAM_COLOR_INDEX, colorIndex, teamName);
       
   189 			TeamInGame team = roomTeamlist.get(teamName);
       
   190 			roomTeamlist.put(teamName, team.withAttribs(team.ingameAttribs.withColorIndex(colorIndex)));
       
   191 		}
       
   192 	}
       
   193 	public void changeTeamHogCount(String teamName, int hogCount) {
       
   194 		if(isChief()) {
       
   195 			sendToNet(MSG_SEND_TEAM_HOG_COUNT, hogCount, teamName);
       
   196 			TeamInGame team = roomTeamlist.get(teamName);
       
   197 			roomTeamlist.put(teamName, team.withAttribs(team.ingameAttribs.withHogCount(hogCount)));
       
   198 		}
       
   199 	}
   186 	public void sendEngineMessage(byte[] engineMessage) { sendToNet(MSG_SEND_ENGINE_MESSAGE, engineMessage); }
   200 	public void sendEngineMessage(byte[] engineMessage) { sendToNet(MSG_SEND_ENGINE_MESSAGE, engineMessage); }
   187 	public void sendRoundFinished(boolean withoutError) { sendToNet(MSG_SEND_ROUND_FINISHED, Boolean.valueOf(withoutError)); }
   201 	public void sendRoundFinished(boolean withoutError) { sendToNet(MSG_SEND_ROUND_FINISHED, Boolean.valueOf(withoutError)); }
   188 	public void sendToggleReady() { sendToNet(MSG_SEND_TOGGLE_READY); }
   202 	public void sendToggleReady() { sendToNet(MSG_SEND_TOGGLE_READY); }
   189 	
   203 	
   190 	public void disconnect() { sendToNet(MSG_DISCONNECT, "User Quit"); }
   204 	public void disconnect() { sendToNet(MSG_DISCONNECT, "User Quit"); }
   417 					roomPlayerlist.put(name, new PlayerInRoom(oldEntry.player, newReadyState));
   431 					roomPlayerlist.put(name, new PlayerInRoom(oldEntry.player, newReadyState));
   418 				}
   432 				}
   419 				break;
   433 				break;
   420 			}
   434 			}
   421 			case MSG_TEAM_ADDED: {
   435 			case MSG_TEAM_ADDED: {
   422 				Team newTeam = (Team)msg.obj;
   436 				TeamInGame newTeam = (TeamInGame)msg.obj;
   423 				int colorIndex = TeamInGame.getUnusedOrRandomColorIndex(roomTeamlist.getMap().values());
       
   424 				TeamIngameAttributes attrs = new TeamIngameAttributes(playerName, colorIndex, TeamIngameAttributes.DEFAULT_HOG_COUNT, true);
       
   425 				TeamInGame tig = new TeamInGame(newTeam, attrs);
       
   426 				roomTeamlist.put(newTeam.name, tig);
       
   427 				if(isChief()) {
   437 				if(isChief()) {
   428 					sendTeamColorIndex(newTeam.name, attrs.colorIndex);
   438 					int freeColor = TeamInGame.getUnusedOrRandomColorIndex(roomTeamlist.getMap().values());
   429 					sendTeamHogCount(newTeam.name, attrs.hogCount);
   439 					sendToNet(MSG_SEND_TEAM_HOG_COUNT, newTeam.ingameAttribs.hogCount, newTeam.team.name);
   430 				}
   440 					sendToNet(MSG_SEND_TEAM_COLOR_INDEX, freeColor, newTeam.team.name);
       
   441 					newTeam = newTeam.withAttribs(newTeam.ingameAttribs.withColorIndex(freeColor));
       
   442 				}
       
   443 				roomTeamlist.put(newTeam.team.name, newTeam);
   431 				break;
   444 				break;
   432 			}
   445 			}
   433 			case MSG_TEAM_DELETED: {
   446 			case MSG_TEAM_DELETED: {
   434 				roomTeamlist.remove((String)msg.obj);
   447 				roomTeamlist.remove((String)msg.obj);
   435 				break;
   448 				break;
   436 			}
   449 			}
   437 			case MSG_TEAM_ACCEPTED: {
   450 			case MSG_TEAM_ACCEPTED: {
   438 				Team requestedTeam = roomRequestedTeams.remove(msg.obj);
   451 				TeamInGame requestedTeam = roomRequestedTeams.remove(msg.obj);
   439 				if(requestedTeam!=null) {
   452 				if(requestedTeam!=null) {
   440 					int colorIndex = TeamInGame.getUnusedOrRandomColorIndex(roomTeamlist.getMap().values());
   453 					roomTeamlist.put(requestedTeam.team.name, requestedTeam);
   441 					TeamIngameAttributes attrs = new TeamIngameAttributes(playerName, colorIndex, TeamIngameAttributes.DEFAULT_HOG_COUNT, false);
       
   442 					TeamInGame tig = new TeamInGame(requestedTeam, attrs);
       
   443 					roomTeamlist.put(requestedTeam.name, tig);
       
   444 					if(isChief()) {
   454 					if(isChief()) {
   445 						sendTeamColorIndex(requestedTeam.name, attrs.colorIndex);
   455 						// Not strictly necessary, but QtFrontend does it...
   446 						sendTeamHogCount(requestedTeam.name, attrs.hogCount);
   456 						sendToNet(MSG_SEND_TEAM_HOG_COUNT, requestedTeam.ingameAttribs.hogCount, requestedTeam.team.name);
   447 					}
   457 					}
   448 				} else {
   458 				} else {
   449 					Log.e("Netplay", "Got accepted message for team that was never requested.");
   459 					Log.e("Netplay", "Got accepted message for team that was never requested.");
   450 				}
   460 				}
   451 				break;
   461 				break;
   452 			}
   462 			}
   453 			case MSG_TEAM_COLOR_CHANGED: {
   463 			case MSG_TEAM_COLOR_CHANGED: {
   454 				TeamInGame oldEntry = roomTeamlist.get((String)msg.obj);
   464 				TeamInGame oldEntry = roomTeamlist.get((String)msg.obj);
   455 				if(oldEntry != null) {
   465 				if(oldEntry != null) {
   456 					TeamIngameAttributes newAttribs = oldEntry.ingameAttribs.withColorIndex(msg.arg1);
   466 					/*
   457 					roomTeamlist.put(oldEntry.team.name, oldEntry.withAttribs(newAttribs));
   467 					 * If we are chief, we ignore colors from the outside. They only come from the server
       
   468 					 * when someone adds a team then, and we override that choice anyway.
       
   469 					 * Worse, that color message arrives *after* we have overridden the color, so it would
       
   470 					 * re-override it right back.
       
   471 					 */
       
   472 					if(!isChief()) {
       
   473 						TeamIngameAttributes newAttribs = oldEntry.ingameAttribs.withColorIndex(msg.arg1);
       
   474 						roomTeamlist.put(oldEntry.team.name, oldEntry.withAttribs(newAttribs));
       
   475 					}
   458 				} else {
   476 				} else {
   459 					Log.e("Netplay", "Color update for unknown team "+msg.obj);
   477 					Log.e("Netplay", "Color update for unknown team "+msg.obj);
   460 				}
   478 				}
   461 				break;
   479 				break;
   462 			}
   480 			}