diff --git a/src/main/java/seng302/client/ClientToServerThread.java b/src/main/java/seng302/client/ClientToServerThread.java index a2f23419..bdf0710b 100644 --- a/src/main/java/seng302/client/ClientToServerThread.java +++ b/src/main/java/seng302/client/ClientToServerThread.java @@ -19,7 +19,6 @@ public class ClientToServerThread extends Thread { private Socket socket; private InputStream is; private OutputStream os; - private final int PORT_NUMBER = 0; private static final int LOG_LEVEL = 1; private Boolean updateClient = true; @@ -36,9 +35,9 @@ public class ClientToServerThread extends Thread { } - static void serverLog(String message, int logLevel){ + static void clientLog(String message, int logLevel){ if(logLevel <= LOG_LEVEL){ - System.out.println("[SERVER] " + message); + System.out.println("[CLIENT] " + message); } } @@ -98,6 +97,7 @@ public class ClientToServerThread extends Thread { try { os.write(boatActionMessage.getBuffer()); } catch (IOException e) { + clientLog("COULD NOT WRITE TO SERVER", 0); e.printStackTrace(); } } diff --git a/src/main/java/seng302/gameServer/GameState.java b/src/main/java/seng302/gameServer/GameState.java index f6ff2bb7..879fc8e9 100644 --- a/src/main/java/seng302/gameServer/GameState.java +++ b/src/main/java/seng302/gameServer/GameState.java @@ -1,11 +1,9 @@ package seng302.gameServer; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; + import seng302.models.Player; -import java.util.ArrayList; import seng302.models.Yacht; import seng302.server.messages.BoatActionType; @@ -15,8 +13,6 @@ import seng302.server.messages.BoatActionType; */ public class GameState { - private static final Integer HEADING_STEP = 3; - private static Long previousUpdateTime; public static Double windDirection; private static Double windSpeed; @@ -30,6 +26,8 @@ public class GameState { public GameState(String hostIpAddress) { windDirection = 170d; windSpeed = 0d; + yachts = new HashMap<>(); + players = new ArrayList<>(); GameState.hostIpAddress = hostIpAddress; @@ -56,8 +54,8 @@ public class GameState { players.remove(player); } - public static void addYacht(Integer sourceId, Yacht yatch) { - yachts.put(sourceId, yatch); + public static void addYacht(Integer sourceId, Yacht yacht) { + yachts.put(sourceId, yacht); } public static Boolean getIsRaceStarted() { @@ -82,25 +80,38 @@ public class GameState { public static void updateBoat(Integer sourceId, BoatActionType actionType) { Yacht playerYacht = yachts.get(sourceId); + System.out.println("-----------------------"); switch (actionType) { case VMG: + System.out.println("Snapping to VMG"); + // TODO: 22/07/17 wmu16 - Add in the vmg calculation code here break; case SAILS_IN: playerYacht.toggleSailIn(); + System.out.println("Toggling Sails"); break; case SAILS_OUT: playerYacht.toggleSailIn(); + System.out.println("Toggling Sails"); break; case TACK_GYBE: playerYacht.tackGybe(windDirection); + System.out.println("Tack/Gybe"); break; case UPWIND: playerYacht.turnUpwind(); + System.out.println("Moving upwind"); break; case DOWNWIND: playerYacht.turnDownwind(); + System.out.println("Moving downwind"); break; } + + System.out.println("-----------------------"); + System.out.println("Heading: " + playerYacht.getHeading()); + System.out.println("Sails are in: " + playerYacht.getSailIn()); + System.out.println("-----------------------\n"); } public static void update() { diff --git a/src/main/java/seng302/gameServer/ServerToClientThread.java b/src/main/java/seng302/gameServer/ServerToClientThread.java index 9a7d8786..69851682 100644 --- a/src/main/java/seng302/gameServer/ServerToClientThread.java +++ b/src/main/java/seng302/gameServer/ServerToClientThread.java @@ -2,7 +2,6 @@ package seng302.gameServer; import java.util.Random; -import seng302.client.ClientPacketParser; import seng302.models.Player; import seng302.models.Yacht; import seng302.models.stream.packets.PacketType; @@ -22,6 +21,8 @@ import seng302.utilities.GeoPoint; * Created by wmu16 on 13/07/17. */ public class ServerToClientThread extends Thread { + + private static final Integer LOG_LEVEL = 1; private static final Integer MAX_ID_ATTEMPTS = 10; private InputStream is; @@ -51,6 +52,13 @@ public class ServerToClientThread extends Thread { GameState.addYacht(sourceId, new Yacht("Kappa", "Kap", new GeoPoint(0.0, 0.0), 0.0)); } + + static void serverLog(String message, int logLevel){ + if(logLevel <= LOG_LEVEL){ + System.out.println("[SERVER] " + message); + } + } + public void run() { int sync1; int sync2; @@ -100,6 +108,7 @@ public class ServerToClientThread extends Thread { } } } catch (Exception e) { + serverLog("ERROR OCCURED, CLOSING SERVER CONNETION: " + socket.getRemoteSocketAddress().toString(), 1); closeSocket(); return; } diff --git a/src/main/java/seng302/models/Yacht.java b/src/main/java/seng302/models/Yacht.java index 02bae325..fb8a86b0 100644 --- a/src/main/java/seng302/models/Yacht.java +++ b/src/main/java/seng302/models/Yacht.java @@ -21,13 +21,39 @@ public class Yacht { private final Double TURN_STEP = 3.0; + private Double lastHeading; + private Boolean sailIn; + + + // Used in boat group + private Color colour; + + private String boatType; private Integer sourceID; + private String hullID; //matches HullNum in the XML spec. + private String shortName; + private String boatName; + private String country; + + // Situational data + + + // Boat status + private Integer boatStatus; + private Integer legNumber; + private Integer penaltiesAwarded; + private Integer penaltiesServed; + private Long estimateTimeAtFinish; + private String position; private GeoPoint location; private Double heading; - private Double lastHeading; private Double velocity; + private Long timeTillNext; + private Long markRoundTime; - private Boolean sailIn; + // Mark rounding + private Mark lastMarkRounded; + private Mark nextMark; /** @@ -41,6 +67,48 @@ public class Yacht { this.sailIn = false; } + + /** + * Used in EventTest and RaceTest. + * + * @param boatName Create a yacht object with name. + */ + public Yacht(String boatName, String shortName, GeoPoint location, Double heading) { + this.boatName = boatName; + this.shortName = shortName; + this.location = location; + this.heading = heading; + this.velocity = 0.0; + this.sailIn = false; + } + + /** + * Used in BoatGroupTest. + * + * @param boatName The name of the team sailing the boat + * @param boatVelocity The speed of the boat in meters/second + * @param shortName A shorter version of the teams name + */ + public Yacht(String boatName, double boatVelocity, String shortName, int id) { + this.boatName = boatName; + this.velocity = boatVelocity; + this.shortName = shortName; + this.sourceID = id; + this.sailIn = false; + } + + public Yacht(String boatType, Integer sourceID, String hullID, String shortName, + String boatName, String country) { + this.boatType = boatType; + this.sourceID = sourceID; + this.hullID = hullID; + this.shortName = shortName; + this.boatName = boatName; + this.country = country; + this.position = "-"; + this.sailIn = false; + } + /** * @param timeInterval since last update in milliseconds */ @@ -122,4 +190,139 @@ public class Yacht { adjustHeading(-TURN_STEP); } } + + + public String getBoatType() { + return boatType; + } + + public Integer getSourceID() { + return sourceID; + } + + public String getHullID() { + return hullID; + } + + public String getShortName() { + return shortName; + } + + public String getBoatName() { + return boatName; + } + + public String getCountry() { + return country; + } + + public Integer getBoatStatus() { + return boatStatus; + } + + public void setBoatStatus(Integer boatStatus) { + this.boatStatus = boatStatus; + } + + public Integer getLegNumber() { + return legNumber; + } + + public void setLegNumber(Integer legNumber) { + if (colour != null && position != "-" && legNumber != this.legNumber&& RaceViewController.sparkLineStatus(sourceID)) { + RaceViewController.updateYachtPositionSparkline(this, legNumber); + } + this.legNumber = legNumber; + } + + public Integer getPenaltiesAwarded() { + return penaltiesAwarded; + } + + public void setPenaltiesAwarded(Integer penaltiesAwarded) { + this.penaltiesAwarded = penaltiesAwarded; + } + + public Integer getPenaltiesServed() { + return penaltiesServed; + } + + public void setPenaltiesServed(Integer penaltiesServed) { + this.penaltiesServed = penaltiesServed; + } + + public void setEstimateTimeAtNextMark(Long estimateTimeAtNextMark) { + timeTillNext = estimateTimeAtNextMark; + } + + public String getEstimateTimeAtFinish() { + DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); + return format.format(estimateTimeAtFinish); + } + + public void setEstimateTimeAtFinish(Long estimateTimeAtFinish) { + this.estimateTimeAtFinish = estimateTimeAtFinish; + } + + public String getPosition() { + return position; + } + + public void setPosition(String position) { + this.position = position; + } + + public Color getColour() { + return colour; + } + + public void setColour(Color colour) { + this.colour = colour; + } + + public void setVelocity(double velocity) { + this.velocity = velocity; + } + + + public void setMarkRoundingTime(Long markRoundingTime) { + this.markRoundTime = markRoundingTime; + } + + public double getVelocity() { + return velocity; + } + + public Long getTimeTillNext() { + return timeTillNext; + } + + public Long getMarkRoundTime() { + return markRoundTime; + } + + public Mark getLastMarkRounded() { + return lastMarkRounded; + } + + public void setLastMarkRounded(Mark lastMarkRounded) { + this.lastMarkRounded = lastMarkRounded; + } + + public void setNextMark(Mark nextMark) { + this.nextMark = nextMark; + } + + public Mark getNextMark(){ + return nextMark; + } + + public Boolean getSailIn() { + return sailIn; + } + + @Override + public String toString() { + return boatName; + } }