From 391f82f94086148c6f84dfe6ce72c9e3f5db3860 Mon Sep 17 00:00:00 2001 From: uo288061 Date: Tue, 9 Apr 2024 19:05:16 +0200 Subject: [PATCH 01/40] frontEnd for multiplayer --- .../uniovi/controllers/GameController.java | 5 ++++ src/main/resources/messages.properties | 6 +++++ src/main/resources/messages_en.properties | 9 +++++++ src/main/resources/messages_es.properties | 7 +++++ src/main/resources/messages_fr.properties | 6 +++++ .../templates/game/multiplayerGame.html | 26 +++++++++++++++++++ src/main/resources/templates/index.html | 6 +++++ 7 files changed, 65 insertions(+) create mode 100644 src/main/resources/templates/game/multiplayerGame.html diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index 59c20083..4d049db2 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -54,6 +54,11 @@ public String getGame(HttpSession session, Model model, Principal principal) { return "game/basicGame"; } + @GetMapping("/multiplayerGame") + public String getMultyplayerGame(HttpSession session, Model model, Principal principal) { + return "game/multiplayerGame"; + } + /** * This method is used to check the answer for a specific question diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 2b91abb1..3281838a 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -37,6 +37,7 @@ error.error=Error: index.heading=WIQ index.subtitle=Responde las preguntas correctamente y GANA!!! index.button=JUGAR +index.multiplayer.button= Juega con amigos # -------------------Statements for the home.html file--------------------- home.heading=Bienvenido @@ -82,6 +83,11 @@ ranking.question.right=Respuestas correctas ranking.question.wrong=Respuestas incorrectas ranking.time=Tiempo +# -------------------Statements for the multiplayerGame.html file--------------------- +multi.text = ¿Aún no tienes un código? Crea uno y compártelo con tus amigos +multi.create = Crear +multi.placeholder= Introduce el código de una partida +multi.label = Únete a una partida # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso. diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 97f0f133..434ff4bc 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -37,6 +37,8 @@ error.error=Error: index.heading=WIQ index.subtitle=Answer the questions correctly and WIN!!! index.button=PLAY +index.multiplayer.button= Play with friends + # -------------------Statements for the home.html file--------------------- home.heading=Welcome @@ -83,6 +85,13 @@ ranking.question.right=Right answers ranking.question.wrong=Wrong answers ranking.time=Time +# -------------------Statements for the multiplayerGame.html file--------------------- +multi.text =Don't have a code yet? Create one and share it with your friends +multi.create = Create +multi.placeholder=Enter the game code +multi.label=Join a game + + # -------------------Statements for the apiHome.html file--------------------- api.doc.title=API Documentation api.doc.description=This document describes the REST API endpoints. diff --git a/src/main/resources/messages_es.properties b/src/main/resources/messages_es.properties index 174dad48..62f487b6 100644 --- a/src/main/resources/messages_es.properties +++ b/src/main/resources/messages_es.properties @@ -38,6 +38,8 @@ error.error=Error: index.heading=WIQ index.subtitle=Responde las preguntas correctamente y GANA!!! index.button=JUGAR +index.multiplayer.button= Juega con amigos + # -------------------Statements for the home.html file--------------------- home.heading=Bienvenido @@ -84,6 +86,11 @@ ranking.question.right=Respuestas correctas ranking.question.wrong=Respuestas incorrectas ranking.time=Tiempo +# -------------------Statements for the multiplayerGame.html file--------------------- +multi.text = ¿Aún no tienes un código? Crea uno y compártelo con tus amigos +multi.create = Crear +multi.placeholder= Introduce el código de una partida +multi.label = Únete a una partida # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso. diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index 1f5d2b22..62aec8e4 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -36,6 +36,7 @@ error.error=Erreur : index.heading=WIQ index.subtitle=Répondez aux questions correctement et GAGNEZ !!! index.button=JOUER +index.multiplayer.button=Jouez avec des amis # -------------------Statements for the home.html file--------------------- home.heading=Bienvenue @@ -78,6 +79,11 @@ ranking.question.right=Réponses correctes ranking.question.wrong=Réponses incorrectes ranking.time=Temps +# -------------------Statements for the multiplayerGame.html file--------------------- +multi.text = Vous n'avez pas encore de code ? Créez-en un et partagez-le avec vos amis. +multi.create = Créer +multi.placeholder=Entrez le code du jeu +multi.label=Rejoignez une partie # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentation de l'API api.doc.description=Ceci est la documentation de l'API de WIQ. Vous pouvez trouver ici des informations sur les ressources disponibles, les paramètres qu'elles acceptent et des exemples d'utilisation. diff --git a/src/main/resources/templates/game/multiplayerGame.html b/src/main/resources/templates/game/multiplayerGame.html new file mode 100644 index 00000000..8176c8e5 --- /dev/null +++ b/src/main/resources/templates/game/multiplayerGame.html @@ -0,0 +1,26 @@ + + + + + + + + + +
+ + +
+ +

+ +

+ + + + + + + + diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index d199e522..434df7e5 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -10,6 +10,7 @@

+ @@ -18,6 +19,11 @@

window.location.href = "/game"; }); + From 152e2164d6ede79030eebbf919a5c0fb46eefea1 Mon Sep 17 00:00:00 2001 From: uo288061 Date: Tue, 9 Apr 2024 20:01:53 +0200 Subject: [PATCH 02/40] init backEnd for multiplayer --- src/main/java/com/uniovi/dto/PlayerDto.java | 3 +++ src/main/java/com/uniovi/entities/Player.java | 5 +++++ .../java/com/uniovi/repositories/PlayerRepository.java | 3 +++ src/main/java/com/uniovi/services/PlayerService.java | 7 +++++++ .../java/com/uniovi/services/impl/PlayerServiceImpl.java | 9 +++++++++ 5 files changed, 27 insertions(+) diff --git a/src/main/java/com/uniovi/dto/PlayerDto.java b/src/main/java/com/uniovi/dto/PlayerDto.java index 027b26fa..b3d1e87d 100644 --- a/src/main/java/com/uniovi/dto/PlayerDto.java +++ b/src/main/java/com/uniovi/dto/PlayerDto.java @@ -22,6 +22,9 @@ public class PlayerDto { @Schema(hidden = true) private String passwordConfirm; + @Schema(description = "code of group of the player", example = "5565") + private Integer multiplayerCode; + @Schema(description = "Roles of the player", example = "[\"ROLE_USER\"]") private String[] roles; } diff --git a/src/main/java/com/uniovi/entities/Player.java b/src/main/java/com/uniovi/entities/Player.java index 491a8bd8..022a385c 100644 --- a/src/main/java/com/uniovi/entities/Player.java +++ b/src/main/java/com/uniovi/entities/Player.java @@ -33,6 +33,9 @@ public class Player implements JsonEntity { @NotEmpty private String password; + @Column + private Integer multiplayerCode; + @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) private Set roles = new HashSet<>(); @@ -42,6 +45,8 @@ public class Player implements JsonEntity { @OneToOne(cascade = CascadeType.ALL, mappedBy = "player") private ApiKey apiKey; + + // Transient: no se almacena en la base de datos @Transient private String passwordConfirm; diff --git a/src/main/java/com/uniovi/repositories/PlayerRepository.java b/src/main/java/com/uniovi/repositories/PlayerRepository.java index 77c2d9f8..247e8964 100644 --- a/src/main/java/com/uniovi/repositories/PlayerRepository.java +++ b/src/main/java/com/uniovi/repositories/PlayerRepository.java @@ -1,9 +1,12 @@ package com.uniovi.repositories; import com.uniovi.entities.Player; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; public interface PlayerRepository extends CrudRepository { Player findByEmail(String email); Player findByUsername(String nickname); + @Query("SELECT player FROM Player player WHERE player.multiplayerCode=:multiplayerCode") + Iterable findAllByMultiplayerCode(int multiplayerCode); } diff --git a/src/main/java/com/uniovi/services/PlayerService.java b/src/main/java/com/uniovi/services/PlayerService.java index f59669e3..278db10a 100644 --- a/src/main/java/com/uniovi/services/PlayerService.java +++ b/src/main/java/com/uniovi/services/PlayerService.java @@ -26,6 +26,13 @@ public interface PlayerService { */ List getUsers(); + + /** + * Get all the players in the database with same multiplayerCode + * @return A list with the players + */ + List getUsersByMultiplayerCode(int multiplayerCode); + /** * Get a player by its id * @param id The id of the player diff --git a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java index f4fa462d..125af896 100644 --- a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java @@ -71,6 +71,13 @@ public List getUsers() { return l; } + @Override + public List getUsersByMultiplayerCode(int multiplayerCode) { + List l = new ArrayList<>(); + playerRepository.findAllByMultiplayerCode(multiplayerCode).forEach(l::add); + return l; + } + @Override public Optional getUser(Long id) { return playerRepository.findById(id); @@ -116,6 +123,8 @@ public void updatePlayer(Long id, PlayerDto playerDto) { p.setUsername(playerDto.getUsername()); if (playerDto.getPassword() != null) p.setPassword(passwordEncoder.encode(playerDto.getPassword())); + if(playerDto.getMultiplayerCode() != null) + p.setMultiplayerCode(playerDto.getMultiplayerCode()); if (playerDto.getRoles() != null) { p.getRoles().clear(); for (String roleStr : playerDto.getRoles()) { From 3eb53d92a13f22f4e420f9b1fe4acc53b75e0181 Mon Sep 17 00:00:00 2001 From: uo288061 Date: Tue, 9 Apr 2024 21:19:54 +0200 Subject: [PATCH 03/40] more backEnd for multiplayer --- .../java/com/uniovi/controllers/GameController.java | 11 ++++++++++- src/main/resources/messages.properties | 1 + src/main/resources/messages_en.properties | 1 + src/main/resources/messages_es.properties | 2 ++ src/main/resources/messages_fr.properties | 2 ++ .../resources/templates/game/multiplayerGame.html | 8 +++++++- 6 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index 4d049db2..bead274f 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -1,5 +1,6 @@ package com.uniovi.controllers; +import com.uniovi.dto.PlayerDto; import com.uniovi.entities.GameSession; import com.uniovi.entities.Player; import com.uniovi.entities.Question; @@ -55,10 +56,18 @@ public String getGame(HttpSession session, Model model, Principal principal) { } @GetMapping("/multiplayerGame") - public String getMultyplayerGame(HttpSession session, Model model, Principal principal) { + public String getMultiplayerGame() { return "game/multiplayerGame"; } + @GetMapping("/multiplayerGame/{code}") + public String joinMultiplayerGame(@PathVariable String code, HttpSession session, Principal principal) { + Optional player = playerService.getUserByUsername(principal.getName()); + Player p = player.orElse(null); + //playerService.changeMultiplayerCode(code,new PlayerDto()); + return "game/multiplayerGame"; + } + /** * This method is used to check the answer for a specific question diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 3281838a..883e3c19 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -88,6 +88,7 @@ multi.text = ¿Aún no tienes un código? Crea uno y compártelo con tus amigos multi.create = Crear multi.placeholder= Introduce el código de una partida multi.label = Únete a una partida +multi.join = Unirse # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso. diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 434ff4bc..bf3b1b6f 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -90,6 +90,7 @@ multi.text =Don't have a code yet? Create one and share it with your friends multi.create = Create multi.placeholder=Enter the game code multi.label=Join a game +multi.join = Join # -------------------Statements for the apiHome.html file--------------------- diff --git a/src/main/resources/messages_es.properties b/src/main/resources/messages_es.properties index 62f487b6..f3df8923 100644 --- a/src/main/resources/messages_es.properties +++ b/src/main/resources/messages_es.properties @@ -91,6 +91,8 @@ multi.text = ¿Aún no tienes un código? Crea uno y compártelo con tus amigos multi.create = Crear multi.placeholder= Introduce el código de una partida multi.label = Únete a una partida +multi.join = Unirse + # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso. diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index 62aec8e4..49103510 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -84,6 +84,8 @@ multi.text = Vous n'avez pas encore de code ? Créez-en un et partagez-le avec v multi.create = Créer multi.placeholder=Entrez le code du jeu multi.label=Rejoignez une partie +multi.join =Rejoindre + # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentation de l'API api.doc.description=Ceci est la documentation de l'API de WIQ. Vous pouvez trouver ici des informations sur les ressources disponibles, les paramètres qu'elles acceptent et des exemples d'utilisation. diff --git a/src/main/resources/templates/game/multiplayerGame.html b/src/main/resources/templates/game/multiplayerGame.html index 8176c8e5..f6154237 100644 --- a/src/main/resources/templates/game/multiplayerGame.html +++ b/src/main/resources/templates/game/multiplayerGame.html @@ -12,11 +12,17 @@ th:placeholder="#{multi.placeholder}"/> + + + +

- + + + From 06f209de12b262f58367639f2490efea8ceb553b Mon Sep 17 00:00:00 2001 From: uo288061 Date: Wed, 10 Apr 2024 13:06:13 +0200 Subject: [PATCH 04/40] fixing backEnd for multiplayer (It is not yet implemented) done about 30% --- .../com/uniovi/configuration/SecurityConfig.java | 1 + .../com/uniovi/controllers/GameController.java | 4 ++-- .../java/com/uniovi/services/PlayerService.java | 7 +++++++ .../uniovi/services/impl/PlayerServiceImpl.java | 12 ++++++++++++ .../resources/templates/game/multiplayerGame.html | 15 ++++++++++----- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/uniovi/configuration/SecurityConfig.java b/src/main/java/com/uniovi/configuration/SecurityConfig.java index c0af5d20..9c3d3e45 100644 --- a/src/main/java/com/uniovi/configuration/SecurityConfig.java +++ b/src/main/java/com/uniovi/configuration/SecurityConfig.java @@ -47,6 +47,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .requestMatchers("/signup/**").permitAll() .requestMatchers("/api/**").permitAll() .requestMatchers("/game/**").authenticated() + .requestMatchers("/multiplayerGame/**").authenticated() .requestMatchers("/ranking/playerRanking").authenticated() .requestMatchers("/**").permitAll() ).formLogin( diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index bead274f..bbdecd71 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -61,10 +61,10 @@ public String getMultiplayerGame() { } @GetMapping("/multiplayerGame/{code}") - public String joinMultiplayerGame(@PathVariable String code, HttpSession session, Principal principal) { + public String joinMultiplayerGame(@PathVariable String code, HttpSession session, Principal principal, Model model) { Optional player = playerService.getUserByUsername(principal.getName()); Player p = player.orElse(null); - //playerService.changeMultiplayerCode(code,new PlayerDto()); + playerService.changeMultiplayerCode(p.getId(),code); return "game/multiplayerGame"; } diff --git a/src/main/java/com/uniovi/services/PlayerService.java b/src/main/java/com/uniovi/services/PlayerService.java index 278db10a..50c31651 100644 --- a/src/main/java/com/uniovi/services/PlayerService.java +++ b/src/main/java/com/uniovi/services/PlayerService.java @@ -74,6 +74,13 @@ public interface PlayerService { */ void updatePlayer(Long id, PlayerDto playerDto); + /** + * Update the multiplayerCode of a player + * @param id The id of the player to update + * @param code The new multiplayerCode of the player + */ + void changeMultiplayerCode(Long id, String code); + /** * Delete a player from the database * @param id The id of the player to delete diff --git a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java index 125af896..17fea133 100644 --- a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java @@ -141,6 +141,18 @@ public void updatePlayer(Long id, PlayerDto playerDto) { playerRepository.save(p); } + @Override + public void changeMultiplayerCode(Long id, String code) { + Optional player = playerRepository.findById(id); + if (player.isEmpty()) + return; + + Player p = player.get(); + p.setMultiplayerCode(Integer.parseInt(code)); + playerRepository.save(p); + + } + @Override public void deletePlayer(Long id) { playerRepository.deleteById(id); diff --git a/src/main/resources/templates/game/multiplayerGame.html b/src/main/resources/templates/game/multiplayerGame.html index f6154237..0b2258b3 100644 --- a/src/main/resources/templates/game/multiplayerGame.html +++ b/src/main/resources/templates/game/multiplayerGame.html @@ -12,15 +12,20 @@ th:placeholder="#{multi.placeholder}"/> - - - - + + + +

- + From 15c35d5a0780a7ae88fd5b930b4876b5a393bbb6 Mon Sep 17 00:00:00 2001 From: uo288061 Date: Wed, 10 Apr 2024 14:57:15 +0200 Subject: [PATCH 05/40] starting multiplayer lobby --- .../uniovi/configuration/SecurityConfig.java | 1 + .../uniovi/controllers/GameController.java | 16 +++++++++++++-- .../com/uniovi/services/PlayerService.java | 2 +- .../services/impl/PlayerServiceImpl.java | 20 ++++++++++++++----- src/main/resources/templates/game/lobby.html | 9 +++++++++ 5 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 src/main/resources/templates/game/lobby.html diff --git a/src/main/java/com/uniovi/configuration/SecurityConfig.java b/src/main/java/com/uniovi/configuration/SecurityConfig.java index 9c3d3e45..0bb5723e 100644 --- a/src/main/java/com/uniovi/configuration/SecurityConfig.java +++ b/src/main/java/com/uniovi/configuration/SecurityConfig.java @@ -48,6 +48,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .requestMatchers("/api/**").permitAll() .requestMatchers("/game/**").authenticated() .requestMatchers("/multiplayerGame/**").authenticated() + .requestMatchers("/lobby/**").authenticated() .requestMatchers("/ranking/playerRanking").authenticated() .requestMatchers("/**").permitAll() ).formLogin( diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index bbdecd71..c41e8bbe 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -1,6 +1,5 @@ package com.uniovi.controllers; -import com.uniovi.dto.PlayerDto; import com.uniovi.entities.GameSession; import com.uniovi.entities.Player; import com.uniovi.entities.Question; @@ -17,6 +16,7 @@ import java.security.Principal; import java.time.Duration; import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; @Controller @@ -64,10 +64,22 @@ public String getMultiplayerGame() { public String joinMultiplayerGame(@PathVariable String code, HttpSession session, Principal principal, Model model) { Optional player = playerService.getUserByUsername(principal.getName()); Player p = player.orElse(null); - playerService.changeMultiplayerCode(p.getId(),code); + if(playerService.changeMultiplayerCode(p.getId(),code)){ + model.addAttribute("multiplayerGameCode",code); + session.setAttribute("multiplayerCode",code); + return "game/lobby"; + } return "game/multiplayerGame"; } + @GetMapping("/game/lobby") + public String createLobby( HttpSession session, Principal principal, Model model) { + int code = Integer.parseInt((String)session.getAttribute("multiplayerCode")); + List players=playerService.getUsersByMultiplayerCode(code); + model.addAttribute("players",players); + return "/game/lobby"; + } + /** * This method is used to check the answer for a specific question diff --git a/src/main/java/com/uniovi/services/PlayerService.java b/src/main/java/com/uniovi/services/PlayerService.java index 50c31651..fa77b9ca 100644 --- a/src/main/java/com/uniovi/services/PlayerService.java +++ b/src/main/java/com/uniovi/services/PlayerService.java @@ -79,7 +79,7 @@ public interface PlayerService { * @param id The id of the player to update * @param code The new multiplayerCode of the player */ - void changeMultiplayerCode(Long id, String code); + boolean changeMultiplayerCode(Long id, String code); /** * Delete a player from the database diff --git a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java index 17fea133..0071aebb 100644 --- a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java @@ -142,15 +142,25 @@ public void updatePlayer(Long id, PlayerDto playerDto) { } @Override - public void changeMultiplayerCode(Long id, String code) { + public boolean changeMultiplayerCode(Long id, String code) { Optional player = playerRepository.findById(id); if (player.isEmpty()) - return; + return false; Player p = player.get(); - p.setMultiplayerCode(Integer.parseInt(code)); - playerRepository.save(p); - + if(true/*existsMultiplayerCode(code)*/){ + p.setMultiplayerCode(Integer.parseInt(code)); + playerRepository.save(p); + return true; + } + return false; + } + /** + * A multiplayerCodeExists if there are any player + * with same multiplayerCode at the moment of the join + * */ + private boolean existsMultiplayerCode(String code){ + return ! getUsersByMultiplayerCode(Integer.parseInt(code)).isEmpty(); } @Override diff --git a/src/main/resources/templates/game/lobby.html b/src/main/resources/templates/game/lobby.html new file mode 100644 index 00000000..632b47ce --- /dev/null +++ b/src/main/resources/templates/game/lobby.html @@ -0,0 +1,9 @@ + + + + + + + +
+
From 7c197e8899b286b3c3dfac315b6dac0c9e6d3e6f Mon Sep 17 00:00:00 2001 From: uo288061 Date: Wed, 10 Apr 2024 18:42:23 +0200 Subject: [PATCH 06/40] done multiplayer lobby, now the list of players updates automatically --- .../uniovi/controllers/GameController.java | 16 +++++++-- src/main/resources/templates/game/lobby.html | 35 ++++++++++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index c41e8bbe..25c73517 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -16,6 +16,7 @@ import java.security.Principal; import java.time.Duration; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -67,16 +68,27 @@ public String joinMultiplayerGame(@PathVariable String code, HttpSession session if(playerService.changeMultiplayerCode(p.getId(),code)){ model.addAttribute("multiplayerGameCode",code); session.setAttribute("multiplayerCode",code); - return "game/lobby"; + return "redirect:/game/lobby"; } return "game/multiplayerGame"; } + @GetMapping("/game/lobby/{code}") + @ResponseBody + public List updatePlayerList(@PathVariable String code) { + List players = playerService.getUsersByMultiplayerCode(Integer.parseInt(code)); + List playerNames = new ArrayList<>(); + for (Player player : players) { + playerNames.add(player.getUsername()); + } + return playerNames; + } @GetMapping("/game/lobby") - public String createLobby( HttpSession session, Principal principal, Model model) { + public String createLobby( HttpSession session, Model model) { int code = Integer.parseInt((String)session.getAttribute("multiplayerCode")); List players=playerService.getUsersByMultiplayerCode(code); model.addAttribute("players",players); + model.addAttribute("code",code); return "/game/lobby"; } diff --git a/src/main/resources/templates/game/lobby.html b/src/main/resources/templates/game/lobby.html index 632b47ce..fa7b8517 100644 --- a/src/main/resources/templates/game/lobby.html +++ b/src/main/resources/templates/game/lobby.html @@ -1,9 +1,42 @@ + -
+
+

Jugadores unidos a la partida:

+

Comparte el código de tu partida con tus amigos

+ +
    +
  • +
+
+ + + + From 989fee51ed55f099f050e0ed7e443c95e165a0c5 Mon Sep 17 00:00:00 2001 From: uo288061 Date: Wed, 10 Apr 2024 23:58:26 +0200 Subject: [PATCH 07/40] fix some problems --- .../uniovi/controllers/GameController.java | 2 +- .../services/impl/PlayerServiceImpl.java | 13 ++++++- src/main/resources/messages.properties | 5 +++ src/main/resources/messages_en.properties | 4 +++ src/main/resources/messages_es.properties | 6 ++++ src/main/resources/messages_fr.properties | 5 +++ src/main/resources/templates/game/lobby.html | 7 ++-- .../templates/game/multiplayerGame.html | 34 ++++++++++++++----- 8 files changed, 61 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index 25c73517..c266dea4 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -70,7 +70,7 @@ public String joinMultiplayerGame(@PathVariable String code, HttpSession session session.setAttribute("multiplayerCode",code); return "redirect:/game/lobby"; } - return "game/multiplayerGame"; + return "redirect:/multiplayerGame"; } @GetMapping("/game/lobby/{code}") diff --git a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java index 0071aebb..26582630 100644 --- a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java @@ -148,7 +148,7 @@ public boolean changeMultiplayerCode(Long id, String code) { return false; Player p = player.get(); - if(true/*existsMultiplayerCode(code)*/){ + if(existsMultiplayerCode(code)){ p.setMultiplayerCode(Integer.parseInt(code)); playerRepository.save(p); return true; @@ -163,6 +163,17 @@ private boolean existsMultiplayerCode(String code){ return ! getUsersByMultiplayerCode(Integer.parseInt(code)).isEmpty(); } + private void createMultiplayerGame(Long id){ + Optional player = playerRepository.findById(id); + if (player.isEmpty()) + return; + + Player p = player.get(); + int code = (int)Math.floor(Math.random()*10000); + p.setMultiplayerCode(code); + playerRepository.save(p); + } + @Override public void deletePlayer(Long id) { playerRepository.deleteById(id); diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 883e3c19..7c52e584 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -89,6 +89,11 @@ multi.create = Crear multi.placeholder= Introduce el código de una partida multi.label = Únete a una partida multi.join = Unirse +multi.onlyNumber = Solo se permiten números + +# -------------------Statements for the lobby.html file--------------------- +lobby.info =Jugadores unidos a la partida: +lobby.friends =Comparte el código de tu partida con tus amigos # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso. diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index bf3b1b6f..822b9262 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -91,6 +91,10 @@ multi.create = Create multi.placeholder=Enter the game code multi.label=Join a game multi.join = Join +multi.onlyNumber=Only numbers allowed +# -------------------Statements for the lobby.html file--------------------- +lobby.info =Players joining the game: +lobby.friends =Share your game code with your friends # -------------------Statements for the apiHome.html file--------------------- diff --git a/src/main/resources/messages_es.properties b/src/main/resources/messages_es.properties index f3df8923..adc24abf 100644 --- a/src/main/resources/messages_es.properties +++ b/src/main/resources/messages_es.properties @@ -92,6 +92,12 @@ multi.create = Crear multi.placeholder= Introduce el código de una partida multi.label = Únete a una partida multi.join = Unirse +multi.onlyNumber = Solo se permiten números + + +# -------------------Statements for the lobby.html file--------------------- +lobby.info =Jugadores unidos a la partida: +lobby.friends =Comparte el código de tu partida con tus amigos # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index 49103510..5da89f5c 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -85,6 +85,11 @@ multi.create = Créer multi.placeholder=Entrez le code du jeu multi.label=Rejoignez une partie multi.join =Rejoindre +multi.onlyNumber=Seuls les chiffres sont autorisés + +# -------------------Statements for the lobby.html file--------------------- +lobby.info =Joueurs rejoignant le jeu : +lobby.friends =Partagez votre code de jeu avec vos amis # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentation de l'API diff --git a/src/main/resources/templates/game/lobby.html b/src/main/resources/templates/game/lobby.html index fa7b8517..e1835a09 100644 --- a/src/main/resources/templates/game/lobby.html +++ b/src/main/resources/templates/game/lobby.html @@ -7,17 +7,14 @@
-

Jugadores unidos a la partida:

-

Comparte el código de tu partida con tus amigos

+

+

- - -

@@ -35,3 +31,25 @@ + + From c1a4d4ee0f8c0dbde9dcc6ea2016a632e08976fa Mon Sep 17 00:00:00 2001 From: uo288061 Date: Thu, 11 Apr 2024 12:51:24 +0200 Subject: [PATCH 08/40] create and join buttons working correctly --- .../uniovi/controllers/GameController.java | 20 ++++++++++++++++--- .../com/uniovi/services/PlayerService.java | 2 ++ .../services/impl/PlayerServiceImpl.java | 10 ++++------ src/main/resources/templates/game/lobby.html | 6 ++++-- .../templates/game/multiplayerGame.html | 7 +++---- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index c266dea4..4b0b83df 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -58,7 +58,10 @@ public String getGame(HttpSession session, Model model, Principal principal) { @GetMapping("/multiplayerGame") public String getMultiplayerGame() { - return "game/multiplayerGame"; + //EL elminar el multiplaterCode del jugador se puede hacer cuando comienze el proximo + //juego con amigos o cuando acaba la partida, lo suyo seria cuando acabe + //ya mirare como + return "game/multiplayerGame"; } @GetMapping("/multiplayerGame/{code}") @@ -69,8 +72,19 @@ public String joinMultiplayerGame(@PathVariable String code, HttpSession session model.addAttribute("multiplayerGameCode",code); session.setAttribute("multiplayerCode",code); return "redirect:/game/lobby"; + } else { + //Hay q tratarlo como como se hace en sinUp, hacienado validate y tal + return "redirect:/multiplayerGame"; } - return "redirect:/multiplayerGame"; + } + + @GetMapping("/multiplayerGame/createGame") + public String createMultiplayerGame(HttpSession session, Principal principal, Model model) { + Optional player = playerService.getUserByUsername(principal.getName()); + Player p = player.orElse(null); + String code=""+playerService.createMultiplayerGame(p.getId()); + session.setAttribute("multiplayerCode",code); + return "redirect:/game/lobby"; } @GetMapping("/game/lobby/{code}") @@ -88,7 +102,7 @@ public String createLobby( HttpSession session, Model model) { int code = Integer.parseInt((String)session.getAttribute("multiplayerCode")); List players=playerService.getUsersByMultiplayerCode(code); model.addAttribute("players",players); - model.addAttribute("code",code); + model.addAttribute("code",session.getAttribute("multiplayerCode")); return "/game/lobby"; } diff --git a/src/main/java/com/uniovi/services/PlayerService.java b/src/main/java/com/uniovi/services/PlayerService.java index fa77b9ca..e80a82c5 100644 --- a/src/main/java/com/uniovi/services/PlayerService.java +++ b/src/main/java/com/uniovi/services/PlayerService.java @@ -81,6 +81,8 @@ public interface PlayerService { */ boolean changeMultiplayerCode(Long id, String code); + int createMultiplayerGame(Long id); + /** * Delete a player from the database * @param id The id of the player to delete diff --git a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java index 26582630..68b3b619 100644 --- a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java @@ -6,16 +6,13 @@ import com.uniovi.entities.Associations; import com.uniovi.entities.Player; import com.uniovi.repositories.PlayerRepository; -import com.uniovi.repositories.RoleRepository; import com.uniovi.services.PlayerService; import com.uniovi.services.RoleService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import com.uniovi.entities.Role; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -162,16 +159,17 @@ public boolean changeMultiplayerCode(Long id, String code) { private boolean existsMultiplayerCode(String code){ return ! getUsersByMultiplayerCode(Integer.parseInt(code)).isEmpty(); } - - private void createMultiplayerGame(Long id){ + @Override + public int createMultiplayerGame(Long id){ Optional player = playerRepository.findById(id); if (player.isEmpty()) - return; + return -1; Player p = player.get(); int code = (int)Math.floor(Math.random()*10000); p.setMultiplayerCode(code); playerRepository.save(p); + return code; } @Override diff --git a/src/main/resources/templates/game/lobby.html b/src/main/resources/templates/game/lobby.html index e1835a09..8eff1a48 100644 --- a/src/main/resources/templates/game/lobby.html +++ b/src/main/resources/templates/game/lobby.html @@ -7,8 +7,10 @@

-

-

+

+

+ +

  • diff --git a/src/main/resources/templates/game/multiplayerGame.html b/src/main/resources/templates/game/multiplayerGame.html index ee208a03..e9c29ccf 100644 --- a/src/main/resources/templates/game/multiplayerGame.html +++ b/src/main/resources/templates/game/multiplayerGame.html @@ -15,16 +15,15 @@
- - +

- + -
From acd0e36bd2b66727fbf95d14cadb8af75ca9b508 Mon Sep 17 00:00:00 2001 From: uo288061 Date: Fri, 12 Apr 2024 00:04:13 +0200 Subject: [PATCH 09/40] players can can play multiplayer games --- .../uniovi/controllers/GameController.java | 14 +++++++++++++ src/main/java/com/uniovi/dto/PlayerDto.java | 4 ++-- .../services/impl/PlayerServiceImpl.java | 4 ++-- src/main/resources/messages.properties | 5 +++++ src/main/resources/messages_en.properties | 3 +++ src/main/resources/messages_es.properties | 4 ++++ src/main/resources/messages_fr.properties | 4 ++++ .../game/fragments/multiFinished.html | 21 +++++++++++++++++++ src/main/resources/templates/game/lobby.html | 3 +++ 9 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/templates/game/fragments/multiFinished.html diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index 4b0b83df..29c87f00 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -106,6 +106,13 @@ public String createLobby( HttpSession session, Model model) { return "/game/lobby"; } + @GetMapping("/game/startMultiplayerGame") + public String startMultiplayerGame( HttpSession session, Model model) { + //La idea seria q dando uno al boton de empezar empezasen todos + return "/game/lobby"; + } + + /** * This method is used to check the answer for a specific question @@ -163,6 +170,13 @@ else if(questionService.checkAnswer(idQuestion, idAnswer)) { public String updateGame(Model model, HttpSession session) { GameSession gameSession = (GameSession) session.getAttribute("gameSession"); Question nextQuestion = gameSession.getCurrentQuestion(); + if(nextQuestion == null && session.getAttribute("multiplayerCode") !=null){ + gameSessionService.endGame(gameSession); + session.removeAttribute("gameSession"); + model.addAttribute("score", gameSession.getScore()); + model.addAttribute("multiplayerCode",session.getAttribute("multiplayerCode")); + return "game/fragments/multiFinished"; + } if (nextQuestion == null) { gameSessionService.endGame(gameSession); session.removeAttribute("gameSession"); diff --git a/src/main/java/com/uniovi/dto/PlayerDto.java b/src/main/java/com/uniovi/dto/PlayerDto.java index b3d1e87d..2e3d6571 100644 --- a/src/main/java/com/uniovi/dto/PlayerDto.java +++ b/src/main/java/com/uniovi/dto/PlayerDto.java @@ -22,8 +22,8 @@ public class PlayerDto { @Schema(hidden = true) private String passwordConfirm; - @Schema(description = "code of group of the player", example = "5565") - private Integer multiplayerCode; + //@Schema(description = "code of group of the player", example = "5565") + //private Integer multiplayerCode; @Schema(description = "Roles of the player", example = "[\"ROLE_USER\"]") private String[] roles; diff --git a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java index 68b3b619..bd96e189 100644 --- a/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/PlayerServiceImpl.java @@ -120,8 +120,8 @@ public void updatePlayer(Long id, PlayerDto playerDto) { p.setUsername(playerDto.getUsername()); if (playerDto.getPassword() != null) p.setPassword(passwordEncoder.encode(playerDto.getPassword())); - if(playerDto.getMultiplayerCode() != null) - p.setMultiplayerCode(playerDto.getMultiplayerCode()); + //if(playerDto.getMultiplayerCode() != null) + // p.setMultiplayerCode(playerDto.getMultiplayerCode()); if (playerDto.getRoles() != null) { p.getRoles().clear(); for (String roleStr : playerDto.getRoles()) { diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 7c52e584..bdcbb083 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -94,6 +94,11 @@ multi.onlyNumber = Solo se permiten números # -------------------Statements for the lobby.html file--------------------- lobby.info =Jugadores unidos a la partida: lobby.friends =Comparte el código de tu partida con tus amigos +lobby.start = Empezar + +# -------------------Statements for the lobby.html file--------------------- +multi.code= Código de la partida + # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso. diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 822b9262..306cd4bd 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -95,7 +95,10 @@ multi.onlyNumber=Only numbers allowed # -------------------Statements for the lobby.html file--------------------- lobby.info =Players joining the game: lobby.friends =Share your game code with your friends +lobby.start =Start +# -------------------Statements for the lobby.html file--------------------- +multi.code= Game code # -------------------Statements for the apiHome.html file--------------------- api.doc.title=API Documentation diff --git a/src/main/resources/messages_es.properties b/src/main/resources/messages_es.properties index adc24abf..cca0ef1a 100644 --- a/src/main/resources/messages_es.properties +++ b/src/main/resources/messages_es.properties @@ -98,6 +98,10 @@ multi.onlyNumber = Solo se permiten números # -------------------Statements for the lobby.html file--------------------- lobby.info =Jugadores unidos a la partida: lobby.friends =Comparte el código de tu partida con tus amigos +lobby.start =Empezar + +# -------------------Statements for the lobby.html file--------------------- +multi.code= Código de la partida # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index 5da89f5c..49502cf8 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -90,6 +90,10 @@ multi.onlyNumber=Seuls les chiffres sont autorisés # -------------------Statements for the lobby.html file--------------------- lobby.info =Joueurs rejoignant le jeu : lobby.friends =Partagez votre code de jeu avec vos amis +lobby.start = Commencer + +# -------------------Statements for the lobby.html file--------------------- +multi.code=Code du jeu # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentation de l'API diff --git a/src/main/resources/templates/game/fragments/multiFinished.html b/src/main/resources/templates/game/fragments/multiFinished.html new file mode 100644 index 00000000..8837fc95 --- /dev/null +++ b/src/main/resources/templates/game/fragments/multiFinished.html @@ -0,0 +1,21 @@ +
+

+

+ + +

+ +

+ + +

+ +
+
+ +
\ No newline at end of file diff --git a/src/main/resources/templates/game/lobby.html b/src/main/resources/templates/game/lobby.html index 8eff1a48..1ee2ebdf 100644 --- a/src/main/resources/templates/game/lobby.html +++ b/src/main/resources/templates/game/lobby.html @@ -16,6 +16,9 @@

  • + + +
    - \ No newline at end of file diff --git a/src/main/resources/templates/game/multiFinished.html b/src/main/resources/templates/game/multiFinished.html new file mode 100644 index 00000000..a2493267 --- /dev/null +++ b/src/main/resources/templates/game/multiFinished.html @@ -0,0 +1,44 @@ + + + + + + + +
    +

    +

    + +

    + + + + + +
      +
    • +
    +
    +
    + From 81accd16306b94d6285a597bc9bb31c373f10e9b Mon Sep 17 00:00:00 2001 From: uo288061 Date: Sun, 14 Apr 2024 12:28:01 +0200 Subject: [PATCH 11/40] Players with same multiplayerCode, have same questions --- .../uniovi/controllers/GameController.java | 22 ++++++++++- .../uniovi/services/GameSessionService.java | 3 ++ .../com/uniovi/services/QuestionService.java | 2 + .../uniovi/services/impl/GameSessionImpl.java | 6 ++- .../services/impl/QuestionServiceImpl.java | 38 +++++++++++++++++-- src/main/resources/templates/game/lobby.html | 2 +- 6 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index 5f13e1bc..85cd910a 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -40,13 +40,13 @@ public GameController(QuestionService questionService, GameSessionService gameSe @GetMapping("/game") public String getGame(HttpSession session, Model model, Principal principal) { GameSession gameSession = (GameSession) session.getAttribute("gameSession"); - if (gameSession != null) { if (checkUpdateGameSession(gameSession, session)) { return "game/fragments/gameFinished"; } } else { gameSession = gameSessionService.startNewGame(getLoggedInPlayer(principal)); + playerService.deleteMultiplayerCode(gameSession.getPlayer().getId()); session.setAttribute("gameSession", gameSession); } @@ -64,6 +64,26 @@ public String getMultiplayerGame() { return "game/multiplayerGame"; } + @GetMapping("/startMultiplayerGame") + public String getMultiplayerGame(HttpSession session, Model model, Principal principal) { + GameSession gameSession = (GameSession) session.getAttribute("gameSession"); + + if (gameSession != null) { + if (checkUpdateGameSession(gameSession, session)) { + return "game/fragments/gameFinished"; + } + } else { + gameSession = gameSessionService.startNewMultiplayerGame(getLoggedInPlayer(principal), + playerService.getUserByUsername(principal.getName()).get().getMultiplayerCode()); + session.setAttribute("gameSession", gameSession); + + } + + model.addAttribute("question", gameSession.getCurrentQuestion()); + model.addAttribute("questionDuration", getRemainingTime(gameSession)); + return "game/basicGame"; + } + @GetMapping("/multiplayerGame/{code}") public String joinMultiplayerGame(@PathVariable String code, HttpSession session, Principal principal, Model model) { Optional player = playerService.getUserByUsername(principal.getName()); diff --git a/src/main/java/com/uniovi/services/GameSessionService.java b/src/main/java/com/uniovi/services/GameSessionService.java index e3eb4c55..a70c194c 100644 --- a/src/main/java/com/uniovi/services/GameSessionService.java +++ b/src/main/java/com/uniovi/services/GameSessionService.java @@ -43,5 +43,8 @@ public interface GameSessionService { Page getPlayerRanking(Pageable pageable, Player player); GameSession startNewGame(Player player); + + GameSession startNewMultiplayerGame(Player player, int code); + void endGame(GameSession gameSession); } diff --git a/src/main/java/com/uniovi/services/QuestionService.java b/src/main/java/com/uniovi/services/QuestionService.java index 7eb7d422..30e36c3d 100644 --- a/src/main/java/com/uniovi/services/QuestionService.java +++ b/src/main/java/com/uniovi/services/QuestionService.java @@ -59,6 +59,8 @@ public interface QuestionService { */ List getRandomQuestions(int num); + List getRandomMultiplayerQuestions(int num, int code); + /** * Check if the answer is correct * @param idquestion The id of the question diff --git a/src/main/java/com/uniovi/services/impl/GameSessionImpl.java b/src/main/java/com/uniovi/services/impl/GameSessionImpl.java index d7e2ad3a..aca3509e 100644 --- a/src/main/java/com/uniovi/services/impl/GameSessionImpl.java +++ b/src/main/java/com/uniovi/services/impl/GameSessionImpl.java @@ -7,7 +7,6 @@ import com.uniovi.services.GameSessionService; import com.uniovi.services.QuestionService; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -51,6 +50,11 @@ public GameSession startNewGame(Player player) { return new GameSession(player, questionService.getRandomQuestions(NORMAL_GAME_QUESTION_NUM)); } + @Override + public GameSession startNewMultiplayerGame(Player player, int code) { + return new GameSession(player, questionService.getRandomMultiplayerQuestions(NORMAL_GAME_QUESTION_NUM,code)); + } + @Override public void endGame(GameSession gameSession) { Associations.PlayerGameSession.addGameSession(gameSession.getPlayer(), gameSession); diff --git a/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java b/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java index f0ec9f7e..79e4c218 100644 --- a/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java @@ -12,15 +12,13 @@ import com.uniovi.services.QuestionService; import jakarta.persistence.EntityManager; import jakarta.transaction.Transactional; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.querydsl.QPageRequest; import org.springframework.stereotype.Service; import java.security.SecureRandom; -import java.sql.SQLException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -110,6 +108,38 @@ public List getRandomQuestions(int num) { return res; } + @Override + public List getRandomMultiplayerQuestions(int num, int code) { + List allQuestions = questionRepository.findAll().stream() + .filter(question -> question.getLanguage().equals(LocaleContextHolder.getLocale().getLanguage())).toList(); + List res = new ArrayList<>(); + int size= allQuestions.size(); + + int currentIndex = generateIndex(code, size) -4; + + for (int i = 0; i < num; i++) { + + Question question = allQuestions.get(currentIndex); + + while (question.hasEmptyOptions() || res.contains(question)) { + question = allQuestions.get(currentIndex); + } + + res.add(question); + currentIndex++; + } + return res; + } + private int generateIndex(int code, int size) { + int hashCode = combineHash(code, LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); + return Math.abs(hashCode) % size; + } + + private int combineHash(int code, String date) { + String combinedString = code + date; + return combinedString.hashCode(); + } + @Override public boolean checkAnswer(Long idquestion, Long idanswer) { Optional q = questionRepository.findById(idquestion); diff --git a/src/main/resources/templates/game/lobby.html b/src/main/resources/templates/game/lobby.html index 1ee2ebdf..16b72e96 100644 --- a/src/main/resources/templates/game/lobby.html +++ b/src/main/resources/templates/game/lobby.html @@ -16,7 +16,7 @@

  • - +
    From 4968265365f587cba0f5b38cded88396cd14f29e Mon Sep 17 00:00:00 2001 From: RicardoDiNu Date: Mon, 15 Apr 2024 20:33:13 +0200 Subject: [PATCH 12/40] Update multiplayerGame.html style --- src/main/resources/static/css/multiplayer.css | 3 ++ .../templates/game/multiplayerGame.html | 38 ++++++++++--------- src/main/resources/templates/index.html | 1 + 3 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 src/main/resources/static/css/multiplayer.css diff --git a/src/main/resources/static/css/multiplayer.css b/src/main/resources/static/css/multiplayer.css new file mode 100644 index 00000000..f52b5a09 --- /dev/null +++ b/src/main/resources/static/css/multiplayer.css @@ -0,0 +1,3 @@ +.display-5 { + margin-top: 3em; +} \ No newline at end of file diff --git a/src/main/resources/templates/game/multiplayerGame.html b/src/main/resources/templates/game/multiplayerGame.html index e9c29ccf..38bbae5e 100644 --- a/src/main/resources/templates/game/multiplayerGame.html +++ b/src/main/resources/templates/game/multiplayerGame.html @@ -2,28 +2,30 @@ - + - -
    - - - - +
    + +
    + + + + +
    + + + +

    + +

    + + +
    - - - -

    - -

    - - -
    diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 434df7e5..3ab7de4e 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -10,6 +10,7 @@

    +

    From 863dc8f8ade5540583cde7cced87469ab60366ab Mon Sep 17 00:00:00 2001 From: RicardoDiNu Date: Mon, 15 Apr 2024 20:33:29 +0200 Subject: [PATCH 13/40] Update lobby.html style --- src/main/resources/messages.properties | 3 +- src/main/resources/messages_en.properties | 3 +- src/main/resources/messages_es.properties | 3 +- src/main/resources/messages_fr.properties | 3 +- src/main/resources/static/css/multiplayer.css | 16 +++++++++- src/main/resources/templates/game/lobby.html | 31 ++++++++++++++----- 6 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 907e3eac..f2c9fd66 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -37,7 +37,7 @@ error.error=Error: index.heading=WIQ index.subtitle=Responde las preguntas correctamente y GANA!!! index.button=JUGAR -index.multiplayer.button= Juega con amigos +index.multiplayer.button=JUGAR CON AMIGOS # -------------------Statements for the home.html file--------------------- home.heading=Bienvenido @@ -90,6 +90,7 @@ multi.placeholder= Introduce el código de una partida multi.label = Únete a una partida multi.join = Unirse multi.onlyNumber = Solo se permiten números +multi.copyCode= Copiar código # -------------------Statements for the lobby.html file--------------------- lobby.info =Jugadores unidos a la partida: diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 07ce0be4..27524e6e 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -37,7 +37,7 @@ error.error=Error: index.heading=WIQ index.subtitle=Answer the questions correctly and WIN!!! index.button=PLAY -index.multiplayer.button= Play with friends +index.multiplayer.button=PLAY WITH FRIENDS # -------------------Statements for the home.html file--------------------- @@ -92,6 +92,7 @@ multi.placeholder=Enter the game code multi.label=Join a game multi.join = Join multi.onlyNumber=Only numbers allowed +multi.copyCode= Copy code # -------------------Statements for the lobby.html file--------------------- lobby.info =Players joining the game: lobby.friends =Share your game code with your friends diff --git a/src/main/resources/messages_es.properties b/src/main/resources/messages_es.properties index b8b91a94..fbc770ba 100644 --- a/src/main/resources/messages_es.properties +++ b/src/main/resources/messages_es.properties @@ -38,7 +38,7 @@ error.error=Error: index.heading=WIQ index.subtitle=Responde las preguntas correctamente y GANA!!! index.button=JUGAR -index.multiplayer.button= Juega con amigos +index.multiplayer.button=JUGAR CON AMIGOS # -------------------Statements for the home.html file--------------------- @@ -93,6 +93,7 @@ multi.placeholder= Introduce el código de una partida multi.label = Únete a una partida multi.join = Unirse multi.onlyNumber = Solo se permiten números +multi.copyCode= Copiar código # -------------------Statements for the lobby.html file--------------------- diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index 9281e4cd..d540adf3 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -36,7 +36,7 @@ error.error=Erreur : index.heading=WIQ index.subtitle=Répondez aux questions correctement et GAGNEZ !!! index.button=JOUER -index.multiplayer.button=Jouez avec des amis +index.multiplayer.button=JOUEZ AVEC DES AMIS # -------------------Statements for the home.html file--------------------- home.heading=Bienvenue @@ -86,6 +86,7 @@ multi.placeholder=Entrez le code du jeu multi.label=Rejoignez une partie multi.join =Rejoindre multi.onlyNumber=Seuls les chiffres sont autorisés +multi.copyCode= Copier le code # -------------------Statements for the lobby.html file--------------------- lobby.info =Joueurs rejoignant le jeu : diff --git a/src/main/resources/static/css/multiplayer.css b/src/main/resources/static/css/multiplayer.css index f52b5a09..f48f3fb4 100644 --- a/src/main/resources/static/css/multiplayer.css +++ b/src/main/resources/static/css/multiplayer.css @@ -1,3 +1,17 @@ .display-5 { margin-top: 3em; -} \ No newline at end of file +} + +#lobbyCode, #label-code { + font-size: 3em; +} + +#lobbyInfo { + margin-top: 3em; +} + +#playerList { + list-style-type: none; + text-align: center; + padding: 0; +} diff --git a/src/main/resources/templates/game/lobby.html b/src/main/resources/templates/game/lobby.html index 16b72e96..c3da3314 100644 --- a/src/main/resources/templates/game/lobby.html +++ b/src/main/resources/templates/game/lobby.html @@ -3,23 +3,26 @@ - + -
    -

    -

    -

    +
    +

    + + +

    + + +
    - - - +
    + From db833af981a47ab554d1077e50e14f2c66258a29 Mon Sep 17 00:00:00 2001 From: RicardoDiNu Date: Mon, 15 Apr 2024 20:45:53 +0200 Subject: [PATCH 14/40] Fix small bug of code validation (only numbers are permited) --- .../resources/templates/game/multiplayerGame.html | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/resources/templates/game/multiplayerGame.html b/src/main/resources/templates/game/multiplayerGame.html index 38bbae5e..1be2cbb6 100644 --- a/src/main/resources/templates/game/multiplayerGame.html +++ b/src/main/resources/templates/game/multiplayerGame.html @@ -42,15 +42,22 @@ function toggleJoinButton() { var codeInput = document.getElementById("code"); var joinBtn = document.getElementById("joinBtn"); + var errorMsg = document.getElementById("error-msg"); + + if (codeInput.value.trim() === "") { + joinBtn.disabled = true; + errorMsg.style.display = "none"; + return; + } var containsOnlyNumbers = /^[0-9]+$/.test(codeInput.value); if (containsOnlyNumbers) { joinBtn.disabled = false; - document.getElementById("error-msg").style.display = "none"; + errorMsg.style.display = "none"; } else { joinBtn.disabled = true; - document.getElementById("error-msg").style.display = "block"; + errorMsg.style.display = "block"; } } From 586805e1ac352871310b3576806c020fce90e2e5 Mon Sep 17 00:00:00 2001 From: RicardoDiNu Date: Tue, 16 Apr 2024 00:01:41 +0200 Subject: [PATCH 15/40] Change multiFinished.html style --- .../resources/templates/game/multiFinished.html | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/resources/templates/game/multiFinished.html b/src/main/resources/templates/game/multiFinished.html index a2493267..59d75576 100644 --- a/src/main/resources/templates/game/multiFinished.html +++ b/src/main/resources/templates/game/multiFinished.html @@ -3,23 +3,26 @@ - + -
    + + +

    -

    +

    +
      +
    • +
    - -
      -
    • -
    +
    + - - - - -
    -

    + +

    + +

    -

    +

    - - - -
    + - +
    \ No newline at end of file From db70848afa61cb069b1445034ac4a6f0b2ad53ca Mon Sep 17 00:00:00 2001 From: RicardoDiNu Date: Tue, 16 Apr 2024 20:04:23 +0200 Subject: [PATCH 18/40] multiFinished style update --- src/main/resources/messages.properties | 4 ++++ src/main/resources/messages_en.properties | 4 ++++ src/main/resources/messages_es.properties | 3 +++ src/main/resources/messages_fr.properties | 4 ++++ src/main/resources/static/css/multiplayer.css | 9 +++++++++ 5 files changed, 24 insertions(+) diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index f2c9fd66..d60dc860 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -101,6 +101,10 @@ lobby.start = Empezar multi.code= Código de la partida multi.results =Ver resultados +# -------------------Statements for the multiFinished.html file--------------------- +multi.finished= Partida finalizada +multi.points = Puntuaciones + # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso. diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 27524e6e..9decb6b2 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -102,6 +102,10 @@ lobby.start =Start multi.code= Game code multi.results = See results +# -------------------Statements for the multiFinished.html file--------------------- +multi.finished= Finished game +multi.points = Points + # -------------------Statements for the apiHome.html file--------------------- api.doc.title=API Documentation api.doc.description=This document describes the REST API endpoints. diff --git a/src/main/resources/messages_es.properties b/src/main/resources/messages_es.properties index fbc770ba..23425529 100644 --- a/src/main/resources/messages_es.properties +++ b/src/main/resources/messages_es.properties @@ -105,6 +105,9 @@ lobby.start =Empezar multi.code= Código de la partida multi.results =Ver resultados +# -------------------Statements for the multiFinished.html file--------------------- +multi.finished= Partida finalizada +multi.points = Puntuaciones # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index d540adf3..cefd1890 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -97,6 +97,10 @@ lobby.start = Commencer multi.code=Code du jeu multi.results=Voir les résultats +# -------------------Statements for the multiFinished.html file--------------------- +multi.finished= Jeu terminé +multi.points = Points + # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentation de l'API diff --git a/src/main/resources/static/css/multiplayer.css b/src/main/resources/static/css/multiplayer.css index f48f3fb4..6715e189 100644 --- a/src/main/resources/static/css/multiplayer.css +++ b/src/main/resources/static/css/multiplayer.css @@ -15,3 +15,12 @@ text-align: center; padding: 0; } + +#finishedGame { + font-size: 5em; + margin-bottom: 0.5em; +} + +#multiPoints { + margin-top: 3em; +} From b4b0d1d3748e34bda10be2d45d7809a3f6337052 Mon Sep 17 00:00:00 2001 From: RicardoDiNu Date: Tue, 16 Apr 2024 21:37:53 +0200 Subject: [PATCH 19/40] Some endMultiGame logic added --- .../com/uniovi/controllers/GameController.java | 15 ++++++++++++++- .../resources/templates/game/multiFinished.html | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index eb7945b8..2fe79f4e 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -148,6 +148,15 @@ public Map updateFinishedGame(@PathVariable String code, HttpSess return playerInfo; } +// @GetMapping("/multiplayerGame/endGame/{code}") +// public String endMultiplayerGame(@PathVariable String code) { +// List players = playerService.getUsersByMultiplayerCode(Integer.parseInt(code)); +// for (Player player : players) { +// player.setScoreMultiplayerCode(null); +// } +// return "redirect:/index.html"; +// } + @GetMapping("/game/lobby/{code}") @@ -232,7 +241,7 @@ else if(questionService.checkAnswer(idQuestion, idAnswer)) { } @GetMapping("/game/update") - public String updateGame(Model model, HttpSession session) { + public String updateGame(Model model, HttpSession session, Principal principal) { GameSession gameSession = (GameSession) session.getAttribute("gameSession"); Question nextQuestion = gameSession.getCurrentQuestion(); if(nextQuestion == null && gameSession.getPlayer().getMultiplayerCode()!=null/*session.getAttribute("multiplayerCode") !=null*/){ @@ -244,6 +253,10 @@ public String updateGame(Model model, HttpSession session) { model.addAttribute("code",session.getAttribute("multiplayerCode")); session.removeAttribute("gameSession"); + Optional player = playerService.getUserByUsername(principal.getName()); + Player p = player.orElse(null); + playerService.setScoreMultiplayerCode(p.getId(),""+gameSession.getScore()); + return "game/multiFinished"; } if (nextQuestion == null) { diff --git a/src/main/resources/templates/game/multiFinished.html b/src/main/resources/templates/game/multiFinished.html index 7a09d28b..5e5860c0 100644 --- a/src/main/resources/templates/game/multiFinished.html +++ b/src/main/resources/templates/game/multiFinished.html @@ -11,6 +11,10 @@

  • + + + + diff --git a/src/main/resources/templates/ranking/multiplayerRanking_table.html b/src/main/resources/templates/ranking/multiplayerRanking_table.html deleted file mode 100644 index de5e46b9..00000000 --- a/src/main/resources/templates/ranking/multiplayerRanking_table.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - -
    PosiciónJugadorPuntuación
    \ No newline at end of file From 147bb7d4c2fa7bec4f72c43568df03948fb1f8ac Mon Sep 17 00:00:00 2001 From: uo288061 Date: Wed, 24 Apr 2024 23:08:36 +0200 Subject: [PATCH 28/40] small changes --- .../com/uniovi/controllers/GameController.java | 7 ++++--- .../resources/templates/game/multiFinished.html | 15 +++++---------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index e5a905d2..53c36810 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -193,10 +193,11 @@ public Map endMultiplayerGameTable(@PathVariable String code) { @GetMapping("/game/lobby/{code}") @ResponseBody public List updatePlayerList(@PathVariable String code) { - List players = playerService.getUsersByMultiplayerCode(Integer.parseInt(code)); + //List players = playerService.getUsersByMultiplayerCode(Integer.parseInt(code)); + Map players= multiplayerSessionService.getPlayersWithScores(Integer.parseInt(code)); List playerNames = new ArrayList<>(); - for (Player player : players) { - playerNames.add(player.getUsername()); + for (Map.Entry player : players.entrySet()) { + playerNames.add(player.getKey().getUsername()); } return playerNames; } diff --git a/src/main/resources/templates/game/multiFinished.html b/src/main/resources/templates/game/multiFinished.html index c3b2d6ff..54fa4c20 100644 --- a/src/main/resources/templates/game/multiFinished.html +++ b/src/main/resources/templates/game/multiFinished.html @@ -6,27 +6,22 @@

    -

    + - -->
    \ No newline at end of file From c85ee32b688f5057582ba42c83c7d7a64ab0f7f8 Mon Sep 17 00:00:00 2001 From: uo288061 Date: Thu, 25 Apr 2024 12:19:39 +0200 Subject: [PATCH 29/40] All multiplayerGame correct --- .../uniovi/controllers/GameController.java | 7 ++-- src/main/resources/messages.properties | 3 +- src/main/resources/messages_en.properties | 4 ++- src/main/resources/messages_es.properties | 4 +-- src/main/resources/messages_fr.properties | 3 +- .../templates/game/multiFinished.html | 35 ++++--------------- .../templates/ranking/multiplayerRanking.html | 7 ++-- 7 files changed, 19 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index 53c36810..25233374 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -70,9 +70,6 @@ public String getGame(HttpSession session, Model model, Principal principal) { @GetMapping("/multiplayerGame") public String getMultiplayerGame() { - //EL elminar el multiplaterCode del jugador se puede hacer cuando comienze el proximo - //juego con amigos o cuando acaba la partida, lo suyo seria cuando acabe - //ya mirare como return "game/multiplayerGame"; } @@ -87,7 +84,6 @@ public String joinMultiplayerGame(@PathVariable String code, HttpSession session session.setAttribute("multiplayerCode",code); return "redirect:/game/lobby"; } else { - //Hay q tratarlo como como se hace en sinUp, hacienado validate y tal return "redirect:/multiplayerGame"; } } @@ -193,12 +189,12 @@ public Map endMultiplayerGameTable(@PathVariable String code) { @GetMapping("/game/lobby/{code}") @ResponseBody public List updatePlayerList(@PathVariable String code) { - //List players = playerService.getUsersByMultiplayerCode(Integer.parseInt(code)); Map players= multiplayerSessionService.getPlayersWithScores(Integer.parseInt(code)); List playerNames = new ArrayList<>(); for (Map.Entry player : players.entrySet()) { playerNames.add(player.getKey().getUsername()); } + Collections.sort(playerNames); return playerNames; } @GetMapping("/game/lobby") @@ -292,6 +288,7 @@ public String updateGame(Model model, HttpSession session, Principal principal) isMultiPlayer=false; //return "game/multiplayerGame/endGame/"+p.getMultiplayerCode(); //return "redirect:multiplayerGame/endGame/"+p.getMultiplayerCode(); + //return "redirect:multiplayerGame/endGame/"+p.getMultiplayerCode(); return "game/multiFinished"; } if (nextQuestion == null) { diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index d60dc860..7a991f88 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -86,11 +86,12 @@ ranking.time=Tiempo # -------------------Statements for the multiplayerGame.html file--------------------- multi.text = ¿Aún no tienes un código? Crea uno y compártelo con tus amigos multi.create = Crear -multi.placeholder= Introduce el código de una partida +multi.placeholder= Introduce el código correcto multi.label = Únete a una partida multi.join = Unirse multi.onlyNumber = Solo se permiten números multi.copyCode= Copiar código +multi.info=Resultados para la partida: # -------------------Statements for the lobby.html file--------------------- lobby.info =Jugadores unidos a la partida: diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 9decb6b2..40a630b2 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -88,11 +88,13 @@ ranking.time=Time # -------------------Statements for the multiplayerGame.html file--------------------- multi.text =Don't have a code yet? Create one and share it with your friends multi.create = Create -multi.placeholder=Enter the game code +multi.placeholder=Enter the correct code multi.label=Join a game multi.join = Join multi.onlyNumber=Only numbers allowed multi.copyCode= Copy code +multi.info=reults for the game: + # -------------------Statements for the lobby.html file--------------------- lobby.info =Players joining the game: lobby.friends =Share your game code with your friends diff --git a/src/main/resources/messages_es.properties b/src/main/resources/messages_es.properties index 23425529..bc94b7b2 100644 --- a/src/main/resources/messages_es.properties +++ b/src/main/resources/messages_es.properties @@ -89,12 +89,12 @@ ranking.time=Tiempo # -------------------Statements for the multiplayerGame.html file--------------------- multi.text = ¿Aún no tienes un código? Crea uno y compártelo con tus amigos multi.create = Crear -multi.placeholder= Introduce el código de una partida +multi.placeholder= Introduce el código correcto multi.label = Únete a una partida multi.join = Unirse multi.onlyNumber = Solo se permiten números multi.copyCode= Copiar código - +multi.info=Resultados para la partida: # -------------------Statements for the lobby.html file--------------------- lobby.info =Jugadores unidos a la partida: diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index cefd1890..b9c54eee 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -82,11 +82,12 @@ ranking.time=Temps # -------------------Statements for the multiplayerGame.html file--------------------- multi.text = Vous n'avez pas encore de code ? Créez-en un et partagez-le avec vos amis. multi.create = Créer -multi.placeholder=Entrez le code du jeu +multi.placeholder=Entrez le bon code multi.label=Rejoignez une partie multi.join =Rejoindre multi.onlyNumber=Seuls les chiffres sont autorisés multi.copyCode= Copier le code +multi.info=résultats du jeu: # -------------------Statements for the lobby.html file--------------------- lobby.info =Joueurs rejoignant le jeu : diff --git a/src/main/resources/templates/game/multiFinished.html b/src/main/resources/templates/game/multiFinished.html index 54fa4c20..3aff9e76 100644 --- a/src/main/resources/templates/game/multiFinished.html +++ b/src/main/resources/templates/game/multiFinished.html @@ -6,37 +6,14 @@

    - - - + - - + \ No newline at end of file diff --git a/src/main/resources/templates/ranking/multiplayerRanking.html b/src/main/resources/templates/ranking/multiplayerRanking.html index 5c85427a..e47b2963 100644 --- a/src/main/resources/templates/ranking/multiplayerRanking.html +++ b/src/main/resources/templates/ranking/multiplayerRanking.html @@ -8,10 +8,8 @@
    -

    Fin multijugador

    - +

    +

    @@ -25,7 +23,6 @@

    Fin multijugador

    function updatePlayerList() { const code = [[${code}]]; $.ajax({ - //url: '/game/multiFinished/' + code, url: '/endGameList/' + code, type: 'GET', dataType: 'json', From af999eb0c38abf74c0c1b4d32939920846395f06 Mon Sep 17 00:00:00 2001 From: uo288061 Date: Thu, 25 Apr 2024 12:55:25 +0200 Subject: [PATCH 30/40] cleaning code --- .../uniovi/controllers/GameController.java | 59 +------------------ .../uniovi/entities/MultiplayerSession.java | 3 +- .../MultiplayerSessionRepository.java | 5 -- .../services/impl/MultiplayerSessionImpl.java | 9 +-- src/main/resources/messages.properties | 2 +- src/main/resources/messages_en.properties | 2 + src/main/resources/messages_es.properties | 2 + src/main/resources/messages_fr.properties | 1 + .../templates/game/multiplayerGame.html | 3 - .../templates/ranking/multiplayerRanking.html | 6 ++ 10 files changed, 18 insertions(+), 74 deletions(-) diff --git a/src/main/java/com/uniovi/controllers/GameController.java b/src/main/java/com/uniovi/controllers/GameController.java index 25233374..a28f181d 100644 --- a/src/main/java/com/uniovi/controllers/GameController.java +++ b/src/main/java/com/uniovi/controllers/GameController.java @@ -65,9 +65,6 @@ public String getGame(HttpSession session, Model model, Principal principal) { return "game/basicGame"; } - - - @GetMapping("/multiplayerGame") public String getMultiplayerGame() { return "game/multiplayerGame"; @@ -121,50 +118,10 @@ public String startMultiplayerGame(HttpSession session, Model model, Principal p return "game/basicGame"; } -// @GetMapping("/games/multiFinished") -// public String createFinishedLobby( HttpSession session, Model model) { -// int code = Integer.parseInt((String)session.getAttribute("multiplayerCode")); -// List players=playerService.getUsersByMultiplayerCode(code); -// model.addAttribute("players",players); -// model.addAttribute("code",session.getAttribute("multiplayerCode")); -// session.removeAttribute("gameSession"); -// return "/game/multiFinished"; -// } - - -// @GetMapping("/multiplayerGame/finishedGame") -// public String goToFinishedLobby(HttpSession session,Principal principal) { -// Optional player = playerService.getUserByUsername(principal.getName()); -// Player p = player.orElse(null); -// GameSession gameSession = (GameSession) session.getAttribute("gameSession"); -// playerService.setScoreMultiplayerCode(p.getId(),""+gameSession.getScore()); -// return "redirect:/game/multiFinished"; -// } -// - - -// @GetMapping("/game/multiFinished/{code}") -// @ResponseBody -// public Map updateFinishedGame(@PathVariable String code) { -// List players = playerService.getUsersByMultiplayerCode(Integer.parseInt(code)); -// Map playerInfo = new HashMap<>(); -// for (Player player : players) { -// String playerName = player.getUsername(); -// String playerScore = player.getScoreMultiplayerCode(); -// if(playerScore==null){ -// playerScore="N/A"; -// } -// playerInfo.put(playerName, playerScore); -// } -// return playerInfo; -// } - @GetMapping("/multiplayerGame/endGame/{code}") public String endMultiplayerGame(Model model,@PathVariable String code) { -// List playersWithScores =multiplayerSessionService.getPlayersWithScores(Integer.parseInt(code)); -// model.addAttribute("MultiplayerRanking", playersWithScores); - model.addAttribute("code",code); - return "ranking/multiplayerRanking"; + model.addAttribute("code",code); + return "ranking/multiplayerRanking"; } @GetMapping("/endGameList/{code}") @ResponseBody @@ -175,7 +132,7 @@ public Map endMultiplayerGameTable(@PathVariable String code) { String playerName = player.getKey().getUsername(); String playerScoreValue; if(player.getValue()==-1){ - playerScoreValue="N/A"; //estaria bien q pusiese jugagando pero internazionalizadp + playerScoreValue="N/A"; }else{ playerScoreValue=""+player.getValue(); } @@ -184,8 +141,6 @@ public Map endMultiplayerGameTable(@PathVariable String code) { return playersNameWithScore; } - - @GetMapping("/game/lobby/{code}") @ResponseBody public List updatePlayerList(@PathVariable String code) { @@ -208,14 +163,9 @@ public String createLobby( HttpSession session, Model model) { @GetMapping("/game/startMultiplayerGame") public String startMultiplayerGame( HttpSession session, Model model) { - //La idea seria q dando uno al boton de empezar empezasen todos return "/game/lobby"; } - - - - /** * This method is used to check the answer for a specific question * @param idQuestion The id of the question. @@ -286,9 +236,6 @@ public String updateGame(Model model, HttpSession session, Principal principal) playerService.setScoreMultiplayerCode(p.getId(),""+gameSession.getScore()); multiplayerSessionService.changeScore(p.getMultiplayerCode()+"",p.getId(),gameSession.getScore()); isMultiPlayer=false; - //return "game/multiplayerGame/endGame/"+p.getMultiplayerCode(); - //return "redirect:multiplayerGame/endGame/"+p.getMultiplayerCode(); - //return "redirect:multiplayerGame/endGame/"+p.getMultiplayerCode(); return "game/multiFinished"; } if (nextQuestion == null) { diff --git a/src/main/java/com/uniovi/entities/MultiplayerSession.java b/src/main/java/com/uniovi/entities/MultiplayerSession.java index e80d72bf..e9b77a5c 100644 --- a/src/main/java/com/uniovi/entities/MultiplayerSession.java +++ b/src/main/java/com/uniovi/entities/MultiplayerSession.java @@ -19,8 +19,7 @@ public class MultiplayerSession { private Long id; @Column private String multiplayerCode; - //@ManyToMany - //private Set players =new HashSet<>(); + @ElementCollection @Column private Map playerScores = new HashMap<>(); diff --git a/src/main/java/com/uniovi/repositories/MultiplayerSessionRepository.java b/src/main/java/com/uniovi/repositories/MultiplayerSessionRepository.java index 01a5b114..fd9e926e 100644 --- a/src/main/java/com/uniovi/repositories/MultiplayerSessionRepository.java +++ b/src/main/java/com/uniovi/repositories/MultiplayerSessionRepository.java @@ -12,11 +12,6 @@ import java.util.List; public interface MultiplayerSessionRepository extends CrudRepository { - //List findAll(); - -// @Query("SELECT m FROM MultiplayerSession m JOIN FETCH m.playerScores p WHERE m.multiplayerCode = :multiplayerCode ORDER BY p.scoreMultiplayerCode") -// Page findPlayersByMultiplayerCode(Pageable pageable, String multiplayerCode); - MultiplayerSession findByMultiplayerCode(String code); } diff --git a/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java b/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java index bd05d178..05acec97 100644 --- a/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java +++ b/src/main/java/com/uniovi/services/impl/MultiplayerSessionImpl.java @@ -14,8 +14,8 @@ @Service public class MultiplayerSessionImpl implements MultiplayerSessionService { - private PlayerRepository playerRepository; - private MultiplayerSessionRepository multiplayerSessionRepository; + private final PlayerRepository playerRepository; + private final MultiplayerSessionRepository multiplayerSessionRepository; public MultiplayerSessionImpl(PlayerRepository playerRepository, MultiplayerSessionRepository multiplayerSessionRepository) { @@ -23,11 +23,6 @@ public MultiplayerSessionImpl(PlayerRepository playerRepository, MultiplayerSess this.multiplayerSessionRepository = multiplayerSessionRepository; } -// @Override -// public Page getMultiplayerPlayerRanking(Pageable pageable, int multiplayerCode) { -// return multiplayerSessionRepository.findPlayersByMultiplayerCode(pageable, ""+multiplayerCode); -// } - @Override public Map getPlayersWithScores(int multiplayerCode) { MultiplayerSession session = multiplayerSessionRepository.findByMultiplayerCode(String.valueOf(multiplayerCode)); diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 7a991f88..2c2e4c47 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -105,7 +105,7 @@ multi.results =Ver resultados # -------------------Statements for the multiFinished.html file--------------------- multi.finished= Partida finalizada multi.points = Puntuaciones - +multi.menu = Ir a la página de inicio # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso. diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 40a630b2..5a2d0015 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -107,6 +107,8 @@ multi.results = See results # -------------------Statements for the multiFinished.html file--------------------- multi.finished= Finished game multi.points = Points +multi.menu =Go to home page + # -------------------Statements for the apiHome.html file--------------------- api.doc.title=API Documentation diff --git a/src/main/resources/messages_es.properties b/src/main/resources/messages_es.properties index bc94b7b2..bdbaca5c 100644 --- a/src/main/resources/messages_es.properties +++ b/src/main/resources/messages_es.properties @@ -108,6 +108,8 @@ multi.results =Ver resultados # -------------------Statements for the multiFinished.html file--------------------- multi.finished= Partida finalizada multi.points = Puntuaciones +multi.menu = Ir a la página de inicio + # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index b9c54eee..8316898a 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -101,6 +101,7 @@ multi.results=Voir les résultats # -------------------Statements for the multiFinished.html file--------------------- multi.finished= Jeu terminé multi.points = Points +multi.menu =aller à la page d'accueil # -------------------Statements for the apiHome.html file--------------------- diff --git a/src/main/resources/templates/game/multiplayerGame.html b/src/main/resources/templates/game/multiplayerGame.html index 1be2cbb6..81af9294 100644 --- a/src/main/resources/templates/game/multiplayerGame.html +++ b/src/main/resources/templates/game/multiplayerGame.html @@ -11,13 +11,10 @@
    -
    -

    diff --git a/src/main/resources/templates/ranking/multiplayerRanking.html b/src/main/resources/templates/ranking/multiplayerRanking.html index e47b2963..5fc16703 100644 --- a/src/main/resources/templates/ranking/multiplayerRanking.html +++ b/src/main/resources/templates/ranking/multiplayerRanking.html @@ -14,6 +14,12 @@

    + +