mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Started implementing the gameState broadcasts.
Initial xml files are almost broad casted, just need to create them (or import the for the regatta). Started the setup for sending boat location packets, should work once we get at least the boat xml working when being sent. #story[1047]
This commit is contained in:
@@ -321,7 +321,7 @@ public class CanvasController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Yacht boat : boats.values()) {
|
for (Yacht boat : boats.values()) {
|
||||||
if (participantIDs.contains(boat.getSourceId())) {
|
if (participantIDs.contains(boat.getSourceID())) {
|
||||||
boat.setColour(Colors.getColor());
|
boat.setColour(Colors.getColor());
|
||||||
BoatGroup boatGroup = new BoatGroup(boat, boat.getColour());
|
BoatGroup boatGroup = new BoatGroup(boat, boat.getColour());
|
||||||
boatGroups.add(boatGroup);
|
boatGroups.add(boatGroup);
|
||||||
|
|||||||
@@ -316,7 +316,7 @@ public class BoatGroup extends Group {
|
|||||||
* @return An array containing all ID's associated with this RaceObject.
|
* @return An array containing all ID's associated with this RaceObject.
|
||||||
*/
|
*/
|
||||||
public long getRaceId() {
|
public long getRaceId() {
|
||||||
return boat.getSourceId();
|
return boat.getSourceID();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Group getWake () {
|
public Group getWake () {
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
|
|||||||
thereAreBoatsNotFinished = true;
|
thereAreBoatsNotFinished = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BoatSubMessage m = new BoatSubMessage(y.getSourceId(), boatStatus, y.getLastMarkRounded().getId(), 0, 0, 1234l, 1234l);
|
BoatSubMessage m = new BoatSubMessage(y.getSourceID(), boatStatus, y.getLastMarkRounded().getId(), 0, 0, 1234l, 1234l);
|
||||||
boatSubMessages.add(m);
|
boatSubMessages.add(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,11 +223,11 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
|
|||||||
server = ServerSocketChannel.open();
|
server = ServerSocketChannel.open();
|
||||||
server.socket().bind(new InetSocketAddress("localhost", PORT_NUMBER));
|
server.socket().bind(new InetSocketAddress("localhost", PORT_NUMBER));
|
||||||
|
|
||||||
serverListenThread = new ServerListenThread(server, this);
|
// serverListenThread = new ServerListenThread(server, this);
|
||||||
heartbeatThread = new HeartbeatThread(this);
|
heartbeatThread = new HeartbeatThread(this);
|
||||||
|
|
||||||
heartbeatThread.start();
|
heartbeatThread.start();
|
||||||
serverListenThread.start();
|
// serverListenThread.start();
|
||||||
}
|
}
|
||||||
catch (IOException e){
|
catch (IOException e){
|
||||||
serverLog("Failed to bind socket: " + e.getMessage(), 0);
|
serverLog("Failed to bind socket: " + e.getMessage(), 0);
|
||||||
@@ -286,7 +286,6 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
|
|||||||
* A client has tried to connect to the server
|
* A client has tried to connect to the server
|
||||||
* @param player The player that connected
|
* @param player The player that connected
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public void clientConnected(Player player) {
|
public void clientConnected(Player player) {
|
||||||
if (GameState.getPlayers().size() < MAX_NUM_PLAYERS && GameState.getCurrentStage() == GameStages.LOBBYING) {
|
if (GameState.getPlayers().size() < MAX_NUM_PLAYERS && GameState.getCurrentStage() == GameStages.LOBBYING) {
|
||||||
serverLog("Player Connected", 0);
|
serverLog("Player Connected", 0);
|
||||||
@@ -295,6 +294,11 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clientConnected(ServerToClientThread serverToClientThread) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A player has left the game, remove the player from the GameState
|
* A player has left the game, remove the player from the GameState
|
||||||
* @param player The player that left
|
* @param player The player that left
|
||||||
@@ -310,7 +314,7 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
|
|||||||
void broadcast(Message message) throws IOException{
|
void broadcast(Message message) throws IOException{
|
||||||
for(Player player : GameState.getPlayers()) {
|
for(Player player : GameState.getPlayers()) {
|
||||||
//heh
|
//heh
|
||||||
player.getSocketChannel().socket().getOutputStream().write(message.getBuffer());
|
player.getSocket().getOutputStream().write(message.getBuffer());
|
||||||
}
|
}
|
||||||
seqNum++;
|
seqNum++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package seng302.gameServer;
|
package seng302.gameServer;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import seng302.models.Player;
|
import seng302.models.Player;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import seng302.models.Yacht;
|
import seng302.models.Yacht;
|
||||||
import seng302.server.messages.BoatActionType;
|
import seng302.server.messages.BoatActionType;
|
||||||
|
|
||||||
@@ -30,6 +28,7 @@ public class GameState {
|
|||||||
players = new ArrayList<>();
|
players = new ArrayList<>();
|
||||||
currentStage = GameStages.LOBBYING;
|
currentStage = GameStages.LOBBYING;
|
||||||
isRaceStarted = false;
|
isRaceStarted = false;
|
||||||
|
yachts = new HashMap<>();
|
||||||
//set this when game stage changes to prerace
|
//set this when game stage changes to prerace
|
||||||
previousUpdateTime = System.currentTimeMillis();
|
previousUpdateTime = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
@@ -74,6 +73,10 @@ public class GameState {
|
|||||||
return windSpeed;
|
return windSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Map<Integer, Yacht> getYachts() {
|
||||||
|
return yachts;
|
||||||
|
}
|
||||||
|
|
||||||
public static void updateBoat(Integer sourceId, BoatActionType actionType) {
|
public static void updateBoat(Integer sourceId, BoatActionType actionType) {
|
||||||
switch (actionType) {
|
switch (actionType) {
|
||||||
case VMG:
|
case VMG:
|
||||||
@@ -92,6 +95,7 @@ public class GameState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void update() {
|
public static void update() {
|
||||||
|
|
||||||
Long timeInterval = System.currentTimeMillis() - previousUpdateTime;
|
Long timeInterval = System.currentTimeMillis() - previousUpdateTime;
|
||||||
previousUpdateTime = System.currentTimeMillis();
|
previousUpdateTime = System.currentTimeMillis();
|
||||||
for (Yacht yacht : yachts.values()) {
|
for (Yacht yacht : yachts.values()) {
|
||||||
|
|||||||
@@ -59,13 +59,15 @@ public class MainServerThread extends Thread implements PacketBufferDelegate, Cl
|
|||||||
|
|
||||||
if (GameState.getCurrentStage() == GameStages.PRE_RACE) {
|
if (GameState.getCurrentStage() == GameStages.PRE_RACE) {
|
||||||
GameState.update();
|
GameState.update();
|
||||||
|
updateClients();
|
||||||
}
|
}
|
||||||
|
|
||||||
//RACING
|
//RACING
|
||||||
if (GameState.getCurrentStage() == GameStages.RACING) {
|
if (GameState.getCurrentStage() == GameStages.RACING) {
|
||||||
GameState.update();
|
GameState.update();
|
||||||
|
updateClients();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//FINISHED
|
//FINISHED
|
||||||
else if (GameState.getCurrentStage() == GameStages.FINISHED) {
|
else if (GameState.getCurrentStage() == GameStages.FINISHED) {
|
||||||
|
|
||||||
@@ -115,13 +117,6 @@ public class MainServerThread extends Thread implements PacketBufferDelegate, Cl
|
|||||||
return packetBuffer.add(streamPacket);
|
return packetBuffer.add(streamPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeRace(){
|
|
||||||
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
|
|
||||||
serverToClientThread.updateClient();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A client has tried to connect to the server
|
* A client has tried to connect to the server
|
||||||
* @param serverToClientThread The player that connected
|
* @param serverToClientThread The player that connected
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ public class ServerListenThread extends Thread{
|
|||||||
Socket thisClient = serverSocket.accept();
|
Socket thisClient = serverSocket.accept();
|
||||||
if (thisClient != null){
|
if (thisClient != null){
|
||||||
ServerToClientThread thisConnection = new ServerToClientThread(thisClient);
|
ServerToClientThread thisConnection = new ServerToClientThread(thisClient);
|
||||||
|
thisConnection.initialiseRace();
|
||||||
thisConnection.start();
|
thisConnection.start();
|
||||||
delegate.clientConnected(thisConnection);
|
delegate.clientConnected(thisConnection);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
package seng302.gameServer;
|
package seng302.gameServer;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import seng302.client.ClientPacketParser;
|
import seng302.client.ClientPacketParser;
|
||||||
import seng302.models.Player;
|
import seng302.models.Player;
|
||||||
import seng302.models.Yacht;
|
import seng302.models.Yacht;
|
||||||
import seng302.models.stream.packets.PacketType;
|
import seng302.models.stream.packets.PacketType;
|
||||||
import seng302.models.stream.packets.StreamPacket;
|
import seng302.models.stream.packets.StreamPacket;
|
||||||
import seng302.server.messages.ChatterMessage;
|
import seng302.models.xml.XMLGenerator;
|
||||||
import seng302.server.messages.Heartbeat;
|
import seng302.server.messages.*;
|
||||||
import seng302.server.messages.BoatActionType;
|
|
||||||
import seng302.server.messages.Message;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
@@ -35,9 +34,13 @@ public class ServerToClientThread extends Thread {
|
|||||||
private Boolean userIdentified = false;
|
private Boolean userIdentified = false;
|
||||||
private Boolean connected = true;
|
private Boolean connected = true;
|
||||||
private Boolean updateClient = true;
|
private Boolean updateClient = true;
|
||||||
|
private Boolean intiialisedRace = false;
|
||||||
|
|
||||||
|
private Integer seqNo;
|
||||||
private Integer sourceId;
|
private Integer sourceId;
|
||||||
|
|
||||||
|
private XMLGenerator xml;
|
||||||
|
|
||||||
public ServerToClientThread(Socket socket) {
|
public ServerToClientThread(Socket socket) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
try {
|
try {
|
||||||
@@ -51,6 +54,7 @@ public class ServerToClientThread extends Thread {
|
|||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
sourceId = rand.nextInt(100000);
|
sourceId = rand.nextInt(100000);
|
||||||
GameState.addYacht(sourceId, new Yacht("Kappa", "Kap", new GeoPoint(0.0, 0.0), 0.0));
|
GameState.addYacht(sourceId, new Yacht("Kappa", "Kap", new GeoPoint(0.0, 0.0), 0.0));
|
||||||
|
seqNo = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -61,12 +65,16 @@ public class ServerToClientThread extends Thread {
|
|||||||
//System.out.print(".");
|
//System.out.print(".");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (intiialisedRace) {
|
||||||
|
sendSetupMessages();
|
||||||
|
}
|
||||||
|
|
||||||
//Perform a write if it is time to as delegated by the MainServerThread
|
//Perform a write if it is time to as delegated by the MainServerThread
|
||||||
if (updateClient) {
|
if (updateClient) {
|
||||||
// TODO: 13/07/17 wmu16 - Write out game state - some function that would write all appropriate messages to this output stream
|
// TODO: 13/07/17 wmu16 - Write out game state - some function that would write all appropriate messages to this output stream
|
||||||
ChatterMessage chatterMessage = new ChatterMessage(4, 14, "Hello, it's me");
|
ChatterMessage chatterMessage = new ChatterMessage(4, 14, "Hello, it's me");
|
||||||
sendMessage(chatterMessage);
|
sendMessage(chatterMessage);
|
||||||
|
sendBoatLocationPackets();
|
||||||
// try {
|
// try {
|
||||||
// GameState.outputState(os);
|
// GameState.outputState(os);
|
||||||
// } catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
@@ -115,6 +123,16 @@ public class ServerToClientThread extends Thread {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendSetupMessages() {
|
||||||
|
xml = new XMLGenerator();
|
||||||
|
XMLMessage xmlMessage = new XMLMessage(xml.getRegattaAsXml(), XMLMessageSubType.REGATTA, xml.getRegattaAsXml().length());
|
||||||
|
sendMessage(xmlMessage);
|
||||||
|
xmlMessage = new XMLMessage(xml.getBoatsAsXml(), XMLMessageSubType.BOAT, xml.getBoatsAsXml().length());
|
||||||
|
sendMessage(xmlMessage);
|
||||||
|
xmlMessage = new XMLMessage(xml.getRaceAsXml(), XMLMessageSubType.RACE, xml.getRaceAsXml().length());
|
||||||
|
sendMessage(xmlMessage);
|
||||||
|
}
|
||||||
|
|
||||||
public void updateClient() {
|
public void updateClient() {
|
||||||
updateClient = true;
|
updateClient = true;
|
||||||
}
|
}
|
||||||
@@ -156,6 +174,11 @@ public class ServerToClientThread extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void initialiseRace(){
|
||||||
|
intiialisedRace = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private int readByte() throws Exception {
|
private int readByte() throws Exception {
|
||||||
int currentByte = -1;
|
int currentByte = -1;
|
||||||
try {
|
try {
|
||||||
@@ -192,4 +215,18 @@ public class ServerToClientThread extends Thread {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getSeqNo(){
|
||||||
|
seqNo++;
|
||||||
|
return seqNo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void sendBoatLocationPackets(){
|
||||||
|
ArrayList<Yacht> yachts = new ArrayList<>(GameState.getYachts().values());
|
||||||
|
for (Yacht yacht: yachts){
|
||||||
|
BoatLocationMessage boatLocationMessage = new BoatLocationMessage(sourceId,getSeqNo(), yacht.getLocation().getLat(), yacht.getLocation().getLng(), yacht.getHeading(), (long) yacht.getVelocity());
|
||||||
|
sendMessage(boatLocationMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -252,4 +252,11 @@ public class Yacht {
|
|||||||
return boatName;
|
return boatName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GeoPoint getLocation() {
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getHeading() {
|
||||||
|
return heading;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -558,7 +558,7 @@ public class XMLParser {
|
|||||||
getNodeAttributeString(currentBoat, "Country"));
|
getNodeAttributeString(currentBoat, "Country"));
|
||||||
this.boats.add(boat);
|
this.boats.add(boat);
|
||||||
if (boat.getBoatType().equals("Yacht")) {
|
if (boat.getBoatType().equals("Yacht")) {
|
||||||
competingBoats.put(boat.getSourceId(), boat);
|
competingBoats.put(boat.getSourceID(), boat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user