Merge request fixes

#story[1246]
This commit is contained in:
William Muir
2017-09-08 15:52:01 +12:00
parent cff15ba07d
commit e2bc613c9d
6 changed files with 80 additions and 70 deletions
@@ -1,6 +1,7 @@
package seng302.gameServer; package seng302.gameServer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@@ -40,6 +41,7 @@ import seng302.utilities.XMLParser;
*/ */
public class GameState implements Runnable { public class GameState implements Runnable {
@FunctionalInterface @FunctionalInterface
interface NewMessageListener { interface NewMessageListener {
void notify(Message message); void notify(Message message);
@@ -675,6 +677,36 @@ public class GameState implements Runnable {
} }
public static void processChatter(ChatterMessage chatterMessage, boolean isHost) {
String chatterText = chatterMessage.getMessage();
String[] words = chatterText.split("\\s+");
if (words.length > 2 && isHost) {
switch (words[2].trim()) {
case ">speed":
try {
setSpeedMultiplier(Double.valueOf(words[3]));
broadcastChatter(new ChatterMessage(
chatterMessage.getMessage_type(),
"SERVER: Speed modifier set to x" + words[3]
));
} catch (Exception e) {
Logger logger = LoggerFactory.getLogger(GameState.class);
logger.error("cannot parse >speed value");
}
return;
case ">finish":
broadcastChatter(new ChatterMessage(
chatterMessage.getMessage_type(),
"SERVER: Game will now finish"
));
endRace();
return;
}
}
broadcastChatter(chatterMessage);
}
public static void addMarkPassListener(NewMessageListener listener) { public static void addMarkPassListener(NewMessageListener listener) {
messageListeners.add(listener); messageListeners.add(listener);
} }
@@ -35,5 +35,12 @@ public class ServerPacketParser {
payload[1], new String(Arrays.copyOfRange(payload, 3, payload.length)) payload[1], new String(Arrays.copyOfRange(payload, 3, payload.length))
); );
} }
public static ChatterMessage extractChatterText(StreamPacket packet) {
byte[] payload = packet.getPayload();
return new ChatterMessage(
payload[1], new String(Arrays.copyOfRange(payload, 3, payload.length))
);
}
} }
@@ -211,10 +211,10 @@ public class ServerToClientThread implements Runnable, Observer {
completeRegistration(requestedType); completeRegistration(requestedType);
break; break;
case CHATTER_TEXT: case CHATTER_TEXT:
// GameState.broadcastChatter( ChatterMessage chatterMessage = ServerPacketParser
// ServerPacketParser.extractChatterText(payload) .extractChatterText(
// ); new StreamPacket(type, payloadLength, timeStamp, payload));
parseChatter(payload); GameState.processChatter(chatterMessage, isHost);
break; break;
case RACE_CUSTOMIZATION_REQUEST: case RACE_CUSTOMIZATION_REQUEST:
Long sourceID = Message Long sourceID = Message
@@ -381,35 +381,36 @@ public class ServerToClientThread implements Runnable, Observer {
isHost = true; isHost = true;
} }
private void parseChatter(byte[] chatterPayload) { private void checkChatterForCommands(ChatterMessage chatterMessage) {
String chatterText = new String(
Arrays.copyOfRange(chatterPayload, 3, 3 + chatterPayload.length) // String chatterText = new String(
); // Arrays.copyOfRange(chatterPayload, 3, 3 + chatterPayload.length)
String[] words = chatterText.split("\\s+"); // );
if (words.length > 2 && isHost) { // String[] words = chatterText.split("\\s+");
switch (words[2].trim()) { // if (words.length > 2 && isHost) {
case ">speed": // switch (words[2].trim()) {
try { // case ">speed":
GameState.setSpeedMultiplier(Double.valueOf(words[3])); // try {
GameState.broadcastChatter(new ChatterMessage( // GameState.setSpeedMultiplier(Double.valueOf(words[3]));
Byte.toUnsignedInt(chatterPayload[1]), // GameState.broadcastChatter(new ChatterMessage(
"SERVER: Speed modifier set to x" + words[3] // Byte.toUnsignedInt(chatterPayload[1]),
)); // "SERVER: Speed modifier set to x" + words[3]
} catch (Exception e) { // ));
logger.error("cannot parse >speed value"); // } catch (Exception e) {
} // logger.error("cannot parse >speed value");
return; // }
case ">finish": // return;
GameState.broadcastChatter(new ChatterMessage( // case ">finish":
chatterPayload[1], // GameState.broadcastChatter(new ChatterMessage(
"SERVER: Game will now finish" // chatterPayload[1],
)); // "SERVER: Game will now finish"
GameState.endRace(); // ));
return; // GameState.endRace();
} // return;
} // }
GameState.broadcastChatter( // }
ServerPacketParser.extractChatterText(chatterPayload) // GameState.broadcastChatter(
); // ServerPacketParser.extractChatterText(chatterPayload)
// );
} }
} }
@@ -36,5 +36,11 @@ public class ChatterMessage extends Message {
return MESSAGE_SIZE + message_size; return MESSAGE_SIZE + message_size;
} }
public String getMessage() {
return message;
}
public int getMessage_type() {
return message_type;
}
} }
@@ -16,8 +16,6 @@ import seng302.visualiser.ClientToServerThread;
*/ */
public class ChatCommandsTest { public class ChatCommandsTest {
// @Rule
// public Timeout globalTimeout = new Timeout(3, TimeUnit.SECONDS);
private boolean dcSent = false; private boolean dcSent = false;
private ClientToServerThread client; private ClientToServerThread client;
@@ -44,8 +42,6 @@ public class ChatCommandsTest {
RaceStatusData rsd = StreamParser.extractRaceStatus(packet); RaceStatusData rsd = StreamParser.extractRaceStatus(packet);
if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) { if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) {
mst.terminate(); mst.terminate();
System.out.println("AY WE DID IT");
// host.setSocketToClose();
Assert.assertTrue(dcSent); Assert.assertTrue(dcSent);
} }
break; break;
@@ -108,9 +104,7 @@ public class ChatCommandsTest {
ie.printStackTrace(); ie.printStackTrace();
} }
Assert.assertEquals(5.0, GameState.getSpeedMultiplier(), 0.00001); Assert.assertEquals(5.0, GameState.getSpeedMultiplier(), 0.00001);
System.out.println("the thing " + GameState.getSpeedMultiplier());
mst.terminate(); mst.terminate();
// host.setSocketToClose();
try { try {
Thread.sleep(2000); Thread.sleep(2000);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
@@ -149,9 +143,7 @@ public class ChatCommandsTest {
ie.printStackTrace(); ie.printStackTrace();
} }
mst.terminate(); mst.terminate();
// host.setSocketToClose();
Assert.assertEquals(1.0, GameState.getSpeedMultiplier(), 0.00001); Assert.assertEquals(1.0, GameState.getSpeedMultiplier(), 0.00001);
System.out.println("value " + GameState.getSpeedMultiplier());
try { try {
Thread.sleep(2000); Thread.sleep(2000);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
@@ -175,7 +167,6 @@ public class ChatCommandsTest {
ie.printStackTrace(); ie.printStackTrace();
} }
client = new ClientToServerThread("localhost", 4942); client = new ClientToServerThread("localhost", 4942);
System.out.println("done client and host assigning");
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
} }
@@ -239,9 +230,6 @@ public class ChatCommandsTest {
RaceStatusData rsd = StreamParser.extractRaceStatus(packet); RaceStatusData rsd = StreamParser.extractRaceStatus(packet);
if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) { if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) {
mst.terminate(); mst.terminate();
System.out.println("TEST COMPLETE");
// client.setSocketToClose();
// host.setSocketToClose();
Assert.assertTrue(dcSent); Assert.assertTrue(dcSent);
} }
break; break;
@@ -253,30 +241,7 @@ public class ChatCommandsTest {
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
} }
// try {
// Thread.sleep(100);
// } catch (InterruptedException ie) {
// ie.printStackTrace();
// }
// mst.startGame();
// try {
// Thread.sleep(100);
// } catch (InterruptedException ie) {
// ie.printStackTrace();
// }
host.sendChatterMessage("[time_prefix] <name_prefix> >finish"); host.sendChatterMessage("[time_prefix] <name_prefix> >finish");
dcSent = true; dcSent = true;
// try {
// Thread.sleep(200);
// } catch (InterruptedException ie) {
// ie.printStackTrace();
// }
//// host.setSocketToClose();
// mst.terminate();
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} }
} }
-1
View File
@@ -21,7 +21,6 @@ public class SendChatSteps {
private MainServerThread mst; private MainServerThread mst;
//TODO Need to mock the controller pane in order to run the full game client
@Given("^There are two games running$") @Given("^There are two games running$")
public void the_are_two_games_running() throws Throwable { public void the_are_two_games_running() throws Throwable {
mst = new MainServerThread(); mst = new MainServerThread();