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

Fix the working game and add image support (without testing) #254

Merged
merged 26 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
01c7183
fix: dependecy warnings
jjgancfer Apr 13, 2024
618736f
feat: time left inside timer
jjgancfer Apr 13, 2024
af332ce
feat: backwards-going timer
jjgancfer Apr 13, 2024
ef0e8b1
feat: disabled next button when no answer is selected
jjgancfer Apr 13, 2024
860493a
feat: add total amount of rounds
jjgancfer Apr 13, 2024
522163d
fix: correct answers not showing correctly
jjgancfer Apr 13, 2024
113c975
feat: use workaround to stop useEffect to create a new game every tim…
jjgancfer Apr 13, 2024
a34c373
fix: backwards timer
jjgancfer Apr 13, 2024
081ceeb
Merge remote-tracking branch 'origin/develop' into fix/webapp/game
jjgancfer Apr 14, 2024
07134e8
chore: change new game endpoint
jjgancfer Apr 14, 2024
4a45c33
chore: remove non-used try-catch blocks. Exceptions now propagate
jjgancfer Apr 14, 2024
cda132f
Merge commit '8f840df83faf85eeb81611a83fb0801edf5c18d1' into fix/weba…
jjgancfer Apr 17, 2024
13596f3
feat: add image support to the game UI
jjgancfer Apr 17, 2024
e3bc385
Merge remote-tracking branch 'origin/feat/webapp/user-menu' into fix/…
jjgancfer Apr 17, 2024
4d5a50a
feat: add localization to the alt attribute
jjgancfer Apr 17, 2024
46bb5ff
chore: axios propagates errors
jjgancfer Apr 17, 2024
6213a31
fix: get current game data correctly
jjgancfer Apr 17, 2024
bda2272
Merge branch 'fix/webapp/user-statistics-multiple-renders' into fix/w…
jjgancfer Apr 18, 2024
115dc3a
Merge branch 'fix/api/missing-select' into fix/webapp/game
jjgancfer Apr 18, 2024
9e3bed2
feat: improved responsiveness of the game
jjgancfer Apr 18, 2024
32ec5d9
feat: even better responsiveness
jjgancfer Apr 18, 2024
7d036f7
chore: reformatted some code
jjgancfer Apr 18, 2024
aac5620
chore: due to the design of the backend, add petition to end the game
jjgancfer Apr 18, 2024
be901da
Merge remote-tracking branch 'origin/develop' into fix/webapp/game
jjgancfer Apr 19, 2024
3d4054e
Merge remote-tracking branch 'origin/develop' into fix/webapp/game
dariogmori Apr 19, 2024
7eb7b29
fix: minor issues due to the merge
jjgancfer Apr 19, 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
338 changes: 169 additions & 169 deletions api/src/main/java/lab/en2b/quizapi/game/Game.java
Original file line number Diff line number Diff line change
@@ -1,169 +1,169 @@
package lab.en2b.quizapi.game;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lab.en2b.quizapi.commons.user.User;
import lab.en2b.quizapi.commons.utils.GameModeUtils;
import lab.en2b.quizapi.game.dtos.CustomGameDto;
import lab.en2b.quizapi.questions.answer.Answer;
import lab.en2b.quizapi.questions.question.Question;
import lab.en2b.quizapi.questions.question.QuestionCategory;
import lombok.*;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

import static lab.en2b.quizapi.game.GameMode.*;

@Entity
@Table(name = "games")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
public class Game {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.NONE)
private Long id;

private Long rounds = 9L;
private Long actualRound = 0L;

private Long correctlyAnsweredQuestions = 0L;
private String language;
private Long roundStartTime = 0L;
@NonNull
private Integer roundDuration;
private boolean currentQuestionAnswered;
@Enumerated(EnumType.STRING)
private GameMode gamemode;
@ManyToOne
@NotNull
@JoinColumn(name = "user_id")
private User user;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="games_questions",
joinColumns=
@JoinColumn(name="game_id", referencedColumnName="id"),
inverseJoinColumns=
@JoinColumn(name="question_id", referencedColumnName="id")
)

@OrderColumn
private List<Question> questions;
private boolean isGameOver;
@Enumerated(EnumType.STRING)
private List<QuestionCategory> questionCategoriesForCustom;

public Game(User user,GameMode gamemode,String lang, CustomGameDto gameDto){
this.user = user;
this.questions = new ArrayList<>();
this.actualRound = 0L;
setLanguage(lang);
if(gamemode == CUSTOM)
setCustomGameMode(gameDto);
else
setGameMode(gamemode);
}

public void newRound(Question question){
if(getActualRound() != 0){
if (isGameOver())
throw new IllegalStateException("You can't start a round for a finished game!");
if(!currentRoundIsOver())
throw new IllegalStateException("You can't start a new round when the current round is not over yet!");
}

setCurrentQuestionAnswered(false);
getQuestions().add(question);
increaseRound();
setRoundStartTime(Instant.now().toEpochMilli());
}

private void increaseRound(){
setActualRound(getActualRound() + 1);
}

public boolean isGameOver(){
return isGameOver && getActualRound() >= getRounds();
}


public Question getCurrentQuestion() {
if(getRoundStartTime() == null){
throw new IllegalStateException("The round is not active!");
}
if(currentRoundIsOver())
throw new IllegalStateException("The current round is over!");
if(isGameOver())
throw new IllegalStateException("The game is over!");
return getQuestions().get(getQuestions().size()-1);
}

private boolean currentRoundIsOver(){
return currentQuestionAnswered || roundTimeHasExpired();
}

private boolean roundTimeHasExpired(){
return getRoundStartTime()!= null && Instant.now().isAfter(Instant.ofEpochMilli(getRoundStartTime()).plusSeconds(getRoundDuration()));
}

public boolean answerQuestion(Long answerId){
if(currentRoundIsOver())
throw new IllegalStateException("You can't answer a question when the current round is over!");
if (isGameOver())
throw new IllegalStateException("You can't answer a question when the game is over!");
Question q = getCurrentQuestion();
if (q.getAnswers().stream().map(Answer::getId).noneMatch(i -> i.equals(answerId)))
throw new IllegalArgumentException("The answer you provided is not one of the options");
if(q.isCorrectAnswer(answerId)){
setCorrectlyAnsweredQuestions(getCorrectlyAnsweredQuestions() + 1);
}
setCurrentQuestionAnswered(true);
return q.isCorrectAnswer(answerId);
}
public void setLanguage(String language){
if(language == null){
language = "en";
}
if(!isLanguageSupported(language))
throw new IllegalArgumentException("The language you provided is not supported");
this.language = language;
}
public void setCustomGameMode(CustomGameDto gameDto){
setRounds(gameDto.getRounds());
setRoundDuration(gameDto.getRoundDuration());
this.gamemode = CUSTOM;
setQuestionCategoriesForCustom(gameDto.getCategories());
}
public void setGameMode(GameMode gamemode){
if(gamemode == null){
gamemode = KIWI_QUEST;
}
this.gamemode = gamemode;
GameModeUtils.setGamemodeParams(this);
}

public void setQuestionCategoriesForCustom(List<QuestionCategory> questionCategoriesForCustom) {
if(gamemode != CUSTOM)
throw new IllegalStateException("You can't set custom categories for a non-custom gamemode!");
if(questionCategoriesForCustom == null || questionCategoriesForCustom.isEmpty())
throw new IllegalArgumentException("You can't set an empty list of categories for a custom gamemode!");
this.questionCategoriesForCustom = questionCategoriesForCustom;
}

public List<QuestionCategory> getQuestionCategoriesForGamemode(){
return GameModeUtils.getQuestionCategoriesForGamemode(gamemode,questionCategoriesForCustom);
}
private boolean isLanguageSupported(String language) {
return language.equals("en") || language.equals("es");
}

public boolean shouldBeGameOver() {
return getActualRound() >= getRounds() && !isGameOver && currentRoundIsOver();
}
}
package lab.en2b.quizapi.game;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lab.en2b.quizapi.commons.user.User;
import lab.en2b.quizapi.commons.utils.GameModeUtils;
import lab.en2b.quizapi.game.dtos.CustomGameDto;
import lab.en2b.quizapi.questions.answer.Answer;
import lab.en2b.quizapi.questions.question.Question;
import lab.en2b.quizapi.questions.question.QuestionCategory;
import lombok.*;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

import static lab.en2b.quizapi.game.GameMode.*;

@Entity
@Table(name = "games")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
public class Game {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.NONE)
private Long id;

private Long rounds = 9L;
private Long actualRound = 0L;

private Long correctlyAnsweredQuestions = 0L;
private String language;
private Long roundStartTime = 0L;
@NonNull
private Integer roundDuration;
private boolean currentQuestionAnswered;
@Enumerated(EnumType.STRING)
private GameMode gamemode;
@ManyToOne
@NotNull
@JoinColumn(name = "user_id")
private User user;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="games_questions",
joinColumns=
@JoinColumn(name="game_id", referencedColumnName="id"),
inverseJoinColumns=
@JoinColumn(name="question_id", referencedColumnName="id")
)

@OrderColumn
private List<Question> questions;
private boolean isGameOver;
@Enumerated(EnumType.STRING)
private List<QuestionCategory> questionCategoriesForCustom;

public Game(User user,GameMode gamemode,String lang, CustomGameDto gameDto){
this.user = user;
this.questions = new ArrayList<>();
this.actualRound = 0L;
setLanguage(lang);
if(gamemode == CUSTOM)
setCustomGameMode(gameDto);
else
setGameMode(gamemode);
}

public void newRound(Question question){
if(getActualRound() != 0){
if (isGameOver())
throw new IllegalStateException("You can't start a round for a finished game!");
if(!currentRoundIsOver())
throw new IllegalStateException("You can't start a new round when the current round is not over yet!");
}

setCurrentQuestionAnswered(false);
getQuestions().add(question);
increaseRound();
setRoundStartTime(Instant.now().toEpochMilli());
}

private void increaseRound(){
setActualRound(getActualRound() + 1);
}

public boolean isGameOver(){
return isGameOver && getActualRound() >= getRounds();
}


public Question getCurrentQuestion() {
if(getRoundStartTime() == null){
throw new IllegalStateException("The round is not active!");
}
if(currentRoundIsOver())
throw new IllegalStateException("The current round is over!");
if(isGameOver())
throw new IllegalStateException("The game is over!");
return getQuestions().get(getQuestions().size()-1);
}

private boolean currentRoundIsOver(){
return currentQuestionAnswered || roundTimeHasExpired();
}

private boolean roundTimeHasExpired(){
return getRoundStartTime()!= null && Instant.now().isAfter(Instant.ofEpochMilli(getRoundStartTime()).plusSeconds(getRoundDuration()));
}

public boolean answerQuestion(Long answerId){
if(currentRoundIsOver())
throw new IllegalStateException("You can't answer a question when the current round is over!");
if (isGameOver())
throw new IllegalStateException("You can't answer a question when the game is over!");
Question q = getCurrentQuestion();
if (q.getAnswers().stream().map(Answer::getId).noneMatch(i -> i.equals(answerId)))
throw new IllegalArgumentException("The answer you provided is not one of the options");
if(q.isCorrectAnswer(answerId)){
setCorrectlyAnsweredQuestions(getCorrectlyAnsweredQuestions() + 1);
}
setCurrentQuestionAnswered(true);
return q.isCorrectAnswer(answerId);
}
public void setLanguage(String language){
if(language == null){
language = "en";
}
if(!isLanguageSupported(language))
throw new IllegalArgumentException("The language you provided is not supported");
this.language = language;
}
public void setCustomGameMode(CustomGameDto gameDto){
setRounds(gameDto.getRounds());
setRoundDuration(gameDto.getRoundDuration());
this.gamemode = CUSTOM;
setQuestionCategoriesForCustom(gameDto.getCategories());
}
public void setGameMode(GameMode gamemode){
if(gamemode == null){
gamemode = KIWI_QUEST;
}
this.gamemode = gamemode;
GameModeUtils.setGamemodeParams(this);
}

public void setQuestionCategoriesForCustom(List<QuestionCategory> questionCategoriesForCustom) {
if(gamemode != CUSTOM)
throw new IllegalStateException("You can't set custom categories for a non-custom gamemode!");
if(questionCategoriesForCustom == null || questionCategoriesForCustom.isEmpty())
throw new IllegalArgumentException("You can't set an empty list of categories for a custom gamemode!");
this.questionCategoriesForCustom = questionCategoriesForCustom;
}

public List<QuestionCategory> getQuestionCategoriesForGamemode(){
return GameModeUtils.getQuestionCategoriesForGamemode(gamemode,questionCategoriesForCustom);
}
private boolean isLanguageSupported(String language) {
return language.equals("en") || language.equals("es");
}

public boolean shouldBeGameOver() {
return getActualRound() >= getRounds() && !isGameOver && currentRoundIsOver();
}
}
24 changes: 12 additions & 12 deletions api/src/main/java/lab/en2b/quizapi/game/GameMode.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package lab.en2b.quizapi.game;

public enum GameMode {
KIWI_QUEST,
FOOTBALL_SHOWDOWN,
GEO_GENIUS,
VIDEOGAME_ADVENTURE,
ANCIENT_ODYSSEY,
RANDOM,
CUSTOM
}

package lab.en2b.quizapi.game;

public enum GameMode {
KIWI_QUEST,
FOOTBALL_SHOWDOWN,
GEO_GENIUS,
VIDEOGAME_ADVENTURE,
ANCIENT_ODYSSEY,
RANDOM,
CUSTOM
}
Loading