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:
Kusal Ekanayake
2017-07-21 13:16:43 +12:00
parent 2fff73c075
commit 5df7efda03
9 changed files with 73 additions and 25 deletions
@@ -321,7 +321,7 @@ public class CanvasController {
}
for (Yacht boat : boats.values()) {
if (participantIDs.contains(boat.getSourceId())) {
if (participantIDs.contains(boat.getSourceID())) {
boat.setColour(Colors.getColor());
BoatGroup boatGroup = new BoatGroup(boat, boat.getColour());
boatGroups.add(boatGroup);
@@ -316,7 +316,7 @@ public class BoatGroup extends Group {
* @return An array containing all ID's associated with this RaceObject.
*/
public long getRaceId() {
return boat.getSourceId();
return boat.getSourceID();
}
public Group getWake () {
@@ -96,7 +96,7 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
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);
}
@@ -223,11 +223,11 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
server = ServerSocketChannel.open();
server.socket().bind(new InetSocketAddress("localhost", PORT_NUMBER));
serverListenThread = new ServerListenThread(server, this);
// serverListenThread = new ServerListenThread(server, this);
heartbeatThread = new HeartbeatThread(this);
heartbeatThread.start();
serverListenThread.start();
// serverListenThread.start();
}
catch (IOException e){
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
* @param player The player that connected
*/
@Override
public void clientConnected(Player player) {
if (GameState.getPlayers().size() < MAX_NUM_PLAYERS && GameState.getCurrentStage() == GameStages.LOBBYING) {
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
* @param player The player that left
@@ -310,7 +314,7 @@ public class GameServerThread implements Runnable, Observer, ClientConnectionDel
void broadcast(Message message) throws IOException{
for(Player player : GameState.getPlayers()) {
//heh
player.getSocketChannel().socket().getOutputStream().write(message.getBuffer());
player.getSocket().getOutputStream().write(message.getBuffer());
}
seqNum++;
}
@@ -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;
@@ -30,6 +28,7 @@ public class GameState {
players = new ArrayList<>();
currentStage = GameStages.LOBBYING;
isRaceStarted = false;
yachts = new HashMap<>();
//set this when game stage changes to prerace
previousUpdateTime = System.currentTimeMillis();
}
@@ -74,6 +73,10 @@ public class GameState {
return windSpeed;
}
public static Map<Integer, Yacht> getYachts() {
return yachts;
}
public static void updateBoat(Integer sourceId, BoatActionType actionType) {
switch (actionType) {
case VMG:
@@ -92,6 +95,7 @@ public class GameState {
}
public static void update() {
Long timeInterval = System.currentTimeMillis() - previousUpdateTime;
previousUpdateTime = System.currentTimeMillis();
for (Yacht yacht : yachts.values()) {
@@ -59,13 +59,15 @@ public class MainServerThread extends Thread implements PacketBufferDelegate, Cl
if (GameState.getCurrentStage() == GameStages.PRE_RACE) {
GameState.update();
updateClients();
}
//RACING
if (GameState.getCurrentStage() == GameStages.RACING) {
GameState.update();
updateClients();
}
//FINISHED
else if (GameState.getCurrentStage() == GameStages.FINISHED) {
@@ -115,13 +117,6 @@ public class MainServerThread extends Thread implements PacketBufferDelegate, Cl
return packetBuffer.add(streamPacket);
}
private void initializeRace(){
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
serverToClientThread.updateClient();
}
}
/**
* A client has tried to connect to the server
* @param serverToClientThread The player that connected
@@ -29,6 +29,7 @@ public class ServerListenThread extends Thread{
Socket thisClient = serverSocket.accept();
if (thisClient != null){
ServerToClientThread thisConnection = new ServerToClientThread(thisClient);
thisConnection.initialiseRace();
thisConnection.start();
delegate.clientConnected(thisConnection);
}
@@ -1,16 +1,15 @@
package seng302.gameServer;
import java.util.ArrayList;
import java.util.Random;
import seng302.client.ClientPacketParser;
import seng302.models.Player;
import seng302.models.Yacht;
import seng302.models.stream.packets.PacketType;
import seng302.models.stream.packets.StreamPacket;
import seng302.server.messages.ChatterMessage;
import seng302.server.messages.Heartbeat;
import seng302.server.messages.BoatActionType;
import seng302.server.messages.Message;
import seng302.models.xml.XMLGenerator;
import seng302.server.messages.*;
import java.io.*;
import java.net.Socket;
@@ -35,9 +34,13 @@ public class ServerToClientThread extends Thread {
private Boolean userIdentified = false;
private Boolean connected = true;
private Boolean updateClient = true;
private Boolean intiialisedRace = false;
private Integer seqNo;
private Integer sourceId;
private XMLGenerator xml;
public ServerToClientThread(Socket socket) {
this.socket = socket;
try {
@@ -51,6 +54,7 @@ public class ServerToClientThread extends Thread {
Random rand = new Random();
sourceId = rand.nextInt(100000);
GameState.addYacht(sourceId, new Yacht("Kappa", "Kap", new GeoPoint(0.0, 0.0), 0.0));
seqNo = 0;
}
public void run() {
@@ -61,12 +65,16 @@ public class ServerToClientThread extends Thread {
//System.out.print(".");
try {
if (intiialisedRace) {
sendSetupMessages();
}
//Perform a write if it is time to as delegated by the MainServerThread
if (updateClient) {
// 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");
sendMessage(chatterMessage);
sendBoatLocationPackets();
// try {
// GameState.outputState(os);
// } 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() {
updateClient = true;
}
@@ -156,6 +174,11 @@ public class ServerToClientThread extends Thread {
}
public void initialiseRace(){
intiialisedRace = true;
}
private int readByte() throws Exception {
int currentByte = -1;
try {
@@ -192,4 +215,18 @@ public class ServerToClientThread extends Thread {
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);
}
}
}
+7
View File
@@ -252,4 +252,11 @@ public class Yacht {
return boatName;
}
public GeoPoint getLocation() {
return location;
}
public Double getHeading() {
return heading;
}
}
@@ -558,7 +558,7 @@ public class XMLParser {
getNodeAttributeString(currentBoat, "Country"));
this.boats.add(boat);
if (boat.getBoatType().equals("Yacht")) {
competingBoats.put(boat.getSourceId(), boat);
competingBoats.put(boat.getSourceID(), boat);
}
}
}