author | sheepluva |
Sat, 07 Dec 2019 16:14:27 +0100 | |
branch | hedgeroid |
changeset 15511 | fb23e49b2d4e |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
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 |
||
15511
fb23e49b2d4e
hedgeroid: fix needlessly creating netplay instance on check for "connecting..."
sheepluva
parents:
10017
diff
changeset
|
191 |
public static boolean hasAppInstance() { |
fb23e49b2d4e
hedgeroid: fix needlessly creating netplay instance on check for "connecting..."
sheepluva
parents:
10017
diff
changeset
|
192 |
return instance != null; |
fb23e49b2d4e
hedgeroid: fix needlessly creating netplay instance on check for "connecting..."
sheepluva
parents:
10017
diff
changeset
|
193 |
} |
fb23e49b2d4e
hedgeroid: fix needlessly creating netplay instance on check for "connecting..."
sheepluva
parents:
10017
diff
changeset
|
194 |
|
10017 | 195 |
/** |
196 |
* Retrieve the single app-wide instance of the netplay interface, creating it if it |
|
197 |
* does not exist yet. |
|
198 |
* |
|
199 |
* @param applicationContext |
|
200 |
* @return |
|
201 |
*/ |
|
202 |
public static Netplay getAppInstance(Context applicationContext) { |
|
15511
fb23e49b2d4e
hedgeroid: fix needlessly creating netplay instance on check for "connecting..."
sheepluva
parents:
10017
diff
changeset
|
203 |
if (!hasAppInstance()) { |
10017 | 204 |
// We will need some default values for rooms, best load them here |
205 |
Scheme defaultScheme = null; |
|
206 |
Weaponset defaultWeaponset = null; |
|
207 |
try { |
|
208 |
List<Scheme> schemes = Schemes.loadBuiltinSchemes(applicationContext); |
|
209 |
for(Scheme scheme : schemes) { |
|
210 |
if(scheme.name.equals(GameConfig.DEFAULT_SCHEME)) { |
|
211 |
defaultScheme = scheme; |
|
212 |
} |
|
213 |
} |
|
214 |
List<Weaponset> weaponsets = Weaponsets.loadBuiltinWeaponsets(applicationContext); |
|
215 |
for(Weaponset weaponset : weaponsets) { |
|
216 |
if(weaponset.name.equals(GameConfig.DEFAULT_WEAPONSET)) { |
|
217 |
defaultWeaponset = weaponset; |
|
218 |
} |
|
219 |
} |
|
220 |
} catch(IOException e) { |
|
221 |
throw new RuntimeException(e); |
|
222 |
} |
|
223 |
||
224 |
if(defaultScheme==null || defaultWeaponset==null) { |
|
225 |
throw new RuntimeException("Unable to load default scheme or weaponset"); |
|
226 |
} |
|
227 |
||
228 |
instance = new Netplay(applicationContext, defaultScheme, defaultWeaponset); |
|
229 |
} |
|
230 |
return instance; |
|
231 |
} |
|
232 |
||
233 |
public State getState() { |
|
234 |
return state; |
|
235 |
} |
|
236 |
||
237 |
private void changeState(State newState) { |
|
238 |
if(newState != state) { |
|
239 |
state = newState; |
|
240 |
broadcastManager.sendBroadcastSync(new Intent(ACTION_STATE_CHANGED)); |
|
241 |
} |
|
242 |
} |
|
243 |
||
244 |
public boolean isChief() { |
|
245 |
if(netRoomState != null) { |
|
246 |
return netRoomState.getChiefStatus(); |
|
247 |
} else { |
|
248 |
return false; |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
public String getPlayerName() { |
|
253 |
return playerName; |
|
254 |
} |
|
255 |
||
256 |
boolean sendToNet(ToNetMsgType what) { |
|
257 |
return sendToNet(what, 0, null); |
|
258 |
} |
|
259 |
||
260 |
boolean sendToNet(ToNetMsgType what, Object obj) { |
|
261 |
return sendToNet(what, 0, obj); |
|
262 |
} |
|
263 |
||
264 |
boolean sendToNet(ToNetMsgType what, int arg1, Object obj) { |
|
265 |
if(connection != null) { |
|
266 |
Handler handler = connection.toNetHandler; |
|
267 |
return handler.sendMessage(handler.obtainMessage(what.ordinal(), arg1, 0, obj)); |
|
268 |
} else { |
|
269 |
return false; |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
private MessageLog getCurrentLog() { |
|
274 |
if(state == State.ROOM) { |
|
275 |
return roomChatlog; |
|
276 |
} else { |
|
277 |
return lobbyChatlog; |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
public static enum FromNetMsgType { |
|
282 |
MSG_LOBBY_JOIN, |
|
283 |
MSG_LOBBY_LEAVE, |
|
284 |
MSG_ROOM_JOIN, |
|
285 |
MSG_ROOM_LEAVE, |
|
286 |
MSG_CLIENT_FLAGS, |
|
287 |
MSG_CHAT, |
|
288 |
MSG_MESSAGE, |
|
289 |
MSG_ROOM_ADD, |
|
290 |
MSG_ROOM_UPDATE, |
|
291 |
MSG_ROOM_DELETE, |
|
292 |
MSG_ROOMLIST, |
|
293 |
MSG_CONNECTED, |
|
294 |
MSG_DISCONNECTED, |
|
295 |
MSG_PASSWORD_REQUEST, |
|
296 |
MSG_ENTER_ROOM_FROM_LOBBY, |
|
297 |
MSG_LEAVE_ROOM, |
|
298 |
MSG_TEAM_ADDED, |
|
299 |
MSG_TEAM_DELETED, |
|
300 |
MSG_TEAM_ACCEPTED, |
|
301 |
MSG_TEAM_COLOR_CHANGED, |
|
302 |
MSG_HOG_COUNT_CHANGED, |
|
303 |
MSG_ENGINE_MESSAGE, |
|
304 |
MSG_RUN_GAME, |
|
305 |
MSG_SCHEME_CHANGED, |
|
306 |
MSG_MAP_CHANGED, |
|
307 |
MSG_SCRIPT_CHANGED, |
|
308 |
MSG_WEAPONSET_CHANGED; |
|
309 |
||
310 |
static final List<FromNetMsgType> values = Collections.unmodifiableList(Arrays.asList(FromNetMsgType.values())); |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Processes messages from the networking system. Always runs on the main thread. |
|
315 |
*/ |
|
316 |
@SuppressLint("HandlerLeak") |
|
317 |
final class FromNetHandler extends Handler { |
|
318 |
public FromNetHandler() { |
|
319 |
super(Looper.getMainLooper()); |
|
320 |
} |
|
321 |
||
322 |
@SuppressWarnings("unchecked") |
|
323 |
@Override |
|
324 |
public void handleMessage(Message msg) { |
|
325 |
switch(FromNetMsgType.values.get(msg.what)) { |
|
326 |
case MSG_LOBBY_JOIN: { |
|
327 |
String name = (String)msg.obj; |
|
328 |
lobbyPlayerlist.put(name, new Player(name, false, false)); |
|
329 |
lobbyChatlog.appendPlayerJoin(name); |
|
330 |
break; |
|
331 |
} |
|
332 |
case MSG_LOBBY_LEAVE: { |
|
333 |
Pair<String, String> args = (Pair<String, String>)msg.obj; |
|
334 |
lobbyPlayerlist.remove(args.first); |
|
335 |
lobbyChatlog.appendPlayerLeave(args.first, args.second); |
|
336 |
break; |
|
337 |
} |
|
338 |
case MSG_ROOM_JOIN: { |
|
339 |
String name = (String)msg.obj; |
|
340 |
Player p = lobbyPlayerlist.get(name); |
|
341 |
if(p==null) { |
|
342 |
Log.w("Netplay", "Unknown player joined room: "+name); |
|
343 |
p = new Player(name, false, false); |
|
344 |
} |
|
345 |
roomPlayerlist.put(name, new PlayerInRoom(p, false, false)); |
|
346 |
roomChatlog.appendPlayerJoin(name); |
|
347 |
break; |
|
348 |
} |
|
349 |
case MSG_ROOM_LEAVE: { |
|
350 |
Pair<String, String> args = (Pair<String, String>)msg.obj; |
|
351 |
roomPlayerlist.remove(args.first); |
|
352 |
roomChatlog.appendPlayerLeave(args.first, args.second); |
|
353 |
break; |
|
354 |
} |
|
355 |
case MSG_CLIENT_FLAGS: { |
|
356 |
ClientFlagsUpdate upd = (ClientFlagsUpdate)msg.obj; |
|
357 |
PlayerInRoom pir = roomPlayerlist.get(upd.nick); |
|
358 |
if(pir != null) { |
|
359 |
roomPlayerlist.put(upd.nick, upd.applyTo(pir)); |
|
360 |
} |
|
361 |
Player p = lobbyPlayerlist.get(upd.nick); |
|
362 |
if(p != null) { |
|
363 |
lobbyPlayerlist.put(upd.nick, upd.applyTo(p)); |
|
364 |
} else { |
|
365 |
Log.w("Netplay", "Received client flags for unknown player "+upd.nick); |
|
366 |
} |
|
367 |
if(playerName.equals(upd.nick) && upd.appliesTo(ClientFlagsUpdate.FLAG_CHIEF)) { |
|
368 |
netRoomState.setChief(upd.newFlagState); |
|
369 |
} |
|
370 |
break; |
|
371 |
} |
|
372 |
case MSG_CHAT: { |
|
373 |
Pair<String, String> args = (Pair<String, String>)msg.obj; |
|
374 |
getCurrentLog().appendChat(args.first, args.second); |
|
375 |
for(GameMessageListener listener : gameMessageListeners) { |
|
376 |
listener.onChatMessage(args.first, args.second); |
|
377 |
} |
|
378 |
break; |
|
379 |
} |
|
380 |
case MSG_MESSAGE: { |
|
381 |
getCurrentLog().appendMessage(msg.arg1, (String)msg.obj); |
|
382 |
for(GameMessageListener listener : gameMessageListeners) { |
|
383 |
listener.onMessage(1, (String)msg.obj); |
|
384 |
} |
|
385 |
break; |
|
386 |
} |
|
387 |
case MSG_ROOM_ADD: { |
|
388 |
Room room = (Room)msg.obj; |
|
389 |
roomList.addRoomWithNewId(room); |
|
390 |
break; |
|
391 |
} |
|
392 |
case MSG_ROOM_UPDATE: { |
|
393 |
Pair<String, Room> args = (Pair<String, Room>)msg.obj; |
|
394 |
roomList.updateRoom(args.first, args.second); |
|
395 |
break; |
|
396 |
} |
|
397 |
case MSG_ROOM_DELETE: { |
|
398 |
roomList.remove((String)msg.obj); |
|
399 |
break; |
|
400 |
} |
|
401 |
case MSG_ROOMLIST: { |
|
402 |
Room[] rooms = (Room[])msg.obj; |
|
403 |
roomList.updateList(rooms); |
|
404 |
break; |
|
405 |
} |
|
406 |
case MSG_CONNECTED: { |
|
407 |
playerName = (String)msg.obj; |
|
408 |
changeState(State.LOBBY); |
|
409 |
broadcastManager.sendBroadcast(new Intent(ACTION_CONNECTED)); |
|
410 |
break; |
|
411 |
} |
|
412 |
case MSG_DISCONNECTED: { |
|
413 |
Pair<Boolean, String> args = (Pair<Boolean, String>)msg.obj; |
|
414 |
for(GameMessageListener listener : gameMessageListeners) { |
|
415 |
listener.onNetDisconnected(); |
|
416 |
} |
|
417 |
changeState(State.NOT_CONNECTED); |
|
418 |
connection = null; |
|
419 |
Intent intent = new Intent(ACTION_DISCONNECTED); |
|
420 |
intent.putExtra(EXTRA_HAS_ERROR, args.first); |
|
421 |
intent.putExtra(EXTRA_MESSAGE, args.second); |
|
422 |
broadcastManager.sendBroadcastSync(intent); |
|
423 |
break; |
|
424 |
} |
|
425 |
case MSG_PASSWORD_REQUEST: { |
|
426 |
Intent intent = new Intent(ACTION_PASSWORD_REQUESTED); |
|
427 |
intent.putExtra(EXTRA_PLAYERNAME, (String)msg.obj); |
|
428 |
broadcastManager.sendBroadcast(intent); |
|
429 |
break; |
|
430 |
} |
|
431 |
case MSG_ENTER_ROOM_FROM_LOBBY: { |
|
432 |
initRoomState((Boolean)msg.obj); |
|
433 |
changeState(State.ROOM); |
|
434 |
Intent intent = new Intent(ACTION_ENTERED_ROOM_FROM_LOBBY); |
|
435 |
broadcastManager.sendBroadcastSync(intent); |
|
436 |
break; |
|
437 |
} |
|
438 |
case MSG_LEAVE_ROOM: { |
|
439 |
changeState(State.LOBBY); |
|
440 |
Intent intent = new Intent(ACTION_LEFT_ROOM); |
|
441 |
intent.putExtra(EXTRA_MESSAGE, (String)msg.obj); |
|
442 |
intent.putExtra(EXTRA_REASON, msg.arg1); |
|
443 |
broadcastManager.sendBroadcastSync(intent); |
|
444 |
break; |
|
445 |
} |
|
446 |
case MSG_TEAM_ADDED: { |
|
447 |
TeamInGame newTeam = (TeamInGame)msg.obj; |
|
448 |
if(isChief()) { |
|
449 |
int freeColor = TeamInGame.getUnusedOrRandomColorIndex(netRoomState.getTeams().values()); |
|
450 |
sendToNet(MSG_SEND_TEAM_HOG_COUNT, newTeam.ingameAttribs.hogCount, newTeam.team.name); |
|
451 |
sendToNet(MSG_SEND_TEAM_COLOR_INDEX, freeColor, newTeam.team.name); |
|
452 |
newTeam = newTeam.withAttribs(newTeam.ingameAttribs.withColorIndex(freeColor)); |
|
453 |
} |
|
454 |
netRoomState.putTeam(newTeam); |
|
455 |
break; |
|
456 |
} |
|
457 |
case MSG_TEAM_DELETED: { |
|
458 |
netRoomState.removeTeam((String)msg.obj); |
|
459 |
break; |
|
460 |
} |
|
461 |
case MSG_TEAM_ACCEPTED: { |
|
462 |
TeamInGame requestedTeam = netRoomState.requestedTeams.remove(msg.obj); |
|
463 |
if(requestedTeam!=null) { |
|
464 |
netRoomState.putTeam(requestedTeam); |
|
465 |
if(isChief()) { |
|
466 |
// Not strictly necessary, but QtFrontend does it... |
|
467 |
sendToNet(MSG_SEND_TEAM_HOG_COUNT, requestedTeam.ingameAttribs.hogCount, requestedTeam.team.name); |
|
468 |
} |
|
469 |
} else { |
|
470 |
Log.e("Netplay", "Got accepted message for team that was never requested."); |
|
471 |
} |
|
472 |
break; |
|
473 |
} |
|
474 |
case MSG_TEAM_COLOR_CHANGED: { |
|
475 |
TeamInGame oldEntry = netRoomState.getTeams().get((String)msg.obj); |
|
476 |
if(oldEntry != null) { |
|
477 |
/* |
|
478 |
* If we are chief, we ignore colors from the outside. They only come from the server |
|
479 |
* when someone adds a team then, and we override that choice anyway. |
|
480 |
* Worse, that color message arrives *after* we have overridden the color, so it would |
|
481 |
* re-override it right back. |
|
482 |
*/ |
|
483 |
if(!isChief()) { |
|
484 |
TeamIngameAttributes newAttribs = oldEntry.ingameAttribs.withColorIndex(msg.arg1); |
|
485 |
netRoomState.putTeam(oldEntry.withAttribs(newAttribs)); |
|
486 |
} |
|
487 |
} else { |
|
488 |
Log.e("Netplay", "Color update for unknown team "+msg.obj); |
|
489 |
} |
|
490 |
break; |
|
491 |
} |
|
492 |
case MSG_HOG_COUNT_CHANGED: { |
|
493 |
TeamInGame oldEntry = netRoomState.getTeams().get((String)msg.obj); |
|
494 |
if(oldEntry != null) { |
|
495 |
TeamIngameAttributes newAttribs = oldEntry.ingameAttribs.withHogCount(msg.arg1); |
|
496 |
netRoomState.putTeam(oldEntry.withAttribs(newAttribs)); |
|
497 |
} else { |
|
498 |
Log.e("Netplay", "Hog count update for unknown team "+msg.obj); |
|
499 |
} |
|
500 |
break; |
|
501 |
} |
|
502 |
case MSG_ENGINE_MESSAGE: { |
|
503 |
byte[] em = (byte[])msg.obj; |
|
504 |
for(GameMessageListener listener : gameMessageListeners) { |
|
505 |
listener.onEngineMessage(em); |
|
506 |
} |
|
507 |
break; |
|
508 |
} |
|
509 |
case MSG_RUN_GAME: { |
|
510 |
GameConfig config = (GameConfig)msg.obj; |
|
511 |
for(RunGameListener listener : runGameListeners) { |
|
512 |
listener.runGame(config); |
|
513 |
} |
|
514 |
break; |
|
515 |
} |
|
516 |
case MSG_MAP_CHANGED: { |
|
517 |
netRoomState.setMapRecipe((MapRecipe)msg.obj); |
|
518 |
break; |
|
519 |
} |
|
520 |
case MSG_SCHEME_CHANGED: { |
|
521 |
netRoomState.setScheme((Scheme)msg.obj); |
|
522 |
break; |
|
523 |
} |
|
524 |
case MSG_SCRIPT_CHANGED: { |
|
525 |
netRoomState.setGameStyle((String)msg.obj); |
|
526 |
break; |
|
527 |
} |
|
528 |
case MSG_WEAPONSET_CHANGED: { |
|
529 |
netRoomState.setWeaponset((Weaponset)msg.obj); |
|
530 |
break; |
|
531 |
} |
|
532 |
default: { |
|
533 |
Log.e("FromNetHandler", "Unknown message type: "+msg.what); |
|
534 |
break; |
|
535 |
} |
|
536 |
} |
|
537 |
} |
|
538 |
} |
|
539 |
} |