From b0756ffe1d9cc629b5466d0804ab5d65b9096c9b Mon Sep 17 00:00:00 2001 From: Pelayori <31128562+Pelayori@users.noreply.github.com> Date: Sat, 9 Mar 2024 18:10:33 +0100 Subject: [PATCH] Add user nav menu, user home and api key requests. Enhanced player ranking --- .../uniovi/configuration/SecurityConfig.java | 2 +- .../uniovi/controllers/HomeController.java | 25 ++++++++++++++- .../java/com/uniovi/entities/GameSession.java | 14 +++++++- src/main/resources/messages.properties | 11 +++++-- src/main/resources/messages_en.properties | 5 +++ src/main/resources/messages_es.properties | 5 +++ src/main/resources/static/css/style.css | 9 ++++++ .../resources/static/script/background.js | 2 +- .../static/script/show_hide_password.js | 14 ++++++++ .../resources/templates/fragments/nav.html | 16 +++++++--- .../templates/player/apiKeyHome.html | 32 +++++++++++++++++++ src/main/resources/templates/player/home.html | 9 ++++++ .../ranking/playerRanking_table.html | 6 ++++ 13 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 src/main/resources/static/script/show_hide_password.js create mode 100644 src/main/resources/templates/player/apiKeyHome.html diff --git a/src/main/java/com/uniovi/configuration/SecurityConfig.java b/src/main/java/com/uniovi/configuration/SecurityConfig.java index cfdbdc61..0a6a2ec5 100644 --- a/src/main/java/com/uniovi/configuration/SecurityConfig.java +++ b/src/main/java/com/uniovi/configuration/SecurityConfig.java @@ -41,7 +41,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .authorizeHttpRequests((authorize) -> authorize .requestMatchers("/css/**", "/img/**", "/script/**").permitAll() - .requestMatchers("/home").authenticated() + .requestMatchers("/home/**").authenticated() .requestMatchers("/signup/**").permitAll() .requestMatchers("/api/**").permitAll() .requestMatchers("/game/**").authenticated() diff --git a/src/main/java/com/uniovi/controllers/HomeController.java b/src/main/java/com/uniovi/controllers/HomeController.java index 0f000ac5..5ae72609 100644 --- a/src/main/java/com/uniovi/controllers/HomeController.java +++ b/src/main/java/com/uniovi/controllers/HomeController.java @@ -1,21 +1,28 @@ package com.uniovi.controllers; +import com.uniovi.entities.ApiKey; +import com.uniovi.entities.Player; +import com.uniovi.services.ApiKeyService; import com.uniovi.services.PlayerService; import com.uniovi.services.QuestionService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import java.util.Random; @Controller public class HomeController{ private final PlayerService playerService; + private final ApiKeyService apiKeyService; @Autowired - public HomeController(PlayerService playerService) { + public HomeController(PlayerService playerService, ApiKeyService apiKeyService) { this.playerService = playerService; + this.apiKeyService = apiKeyService; } @GetMapping("/") @@ -27,4 +34,20 @@ public String home(){ public String apiHome() { return "api/apiHome"; } + + @GetMapping("/home/apikey") + public String apiKeyHome(Authentication auth, Model model) { + Player player = playerService.getUserByUsername(auth.getName()).get(); + model.addAttribute("apiKey", player.getApiKey()); + return "player/apiKeyHome"; + } + + @GetMapping("/home/apikey/create") + public String createApiKey(Authentication auth) { + Player player = playerService.getUserByUsername(auth.getName()).get(); + if (player.getApiKey() == null) { + apiKeyService.createApiKey(player); + } + return "redirect:/home/apikey"; + } } diff --git a/src/main/java/com/uniovi/entities/GameSession.java b/src/main/java/com/uniovi/entities/GameSession.java index c124cdc7..1a7f7fa3 100644 --- a/src/main/java/com/uniovi/entities/GameSession.java +++ b/src/main/java/com/uniovi/entities/GameSession.java @@ -9,6 +9,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; +import java.time.Duration; import java.time.LocalDateTime; import java.util.*; @@ -59,7 +60,6 @@ public void addQuestion(boolean correct, int timeLeft) { correctQuestions++; totalQuestions++; - // TODO: Calculate score if (correct) { score += timeLeft + 10 /* magic number TBD */; } @@ -109,4 +109,16 @@ public boolean hasQuestionId(Long idQuestion) { } return false; } + + public String getDuration() { + if (createdAt != null && finishTime != null) { + Duration duration = Duration.between(createdAt, finishTime); + long hours = duration.toHours(); + long minutes = duration.toMinutes() % 60; + long seconds = duration.getSeconds() % 60; + return String.format("%02d:%02d:%02d", hours, minutes, seconds); + } else { + return "00:00:00"; + } + } } diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index abc095bf..a0ce684b 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -19,6 +19,7 @@ navbar.login=Inicia sesión # Buttons for authenticated users navbar.profile=Perfil navbar.logout=Cerrar sesión +navbar.profile.apikey=Clave de la API # -------------------Statements for the footer.html file--------------------- footer.copyright=© ASW - Grupo 04 B @@ -39,7 +40,10 @@ index.button=JUGAR home.heading=Bienvenido home.private_zone=Esta es una zona privada de la web home.authenticated_as=Usuario autenticado como: - +home.apikey.title=Clave de la API +home.apikey.description=Esta es tu clave de la API. Utilízala para acceder a los recursos de la API de WIQ. +home.apikey.missing=No tienes una clave de la API. Obtenla pulsando en este botón. +home.apikey.create=Obtener clave # -------------------Statements for the login.html file--------------------- login.username.label=Usuario: @@ -67,7 +71,10 @@ ranking.title=Ranking ranking.position=Posición ranking.score=Puntuación ranking.date=Fecha -ranking.player=Player +ranking.player=Jugador +ranking.time=Tiempo +ranking.question.right=Respuestas correctas +ranking.question.wrong=Respuestas incorrectas # -------------------Statements for the apiHome.html file--------------------- api.doc.title=Documentación de la API diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 25f62d3e..2f8bed6c 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -19,6 +19,7 @@ navbar.login=Log In # Buttons for authenticated users navbar.profile=Profile navbar.logout=Log Out +navbar.profile.apikey=API Key # -------------------Statements for the footer.html file--------------------- footer.copyright=© ASW - Group 04 B @@ -39,6 +40,10 @@ index.button=PLAY home.heading=Welcome home.private_zone=This is a private zone of the website home.authenticated_as=Authenticated User as: +home.apikey.title=API Key +home.apikey.description=This is your api key. You can use it to access the WIQ's REST API. +home.apikey.missing=You don't have an API key yet. You can generate one by clicking the button below. +home.apikey.create=Obtain API Key # -------------------Statements for the login.html file--------------------- login.username.label=Username: diff --git a/src/main/resources/messages_es.properties b/src/main/resources/messages_es.properties index f7311554..8ba5daaf 100644 --- a/src/main/resources/messages_es.properties +++ b/src/main/resources/messages_es.properties @@ -15,6 +15,7 @@ navbar.toSpanish=Español # Buttons for non-authenticated users navbar.signup=Regístrate navbar.login=Inicia sesión +navbar.profile.apikey=Clave de la API # Buttons for authenticated users navbar.profile=Perfil @@ -40,6 +41,10 @@ index.button=JUGAR home.heading=Bienvenido home.private_zone=Esta es una zona privada de la web home.authenticated_as=Usuario autenticado como: +home.apikey.title=Clave de la API +home.apikey.description=Esta es tu clave de la API. Utilízala para acceder a los recursos de la API de WIQ. +home.apikey.missing=No tienes una clave de la API. Obtenla pulsando en este botón. +home.apikey.create=Obtener clave # -------------------Statements for the login.html file--------------------- login.username.label=Usuario: diff --git a/src/main/resources/static/css/style.css b/src/main/resources/static/css/style.css index e69de29b..84d2796c 100644 --- a/src/main/resources/static/css/style.css +++ b/src/main/resources/static/css/style.css @@ -0,0 +1,9 @@ +#apiKeyDiv { + display: flex; + flex-direction: column; + margin: 0 5% 5% 5%; +} + +#apiKeyDiv form { + margin: 5% 20% 0 20%; +} \ No newline at end of file diff --git a/src/main/resources/static/script/background.js b/src/main/resources/static/script/background.js index 1aab4905..4be291bf 100644 --- a/src/main/resources/static/script/background.js +++ b/src/main/resources/static/script/background.js @@ -3,7 +3,7 @@ function normalPool(o){var r=0;do{var a=Math.round(normal({mean:o.mean,dev:o.dev const NUM_PARTICLES = 350; const PARTICLE_SIZE = 4; // View heights -const SPEED = 20000; // Milliseconds +const SPEED = 40000; // Milliseconds let particles = []; diff --git a/src/main/resources/static/script/show_hide_password.js b/src/main/resources/static/script/show_hide_password.js new file mode 100644 index 00000000..b14ec016 --- /dev/null +++ b/src/main/resources/static/script/show_hide_password.js @@ -0,0 +1,14 @@ +$(document).ready(function() { + $(".show_hide_password a").on('click', function(event) { + event.preventDefault(); + if($('.show_hide_password input').attr("type") == "text"){ + $('.show_hide_password input').attr('type', 'password'); + $('.show_hide_password i').addClass( "fa-eye-slash" ); + $('.show_hide_password i').removeClass( "fa-eye" ); + }else if($('.show_hide_password input').attr("type") == "password"){ + $('.show_hide_password input').attr('type', 'text'); + $('.show_hide_password i').removeClass( "fa-eye-slash" ); + $('.show_hide_password i').addClass( "fa-eye" ); + } + }); +}); \ No newline at end of file diff --git a/src/main/resources/templates/fragments/nav.html b/src/main/resources/templates/fragments/nav.html index f315ec77..2a6bdc50 100644 --- a/src/main/resources/templates/fragments/nav.html +++ b/src/main/resources/templates/fragments/nav.html @@ -75,11 +75,19 @@ -