Merge pull request #244 from aguibert/ai-defensive-coding

More defensive checks for game server
This commit is contained in:
Andrew Guibert 2019-10-09 20:36:02 -05:00 committed by GitHub
commit 888016a1e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -76,6 +76,7 @@ public class GameRound implements Runnable {
private final Deque<Player> playerRanks = new ArrayDeque<>();
private final Set<LifecycleCallback> lifecycleCallbacks = new HashSet<>();
private final int GAME_TICK_SPEED, MAX_TIME_BETWEEN_ROUNDS;
private volatile boolean broadcastPlayerList = true;
private LobbyCountdown lobbyCountdown;
private AtomicBoolean lobbyCountdownStarted = new AtomicBoolean();
@ -151,6 +152,7 @@ public class GameRound implements Runnable {
if (c == null)
return false;
c.player.ifPresent((p) -> p.setDirection(msg.direction));
broadcastPlayerList = true;
return true;
}
@ -223,6 +225,7 @@ public class GameRound implements Runnable {
public void addSpectator(Session s) {
log("A spectator has joined.");
broadcastPlayerList = true;
clients.put(s, new Client(s));
sendToClient(s, new OutboundMessage.PlayerList(getPlayers()));
sendToClient(s, board);
@ -475,6 +478,7 @@ public class GameRound implements Runnable {
private void broadcastPlayerList() {
sendToClients(getNonMobileSessions(), new OutboundMessage.PlayerList(getPlayers()));
broadcastPlayerList = false;
}
private void checkForWinner() {
@ -591,7 +595,8 @@ public class GameRound implements Runnable {
for (int i = 0; i < (STARTING_COUNTDOWN * 4); i++) {
delay(250);
broadcastPlayerList();
if (broadcastPlayerList)
broadcastPlayerList();
}
paused.set(false);
@ -623,7 +628,8 @@ public class GameRound implements Runnable {
while (isOpen() || gameState == State.FULL) {
for (int i = 0; i < 4; i++) {
delay(250);
broadcastPlayerList();
if (broadcastPlayerList)
broadcastPlayerList();
}
roundStartCountdown--;
if (roundStartCountdown < 1) {

View File

@ -5,6 +5,8 @@ package org.libertybikes.game.round.service;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
@ -130,9 +132,10 @@ public class GameRoundWebsocket {
public static void sendToClient(Session client, Object message) {
if (client != null) {
String msg = message instanceof String ? (String) message : jsonb.toJson(message);
Future<Void> f = client.getAsyncRemote().sendText(msg);
try {
client.getBasicRemote().sendText(msg);
} catch (IOException e) {
f.get(50, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
}