mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Merge remote-tracking branch 'origin/Story1055_Lobby_View_Update' into Merging_GameState_With_Lobby
# Conflicts: # src/main/java/seng302/client/ClientPacketParser.java # src/main/java/seng302/client/ClientToServerThread.java # src/main/java/seng302/controllers/LobbyController.java # src/main/java/seng302/controllers/StartScreenController.java # src/main/java/seng302/gameServer/ServerToClientThread.java
This commit is contained in:
@@ -36,11 +36,9 @@ public class ClientPacketParser {
|
||||
|
||||
public static ConcurrentHashMap<Long, PriorityBlockingQueue<BoatPositionPacket>> markLocations = new ConcurrentHashMap<>();
|
||||
public static ConcurrentHashMap<Long, PriorityBlockingQueue<BoatPositionPacket>> boatLocations = new ConcurrentHashMap<>();
|
||||
private String threadName;
|
||||
private Thread t;
|
||||
private static boolean newRaceXmlReceived = false;
|
||||
private static boolean raceStarted = false;
|
||||
private static XMLParser xmlObject;
|
||||
private static XMLParser xmlObject = new XMLParser();
|
||||
private static boolean raceFinished = false;
|
||||
private static boolean streamStatus = false;
|
||||
private static long timeSinceStart = -1;
|
||||
@@ -48,10 +46,10 @@ public class ClientPacketParser {
|
||||
private static Map<Integer, Yacht> boatsPos = new ConcurrentSkipListMap<>();
|
||||
private static double windDirection = 0;
|
||||
private static Double windSpeed = 0d;
|
||||
private static Long currentTimeLong;
|
||||
private static Long currentTimeLong;
|
||||
private static String currentTimeString;
|
||||
private static boolean appRunning;
|
||||
|
||||
private static Map<Integer, Yacht> clientStateBoats = new ConcurrentHashMap<>();
|
||||
|
||||
//CONVERSION CONSTANTS
|
||||
private static final Double MS_TO_KNOTS = 1.94384;
|
||||
@@ -121,7 +119,7 @@ public class ClientPacketParser {
|
||||
*/
|
||||
private static void extractHeartBeat(StreamPacket packet) {
|
||||
long heartbeat = bytesToLong(packet.getPayload());
|
||||
System.out.println("heartbeat = " + heartbeat);
|
||||
System.out.println("[CLIENT] Received heartbeat = " + heartbeat);
|
||||
}
|
||||
|
||||
private static String getTimeZoneString() {
|
||||
@@ -173,8 +171,10 @@ public class ClientPacketParser {
|
||||
if (raceStatus == 4 || raceStatus == 8) {
|
||||
raceFinished = true;
|
||||
raceStarted = false;
|
||||
ClientState.setRaceStarted(false);
|
||||
} else if (!raceStarted) {
|
||||
raceStarted = true;
|
||||
ClientState.setRaceStarted(true);
|
||||
raceFinished = false;
|
||||
}
|
||||
timeSinceStart = timeTillStart;
|
||||
@@ -186,41 +186,35 @@ public class ClientPacketParser {
|
||||
|
||||
int noBoats = payload[22];
|
||||
int raceType = payload[23];
|
||||
clientStateBoats = ClientState.getBoats();
|
||||
for (int i = 0; i < noBoats; i++) {
|
||||
long boatStatusSourceID = bytesToLong(
|
||||
Arrays.copyOfRange(payload, 24 + (i * 20), 28 + (i * 20)));
|
||||
Yacht boat = boats.get((int) boatStatusSourceID);
|
||||
boat.setBoatStatus((int) payload[28 + (i * 20)]);
|
||||
setBoatLegPosition(boat, (int) payload[29 + (i * 20)]);
|
||||
boat.setPenaltiesAwarded((int) payload[30 + (i * 20)]);
|
||||
boat.setPenaltiesServed((int) payload[31 + (i * 20)]);
|
||||
Long estTimeAtNextMark = bytesToLong(
|
||||
int boatStatus = (int) payload[28 + (i * 20)];
|
||||
int boatLegNumber = (int) payload[29 + (i * 20)];
|
||||
int boatPenaltyAwarded = (int) payload[30 + (i * 20)];
|
||||
int boatPenaltyServed = (int) payload[31 + (i * 20)];
|
||||
long estTimeAtNextMark = bytesToLong(
|
||||
Arrays.copyOfRange(payload, 32 + (i * 20), 38 + (i * 20)));
|
||||
boat.setEstimateTimeAtNextMark(estTimeAtNextMark);
|
||||
Long estTimeAtFinish = bytesToLong(
|
||||
long estTimeAtFinish = bytesToLong(
|
||||
Arrays.copyOfRange(payload, 38 + (i * 20), 44 + (i * 20)));
|
||||
|
||||
Yacht boat = boats.get((int) boatStatusSourceID);
|
||||
boat.setBoatStatus((boatStatus));
|
||||
setBoatLegPosition(boat, boatLegNumber);
|
||||
boat.setPenaltiesAwarded(boatPenaltyAwarded);
|
||||
boat.setPenaltiesServed(boatPenaltyServed);
|
||||
boat.setEstimateTimeAtNextMark(estTimeAtNextMark);
|
||||
boat.setEstimateTimeAtFinish(estTimeAtFinish);
|
||||
// boatsPos.put(estTimeAtFinish, boat);
|
||||
// String boatStatus = "SourceID: " + boatStatusSourceID;
|
||||
// boatStatus += "\nBoat Status: " + (int)payload[28 + (i * 20)];
|
||||
// boatStatus += "\nLegNumber: " + (int)payload[29 + (i * 20)];
|
||||
// boatStatus += "\nPenaltiesAwarded: " + (int)payload[29 + (i * 20)];
|
||||
// boatStatus += "\nPenaltiesServed: " + (int)payload[30 + (i * 20)];
|
||||
// boatStatus += "\nEstTimeAtNextMark: " + bytesToLong(Arrays.copyOfRange(payload,31 + (i * 20),37+ (i * 20)));
|
||||
// boatStatus += "\nEstTimeAtFinish: " + bytesToLong(Arrays.copyOfRange(payload,37 + (i * 20),43+ (i * 20)));
|
||||
// boatStatuses.add(boatStatus);
|
||||
|
||||
Yacht clientBoat = clientStateBoats.get((int) boatStatusSourceID);
|
||||
clientBoat.setBoatStatus((boatStatus));
|
||||
setBoatLegPosition(clientBoat, boatLegNumber);
|
||||
clientBoat.setPenaltiesAwarded(boatPenaltyAwarded);
|
||||
clientBoat.setPenaltiesServed(boatPenaltyServed);
|
||||
clientBoat.setEstimateTimeAtNextMark(estTimeAtNextMark);
|
||||
clientBoat.setEstimateTimeAtFinish(estTimeAtFinish);
|
||||
}
|
||||
// if (isRaceStarted()) {
|
||||
// int pos = 1;
|
||||
// for (Yacht yacht : boatsPos.values()) {
|
||||
// yacht.setPosition(String.valueOf(pos));
|
||||
// pos++;
|
||||
// }
|
||||
// } else {
|
||||
// for (Yacht yacht : boatsPos.values()) {
|
||||
// yacht.setPosition("-");
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
private static void setBoatLegPosition(Yacht updatingBoat, Integer leg){
|
||||
@@ -288,9 +282,10 @@ public class ClientPacketParser {
|
||||
xmlObject.constructXML(doc, messageType);
|
||||
if (messageType == 7) { //7 is the boat XML
|
||||
boats = xmlObject.getBoatXML().getCompetingBoats();
|
||||
ClientState.setBoats(xmlObject.getBoatXML().getCompetingBoats());
|
||||
ClientState.setDirtyState(true);
|
||||
}
|
||||
if (messageType == 6) { //6 is race info xml
|
||||
|
||||
newRaceXmlReceived = true;
|
||||
}
|
||||
}
|
||||
@@ -378,7 +373,7 @@ public class ClientPacketParser {
|
||||
//type 1 is a racing yacht and type 3 is a mark, needed for updating positions of the mark and boat
|
||||
if (deviceType == 1){
|
||||
Yacht boat = boats.get((int) boatId);
|
||||
// boat.setVelocity(groundSpeed);
|
||||
boat.setVelocity(groundSpeed);
|
||||
BoatPositionPacket boatPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, heading, groundSpeed);
|
||||
|
||||
//add a new priority que to the boatLocations HashMap
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package seng302.client;
|
||||
|
||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import seng302.models.Yacht;
|
||||
|
||||
/**
|
||||
* Used by the client to store static variables to be used in game.
|
||||
*/
|
||||
public class ClientState {
|
||||
|
||||
private static String hostIp = "";
|
||||
private static Boolean isHost = false;
|
||||
private static Boolean raceStarted = false;
|
||||
private static Boolean connectedToHost = false;
|
||||
private static Map<Integer, Yacht> boats = new ConcurrentHashMap<>();
|
||||
private static Boolean dirtyState = true;
|
||||
private static String clientSourceId = "";
|
||||
|
||||
public static String getHostIp() {
|
||||
return hostIp;
|
||||
}
|
||||
|
||||
public static void setHostIp(String hostIp) {
|
||||
ClientState.hostIp = hostIp;
|
||||
}
|
||||
|
||||
public static Boolean isHost() {
|
||||
return isHost;
|
||||
}
|
||||
|
||||
public static void setHost(Boolean isHost) {
|
||||
ClientState.isHost = isHost;
|
||||
}
|
||||
|
||||
public static Boolean isRaceStarted() {
|
||||
return raceStarted;
|
||||
}
|
||||
|
||||
public static void setRaceStarted(Boolean raceStarted) {
|
||||
ClientState.raceStarted = raceStarted;
|
||||
}
|
||||
|
||||
public static Boolean isConnectedToHost() {
|
||||
return connectedToHost;
|
||||
}
|
||||
|
||||
public static void setConnectedToHost(Boolean connectedToHost) {
|
||||
ClientState.connectedToHost = connectedToHost;
|
||||
}
|
||||
|
||||
public static Map<Integer, Yacht> getBoats() {
|
||||
return boats;
|
||||
}
|
||||
|
||||
public static Boolean isDirtyState() {
|
||||
return dirtyState;
|
||||
}
|
||||
|
||||
public static void setDirtyState(Boolean dirtyState) {
|
||||
ClientState.dirtyState = dirtyState;
|
||||
}
|
||||
|
||||
public static String getClientSourceId() {
|
||||
return clientSourceId;
|
||||
}
|
||||
|
||||
public static void setClientSourceId(String clientSourceId) {
|
||||
ClientState.clientSourceId = clientSourceId;
|
||||
}
|
||||
|
||||
public static void setBoats(Map<Integer, Yacht> boats) {
|
||||
ClientState.boats = boats;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package seng302.client;
|
||||
|
||||
import java.util.Observable;
|
||||
|
||||
/**
|
||||
* Used by LobbyController to run a separate thread-loop
|
||||
* updates the controller when change is detected.
|
||||
*/
|
||||
public class ClientStateQueryingRunnable extends Observable implements Runnable {
|
||||
|
||||
private Boolean terminate = false;
|
||||
|
||||
public ClientStateQueryingRunnable() {}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(!terminate) {
|
||||
// if (ClientState.isRaceStarted() && ClientState.isConnectedToHost()) {
|
||||
// setChanged();
|
||||
// notifyObservers();
|
||||
// }
|
||||
// Sleeping the thread so it will respond to the if statement below
|
||||
// if you know a better fix, pls tell me :) -ryan
|
||||
try {
|
||||
Thread.sleep(0);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (ClientState.isDirtyState()) {
|
||||
setChanged();
|
||||
notifyObservers();
|
||||
ClientState.setDirtyState(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void terminate() {
|
||||
terminate = true;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.Checksum;
|
||||
|
||||
@@ -30,19 +31,16 @@ public class ClientToServerThread implements Runnable {
|
||||
private Boolean updateClient = true;
|
||||
private ByteArrayOutputStream crcBuffer;
|
||||
|
||||
public ClientToServerThread(String ipAddress, Integer portNumber){
|
||||
try {
|
||||
socket = new Socket(ipAddress, portNumber);
|
||||
is = socket.getInputStream();
|
||||
os = socket.getOutputStream();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
public ClientToServerThread(String ipAddress, Integer portNumber) throws Exception{
|
||||
socket = new Socket(ipAddress, portNumber);
|
||||
is = socket.getInputStream();
|
||||
os = socket.getOutputStream();
|
||||
|
||||
Integer allocatedID = threeWayHandshake();
|
||||
if (allocatedID != null) {
|
||||
ourID = allocatedID;
|
||||
clientLog("Successful handshake. Allocated ID: " + ourID, 1);
|
||||
ClientState.setClientSourceId(String.valueOf(ourID));
|
||||
} else {
|
||||
clientLog("Unsuccessful handhsake", 1);
|
||||
closeSocket();
|
||||
@@ -64,7 +62,7 @@ public class ClientToServerThread implements Runnable {
|
||||
int sync1;
|
||||
int sync2;
|
||||
// TODO: 14/07/17 wmu16 - Work out how to fix this while loop
|
||||
while(true) {
|
||||
while(ClientState.isConnectedToHost()) {
|
||||
try {
|
||||
//Perform a write if it is time to as delegated by the MainServerThread
|
||||
if (updateClient) {
|
||||
@@ -99,14 +97,17 @@ public class ClientToServerThread implements Runnable {
|
||||
} else {
|
||||
System.err.println("Packet has been dropped");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
closeSocket();
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
closeSocket();
|
||||
System.out.println("[CLIENT] Disconnected from server");
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user