10017
|
1 |
/*
|
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
|
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
|
|
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
package org.hedgewars.hedgeroid.netplay;
|
|
21 |
|
|
22 |
import static org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType.*;
|
|
23 |
|
|
24 |
import java.io.IOException;
|
|
25 |
import java.util.Arrays;
|
|
26 |
import java.util.Collections;
|
|
27 |
import java.util.LinkedList;
|
|
28 |
import java.util.List;
|
|
29 |
|
|
30 |
import org.hedgewars.hedgeroid.RoomStateManager;
|
|
31 |
import org.hedgewars.hedgeroid.Datastructures.GameConfig;
|
|
32 |
import org.hedgewars.hedgeroid.Datastructures.MapRecipe;
|
|
33 |
import org.hedgewars.hedgeroid.Datastructures.Player;
|
|
34 |
import org.hedgewars.hedgeroid.Datastructures.PlayerInRoom;
|
|
35 |
import org.hedgewars.hedgeroid.Datastructures.Room;
|
|
36 |
import org.hedgewars.hedgeroid.Datastructures.Scheme;
|
|
37 |
import org.hedgewars.hedgeroid.Datastructures.Schemes;
|
|
38 |
import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
|
|
39 |
import org.hedgewars.hedgeroid.Datastructures.TeamIngameAttributes;
|
|
40 |
import org.hedgewars.hedgeroid.Datastructures.Weaponset;
|
|
41 |
import org.hedgewars.hedgeroid.Datastructures.Weaponsets;
|
|
42 |
import org.hedgewars.hedgeroid.netplay.ThreadedNetConnection.ToNetMsgType;
|
|
43 |
import org.hedgewars.hedgeroid.util.ObservableTreeMap;
|
|
44 |
|
|
45 |
import android.annotation.SuppressLint;
|
|
46 |
import android.content.Context;
|
|
47 |
import android.content.Intent;
|
|
48 |
import android.os.Handler;
|
|
49 |
import android.os.Looper;
|
|
50 |
import android.os.Message;
|
|
51 |
import android.support.v4.content.LocalBroadcastManager;
|
|
52 |
import android.util.Log;
|
|
53 |
import android.util.Pair;
|
|
54 |
|
|
55 |
|
|
56 |
/**
|
|
57 |
* This class manages the application's networking state.
|
|
58 |
*/
|
|
59 |
public class Netplay {
|
|
60 |
public static enum State { NOT_CONNECTED, CONNECTING, LOBBY, ROOM }
|
|
61 |
|
|
62 |
// Extras in broadcasts
|
|
63 |
public static final String EXTRA_PLAYERNAME = "playerName";
|
|
64 |
public static final String EXTRA_MESSAGE = "message";
|
|
65 |
public static final String EXTRA_HAS_ERROR = "hasError";
|
|
66 |
public static final String EXTRA_REASON = "reason";
|
|
67 |
|
|
68 |
private static final String ACTIONPREFIX = "org.hedgewars.hedgeroid.netconn.";
|
|
69 |
public static final String ACTION_DISCONNECTED = ACTIONPREFIX+"DISCONNECTED";
|
|
70 |
public static final String ACTION_CONNECTED = ACTIONPREFIX+"CONNECTED";
|
|
71 |
public static final String ACTION_PASSWORD_REQUESTED = ACTIONPREFIX+"PASSWORD_REQUESTED";
|
|
72 |
public static final String ACTION_ENTERED_ROOM_FROM_LOBBY = ACTIONPREFIX+"ENTERED_ROOM";
|
|
73 |
public static final String ACTION_LEFT_ROOM = ACTIONPREFIX+"LEFT_ROOM";
|
|
74 |
public static final String ACTION_STATE_CHANGED = ACTIONPREFIX+"STATE_CHANGED";
|
|
75 |
|
|
76 |
public static final String DEFAULT_SERVER = "netserver.hedgewars.org";
|
|
77 |
public static final int DEFAULT_PORT = 46631;
|
|
78 |
|
|
79 |
private final Context appContext;
|
|
80 |
private final LocalBroadcastManager broadcastManager;
|
|
81 |
private final FromNetHandler fromNetHandler = new FromNetHandler();
|
|
82 |
public final Scheme defaultScheme;
|
|
83 |
public final Weaponset defaultWeaponset;
|
|
84 |
|
|
85 |
private State state = State.NOT_CONNECTED;
|
|
86 |
private String playerName;
|
|
87 |
|
|
88 |
// null or stale if not in room state
|
|
89 |
private final NetRoomState netRoomState = new NetRoomState(this);
|
|
90 |
|
|
91 |
// null if there is no running connection (==state is NOT_CONNECTED)
|
|
92 |
private ThreadedNetConnection connection;
|
|
93 |
|
|
94 |
public final ObservableTreeMap<String, Player> lobbyPlayerlist = new ObservableTreeMap<String, Player>();
|
|
95 |
public final ObservableTreeMap<String, PlayerInRoom> roomPlayerlist = new ObservableTreeMap<String, PlayerInRoom>();
|
|
96 |
public final Roomlist roomList = new Roomlist();
|
|
97 |
public final MessageLog lobbyChatlog;
|
|
98 |
public final MessageLog roomChatlog;
|
|
99 |
|
|
100 |
private final List<GameMessageListener> gameMessageListeners = new LinkedList<GameMessageListener>();
|
|
101 |
private final List<RunGameListener> runGameListeners = new LinkedList<RunGameListener>();
|
|
102 |
|
|
103 |
public Netplay(Context appContext, Scheme defaultScheme, Weaponset defaultWeaponset) {
|
|
104 |
this.appContext = appContext;
|
|
105 |
broadcastManager = LocalBroadcastManager.getInstance(appContext);
|
|
106 |
lobbyChatlog = new MessageLog(appContext);
|
|
107 |
roomChatlog = new MessageLog(appContext);
|
|
108 |
this.defaultScheme = defaultScheme;
|
|
109 |
this.defaultWeaponset = defaultWeaponset;
|
|
110 |
}
|
|
111 |
|
|
112 |
public RoomStateManager getRoomStateManager() {
|
|
113 |
return netRoomState;
|
|
114 |
}
|
|
115 |
|
|
116 |
private void clearLobbyState() {
|
|
117 |
lobbyPlayerlist.clear();
|
|
118 |
roomList.clear();
|
|
119 |
lobbyChatlog.clear();
|
|
120 |
}
|
|
121 |
|
|
122 |
private void initRoomState(boolean chief) {
|
|
123 |
roomChatlog.clear();
|
|
124 |
roomPlayerlist.clear();
|
|
125 |
netRoomState.initRoomState(chief);
|
|
126 |
}
|
|
127 |
|
|
128 |
public void registerGameMessageListener(GameMessageListener listener) {
|
|
129 |
gameMessageListeners.add(listener);
|
|
130 |
}
|
|
131 |
|
|
132 |
public void unregisterGameMessageListener(GameMessageListener listener) {
|
|
133 |
gameMessageListeners.remove(listener);
|
|
134 |
}
|
|
135 |
|
|
136 |
public void registerRunGameListener(RunGameListener listener) {
|
|
137 |
runGameListeners.add(listener);
|
|
138 |
}
|
|
139 |
|
|
140 |
public void unregisterRunGameListener(RunGameListener listener) {
|
|
141 |
runGameListeners.remove(listener);
|
|
142 |
}
|
|
143 |
|
|
144 |
public void connectToDefaultServer(String playerName) {
|
|
145 |
connect(playerName, DEFAULT_SERVER, DEFAULT_PORT);
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Establish a new connection. Only call if the current state is NOT_CONNECTED.
|
|
150 |
*
|
|
151 |
* The state will switch to CONNECTING immediately. After that, it can asynchronously change to any other state.
|
|
152 |
* State changes are indicated by broadcasts. In particular, if an error occurs while trying to connect, the state
|
|
153 |
* will change back to NOT_CONNECTED and an ACTION_DISCONNECTED broadcast is sent.
|
|
154 |
*/
|
|
155 |
public void connect(String name, String host, int port) {
|
|
156 |
playerName = name;
|
|
157 |
if(state != State.NOT_CONNECTED) {
|
|
158 |
throw new IllegalStateException("Attempt to start a new connection while the old one was still running.");
|
|
159 |
}
|
|
160 |
|
|
161 |
clearLobbyState();
|
|
162 |
changeState(State.CONNECTING);
|
|
163 |
connection = ThreadedNetConnection.startConnection(appContext, fromNetHandler, name, host, port);
|
|
164 |
connection.setFastTickRate(true);
|
|
165 |
}
|
|
166 |
|
|
167 |
public void sendNick(String nick) {
|
|
168 |
playerName = nick;
|
|
169 |
sendToNet(MSG_SEND_NICK, nick);
|
|
170 |
}
|
|
171 |
public void sendPassword(String password) { sendToNet(MSG_SEND_PASSWORD, password); }
|
|
172 |
public void sendQuit(String message) { sendToNet(MSG_SEND_QUIT, message); }
|
|
173 |
public void sendRoomlistRequest() { sendToNet(MSG_SEND_ROOMLIST_REQUEST); }
|
|
174 |
public void sendPlayerInfoQuery(String name) { sendToNet(MSG_SEND_PLAYER_INFO_REQUEST, name); }
|
|
175 |
public void sendChat(String s) { sendToNet(MSG_SEND_CHAT, s); }
|
|
176 |
public void sendTeamChat(String s) { sendToNet(MSG_SEND_TEAMCHAT, s); }
|
|
177 |
public void sendFollowPlayer(String nick) { sendToNet(MSG_SEND_FOLLOW_PLAYER, nick); }
|
|
178 |
public void sendJoinRoom(String name) { sendToNet(MSG_SEND_JOIN_ROOM, name); }
|
|
179 |
public void sendCreateRoom(String name) { sendToNet(MSG_SEND_CREATE_ROOM, name); }
|
|
180 |
public void sendLeaveRoom(String message) { sendToNet(MSG_SEND_LEAVE_ROOM, message); }
|
|
181 |
public void sendKick(String player) { sendToNet(MSG_SEND_KICK, player); }
|
|
182 |
public void sendEngineMessage(byte[] engineMessage) { sendToNet(MSG_SEND_ENGINE_MESSAGE, engineMessage); }
|
|
183 |
public void sendRoundFinished(boolean withoutError) { sendToNet(MSG_SEND_ROUND_FINISHED, Boolean.valueOf(withoutError)); }
|
|
184 |
public void sendToggleReady() { sendToNet(MSG_SEND_TOGGLE_READY); }
|
|
185 |
public void sendStartGame() { sendToNet(MSG_SEND_START_GAME); }
|
|
186 |
|
|
187 |
public void disconnect() { sendToNet(MSG_DISCONNECT, "User Quit"); }
|
|
188 |
|
|
189 |
private static Netplay instance;
|
|
190 |
|
|
191 |
/**
|
|
192 |
* Retrieve the single app-wide instance of the netplay interface, creating it if it
|
|
193 |
* does not exist yet.
|
|
194 |
*
|
|
195 |
* @param applicationContext
|
|
196 |
* @return
|
|
197 |
*/
|
|
198 |
public static Netplay getAppInstance(Context applicationContext) {
|
|
199 |
if(instance == null) {
|
|
200 |
// We will need some default values for rooms, best load them here
|
|
201 |
Scheme defaultScheme = null;
|
|
202 |
Weaponset defaultWeaponset = null;
|
|
203 |
try {
|
|
204 |
List<Scheme> schemes = Schemes.loadBuiltinSchemes(applicationContext);
|
|
205 |
for(Scheme scheme : schemes) {
|
|
206 |
if(scheme.name.equals(GameConfig.DEFAULT_SCHEME)) {
|
|
207 |
defaultScheme = scheme;
|
|
208 |
}
|
|
209 |
}
|
|
210 |
List<Weaponset> weaponsets = Weaponsets.loadBuiltinWeaponsets(applicationContext);
|
|
211 |
for(Weaponset weaponset : weaponsets) {
|
|
212 |
if(weaponset.name.equals(GameConfig.DEFAULT_WEAPONSET)) {
|
|
213 |
defaultWeaponset = weaponset;
|
|
214 |
}
|
|
215 |
}
|
|
216 |
} catch(IOException e) {
|
|
217 |
throw new RuntimeException(e);
|
|
218 |
}
|
|
219 |
|
|
220 |
if(defaultScheme==null || defaultWeaponset==null) {
|
|
221 |
throw new RuntimeException("Unable to load default scheme or weaponset");
|
|
222 |
}
|
|
223 |
|
|
224 |
instance = new Netplay(applicationContext, defaultScheme, defaultWeaponset);
|
|
225 |
}
|
|
226 |
return instance;
|
|
227 |
}
|
|
228 |
|
|
229 |
public State getState() {
|
|
230 |
return state;
|
|
231 |
}
|
|
232 |
|
|
233 |
private void changeState(State newState) {
|
|
234 |
if(newState != state) {
|
|
235 |
state = newState;
|
|
236 |
broadcastManager.sendBroadcastSync(new Intent(ACTION_STATE_CHANGED));
|
|
237 |
}
|
|
238 |
}
|
|
239 |
|
|
240 |
public boolean isChief() {
|
|
241 |
if(netRoomState != null) {
|
|
242 |
return netRoomState.getChiefStatus();
|
|
243 |
} else {
|
|
244 |
return false;
|
|
245 |
}
|
|
246 |
}
|
|
247 |
|
|
248 |
public String getPlayerName() {
|
|
249 |
return playerName;
|
|
250 |
}
|
|
251 |
|
|
252 |
boolean sendToNet(ToNetMsgType what) {
|
|
253 |
return sendToNet(what, 0, null);
|
|
254 |
}
|
|
255 |
|
|
256 |
boolean sendToNet(ToNetMsgType what, Object obj) {
|
|
257 |
return sendToNet(what, 0, obj);
|
|
258 |
}
|
|
259 |
|
|
260 |
boolean sendToNet(ToNetMsgType what, int arg1, Object obj) {
|
|
261 |
if(connection != null) {
|
|
262 |
Handler handler = connection.toNetHandler;
|
|
263 |
return handler.sendMessage(handler.obtainMessage(what.ordinal(), arg1, 0, obj));
|
|
264 |
} else {
|
|
265 |
return false;
|
|
266 |
}
|
|
267 |
}
|
|
268 |
|
|
269 |
private MessageLog getCurrentLog() {
|
|
270 |
if(state == State.ROOM) {
|
|
271 |
return roomChatlog;
|
|
272 |
} else {
|
|
273 |
return lobbyChatlog;
|
|
274 |
}
|
|
275 |
}
|
|
276 |
|
|
277 |
public static enum FromNetMsgType {
|
|
278 |
MSG_LOBBY_JOIN,
|
|
279 |
MSG_LOBBY_LEAVE,
|
|
280 |
MSG_ROOM_JOIN,
|
|
281 |
MSG_ROOM_LEAVE,
|
|
282 |
MSG_CLIENT_FLAGS,
|
|
283 |
MSG_CHAT,
|
|
284 |
MSG_MESSAGE,
|
|
285 |
MSG_ROOM_ADD,
|
|
286 |
MSG_ROOM_UPDATE,
|
|
287 |
MSG_ROOM_DELETE,
|
|
288 |
MSG_ROOMLIST,
|
|
289 |
MSG_CONNECTED,
|
|
290 |
MSG_DISCONNECTED,
|
|
291 |
MSG_PASSWORD_REQUEST,
|
|
292 |
MSG_ENTER_ROOM_FROM_LOBBY,
|
|
293 |
MSG_LEAVE_ROOM,
|
|
294 |
MSG_TEAM_ADDED,
|
|
295 |
MSG_TEAM_DELETED,
|
|
296 |
MSG_TEAM_ACCEPTED,
|
|
297 |
MSG_TEAM_COLOR_CHANGED,
|
|
298 |
MSG_HOG_COUNT_CHANGED,
|
|
299 |
MSG_ENGINE_MESSAGE,
|
|
300 |
MSG_RUN_GAME,
|
|
301 |
MSG_SCHEME_CHANGED,
|
|
302 |
MSG_MAP_CHANGED,
|
|
303 |
MSG_SCRIPT_CHANGED,
|
|
304 |
MSG_WEAPONSET_CHANGED;
|
|
305 |
|
|
306 |
static final List<FromNetMsgType> values = Collections.unmodifiableList(Arrays.asList(FromNetMsgType.values()));
|
|
307 |
}
|
|
308 |
|
|
309 |
/**
|
|
310 |
* Processes messages from the networking system. Always runs on the main thread.
|
|
311 |
*/
|
|
312 |
@SuppressLint("HandlerLeak")
|
|
313 |
final class FromNetHandler extends Handler {
|
|
314 |
public FromNetHandler() {
|
|
315 |
super(Looper.getMainLooper());
|
|
316 |
}
|
|
317 |
|
|
318 |
@SuppressWarnings("unchecked")
|
|
319 |
@Override
|
|
320 |
public void handleMessage(Message msg) {
|
|
321 |
switch(FromNetMsgType.values.get(msg.what)) {
|
|
322 |
case MSG_LOBBY_JOIN: {
|
|
323 |
String name = (String)msg.obj;
|
|
324 |
lobbyPlayerlist.put(name, new Player(name, false, false));
|
|
325 |
lobbyChatlog.appendPlayerJoin(name);
|
|
326 |
break;
|
|
327 |
}
|
|
328 |
case MSG_LOBBY_LEAVE: {
|
|
329 |
Pair<String, String> args = (Pair<String, String>)msg.obj;
|
|
330 |
lobbyPlayerlist.remove(args.first);
|
|
331 |
lobbyChatlog.appendPlayerLeave(args.first, args.second);
|
|
332 |
break;
|
|
333 |
}
|
|
334 |
case MSG_ROOM_JOIN: {
|
|
335 |
String name = (String)msg.obj;
|
|
336 |
Player p = lobbyPlayerlist.get(name);
|
|
337 |
if(p==null) {
|
|
338 |
Log.w("Netplay", "Unknown player joined room: "+name);
|
|
339 |
p = new Player(name, false, false);
|
|
340 |
}
|
|
341 |
roomPlayerlist.put(name, new PlayerInRoom(p, false, false));
|
|
342 |
roomChatlog.appendPlayerJoin(name);
|
|
343 |
break;
|
|
344 |
}
|
|
345 |
case MSG_ROOM_LEAVE: {
|
|
346 |
Pair<String, String> args = (Pair<String, String>)msg.obj;
|
|
347 |
roomPlayerlist.remove(args.first);
|
|
348 |
roomChatlog.appendPlayerLeave(args.first, args.second);
|
|
349 |
break;
|
|
350 |
}
|
|
351 |
case MSG_CLIENT_FLAGS: {
|
|
352 |
ClientFlagsUpdate upd = (ClientFlagsUpdate)msg.obj;
|
|
353 |
PlayerInRoom pir = roomPlayerlist.get(upd.nick);
|
|
354 |
if(pir != null) {
|
|
355 |
roomPlayerlist.put(upd.nick, upd.applyTo(pir));
|
|
356 |
}
|
|
357 |
Player p = lobbyPlayerlist.get(upd.nick);
|
|
358 |
if(p != null) {
|
|
359 |
lobbyPlayerlist.put(upd.nick, upd.applyTo(p));
|
|
360 |
} else {
|
|
361 |
Log.w("Netplay", "Received client flags for unknown player "+upd.nick);
|
|
362 |
}
|
|
363 |
if(playerName.equals(upd.nick) && upd.appliesTo(ClientFlagsUpdate.FLAG_CHIEF)) {
|
|
364 |
netRoomState.setChief(upd.newFlagState);
|
|
365 |
}
|
|
366 |
break;
|
|
367 |
}
|
|
368 |
case MSG_CHAT: {
|
|
369 |
Pair<String, String> args = (Pair<String, String>)msg.obj;
|
|
370 |
getCurrentLog().appendChat(args.first, args.second);
|
|
371 |
for(GameMessageListener listener : gameMessageListeners) {
|
|
372 |
listener.onChatMessage(args.first, args.second);
|
|
373 |
}
|
|
374 |
break;
|
|
375 |
}
|
|
376 |
case MSG_MESSAGE: {
|
|
377 |
getCurrentLog().appendMessage(msg.arg1, (String)msg.obj);
|
|
378 |
for(GameMessageListener listener : gameMessageListeners) {
|
|
379 |
listener.onMessage(1, (String)msg.obj);
|
|
380 |
}
|
|
381 |
break;
|
|
382 |
}
|
|
383 |
case MSG_ROOM_ADD: {
|
|
384 |
Room room = (Room)msg.obj;
|
|
385 |
roomList.addRoomWithNewId(room);
|
|
386 |
break;
|
|
387 |
}
|
|
388 |
case MSG_ROOM_UPDATE: {
|
|
389 |
Pair<String, Room> args = (Pair<String, Room>)msg.obj;
|
|
390 |
roomList.updateRoom(args.first, args.second);
|
|
391 |
break;
|
|
392 |
}
|
|
393 |
case MSG_ROOM_DELETE: {
|
|
394 |
roomList.remove((String)msg.obj);
|
|
395 |
break;
|
|
396 |
}
|
|
397 |
case MSG_ROOMLIST: {
|
|
398 |
Room[] rooms = (Room[])msg.obj;
|
|
399 |
roomList.updateList(rooms);
|
|
400 |
break;
|
|
401 |
}
|
|
402 |
case MSG_CONNECTED: {
|
|
403 |
playerName = (String)msg.obj;
|
|
404 |
changeState(State.LOBBY);
|
|
405 |
broadcastManager.sendBroadcast(new Intent(ACTION_CONNECTED));
|
|
406 |
break;
|
|
407 |
}
|
|
408 |
case MSG_DISCONNECTED: {
|
|
409 |
Pair<Boolean, String> args = (Pair<Boolean, String>)msg.obj;
|
|
410 |
for(GameMessageListener listener : gameMessageListeners) {
|
|
411 |
listener.onNetDisconnected();
|
|
412 |
}
|
|
413 |
changeState(State.NOT_CONNECTED);
|
|
414 |
connection = null;
|
|
415 |
Intent intent = new Intent(ACTION_DISCONNECTED);
|
|
416 |
intent.putExtra(EXTRA_HAS_ERROR, args.first);
|
|
417 |
intent.putExtra(EXTRA_MESSAGE, args.second);
|
|
418 |
broadcastManager.sendBroadcastSync(intent);
|
|
419 |
break;
|
|
420 |
}
|
|
421 |
case MSG_PASSWORD_REQUEST: {
|
|
422 |
Intent intent = new Intent(ACTION_PASSWORD_REQUESTED);
|
|
423 |
intent.putExtra(EXTRA_PLAYERNAME, (String)msg.obj);
|
|
424 |
broadcastManager.sendBroadcast(intent);
|
|
425 |
break;
|
|
426 |
}
|
|
427 |
case MSG_ENTER_ROOM_FROM_LOBBY: {
|
|
428 |
initRoomState((Boolean)msg.obj);
|
|
429 |
changeState(State.ROOM);
|
|
430 |
Intent intent = new Intent(ACTION_ENTERED_ROOM_FROM_LOBBY);
|
|
431 |
broadcastManager.sendBroadcastSync(intent);
|
|
432 |
break;
|
|
433 |
}
|
|
434 |
case MSG_LEAVE_ROOM: {
|
|
435 |
changeState(State.LOBBY);
|
|
436 |
Intent intent = new Intent(ACTION_LEFT_ROOM);
|
|
437 |
intent.putExtra(EXTRA_MESSAGE, (String)msg.obj);
|
|
438 |
intent.putExtra(EXTRA_REASON, msg.arg1);
|
|
439 |
broadcastManager.sendBroadcastSync(intent);
|
|
440 |
break;
|
|
441 |
}
|
|
442 |
case MSG_TEAM_ADDED: {
|
|
443 |
TeamInGame newTeam = (TeamInGame)msg.obj;
|
|
444 |
if(isChief()) {
|
|
445 |
int freeColor = TeamInGame.getUnusedOrRandomColorIndex(netRoomState.getTeams().values());
|
|
446 |
sendToNet(MSG_SEND_TEAM_HOG_COUNT, newTeam.ingameAttribs.hogCount, newTeam.team.name);
|
|
447 |
sendToNet(MSG_SEND_TEAM_COLOR_INDEX, freeColor, newTeam.team.name);
|
|
448 |
newTeam = newTeam.withAttribs(newTeam.ingameAttribs.withColorIndex(freeColor));
|
|
449 |
}
|
|
450 |
netRoomState.putTeam(newTeam);
|
|
451 |
break;
|
|
452 |
}
|
|
453 |
case MSG_TEAM_DELETED: {
|
|
454 |
netRoomState.removeTeam((String)msg.obj);
|
|
455 |
break;
|
|
456 |
}
|
|
457 |
case MSG_TEAM_ACCEPTED: {
|
|
458 |
TeamInGame requestedTeam = netRoomState.requestedTeams.remove(msg.obj);
|
|
459 |
if(requestedTeam!=null) {
|
|
460 |
netRoomState.putTeam(requestedTeam);
|
|
461 |
if(isChief()) {
|
|
462 |
// Not strictly necessary, but QtFrontend does it...
|
|
463 |
sendToNet(MSG_SEND_TEAM_HOG_COUNT, requestedTeam.ingameAttribs.hogCount, requestedTeam.team.name);
|
|
464 |
}
|
|
465 |
} else {
|
|
466 |
Log.e("Netplay", "Got accepted message for team that was never requested.");
|
|
467 |
}
|
|
468 |
break;
|
|
469 |
}
|
|
470 |
case MSG_TEAM_COLOR_CHANGED: {
|
|
471 |
TeamInGame oldEntry = netRoomState.getTeams().get((String)msg.obj);
|
|
472 |
if(oldEntry != null) {
|
|
473 |
/*
|
|
474 |
* If we are chief, we ignore colors from the outside. They only come from the server
|
|
475 |
* when someone adds a team then, and we override that choice anyway.
|
|
476 |
* Worse, that color message arrives *after* we have overridden the color, so it would
|
|
477 |
* re-override it right back.
|
|
478 |
*/
|
|
479 |
if(!isChief()) {
|
|
480 |
TeamIngameAttributes newAttribs = oldEntry.ingameAttribs.withColorIndex(msg.arg1);
|
|
481 |
netRoomState.putTeam(oldEntry.withAttribs(newAttribs));
|
|
482 |
}
|
|
483 |
} else {
|
|
484 |
Log.e("Netplay", "Color update for unknown team "+msg.obj);
|
|
485 |
}
|
|
486 |
break;
|
|
487 |
}
|
|
488 |
case MSG_HOG_COUNT_CHANGED: {
|
|
489 |
TeamInGame oldEntry = netRoomState.getTeams().get((String)msg.obj);
|
|
490 |
if(oldEntry != null) {
|
|
491 |
TeamIngameAttributes newAttribs = oldEntry.ingameAttribs.withHogCount(msg.arg1);
|
|
492 |
netRoomState.putTeam(oldEntry.withAttribs(newAttribs));
|
|
493 |
} else {
|
|
494 |
Log.e("Netplay", "Hog count update for unknown team "+msg.obj);
|
|
495 |
}
|
|
496 |
break;
|
|
497 |
}
|
|
498 |
case MSG_ENGINE_MESSAGE: {
|
|
499 |
byte[] em = (byte[])msg.obj;
|
|
500 |
for(GameMessageListener listener : gameMessageListeners) {
|
|
501 |
listener.onEngineMessage(em);
|
|
502 |
}
|
|
503 |
break;
|
|
504 |
}
|
|
505 |
case MSG_RUN_GAME: {
|
|
506 |
GameConfig config = (GameConfig)msg.obj;
|
|
507 |
for(RunGameListener listener : runGameListeners) {
|
|
508 |
listener.runGame(config);
|
|
509 |
}
|
|
510 |
break;
|
|
511 |
}
|
|
512 |
case MSG_MAP_CHANGED: {
|
|
513 |
netRoomState.setMapRecipe((MapRecipe)msg.obj);
|
|
514 |
break;
|
|
515 |
}
|
|
516 |
case MSG_SCHEME_CHANGED: {
|
|
517 |
netRoomState.setScheme((Scheme)msg.obj);
|
|
518 |
break;
|
|
519 |
}
|
|
520 |
case MSG_SCRIPT_CHANGED: {
|
|
521 |
netRoomState.setGameStyle((String)msg.obj);
|
|
522 |
break;
|
|
523 |
}
|
|
524 |
case MSG_WEAPONSET_CHANGED: {
|
|
525 |
netRoomState.setWeaponset((Weaponset)msg.obj);
|
|
526 |
break;
|
|
527 |
}
|
|
528 |
default: {
|
|
529 |
Log.e("FromNetHandler", "Unknown message type: "+msg.what);
|
|
530 |
break;
|
|
531 |
}
|
|
532 |
}
|
|
533 |
}
|
|
534 |
}
|
|
535 |
}
|