Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
uo287545 committed May 6, 2024
1 parent e382d8a commit eb6d5e9
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 43 deletions.
7 changes: 0 additions & 7 deletions docs/src/10_quality_requirements.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ ifndef::imagesdir[:imagesdir: ../images]
|The ability of a system to handle increasing workload or growing demands by adapting or expanding its capacity without compromising performance.
|SC7

|Interoperability
|The system must have the ability of seamlessly communicate, exchange data, and work together effectively with different systems, applications, or components, even if they use different technologies.
|SC8


|===

Expand Down Expand Up @@ -69,7 +65,4 @@ ifndef::imagesdir[:imagesdir: ../images]
| SC7
| As user demand grows, the deployment does not grow dynamically. However a system can be put in place to deploy more instances of the application to handle the increased workload, trough a load balancer, for example.

| SC8
| The application seamlessly integrates with other systems, allowing for the exchange of data and functionality without compatibility issues, enhancing its overall interoperability.

|===
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ private String prepareStatement(JsonNode question) {
}

private JsonNode getQueryResult(String query) throws IOException, InterruptedException {

logger.info("Query: {}", query);
HttpClient client = HttpClient.newHttpClient();
JsonNode resultsNode;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/uniovi/configuration/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static PasswordEncoder passwordEncoder(){
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf
.ignoringRequestMatchers("/api/**")
.ignoringRequestMatchers("/api/**", "/player/admin/**")
)
.authorizeHttpRequests(authorize ->
authorize
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/uniovi/controllers/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public String createLobby( HttpSession session, Model model) {
List<Player> players = playerService.getUsersByMultiplayerCode(code);
model.addAttribute("players",players);
model.addAttribute("code",session.getAttribute("multiplayerCode"));
return "/game/lobby";
return "game/lobby";
}

/**
Expand Down
33 changes: 20 additions & 13 deletions src/main/java/com/uniovi/controllers/PlayersController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
Expand All @@ -30,6 +32,8 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.Optional;
import java.util.List;
Expand All @@ -50,6 +54,7 @@ public PlayersController(PlayerService playerService, SignUpValidator signUpVali
this.signUpValidator = signUpValidator;
this.gameSessionService = gameSessionService;
this.roleService = roleService;
this.questionService = questionService;
}

@GetMapping("/signup")
Expand Down Expand Up @@ -245,9 +250,9 @@ public String changeRoles(HttpServletResponse response, @RequestParam String use

@GetMapping("/player/admin/questionManagement")
public String showQuestionManagementFragment(Model model) throws IOException {
File jsonFile = new File(QuestionGeneratorService.JSON_FILE_PATH);
Resource jsonFile = new ClassPathResource(QuestionGeneratorService.JSON_FILE_PATH);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode json = objectMapper.readTree(jsonFile);
JsonNode json = objectMapper.readTree(jsonFile.getInputStream());
model.addAttribute("jsonContent", json.toString());

return "player/admin/questionManagement";
Expand All @@ -260,28 +265,30 @@ public String deleteAllQuestions() throws IOException {
return "Questions deleted";
}

@GetMapping("/player/admin/saveQuestions")
@PutMapping("/player/admin/saveQuestions")
@ResponseBody
public String saveQuestions(HttpServletResponse response, @RequestParam String json) throws IOException {
public String saveQuestions(HttpServletResponse response, @RequestBody String json) throws IOException {
try {
JsonNode node = new ObjectMapper().readTree(json);
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter();
printer.indentObjectsWith(indenter); // Indent JSON objects
printer.indentArraysWith(indenter); // Indent JSON arrays

ObjectMapper mapper = new ObjectMapper();
mapper.writer(printer).writeValue(new FileOutputStream(QuestionGeneratorService.JSON_FILE_PATH), node);
questionService.setJsonGenerator(node);
return "Questions saved";
}
catch (Exception e) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return "Invalid JSON";
return e.getMessage();
}
}

@GetMapping("/questions/getJson")
@ResponseBody
public String getJson() {
return questionService.getJsonGenerator().toString();
}

@GetMapping("/player/admin/monitoring")
public String showMonitoring(Model model) {
public String showMonitoring(HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
return "player/admin/monitoring";
}
}
20 changes: 16 additions & 4 deletions src/main/java/com/uniovi/services/QuestionGeneratorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

Expand All @@ -31,7 +33,7 @@ public class QuestionGeneratorService {

private final QuestionService questionService;

public static final String JSON_FILE_PATH = "src/main/resources/static/JSON/QuestionTemplates.json";
public static final String JSON_FILE_PATH = "static/JSON/QuestionTemplates.json";

private Deque<QuestionType> types = new ArrayDeque<>();

Expand All @@ -52,9 +54,11 @@ public QuestionGeneratorService(QuestionService questionService, Environment env
}

private void parseQuestionTypes() throws IOException {
File jsonFile = new File(JSON_FILE_PATH);
ObjectMapper objectMapper = new ObjectMapper();
json = objectMapper.readTree(jsonFile);
if (json == null) {
Resource resource = new ClassPathResource(JSON_FILE_PATH);
ObjectMapper objectMapper = new ObjectMapper();
json = objectMapper.readTree(resource.getInputStream());
}
JsonNode categories = json.findValue("categories");
for (JsonNode category : categories) {
String categoryName = category.get("name").textValue();
Expand Down Expand Up @@ -125,11 +129,19 @@ public void generateTestQuestions(String cat) {
questionService.addNewQuestion(new QuestionDto(q));
}

public void setJsonGeneration(JsonNode json) {
this.json = json;
}

public void resetGeneration() throws IOException {
types.clear();
parseQuestionTypes();
}

public JsonNode getJsonGeneration() {
return json;
}

@Getter
@AllArgsConstructor
private static class QuestionType {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/uniovi/services/QuestionService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uniovi.services;

import com.fasterxml.jackson.databind.JsonNode;
import com.uniovi.dto.QuestionDto;
import com.uniovi.entities.Category;
import com.uniovi.entities.Question;
Expand Down Expand Up @@ -102,4 +103,16 @@ public interface QuestionService {
* Delete all the questions
*/
void deleteAllQuestions() throws IOException;

/**
* Set the json generator
* @param json The json generator
*/
void setJsonGenerator(JsonNode json);

/**
* Get the json generator
* @return The json generator
*/
JsonNode getJsonGenerator();
}
13 changes: 11 additions & 2 deletions src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uniovi.services.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.uniovi.dto.QuestionDto;
import com.uniovi.entities.Answer;
import com.uniovi.entities.Associations;
Expand All @@ -21,8 +22,6 @@

import java.io.IOException;
import java.security.SecureRandom;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -179,4 +178,14 @@ public void deleteAllQuestions() throws IOException {
questionRepository.deleteAll();
}

@Override
public void setJsonGenerator(JsonNode json) {
questionGeneratorService.setJsonGeneration(json);
}

@Override
public JsonNode getJsonGenerator() {
return questionGeneratorService.getJsonGeneration();
}

}
3 changes: 3 additions & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ navbar.admin.zone=Zona de administración
# -------------------Statements for the footer.html file---------------------
footer.copyright=© ASW - Grupo 04 B
footer.nav=Menú de navegación
footer.toggle.on=
footer.toggle.off=No
footer.animation=Animación de fondo

# -------------------Statements for the error.html file---------------------
error.page.title=Error!
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ navbar.admin.zone=Admin Zone
# -------------------Statements for the footer.html file---------------------
footer.copyright=© ASW - Group 04 B
footer.nav=Navigation
footer.toggle.on=On
footer.toggle.off=Off
footer.animation=Background animation

# -------------------Statements for the error.html file---------------------
error.page.title=Error!
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ navbar.admin.zone=Zona de administración
# -------------------Statements for the footer.html file---------------------
footer.copyright=© ASW - Grupo 04 B
footer.nav=Menú de navegación
footer.toggle.on=
footer.toggle.off=No
footer.animation=Animación de fondo

# -------------------Statements for the error.html file---------------------
error.page.title=Error!
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ navbar.admin.zone=Espace administrateur
# -------------------Statements for the footer.html file---------------------
footer.copyright=© ASW - Groupe 04 B
footer.nav=Menu de navigation
footer.toggle.on=Oui
footer.toggle.off=Non
footer.animation=Animation de la page

# -------------------Statements for the error.html file---------------------
error.page.title=Erreur !
Expand Down
19 changes: 19 additions & 0 deletions src/main/resources/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,23 @@ html {
}
body {
margin-bottom: 60px; /* Margin bottom by footer height */
}

div div .btn-danger {
color: #fff;
background-color: #dc3545 !important;
border-color: #dc3545 !important;
}

div div .btn-success {
color: #fff;
background-color: #218838 !important;
border-color: #1e7e34 !important;
}

div div .toggle-handle {
border-width: 0 1px;
background-color: #fff !important;
color: #212529;
border-color: #f8f9fa;
}
24 changes: 23 additions & 1 deletion src/main/resources/static/script/background.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
// modified version of random-normal
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}

function normalPool(o){var r=0;do{var a=Math.round(normal({mean:o.mean,dev:o.dev}));if(a<o.pool.length&&a>=0)return o.pool[a];r++}while(r<100)}function randomNormal(o){if(o=Object.assign({mean:0,dev:1,pool:[]},o),Array.isArray(o.pool)&&o.pool.length>0)return normalPool(o);var r,a,n,e,l=o.mean,t=o.dev;do{r=(a=2*Math.random()-1)*a+(n=2*Math.random()-1)*n}while(r>=1);return e=a*Math.sqrt(-2*Math.log(r)/r),t*e+l}

const NUM_PARTICLES = 350;
Expand Down Expand Up @@ -51,8 +67,13 @@ function drawParticle(particle, canvas, ctx) {
ctx.fillText("?", particle.x * canvas.width, particle.y * vh + (canvas.height / 2));
}


let init = false;
function draw(time, canvas, ctx) {
if (init && getCookie("backgroundAnimation") === "false") {
requestAnimationFrame((time) => draw(time, canvas, ctx));
return;
}

// Move particles
particles.forEach((particle, index) => {
particles[index] = moveParticle(particle, canvas, time);
Expand All @@ -66,6 +87,7 @@ function draw(time, canvas, ctx) {
drawParticle(particle, canvas, ctx);
})

init = true;
// Schedule next frame
requestAnimationFrame((time) => draw(time, canvas, ctx));
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/static/script/questionManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ function setupQuestionManagement() {
$("#saveButton").on("click", function () {
$.ajax({
url: "/player/admin/saveQuestions",
type: "GET",
data: {
json: JSON.stringify(editor.get())
},
type: "PUT",
data: JSON.stringify(
editor.get()
),
contentType: "application/json"
});
});

$.ajax({
url: '/JSON/QuestionTemplates.json',
url: '/questions/getJson',
type: 'GET',
success: function (data) {
let json = data;
let json = JSON.parse(data);
const element = document.getElementById('jsonEditorElement');
const options = {}
editor = new JSONEditor(element, options)
Expand Down
Loading

0 comments on commit eb6d5e9

Please sign in to comment.