Skip to content

Commit f50e01e

Browse files
authored
Merge pull request #59 from jessetilro/boost-robustness
Boost robustness
2 parents 974fb89 + 6c30973 commit f50e01e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1066
-408
lines changed

app/src/main/AndroidManifest.xml

+49-16
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@
1515
android:supportsRtl="true"
1616
android:theme="@style/AppTheme"
1717
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
18-
<activity android:name=".MainActivity">
18+
<activity
19+
android:name=".SplashActivity"
20+
android:theme="@style/SplashTheme">
1921
<intent-filter>
2022
<action android:name="android.intent.action.MAIN" />
2123

2224
<category android:name="android.intent.category.LAUNCHER" />
2325
</intent-filter>
2426
</activity>
25-
<activity android:name=".LobbyActivity"
27+
<activity
28+
android:name=".MainActivity"
29+
android:screenOrientation="portrait">
30+
</activity>
31+
<activity
32+
android:name=".LobbyActivity"
2633
android:screenOrientation="portrait">
2734
<meta-data
2835
android:name="android.support.PARENT_ACTIVITY"
@@ -38,49 +45,75 @@
3845
<activity
3946
android:name=".location.LocationArmoryActivity"
4047
android:configChanges="keyboardHidden|orientation"
41-
android:screenOrientation="sensorLandscape"></activity>
48+
android:screenOrientation="sensorLandscape">
49+
<meta-data
50+
android:name="android.support.PARENT_ACTIVITY"
51+
android:value=".RoleActivity" />
52+
</activity>
4253
<activity
4354
android:name=".location.LocationEngineroomActivity"
4455
android:configChanges="keyboardHidden|orientation"
45-
android:screenOrientation="sensorLandscape"></activity>
56+
android:screenOrientation="sensorLandscape">
57+
<meta-data
58+
android:name="android.support.PARENT_ACTIVITY"
59+
android:value=".RoleActivity" />
60+
</activity>
4661
<activity
4762
android:name=".location.LocationLabActivity"
4863
android:configChanges="keyboardHidden|orientation"
49-
android:screenOrientation="sensorLandscape"></activity>
64+
android:screenOrientation="sensorLandscape">
65+
<meta-data
66+
android:name="android.support.PARENT_ACTIVITY"
67+
android:value=".RoleActivity" />
68+
</activity>
5069
<activity
5170
android:name=".location.LocationDeckActivity"
5271
android:configChanges="keyboardHidden|orientation"
53-
android:screenOrientation="sensorLandscape"></activity>
72+
android:screenOrientation="sensorLandscape">
73+
<meta-data
74+
android:name="android.support.PARENT_ACTIVITY"
75+
android:value=".RoleActivity" />
76+
</activity>
5477
<activity
5578
android:name=".minigame.CoffeeBoostActivity"
5679
android:configChanges="keyboardHidden|orientation"
57-
android:screenOrientation="sensorLandscape"></activity>
80+
android:screenOrientation="sensorLandscape">
81+
<meta-data
82+
android:name="android.support.PARENT_ACTIVITY"
83+
android:value=".location.LocationDeckActivity" />
84+
</activity>
5885
<activity
5986
android:name=".minigame.AsteroidImpactActivity"
6087
android:configChanges="keyboardHidden|orientation"
6188
android:screenOrientation="sensorLandscape">
6289
<meta-data
6390
android:name="android.support.PARENT_ACTIVITY"
64-
android:value="nl.tudelft.pixelperfect.pixelperfect.location.LocationLabActivity" />
91+
android:value=".location.LocationEngineroomActivity" />
6592
</activity>
6693
<activity
6794
android:name=".minigame.PlasmaLeakActivity"
6895
android:configChanges="keyboardHidden|orientation"
69-
android:screenOrientation="sensorLandscape"></activity>
96+
android:screenOrientation="sensorLandscape">
97+
<meta-data
98+
android:name="android.support.PARENT_ACTIVITY"
99+
android:value=".location.LocationLabActivity" />
100+
</activity>
70101
<activity
71102
android:name=".minigame.HostileShipActivity"
72103
android:configChanges="keyboardHidden|orientation"
73-
android:screenOrientation="sensorLandscape"></activity>
104+
android:screenOrientation="sensorLandscape">
105+
<meta-data
106+
android:name="android.support.PARENT_ACTIVITY"
107+
android:value=".location.LocationArmoryActivity" />
108+
</activity>
74109
<activity
75110
android:name=".minigame.FireOutbreakActivity"
76-
android:screenOrientation="sensorLandscape"
77-
android:configChanges="keyboardHidden|orientation">
78-
</activity>
111+
android:configChanges="keyboardHidden|orientation"
112+
android:screenOrientation="sensorLandscape" />
79113
<activity
80114
android:name=".minigame.IdleGameActivity"
81-
android:screenOrientation="sensorLandscape"
82-
android:configChanges="keyboardHidden|orientation">
83-
</activity>
115+
android:configChanges="keyboardHidden|orientation"
116+
android:screenOrientation="sensorLandscape" />
84117
</application>
85118

86119
</manifest>

app/src/main/java/nl/tudelft/pixelperfect/client/ConnectTask.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
import nl.tudelft.pixelperfect.client.message.DisconnectMessage;
1212
import nl.tudelft.pixelperfect.client.message.EventCompletedMessage;
13+
import nl.tudelft.pixelperfect.client.message.PlayerDetailsMessage;
1314
import nl.tudelft.pixelperfect.client.message.RepairMessage;
1415
import nl.tudelft.pixelperfect.client.message.NewGameMessage;
15-
import nl.tudelft.pixelperfect.client.message.RoleChosenMessage;
16-
import nl.tudelft.pixelperfect.player.PlayerRoles;
16+
import nl.tudelft.pixelperfect.client.message.RoleAllocationMessage;
1717

1818
/**
1919
* This class will initialize the Network.
@@ -34,26 +34,36 @@ public class ConnectTask extends AsyncTask<String, Void, Client> {
3434
* @return a Client.
3535
*/
3636
protected Client doInBackground(String... ip) {
37+
GameClient game = GameClient.getInstance();
38+
NetworkClientMessageListener messageListener;
3739
Client client;
3840
try {
3941
client = Network.connectToServer(ip[0], 6143);
4042
Serializer.registerClass(EventCompletedMessage.class);
41-
Serializer.registerClass(RoleChosenMessage.class);
43+
Serializer.registerClass(RoleAllocationMessage.class);
44+
Serializer.registerClass(PlayerDetailsMessage.class);
4245
Serializer.registerClass(NewGameMessage.class);
4346
Serializer.registerClass(RepairMessage.class);
4447
Serializer.registerClass(DisconnectMessage.class);
4548
client.start();
46-
client.addMessageListener(new ClientListener(), EventCompletedMessage.class);
47-
client.addMessageListener(new ClientListener(), RoleChosenMessage.class);
48-
client.addMessageListener(new ClientListener(), RepairMessage.class);
49-
client.addMessageListener(new ClientListener(), NewGameMessage.class);
50-
client.addMessageListener(new ClientListener(), DisconnectMessage.class);
49+
client.addMessageListener(makeMessageListener(), EventCompletedMessage.class);
50+
client.addMessageListener(makeMessageListener(), RoleAllocationMessage.class);
51+
client.addMessageListener(makeMessageListener(), PlayerDetailsMessage.class);
52+
client.addMessageListener(makeMessageListener(), RepairMessage.class);
53+
client.addMessageListener(makeMessageListener(), NewGameMessage.class);
54+
client.addMessageListener(makeMessageListener(), DisconnectMessage.class);
5155
} catch (IOException e) {
5256
client = null;
5357
}
5458
return client;
5559
}
5660

61+
private NetworkClientMessageListener makeMessageListener() {
62+
NetworkClientMessageListener messageListener = new NetworkClientMessageListener();
63+
GameClient.getInstance().registerMessageListener(messageListener);
64+
return messageListener;
65+
}
66+
5767
protected void onPostExecute(Client client) {
5868
delegate.connectFinish(client);
5969
}

app/src/main/java/nl/tudelft/pixelperfect/client/GameClient.java

+60
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
import com.jme3.network.ClientStateListener;
55
import com.jme3.network.Message;
66

7+
import java.util.ArrayList;
8+
9+
import nl.tudelft.pixelperfect.pixelperfect.PixelPerfectActivity;
10+
import nl.tudelft.pixelperfect.player.PlayerRoles;
11+
712
/**
813
* The GameClient within the Android app.
914
*
@@ -15,14 +20,21 @@ public class GameClient {
1520

1621
private Client client;
1722
private NetworkClientStateListener connectionListener;
23+
private ArrayList<NetworkClientMessageListener> messageListeners;
24+
private PlayerRoles role;
25+
private boolean running;
26+
27+
private PixelPerfectActivity delegate;
1828

1929
private static volatile GameClient instance;
2030

2131
/**
2232
* Whenever the RouteGenerator is created a new Route will be created (in this factory).
2333
*/
2434
private GameClient() {
35+
messageListeners = new ArrayList<>();
2536
connectionListener = new NetworkClientStateListener();
37+
running = false;
2638
}
2739

2840
/**
@@ -41,6 +53,54 @@ public static GameClient getInstance() {
4153
return instance;
4254
}
4355

56+
/**
57+
* Reset the singleton to a fresh instance.
58+
* Used to reset the game when getting disconnected.
59+
*/
60+
public static void reset() {
61+
if (instance != null) {
62+
synchronized (GameClient.class) {
63+
if (instance != null) {
64+
instance.disconnect();
65+
instance = new GameClient();
66+
}
67+
}
68+
}
69+
}
70+
71+
public void registerMessageListener(NetworkClientMessageListener messageListener) {
72+
messageListeners.add(messageListener);
73+
}
74+
75+
public void delegateTo(PixelPerfectActivity activity) {
76+
delegate = activity;
77+
connectionListener.delegateTo(activity);
78+
for (NetworkClientMessageListener messageListener : messageListeners) {
79+
System.out.println("Delegating to the game's message listeners.");
80+
messageListener.delegateTo(activity);
81+
}
82+
}
83+
84+
public void start() {
85+
running = true;
86+
}
87+
88+
public void stop() {
89+
running = false;
90+
}
91+
92+
public boolean isRunning() {
93+
return running;
94+
}
95+
96+
public void assignRole(PlayerRoles role) {
97+
this.role = role;
98+
}
99+
100+
public PlayerRoles getRole() {
101+
return role;
102+
}
103+
44104
public void setClient(Client client) {
45105
this.client = client;
46106
client.addClientStateListener(connectionListener);

app/src/main/java/nl/tudelft/pixelperfect/client/ClientListener.java app/src/main/java/nl/tudelft/pixelperfect/client/NetworkClientMessageListener.java

+29-10
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
import nl.tudelft.pixelperfect.client.message.DisconnectMessage;
88
import nl.tudelft.pixelperfect.client.message.NewGameMessage;
9-
import nl.tudelft.pixelperfect.client.message.RoleChosenMessage;
10-
import nl.tudelft.pixelperfect.pixelperfect.LobbyActivity;
11-
import nl.tudelft.pixelperfect.pixelperfect.RoleActivity;
9+
import nl.tudelft.pixelperfect.client.message.RoleAllocationMessage;
10+
import nl.tudelft.pixelperfect.pixelperfect.PixelPerfectActivity;
1211

1312
/**
1413
* The ClientListeners waits for incoming messages from the server and interpret them.
@@ -18,35 +17,55 @@
1817
* @author Dmitry Malarev
1918
*/
2019
@SuppressWarnings("unused")
21-
public class ClientListener implements MessageListener<Client> {
20+
public class NetworkClientMessageListener implements MessageListener<Client> {
21+
22+
private PixelPerfectActivity delegate;
23+
24+
public void delegateTo(PixelPerfectActivity activity) {
25+
delegate = activity;
26+
}
2227

2328
/**
2429
* Whenever the client listener receives a message from the Server it will determine what
2530
* to do with it per message.
2631
*
27-
* @param source the source of the message.
32+
* @param source the source of the message.
2833
* @param message the message received.
2934
*/
3035
public void messageReceived(Client source, Message message) {
31-
if (message instanceof RoleChosenMessage) {
32-
handleRoleChosenMessage((RoleChosenMessage) message);
36+
if (message instanceof RoleAllocationMessage) {
37+
handleRoleChosenMessage((RoleAllocationMessage) message);
3338
} else if (message instanceof NewGameMessage) {
34-
LobbyActivity.startGame();
39+
handleNewGameMessage();
3540
} else if (message instanceof DisconnectMessage) {
3641
GameClient game = GameClient.getInstance();
3742
game.disconnect();
3843
}
3944
}
4045

46+
/**
47+
* When a New Game Message has been received, this confirms that an active game session
48+
* is currently going, and we should start playing.
49+
*/
50+
public void handleNewGameMessage() {
51+
if (delegate != null) {
52+
System.out.println("Starting game!");
53+
delegate.startGame();
54+
}
55+
}
56+
4157
/**
4258
* Handle a received RoleChoseMessage.
59+
*
4360
* @param message The message to handle.
4461
*/
45-
public void handleRoleChosenMessage(RoleChosenMessage message) {
62+
public void handleRoleChosenMessage(RoleAllocationMessage message) {
4663
System.out.println("Received role chosen message.");
4764
if (message.isAllocated()) {
4865
System.out.println("Server granted request for " + message.getRole().toString());
49-
RoleActivity.enterLobby(message.getRole());
66+
if (delegate != null) {
67+
delegate.enterLobby(message.getRole());
68+
}
5069
} else {
5170
System.out.println("Server denied request for " + message.getRole().toString());
5271
}

app/src/main/java/nl/tudelft/pixelperfect/client/NetworkClientStateListener.java

+11
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
import com.jme3.network.Client;
44
import com.jme3.network.ClientStateListener;
55

6+
import nl.tudelft.pixelperfect.pixelperfect.PixelPerfectActivity;
7+
68
/**
79
* Monitors the status of the connection with the server.
810
*
911
* @author Jesse Tilro
1012
*/
1113
public class NetworkClientStateListener implements ClientStateListener {
1214

15+
private PixelPerfectActivity delegate;
16+
17+
public void delegateTo(PixelPerfectActivity activity) {
18+
delegate = activity;
19+
}
20+
1321
@Override
1422
public void clientConnected(Client c) {
1523
System.out.println("Connected");
@@ -18,5 +26,8 @@ public void clientConnected(Client c) {
1826
@Override
1927
public void clientDisconnected(Client c, DisconnectInfo info) {
2028
System.out.println("Disconnected");
29+
if (delegate != null) {
30+
delegate.reset();
31+
}
2132
}
2233
}

0 commit comments

Comments
 (0)