diff --git a/src/main/java/com/uniovi/entities/Player.java b/src/main/java/com/uniovi/entities/Player.java index b70a0d5f..97b93fd2 100644 --- a/src/main/java/com/uniovi/entities/Player.java +++ b/src/main/java/com/uniovi/entities/Player.java @@ -39,8 +39,6 @@ public class Player implements JsonEntity { @Column private String scoreMultiplayerCode; - //@ManyToMany - //private Set multiplayerSessions = new HashSet<>(); @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) private Set roles = new HashSet<>(); diff --git a/src/main/java/com/uniovi/services/MultiplayerSessionService.java b/src/main/java/com/uniovi/services/MultiplayerSessionService.java index 440a263d..c0932339 100644 --- a/src/main/java/com/uniovi/services/MultiplayerSessionService.java +++ b/src/main/java/com/uniovi/services/MultiplayerSessionService.java @@ -13,8 +13,6 @@ @Service public interface MultiplayerSessionService { - //Page getMultiplayerPlayerRanking(Pageable pageable, int multiplayerCode); - Map getPlayersWithScores(int multiplayerCode); void multiCreate(String code, Long id); diff --git a/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java b/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java index 05acec97..a690d37d 100644 --- a/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java +++ b/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java @@ -5,6 +5,7 @@ import com.uniovi.repositories.MultiplayerSessionRepository; import com.uniovi.repositories.PlayerRepository; import com.uniovi.services.MultiplayerSessionService; +import jakarta.transaction.Transactional; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -24,6 +25,7 @@ public MultiplayerSessionImpl(PlayerRepository playerRepository, MultiplayerSess } @Override + //@Transactional public Map getPlayersWithScores(int multiplayerCode) { MultiplayerSession session = multiplayerSessionRepository.findByMultiplayerCode(String.valueOf(multiplayerCode)); Map playerScores = session.getPlayerScores(); @@ -48,6 +50,7 @@ public void multiCreate(String code, Long id) { } @Override + //@Transactional public void addToLobby(String code, Long id) { Player p = playerRepository.findById(id).get(); MultiplayerSession ms=multiplayerSessionRepository.findByMultiplayerCode(code); @@ -56,6 +59,7 @@ public void addToLobby(String code, Long id) { } @Override + //@Transactional public void changeScore(String code, Long id, int score) { Player p = playerRepository.findById(id).get(); MultiplayerSession ms=multiplayerSessionRepository.findByMultiplayerCode(code); diff --git a/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java b/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java index 79e4c218..6225e8b6 100644 --- a/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java @@ -108,6 +108,7 @@ public List getRandomQuestions(int num) { return res; } + @Override public List getRandomMultiplayerQuestions(int num, int code) { List allQuestions = questionRepository.findAll().stream() diff --git a/src/test/java/com/uniovi/Wiq_UnitTests.java b/src/test/java/com/uniovi/Wiq_UnitTests.java index 52494d87..521a34ff 100644 --- a/src/test/java/com/uniovi/Wiq_UnitTests.java +++ b/src/test/java/com/uniovi/Wiq_UnitTests.java @@ -75,6 +75,8 @@ public class Wiq_UnitTests { private GameSessionService gameSessionService; @Autowired private InsertSampleDataService sampleDataService; + @Autowired + private MultiplayerSessionService multiplayerSessionService; @Autowired PlayerRepository playerRepository; @@ -90,12 +92,18 @@ public class Wiq_UnitTests { GameSessionRepository gameSessionRepository; @Autowired QuestionRepository questionRepository; + @Autowired + MultiplayerSessionRepository multiplayerSessionRepository; private final HttpClient httpClient = HttpClient.newHttpClient(); private Player createPlayer(){ return new Player("name","test@email.com","password"); } + private Player createDiferentPlayer(String word){ + return new Player("name"+word,word+"test@email.com","password"); + + } @Test @Order(1) public void testPlayerService() { @@ -1555,6 +1563,88 @@ public void testDeleteQuestion() throws IOException, InterruptedException, JSONE Assertions.assertTrue(deletedQuestion.isEmpty()); } + @Test + @Order(97) + public void testGetPlayersWithScores() { + Player player1 = playerRepository.save(createDiferentPlayer("aa")); + Player player2 = playerRepository.save(createDiferentPlayer("bb")); + Player player3 = playerRepository.save(createDiferentPlayer("cc")); + + MultiplayerSession session = new MultiplayerSession("123",player3); + Map playerScores = new HashMap<>(); + playerScores.put(player1, 10); + playerScores.put(player2, 5); + session.setPlayerScores(playerScores); + + multiplayerSessionRepository.save(session); + + Map result = multiplayerSessionService.getPlayersWithScores(123); + + assertNotNull(result); + Assertions.assertEquals(2, result.size()); + } + + @Test + @Order(98) + public void testMultiCreate() { + // Given + Long playerId = 1L; + Player player = createPlayer(); + player.setId(playerId); + + String code = "123"; + playerRepository.findById(playerId); + multiplayerSessionService.multiCreate(code, playerId); + + MultiplayerSession ms = multiplayerSessionRepository.findByMultiplayerCode("123"); + Assertions.assertNotNull(ms); + Assertions.assertEquals("123",ms.getMultiplayerCode()); + } + + @Test + @Order(99) + public void testAddToLobby() { + + Long playerId = 1L; + Player player = createPlayer(); + player.setId(playerId); + + String code = "123"; + MultiplayerSession multiplayerSession = new MultiplayerSession(); + multiplayerSession.setMultiplayerCode(code); + + playerRepository.save(player); + multiplayerSessionRepository.save(multiplayerSession); + + multiplayerSessionService.addToLobby(code, playerId); + + MultiplayerSession ms = multiplayerSessionRepository.findByMultiplayerCode(code); + Assertions.assertNotNull(ms); + Assertions.assertEquals("123",ms.getMultiplayerCode()); + } + + @Test + @Order(100) + public void testChangeScore() { + Long playerId = 1L; + Player player = createPlayer(); + player.setId(playerId); + + String code = "123"; + int newScore = 100; + MultiplayerSession multiplayerSession = new MultiplayerSession(); + multiplayerSession.setMultiplayerCode(code); + multiplayerSession.addPlayer(player); + + playerRepository.save(player); + multiplayerSessionRepository.save(multiplayerSession); + + multiplayerSessionService.changeScore(code, playerId, newScore); + + multiplayerSession = multiplayerSessionRepository.findByMultiplayerCode("123"); + Assertions.assertNotNull(multiplayerSession); + } + /** * Sends an HTTP request to the API * @param method HTTP method