mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Wrote tests that are currently broken for sending server commands through text chat.
#tests
This commit is contained in:
@@ -697,10 +697,15 @@ public class GameState implements Runnable {
|
||||
|
||||
public static void endRace () {
|
||||
yachts.forEach((id, yacht) -> yacht.setBoatStatus(BoatStatus.FINISHED));
|
||||
// currentStage = GameStages.FINISHED;
|
||||
currentStage = GameStages.FINISHED;
|
||||
System.out.println("FOR FUCKS SAKE YOU FUCKING DEGENERATE");
|
||||
}
|
||||
|
||||
public static void setSpeedMultiplier (double multiplier) {
|
||||
speedMultiplier = multiplier;
|
||||
}
|
||||
|
||||
public static double getSpeedMultiplier () {
|
||||
return speedMultiplier;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
|
||||
//You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app.
|
||||
while (!terminated) {
|
||||
System.out.println("CUNT GF" + GameState.getCurrentStage());
|
||||
try {
|
||||
Thread.sleep(1000 / CLIENT_UPDATES_PER_SECOND);
|
||||
} catch (InterruptedException e) {
|
||||
@@ -87,22 +88,21 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
//FINISHED
|
||||
else if (GameState.getCurrentStage() == GameStages.FINISHED) {
|
||||
broadcastMessage(makeRaceStatusMessage());
|
||||
System.out.println("BUT I WAS HERE CUNTFACE");
|
||||
try {
|
||||
Thread.sleep(1000); //Hackish fix to make sure all threads have broadcasted
|
||||
Thread.sleep(100); //Hackish fix to make sure all threads have broadcasted
|
||||
terminate();
|
||||
} catch (InterruptedException ie) {
|
||||
serverLog("Thread interrupted while waiting to terminate clients", 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: 14/07/17 wmu16 - Send out disconnect packet to clients
|
||||
try {
|
||||
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
|
||||
serverToClientThread.terminate();
|
||||
}
|
||||
serverSocket.close();
|
||||
return;
|
||||
System.out.println("closed");
|
||||
} catch (IOException e) {
|
||||
System.out.println("IO error in server thread handler upon closing socket");
|
||||
}
|
||||
@@ -245,6 +245,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
|
||||
for (Player player : GameState.getPlayers()) {
|
||||
ServerYacht y = player.getYacht();
|
||||
System.out.println(y.getBoatStatus());
|
||||
BoatSubMessage m = new BoatSubMessage(y.getSourceId(), y.getBoatStatus(),
|
||||
y.getLegNumber(),
|
||||
0, 0, 1234L,
|
||||
@@ -267,6 +268,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
raceStatus = RaceStatus.PREPARATORY;
|
||||
}
|
||||
} else if (GameState.getCurrentStage() == GameStages.FINISHED) {
|
||||
System.out.println("WHAT THE FUCKING FUCK");
|
||||
raceStatus = RaceStatus.TERMINATED;
|
||||
} else {
|
||||
raceStatus = RaceStatus.STARTED;
|
||||
@@ -279,6 +281,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
}
|
||||
|
||||
public void terminate() {
|
||||
System.out.println("done");
|
||||
terminated = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -386,32 +386,34 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
Arrays.copyOfRange(chatterPayload, 3, 3 + chatterPayload.length)
|
||||
);
|
||||
String[] words = chatterText.split("\\s+");
|
||||
if (words.length < 3) {
|
||||
return;
|
||||
for (String s : words) {
|
||||
System.out.println(s);
|
||||
}
|
||||
switch (words[2].trim()) {
|
||||
case ">speed":
|
||||
try {
|
||||
GameState.setSpeedMultiplier(Double.valueOf(words[3]));
|
||||
if (words.length > 2 && isHost) {
|
||||
switch (words[2].trim()) {
|
||||
case ">speed":
|
||||
try {
|
||||
GameState.setSpeedMultiplier(Double.valueOf(words[3]));
|
||||
GameState.broadcastChatter(new ChatterMessage(
|
||||
Byte.toUnsignedInt(chatterPayload[1]),
|
||||
words[0] + "Host has set speed modifier to x" + words[3]
|
||||
));
|
||||
} catch (Exception e) {
|
||||
logger.error("cannot parse >speed value");
|
||||
}
|
||||
return;
|
||||
case ">finish":
|
||||
System.out.println(words[2].trim());
|
||||
GameState.endRace();
|
||||
GameState.broadcastChatter(new ChatterMessage(
|
||||
Byte.toUnsignedInt(chatterPayload[1]),
|
||||
words[0] + "Host has set speed modifier to x" + words[3]
|
||||
chatterPayload[1],
|
||||
words[0] + "Host has ended the game"
|
||||
));
|
||||
} catch (Exception e) {
|
||||
logger.error("cannot parse >speed value");
|
||||
}
|
||||
break;
|
||||
case ">finish":
|
||||
GameState.endRace();
|
||||
GameState.broadcastChatter(new ChatterMessage(
|
||||
chatterPayload[1],
|
||||
words[0] + "Host has ended the game"
|
||||
));
|
||||
break;
|
||||
default:
|
||||
GameState.broadcastChatter(
|
||||
ServerPacketParser.extractChatterText(chatterPayload)
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
GameState.broadcastChatter(
|
||||
ServerPacketParser.extractChatterText(chatterPayload)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class RaceStatusData {
|
||||
* Returns the data for boats collected form race status packets.
|
||||
*
|
||||
* @return A list of boat data. Boat data is in the form
|
||||
* [boatID, estTimeToNextMark, estTimeToFinish, legNumber].
|
||||
* [boatID, estTimeToNextMark, estTimeToFinish, legNumber, status].
|
||||
*/
|
||||
public List<long[]> getBoatData () {
|
||||
return boatData;
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package seng302.visualiser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
@@ -148,7 +146,9 @@ public class GameClient {
|
||||
}
|
||||
|
||||
private void loadStartScreen() {
|
||||
socketThread.setSocketToClose();
|
||||
if (socketThread != null) {
|
||||
socketThread.setSocketToClose();
|
||||
}
|
||||
if (server != null) {
|
||||
server.terminate();
|
||||
server = null;
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
package seng302.gameServer.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import seng302.gameServer.GameState;
|
||||
import seng302.gameServer.MainServerThread;
|
||||
import seng302.gameServer.messages.BoatStatus;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
import seng302.model.stream.parser.RaceStatusData;
|
||||
import seng302.utilities.StreamParser;
|
||||
import seng302.visualiser.ClientToServerThread;
|
||||
|
||||
/**
|
||||
* Created by cir27 on 3/09/17.
|
||||
*/
|
||||
public class ChatCommandsTest {
|
||||
|
||||
// @Rule
|
||||
// public Timeout globalTimeout = new Timeout(3, TimeUnit.SECONDS);
|
||||
|
||||
@Test
|
||||
public void sendFinishAsHost () {
|
||||
try {
|
||||
final MainServerThread mst = new MainServerThread();
|
||||
final ClientToServerThread host = new ClientToServerThread("localhost", 4942);
|
||||
host.addStreamObserver(() -> {
|
||||
while (host.getPacketQueue().peek() != null) {
|
||||
StreamPacket packet = host.getPacketQueue().poll();
|
||||
switch (packet.getType()) {
|
||||
case RACE_STATUS:
|
||||
RaceStatusData rsd = StreamParser.extractRaceStatus(packet);
|
||||
System.out.println("yas");
|
||||
System.out.println(rsd.getBoatData().get(0)[4]);
|
||||
if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) {
|
||||
System.out.println("why tho");
|
||||
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAA");
|
||||
mst.terminate();
|
||||
host.setSocketToClose();
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
mst.startGame();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
host.sendChatterMessage("[time_prefix] <name_prefix> >finish");
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendSpeedAsHostValid () {
|
||||
MainServerThread mst = new MainServerThread();
|
||||
ClientToServerThread host = null;
|
||||
try {
|
||||
host = new ClientToServerThread("localhost", 4942);
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
mst.startGame();
|
||||
host.sendChatterMessage("[time_prefix] <name_prefix> >speed 5.0");
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
mst.terminate();
|
||||
host.setSocketToClose();
|
||||
Assert.assertEquals(5.0, GameState.getSpeedMultiplier(), 0.00001);
|
||||
}
|
||||
|
||||
@Test public void sendSpeedAsHostInvalid () {
|
||||
MainServerThread mst = new MainServerThread();
|
||||
ClientToServerThread host = null;
|
||||
try {
|
||||
host = new ClientToServerThread("localhost", 4942);
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
mst.startGame();
|
||||
host.sendChatterMessage("[time_prefix] <name_prefix> >speed fdgdgdfg");
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
mst.terminate();
|
||||
host.setSocketToClose();
|
||||
Assert.assertEquals(1.0, GameState.getSpeedMultiplier(), 0.00001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendFinishAsClient () {
|
||||
MainServerThread mst = new MainServerThread();
|
||||
ClientToServerThread host = null;
|
||||
ClientToServerThread client = null;
|
||||
try {
|
||||
host = new ClientToServerThread("localhost", 4942);
|
||||
client = new ClientToServerThread("localhost", 4942);
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
mst.startGame();
|
||||
client.sendChatterMessage("[time_prefix] <name_prefix> >speed 5.0");
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
mst.terminate();
|
||||
host.setSocketToClose();
|
||||
client.setSocketToClose();
|
||||
Assert.assertEquals(1.0, GameState.getSpeedMultiplier(), 0.00001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveFinishedAsClient () {
|
||||
MainServerThread mst = new MainServerThread();
|
||||
ClientToServerThread host = null;
|
||||
try {
|
||||
host = new ClientToServerThread("localhost", 4942);
|
||||
ClientToServerThread client = new ClientToServerThread("localhost", 4942);
|
||||
client.addStreamObserver(() -> {
|
||||
while (client.getPacketQueue().peek() != null) {
|
||||
StreamPacket packet = client.getPacketQueue().poll();
|
||||
switch (packet.getType()) {
|
||||
case RACE_STATUS:
|
||||
RaceStatusData rsd = StreamParser.extractRaceStatus(packet);
|
||||
if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) {
|
||||
mst.terminate();
|
||||
client.setSocketToClose();
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
mst.startGame();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
host.sendChatterMessage("[time_prefix] <name_prefix> >finish");
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ie) {
|
||||
ie.printStackTrace();
|
||||
}
|
||||
host.setSocketToClose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user