mirror of
https://github.com/OpenLiberty/liberty-bikes.git
synced 2025-01-18 10:23:58 +08:00
Merge pull request #244 from aguibert/ai-defensive-coding
More defensive checks for game server
This commit is contained in:
commit
888016a1e4
@ -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) {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user