diff --git a/src/main/java/com/uniovi/WiqEs04bApplication.java b/src/main/java/com/uniovi/WiqEs04bApplication.java index 7cd90279..5bfb46bc 100644 --- a/src/main/java/com/uniovi/WiqEs04bApplication.java +++ b/src/main/java/com/uniovi/WiqEs04bApplication.java @@ -1,12 +1,7 @@ package com.uniovi; -import jakarta.persistence.Persistence; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication public class WiqEs04bApplication { diff --git a/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java b/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java index 81006a95..7c534d91 100644 --- a/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java +++ b/src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java @@ -6,10 +6,6 @@ import com.uniovi.entities.Category; import com.uniovi.entities.Question; import com.uniovi.services.CategoryService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - import java.net.URI; import java.net.URLEncoder; import java.net.http.HttpClient; @@ -18,11 +14,14 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.Random; public abstract class AbstractQuestionGenerator implements QuestionGenerator{ private List questions = new ArrayList<>(); protected final CategoryService categoryService; - private String query; + + protected Random random = new Random(); + protected String statement; protected String language; @@ -51,7 +50,7 @@ public List getQuestions() { try { String endpointUrl = "https://query.wikidata.org/sparql?query=" + - URLEncoder.encode(this.getQuery(), StandardCharsets.UTF_8.toString()) + + URLEncoder.encode(this.getQuery(), StandardCharsets.UTF_8) + "&format=json"; HttpRequest request = HttpRequest.newBuilder() @@ -72,12 +71,14 @@ public List getQuestions() { List options = this.generateOptions(resultsNode, result); String correctAnswer = this.generateCorrectAnswer(result); - String statement = this.getQuestionSubject(result); - questionGenerator(statement, options, correctAnswer, this.getCategory()); + String questionStatement = this.getQuestionSubject(result); + questionGenerator(questionStatement, options, correctAnswer, this.getCategory()); } + } catch (InterruptedException e) { + throw new QuestionGeneratorException("Generation of questions was interrupted"); } catch (Exception e) { - throw new RuntimeException(e); + throw new QuestionGeneratorException("An error occurred while generating questions"); } return questions; @@ -87,4 +88,10 @@ public List getQuestions() { protected abstract String generateCorrectAnswer(JsonNode result); protected abstract String getQuestionSubject(JsonNode result); + + private static class QuestionGeneratorException extends RuntimeException { + public QuestionGeneratorException(String message) { + super(message); + } + } } diff --git a/src/main/java/com/uniovi/components/generators/geography/BorderQuestionGenerator.java b/src/main/java/com/uniovi/components/generators/geography/BorderQuestionGenerator.java index f791f1e5..81d5cdb0 100644 --- a/src/main/java/com/uniovi/components/generators/geography/BorderQuestionGenerator.java +++ b/src/main/java/com/uniovi/components/generators/geography/BorderQuestionGenerator.java @@ -10,6 +10,7 @@ public class BorderQuestionGenerator extends AbstractGeographyGenerator{ { put("en", "Which countries share a border with "); put("es", "¿Con qué países comparte frontera "); + put("fr", "Avec quels pays partage-t-il une frontière "); } }; private Set usedCountries = new HashSet<>(); @@ -33,7 +34,6 @@ private List getAllBorderingCountries(JsonNode resultsNode, String corre private List selectRandomIncorrectBorderingCountries(List allBorderingCountries, String correctCountry, int count) { List incorrectBorderingCountries = new ArrayList<>(); - Random random = new Random(); while (incorrectBorderingCountries.size() < count && allBorderingCountries.size() > 0) { int randomIndex = random.nextInt(allBorderingCountries.size()); String selectedBorderingCountry = allBorderingCountries.remove(randomIndex); diff --git a/src/main/java/com/uniovi/components/generators/geography/CapitalQuestionGenerator.java b/src/main/java/com/uniovi/components/generators/geography/CapitalQuestionGenerator.java index e5d91727..199d4dc5 100644 --- a/src/main/java/com/uniovi/components/generators/geography/CapitalQuestionGenerator.java +++ b/src/main/java/com/uniovi/components/generators/geography/CapitalQuestionGenerator.java @@ -12,6 +12,7 @@ public class CapitalQuestionGenerator extends AbstractGeographyGenerator{ { put("en", "What is the capital of "); put("es", "¿Cuál es la capital de "); + put("fr", "Quelle est la capitale de "); } }; @@ -48,7 +49,6 @@ private List getAllCapitals(JsonNode resultsNode, String correctCapital) private List selectRandomIncorrectCapitals(List allCapitals, String correctCapital, int count) { List incorrectCapitals = new ArrayList<>(); - Random random = new Random(); while (incorrectCapitals.size() < count && allCapitals.size() > 0) { int randomIndex = random.nextInt(allCapitals.size()); String selectedCapital = allCapitals.remove(randomIndex); diff --git a/src/main/java/com/uniovi/components/generators/geography/ContinentQuestionGeneration.java b/src/main/java/com/uniovi/components/generators/geography/ContinentQuestionGeneration.java index b1d476ae..dec77dc0 100644 --- a/src/main/java/com/uniovi/components/generators/geography/ContinentQuestionGeneration.java +++ b/src/main/java/com/uniovi/components/generators/geography/ContinentQuestionGeneration.java @@ -11,6 +11,7 @@ public class ContinentQuestionGeneration extends AbstractGeographyGenerator{ { put("en", "In which continent is "); put("es", "¿En qué continente se encuentra "); + put("fr", "Sur quel continent est-il situé "); } }; @@ -34,7 +35,6 @@ private List getAllContinents(JsonNode resultsNode, String correctContin private List selectRandomIncorrectContinents(List allContinents, String correctContinent, int count) { List incorrectContinents = new ArrayList<>(); - Random random = new Random(); while (incorrectContinents.size() < count && allContinents.size() > 0) { int randomIndex = random.nextInt(allContinents.size()); String selectedCapital = allContinents.remove(randomIndex); diff --git a/src/main/java/com/uniovi/entities/Question.java b/src/main/java/com/uniovi/entities/Question.java index 27dc8dbc..ea66844d 100644 --- a/src/main/java/com/uniovi/entities/Question.java +++ b/src/main/java/com/uniovi/entities/Question.java @@ -23,6 +23,8 @@ public class Question implements JsonEntity { public static final String ENGLISH = "en"; public static final String SPANISH = "es"; + public static final String FRENCH = "fr"; + @Id @GeneratedValue diff --git a/src/main/java/com/uniovi/services/InsertSampleDataService.java b/src/main/java/com/uniovi/services/InsertSampleDataService.java index f05dc00c..1ac58636 100644 --- a/src/main/java/com/uniovi/services/InsertSampleDataService.java +++ b/src/main/java/com/uniovi/services/InsertSampleDataService.java @@ -88,6 +88,14 @@ public void generateSampleData() { List questionsEs = allQuestionGenerator.getQuestions(); questionsEs.forEach(questionService::addNewQuestion); + allQuestionGenerator = new MultipleQuestionGenerator( + new ContinentQuestionGeneration(categoryService, Question.FRENCH), + new CapitalQuestionGenerator(categoryService, Question.FRENCH), + new BorderQuestionGenerator(categoryService, Question.FRENCH) + ); + List questionsFr = allQuestionGenerator.getQuestions(); + questionsFr.forEach(questionService::addNewQuestion); + log.info("Sample questions inserted"); } } diff --git a/src/main/java/com/uniovi/services/impl/GameSessionImpl.java b/src/main/java/com/uniovi/services/impl/GameSessionImpl.java index bed23a3f..d7e2ad3a 100644 --- a/src/main/java/com/uniovi/services/impl/GameSessionImpl.java +++ b/src/main/java/com/uniovi/services/impl/GameSessionImpl.java @@ -16,7 +16,7 @@ @Service public class GameSessionImpl implements GameSessionService { - public static final Integer NORMAL_GAME_QUESTION_NUM = 20; + public static final Integer NORMAL_GAME_QUESTION_NUM = 4; private final GameSessionRepository gameSessionRepository; private final QuestionService questionService; diff --git a/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java b/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java index d45c1b5f..6caf6b60 100644 --- a/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java @@ -14,11 +14,13 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.Random; @Service public class QuestionServiceImpl implements QuestionService { private final QuestionRepository questionRepository; + private final Random random = new Random(); public QuestionServiceImpl(QuestionRepository questionRepository) { this.questionRepository = questionRepository; @@ -47,9 +49,9 @@ public List getRandomQuestions(int num) { .filter(question -> question.getLanguage().equals(LocaleContextHolder.getLocale().getLanguage())).toList(); List res = new ArrayList<>(); for (int i = 0; i < num; i++) { - int idx = (int) (Math.random() * allQuestions.size()); + int idx = random.nextInt(allQuestions.size()); while (allQuestions.get(idx).hasEmptyOptions()){ - idx = (int) (Math.random() * allQuestions.size()); + idx = random.nextInt(allQuestions.size()); } res.add(allQuestions.get(idx)); }