From 4922683e11ef42cc065ff740e94f6ac0f7128f86 Mon Sep 17 00:00:00 2001 From: Zelytra Date: Fri, 5 Jul 2024 22:50:37 +0200 Subject: [PATCH] Offline player (#55) --- .../manager/socket/PoolSocketService.java | 34 ++++++++-- .../java/fr/zelytra/game/pool/PoolParty.java | 18 ++++- .../java/fr/zelytra/game/pool/PoolPlayer.java | 8 +++ .../game/pool/game/PoolGameManager.java | 2 +- .../manager/socket/PoolSocketServiceTest.java | 2 + .../fr/zelytra/game/pool/PoolPartyTest.java | 2 +- .../game/pool/game/PoolGameManagerTest.java | 68 +++++++++++++++++++ 7 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 backend/src/test/java/fr/zelytra/game/pool/game/PoolGameManagerTest.java diff --git a/backend/src/main/java/fr/zelytra/game/manager/socket/PoolSocketService.java b/backend/src/main/java/fr/zelytra/game/manager/socket/PoolSocketService.java index a2dce45..16459e6 100644 --- a/backend/src/main/java/fr/zelytra/game/manager/socket/PoolSocketService.java +++ b/backend/src/main/java/fr/zelytra/game/manager/socket/PoolSocketService.java @@ -80,15 +80,24 @@ public void joinPool(String username, String sessionID, Session socketSession) { PoolPlayer poolPlayer = new PoolPlayer(user, socketSession); + if (isPlayerInPool(username)) { + Log.warn("[joinPool][N/A] User already in pool: " + username); + PoolParty poolParty = getPoolPartyByPlayer(username); + PoolPlayer retrievePlayer = poolParty.getPoolPlayerByName(username); + if (poolParty.getState() == GameStatus.RUNNING && retrievePlayer.getSocketSession() == null) { + retrievePlayer.setSocketSession(socketSession); + broadcastPoolDataToParty(poolParty); + return; + } else { + removePlayerFromPool(poolPlayer, poolParty.getUuid()); + } + } + if (sessionID.isBlank() || sessionID.isEmpty()) { PoolParty poolParty = createParty(username, socketSession); SocketMessage socketMessage = new SocketMessage<>(MessageType.UPDATE_POOL_DATA, poolParty); socketMessage.sendDataToPlayer(poolPlayer.getSocketSession()); return; - } else if (isPlayerInPool(username)) { - Log.warn("[joinPool][N/A] User already in pool: " + username); - PoolParty poolParty = getPoolPartyByPlayer(username); - removePlayerFromPool(poolPlayer, poolParty.getUuid()); } else if (!games.containsKey(sessionID)) { Log.warn("[joinPool][N/A] Pool not found: " + sessionID); return; @@ -185,10 +194,16 @@ public PoolPlayer getPlayerBySocketSessionId(String socketSessionId) { Log.info("[getPlayerBySocketSessionId][N/A] User: " + socketSessionId); for (PoolParty poolParty : games.values()) { + if (poolParty.getGameOwner().getSocketSession() == null) { + continue; + } if (Objects.equals(poolParty.getGameOwner().getSocketSession().getId(), socketSessionId)) { return poolParty.getGameOwner(); } for (PoolPlayer player : poolParty.getPlayers()) { + if (player.getSocketSession() == null) { + continue; + } if (Objects.equals(player.getSocketSession().getId(), socketSessionId)) { return player; } @@ -222,7 +237,14 @@ public boolean removePlayerFromPool(PoolPlayer user, String sessionID) { broadcastPoolDataToParty(poolParty); if (removed) { Log.info("[removePlayerFromPool][" + sessionID + "] Removed user: " + user.getUsername()); - if (poolParty.getPlayers().isEmpty()) { + boolean isUserRemaining = false; + for (PoolPlayer player : poolParty.getPlayers()) { + if (player.getSocketSession() != null) { + isUserRemaining = true; + break; + } + } + if (poolParty.getPlayers().isEmpty() || !isUserRemaining) { games.remove(sessionID); Log.info("[removePlayerFromPool][" + sessionID + "] Pool deleted"); } @@ -323,7 +345,7 @@ public void playAction(GameAction gameAction, String socketSessionId) { PoolVictoryState victoryState = poolParty.getGame().winDetection(); if (victoryState != PoolVictoryState.NONE) { - GameReport gameReport = poolParty.winHandler(victoryState); + GameReport gameReport = poolParty.getGameReport(victoryState); poolParty.setGameReport(gameReport); // Updating players data in DB diff --git a/backend/src/main/java/fr/zelytra/game/pool/PoolParty.java b/backend/src/main/java/fr/zelytra/game/pool/PoolParty.java index 5b11209..a541d2d 100644 --- a/backend/src/main/java/fr/zelytra/game/pool/PoolParty.java +++ b/backend/src/main/java/fr/zelytra/game/pool/PoolParty.java @@ -125,16 +125,23 @@ public PoolGameInterface getGame() { * @return true if the user was removed, false otherwise */ public boolean removePlayer(UserEntity user) { + // If game is running don't remove the player from the list but just setting is session to null; + if (state == GameStatus.RUNNING) { + players.get(players.indexOf(user)).resetSession(); + return true; + } + if (players.remove(user)) { if (user.equals(gameOwner) && !players.isEmpty()) { gameOwner = players.getFirst(); } return true; } + return false; } - public GameReport winHandler(PoolVictoryState victoryState) { + public GameReport getGameReport(PoolVictoryState victoryState) { game.setVictoryState(victoryState); setState(GameStatus.END); GameReport gameReport = new GameReport(new ArrayList<>(), new ArrayList<>()); @@ -167,4 +174,13 @@ public List getPoolPlayersByTeam(PoolVictoryState poolVictoryState) } return poolPlayers; } + + public PoolPlayer getPoolPlayerByName(String username) { + for (PoolPlayer player : players) { + if (player.getAuthUsername().equals(username)) { + return player; + } + } + return null; + } } diff --git a/backend/src/main/java/fr/zelytra/game/pool/PoolPlayer.java b/backend/src/main/java/fr/zelytra/game/pool/PoolPlayer.java index ba52fcf..ef63509 100644 --- a/backend/src/main/java/fr/zelytra/game/pool/PoolPlayer.java +++ b/backend/src/main/java/fr/zelytra/game/pool/PoolPlayer.java @@ -17,4 +17,12 @@ public PoolPlayer(UserEntity user, Session socketSession) { public Session getSocketSession() { return socketSession; } + + public void setSocketSession(Session socketSession) { + this.socketSession = socketSession; + } + + public void resetSession() { + socketSession = null; + } } diff --git a/backend/src/main/java/fr/zelytra/game/pool/game/PoolGameManager.java b/backend/src/main/java/fr/zelytra/game/pool/game/PoolGameManager.java index 68f601c..f9c4a24 100644 --- a/backend/src/main/java/fr/zelytra/game/pool/game/PoolGameManager.java +++ b/backend/src/main/java/fr/zelytra/game/pool/game/PoolGameManager.java @@ -83,7 +83,7 @@ public String getNextPlayer() { } // If all players from the current team have played, reset and return the first player from the opposite team - return currentTeam.get(0); + return currentTeam.getFirst(); } diff --git a/backend/src/test/java/fr/zelytra/game/manager/socket/PoolSocketServiceTest.java b/backend/src/test/java/fr/zelytra/game/manager/socket/PoolSocketServiceTest.java index 4f6f3ec..fb82368 100644 --- a/backend/src/test/java/fr/zelytra/game/manager/socket/PoolSocketServiceTest.java +++ b/backend/src/test/java/fr/zelytra/game/manager/socket/PoolSocketServiceTest.java @@ -4,6 +4,7 @@ import fr.zelytra.game.pool.PoolPlayer; import fr.zelytra.user.UserEntity; import fr.zelytra.user.UserService; +import io.quarkus.logging.Log; import io.quarkus.test.InjectMock; import io.quarkus.test.junit.QuarkusTest; import jakarta.transaction.Transactional; @@ -82,6 +83,7 @@ void joinPool_emptySessionId() { void joinPool_sameUserConnectToAnotherSession() { when(userService.getUserByName("user1")).thenReturn(new UserEntity("user1")); socketService.joinPool("user1", "", session); + Log.info("splitter"); socketService.joinPool("user1", "", session); ConcurrentMap games = socketService.getGames(); diff --git a/backend/src/test/java/fr/zelytra/game/pool/PoolPartyTest.java b/backend/src/test/java/fr/zelytra/game/pool/PoolPartyTest.java index dd37a04..64cb5f2 100644 --- a/backend/src/test/java/fr/zelytra/game/pool/PoolPartyTest.java +++ b/backend/src/test/java/fr/zelytra/game/pool/PoolPartyTest.java @@ -116,7 +116,7 @@ public void testRemovePlayer() { @Test @Transactional - public void testWinHandler() { + public void testGetGameReport() { /* PoolVictoryState victoryState = PoolVictoryState.TEAM1; poolParty.setRules(GameRules.AMERICAN_8); diff --git a/backend/src/test/java/fr/zelytra/game/pool/game/PoolGameManagerTest.java b/backend/src/test/java/fr/zelytra/game/pool/game/PoolGameManagerTest.java new file mode 100644 index 0000000..4423e27 --- /dev/null +++ b/backend/src/test/java/fr/zelytra/game/pool/game/PoolGameManagerTest.java @@ -0,0 +1,68 @@ +package fr.zelytra.game.pool.game; + +import fr.zelytra.game.pool.data.GameAction; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@QuarkusTest +class PoolGameManagerTest { + + private PoolGameManager game; + + @BeforeEach + void setUp() { + game = new PoolGameManager(); + game.getTeams().team1().add("player1"); + game.getTeams().team1().add("player3"); + game.getTeams().team2().add("player2"); + game.getTeams().team2().add("player4"); + game.initGame(); + } + + @Test + void getNextPlayer_nextPlayer_next() { + GameAction action = new GameAction(1, List.of(), new ArrayList<>(), "player1"); + game.getHistory().add(action); + assertEquals("player2", game.getNextPlayer()); + action = new GameAction(1, List.of(), new ArrayList<>(), game.getNextPlayer()); + game.getHistory().add(action); + assertEquals("player3", game.getNextPlayer()); + action = new GameAction(1, List.of(), new ArrayList<>(), game.getNextPlayer()); + game.getHistory().add(action); + assertEquals("player4", game.getNextPlayer()); + action = new GameAction(1, List.of(), new ArrayList<>(), game.getNextPlayer()); + game.getHistory().add(action); + assertEquals("player1", game.getNextPlayer()); + } + + @Test + void getNextPlayer_nextPlayerEvenTeam_next() { + game = new PoolGameManager(); + game.getTeams().team1().add("player1"); + game.getTeams().team1().add("player3"); + game.getTeams().team2().add("player2"); + game.initGame(); + + GameAction action = new GameAction(1, List.of(), new ArrayList<>(), "player1"); + game.getHistory().add(action); + assertEquals("player2", game.getNextPlayer()); + action = new GameAction(1, List.of(), new ArrayList<>(), game.getNextPlayer()); + game.getHistory().add(action); + assertEquals("player3", game.getNextPlayer()); + action = new GameAction(1, List.of(), new ArrayList<>(), game.getNextPlayer()); + game.getHistory().add(action); + assertEquals("player2", game.getNextPlayer()); + action = new GameAction(1, List.of(), new ArrayList<>(), game.getNextPlayer()); + game.getHistory().add(action); + assertEquals("player1", game.getNextPlayer()); + action = new GameAction(1, List.of(), new ArrayList<>(), game.getNextPlayer()); + game.getHistory().add(action); + assertEquals("player2", game.getNextPlayer()); + } +} \ No newline at end of file