Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge into multiplayer branch #216

Merged
merged 64 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
f483f14
New geography question template: Area
RicardoDiNu Apr 6, 2024
9f26f04
New geography question template: Currency
RicardoDiNu Apr 6, 2024
6d1b749
New geography question template: Population
RicardoDiNu Apr 6, 2024
8983649
New geography question category: History
RicardoDiNu Apr 6, 2024
3f41285
Revert "New geography question category: History"
RicardoDiNu Apr 6, 2024
2888e2d
New history question template: Artifact
RicardoDiNu Apr 6, 2024
6d67b5e
New history question template: Battle
RicardoDiNu Apr 6, 2024
760b15a
New history question template: Dynasty
RicardoDiNu Apr 6, 2024
53c1dff
New history question template: Event
RicardoDiNu Apr 6, 2024
d559d2a
New history question template: Invention
RicardoDiNu Apr 6, 2024
277b862
New history question template: Leader
RicardoDiNu Apr 6, 2024
983432a
New history question template: Period
RicardoDiNu Apr 6, 2024
4ccf348
InsertSampleDataService updated with new question templates
RicardoDiNu Apr 6, 2024
5b978a5
Fix - Add new question category: History
RicardoDiNu Apr 6, 2024
79cbfb5
Add new question category and templates: Science
RicardoDiNu Apr 6, 2024
8fe0cac
Add new question category and templates: Art
RicardoDiNu Apr 6, 2024
41b2333
Fix generators
RicardoDiNu Apr 6, 2024
61ec4bf
Add new question category and templates: Movie
RicardoDiNu Apr 6, 2024
f7080ce
Update InsertSampleDataService with the new templates
RicardoDiNu Apr 6, 2024
5aaa8db
Undo CategoryServiceImpl and InsertSampleDataService changes to only …
RicardoDiNu Apr 15, 2024
e603718
Remove old commented code
Pelayori Apr 24, 2024
1688771
First version of the base JSON done
uo287545 Apr 24, 2024
48fe605
Added statements for the capital question
uo287545 Apr 24, 2024
0c7930b
Add table and dropdown to user management
Pelayori Apr 24, 2024
f8374a8
Added currency question
uo287545 Apr 24, 2024
11f09d3
Initial implementation of the new question generator
uo287545 Apr 25, 2024
e5a6f37
First part of the request system
uo287545 Apr 25, 2024
8a8ef29
Added question and answer placeholders to the JSON
uo287545 Apr 25, 2024
e319116
Added statement generation based on language
uo287545 Apr 25, 2024
28e1068
First version of the question generator finished
uo287545 Apr 25, 2024
b812a5f
Added name only constructor for category
uo287545 Apr 25, 2024
a079a21
Removed old templates
uo287545 Apr 25, 2024
4ac734a
Test controller and temporary comments on some classes that used the …
uo287545 Apr 25, 2024
bd06490
New question generator tested and working
uo287545 Apr 25, 2024
3f4c703
Removed unused method from the question generator interface
uo287545 Apr 25, 2024
d0c2200
Added new question and improved the queries
uo287545 Apr 25, 2024
51f41d8
Added the fourth question
uo287545 Apr 25, 2024
7a27e6e
Started implementation of the QuestionGeneratorService with schedulin…
uo287545 Apr 25, 2024
d829ec9
Added stack to store all the different types of questions along with …
uo287545 Apr 25, 2024
7865da5
Deleted deprecated class
uo287545 Apr 25, 2024
9659f1b
Scheduled question generation working
uo287545 Apr 25, 2024
e848907
Fixed correct question not being associated with the question
uo287545 Apr 25, 2024
5346fe0
Fixed SampleDataService and tests
uo287545 Apr 25, 2024
fb1d726
Merge branch 'develop' into add-question-templates
uo287545 Apr 25, 2024
047bf0f
Fixed unit tests
uo287545 Apr 25, 2024
8f4f72d
All tests working
uo287545 Apr 25, 2024
62f5618
Removed unused code
uo287545 Apr 25, 2024
a49cc53
Trying to fix the game controller test
uo287545 Apr 26, 2024
ee0ae97
Finish user management
Pelayori Apr 26, 2024
afa0bbe
Start adding question management
Pelayori Apr 26, 2024
8c1a4ae
Merge branch 'add-question-templates' into admin-view
Pelayori Apr 26, 2024
2776571
Question management mostly done
Pelayori Apr 26, 2024
89a6352
Modify so question generation starts over with new JSON
Pelayori Apr 26, 2024
da799e9
Fix sticky footer
Pelayori Apr 26, 2024
ddf24c3
Fix code hotspot
Pelayori Apr 26, 2024
78cde1b
Fix nav
Pelayori Apr 26, 2024
6dbbd28
Fix nav in small widths
Pelayori Apr 26, 2024
33fdce6
Added new scheduled method to regenerate the questions each 24 hours
uo287545 Apr 26, 2024
3725378
User management integration tests
Pelayori Apr 26, 2024
6b8b6dc
Question management integration tests
Pelayori Apr 26, 2024
eec984f
Merge pull request #210 from Arquisoft/add-question-templates
Pelayori Apr 26, 2024
35ecb0f
Merge branch 'develop' into admin-view
Pelayori Apr 26, 2024
8d6c06e
Merge pull request #211 from Arquisoft/admin-view
Pelayori Apr 26, 2024
88ccb95
Fix sonarcloud issues
Pelayori Apr 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/uniovi/WiqEs04bApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class WiqEs04bApplication {
public static void main(String[] args) {
SpringApplication.run(WiqEs04bApplication.class, args);
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/com/uniovi/components/MultipleQuestionGenerator.java

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uniovi.components.generators;

import com.fasterxml.jackson.databind.JsonNode;
import com.uniovi.entities.Category;
import com.uniovi.entities.Question;
import org.springframework.stereotype.Component;
Expand All @@ -9,11 +10,7 @@

@Component
public interface QuestionGenerator {
List<Question> getQuestions(String language) throws IOException, InterruptedException;

String getQuery();
List<Question> getQuestions() throws InterruptedException, IOException;

Category getCategory();


List<Question> getQuestions(String language, JsonNode question, Category cat) throws IOException, InterruptedException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package com.uniovi.components.generators;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.uniovi.dto.AnswerDto;
import com.uniovi.dto.CategoryDto;
import com.uniovi.dto.QuestionDto;
import com.uniovi.entities.Answer;
import com.uniovi.entities.Category;
import com.uniovi.entities.Question;

import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class QuestionGeneratorV2 implements QuestionGenerator{

private JsonNode jsonNode;
private String language_placeholder;
private String question_placeholder;
private String answer_placeholder;
private String language;

private Random random = new SecureRandom();

public QuestionGeneratorV2(JsonNode jsonNode) {
this.jsonNode = jsonNode;
this.language_placeholder = jsonNode.get("language_placeholder").textValue();
this.question_placeholder = jsonNode.get("question_placeholder").textValue();
this.answer_placeholder = jsonNode.get("answer_placeholder").textValue();
}

@Override
public List<Question> getQuestions(String language) throws IOException, InterruptedException {
this.language = language;
List<Question> questions = new ArrayList<>();
JsonNode categories = jsonNode.findValue("categories");
for(JsonNode category : categories){
String categoryName = category.get("name").textValue();
Category cat = new Category(categoryName);
JsonNode questionsNode = category.findValue("questions");
for(JsonNode question : questionsNode){
questions.addAll(this.generateQuestion(question, cat));
}
}
return questions;
}

@Override
public List<Question> getQuestions(String language, JsonNode question, Category cat) throws IOException, InterruptedException {
this.language = language;
return this.generateQuestion(question, cat);
}

private List<Question> generateQuestion(JsonNode question, Category cat) throws IOException, InterruptedException {
// Get the SPARQL query from the JSON
String query = question.get("sparqlQuery").textValue();

// Get the question and answer words from the JSON
String questionLabel = question.get("question").textValue();
String answerLabel= question.get("answer").textValue();

// Replace the placeholders in the query with the actual values
query = query.replace(language_placeholder, language).
replace(question_placeholder, questionLabel).
replace(answer_placeholder, answerLabel);

// Execute the query and get the results
JsonNode results = getQueryResult(query);
List<Question> questions = new ArrayList<>();

// Prepare the statement base based on the language
String statement = this.prepareStatement(question);

for (JsonNode result : results) {
// Generate the correct answer
String correctAnswer = result.path(answerLabel).path("value").asText();
Answer correct = new Answer(correctAnswer, true);

// Generate the options
List<Answer> options = this.generateOptions(results, correctAnswer, answerLabel);
options.add(correct);

if (statement != null) {
// Generate the question statement
String questionStatement = statement.replace(question_placeholder, result.path(questionLabel).path("value").asText());

// Generate the question
Question q = new Question(questionStatement, options, correct, cat, language);

// Add the question to the list
questions.add(q);
}
}
return questions;
}

private List<Answer> generateOptions(JsonNode results, String correctAnswer, String answerLabel) {
List<Answer> options = new ArrayList<>();
List<String> usedOptions = new ArrayList<>();
int size = results.size();
int tries = 0;

while (options.size() < 3 && tries < 10) {
int random = (int) (this.random.nextFloat() * size);
String option = results.get(random).path(answerLabel).path("value").asText();
if (!option.equals(correctAnswer) && !usedOptions.contains(option) ) {
usedOptions.add(option);
options.add(new Answer(option, false));
}
tries++;
}
return options;
}

/**
* Generates a statement based on the language of the question
* @param question The question node
* @return The statement in the language of the question or null if the language is not found
*/
private String prepareStatement(JsonNode question) {
JsonNode statementNode = question.findValue("statements");
for (JsonNode statement : statementNode) {
if (statement.get("language").textValue().equals(language)) {
return statement.get("statement").textValue();
}
}
return null;
}

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

System.out.println("Query: " + query);
HttpClient client = HttpClient.newHttpClient();
JsonNode resultsNode;
String endpointUrl = "https://query.wikidata.org/sparql?query=" +
URLEncoder.encode(query, StandardCharsets.UTF_8) +
"&format=json";

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endpointUrl))
.header("Accept", "application/json")
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

// Process the JSON response using Jackson ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonResponse = objectMapper.readTree(response.body());

// Access the data from the JSON response
resultsNode = jsonResponse.path("results").path("bindings");
return resultsNode;
}
}

This file was deleted.

Loading
Loading