Slight improvements to hosting.

Allow a host/client to disconnect and reconnect/make lobby, leave lobby and play the game.

#pair[kre39,hyi25] #story[1047]
This commit is contained in:
Kusal Ekanayake
2017-07-27 12:45:22 +12:00
parent 34704bd93d
commit 870dc07fd2
8 changed files with 64 additions and 28 deletions
@@ -33,10 +33,6 @@ public class GameState implements Runnable {
public GameState(String hostIpAddress) {
windDirection = 180d;
windSpeed = 10000d;
yachts = new HashMap<>();
players = new ArrayList<>();
this.hostIpAddress = hostIpAddress;
players = new ArrayList<>();
currentStage = GameStages.LOBBYING;
@@ -25,6 +25,7 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
private static final int PORT = 4942;
private static final Integer CLIENT_UPDATES_PER_SECOND = 10;
private static final int LOG_LEVEL = 1;
private boolean terminated;
private Thread thread;
@@ -38,6 +39,7 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
serverLog("IO error in server thread handler upon trying to make new server socket", 0);
}
terminated = false;
thread = new Thread(this);
thread.start();
}
@@ -54,7 +56,7 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
serverListenThread.start();
//You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app.
while (!thread.isInterrupted()) {
while (!terminated) {
try {
Thread.sleep(1000 / CLIENT_UPDATES_PER_SECOND);
} catch (InterruptedException e) {
@@ -148,4 +150,8 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
}
}, 0, 500);
}
public void terminate() {
terminated = true;
}
}