Skip to content

Commit

Permalink
Offline player (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
zelytra authored Jul 5, 2024
1 parent 6add1fd commit 4922683
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<PoolParty> 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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion backend/src/main/java/fr/zelytra/game/pool/PoolParty.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<>());
Expand Down Expand Up @@ -167,4 +174,13 @@ public List<PoolPlayer> getPoolPlayersByTeam(PoolVictoryState poolVictoryState)
}
return poolPlayers;
}

public PoolPlayer getPoolPlayerByName(String username) {
for (PoolPlayer player : players) {
if (player.getAuthUsername().equals(username)) {
return player;
}
}
return null;
}
}
8 changes: 8 additions & 0 deletions backend/src/main/java/fr/zelytra/game/pool/PoolPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, PoolParty> games = socketService.getGames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void testRemovePlayer() {

@Test
@Transactional
public void testWinHandler() {
public void testGetGameReport() {
/*
PoolVictoryState victoryState = PoolVictoryState.TEAM1;
poolParty.setRules(GameRules.AMERICAN_8);
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 4922683

Please sign in to comment.