Merge pull request #57 from aguibert/preferred-player-slots

Preferred player slots
This commit is contained in:
Andrew Guibert 2018-04-02 14:07:48 -05:00 committed by GitHub
commit cf468fb681
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 9 deletions

View File

@ -5,7 +5,9 @@ package org.libertybikes.game.core;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.json.bind.annotation.JsonbTransient;
@ -15,6 +17,7 @@ public class GameBoard {
public static final int BOARD_SIZE = 121;
public static final short SPOT_AVAILABLE = 0, TRAIL_SPOT_TAKEN = -10, OBJECT_SPOT_TAKEN = -8, PLAYER_SPOT_TAKEN = 1;
private static final Map<String, Short> preferredPlayerSlots = new ConcurrentHashMap<>();
@JsonbTransient
public final short[][] board = new short[BOARD_SIZE][BOARD_SIZE];
@ -58,16 +61,28 @@ public class GameBoard {
public Player addPlayer(String playerId, String playerName) {
// Find first open player slot to fill, which determines position
short playerNum = -1;
for (short i = 0; i < takenPlayerSlots.length; i++) {
if (!takenPlayerSlots[i]) {
playerNum = i;
takenPlayerSlots[i] = true;
System.out.println("Player slot " + i + " taken");
break;
// Try to keep players in the same slots across rounds if possible
Short preferredSlot = preferredPlayerSlots.get(playerId);
if (preferredSlot != null && !takenPlayerSlots[preferredSlot]) {
playerNum = preferredSlot;
} else {
// Find first open player slot to fill, which determines position
for (short i = 0; i < takenPlayerSlots.length; i++) {
if (!takenPlayerSlots[i]) {
playerNum = i;
break;
}
}
preferredPlayerSlots.put(playerId, playerNum);
}
takenPlayerSlots[playerNum] = true;
System.out.println("Player slot " + playerNum + " taken");
// Don't let the preferred player slot map take up too much memory
if (preferredPlayerSlots.size() > 1000)
preferredPlayerSlots.clear();
// Initialize Player
Player p = new Player(playerId, playerName, playerNum);

View File

@ -40,15 +40,17 @@ public class GameRoundWebsocket {
@OnOpen
public void onOpen(@PathParam("roundId") String roundId, Session session) {
log(roundId, "Opened a session");
checkIdleTimeout(session, roundId);
}
@OnClose
public void onClose(@PathParam("roundId") String roundId, Session peer) {
public void onClose(@PathParam("roundId") String roundId, Session session) {
checkIdleTimeout(session, roundId);
log(roundId, "Closed a session");
try {
GameRound round = gameSvc.getRound(roundId);
if (round != null)
if (round.removeClient(peer) == 0)
if (round.removeClient(session) == 0)
gameSvc.deleteRound(roundId);
} catch (Exception e) {
e.printStackTrace();
@ -57,6 +59,7 @@ public class GameRoundWebsocket {
@OnMessage
public void onMessage(@PathParam("roundId") final String roundId, String message, Session session) {
checkIdleTimeout(session, roundId);
try {
final InboundMessage msg = jsonb.fromJson(message, InboundMessage.class);
final GameRound round = gameSvc.getRound(roundId);
@ -116,4 +119,15 @@ public class GameRoundWebsocket {
System.out.println("[websocket-" + roundId + "] " + msg);
}
// TODO: remove this method once we find out if session timeout is being altered on cloud env
private static void checkIdleTimeout(Session session, String roundId) {
// See if we can catch a reason why sessions timeout in cloud env
if (session.getMaxIdleTimeout() > 0) {
log(roundId, "WARNING: Session idle timeout is: " + session.getMaxIdleTimeout());
}
if (session.getContainer().getDefaultMaxSessionIdleTimeout() > 0) {
log(roundId, "WARNING: Default session idle timeout is: " + session.getContainer().getDefaultMaxSessionIdleTimeout());
}
}
}