Skip to content

Commit

Permalink
refactor: moved game mode logic to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Toto-hitori committed Apr 12, 2024
1 parent 5523540 commit 871dddd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package lab.en2b.quizapi.commons.utils;

import lab.en2b.quizapi.game.Game;
import lab.en2b.quizapi.game.GameMode;
import lab.en2b.quizapi.questions.question.QuestionCategory;

import java.util.List;

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

public class GameModeUtils {
public static List<QuestionCategory> getQuestionCategoriesForGamemode(GameMode gamemode, List<QuestionCategory> questionCategoriesForCustom){
if(gamemode == null){
gamemode = KIWI_QUEST;
}
return switch (gamemode) {
case KIWI_QUEST -> List.of(QuestionCategory.ART, QuestionCategory.MUSIC, QuestionCategory.GEOGRAPHY);
case FOOTBALL_SHOWDOWN -> List.of(QuestionCategory.SPORTS);
case GEO_GENIUS -> List.of(QuestionCategory.GEOGRAPHY);
case VIDEOGAME_ADVENTURE -> List.of(QuestionCategory.VIDEOGAMES);
case ANCIENT_ODYSSEY -> List.of(QuestionCategory.MUSIC,QuestionCategory.ART);
case RANDOM -> List.of(QuestionCategory.values());
case CUSTOM -> questionCategoriesForCustom;
};
}
public static void setGamemodeParams(Game game){
switch(game.getGamemode()){
case KIWI_QUEST:
game.setRounds(9L);
game.setRoundDuration(30);
break;
case FOOTBALL_SHOWDOWN:
game.setRounds(9L);
game.setRoundDuration(30);
break;
case GEO_GENIUS:
game.setRounds(9L);
game.setRoundDuration(30);
break;
case VIDEOGAME_ADVENTURE:
game.setRounds(9L);
game.setRoundDuration(30);
break;
case ANCIENT_ODYSSEY:
game.setRounds(9L);
game.setRoundDuration(30);
break;
case RANDOM:
game.setRounds(9L);
game.setRoundDuration(30);
break;
default:
game.setRounds(9L);
game.setRoundDuration(30);
}
}
}
52 changes: 5 additions & 47 deletions api/src/main/java/lab/en2b/quizapi/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -66,7 +67,7 @@ public Game(User user,GameMode gamemode,String lang, CustomGameDto gameDto){
if(gamemode == CUSTOM)
setCustomGameMode(gameDto);
else
setGamemode(gamemode);
setGameMode(gamemode);
}

public void newRound(Question question){
Expand Down Expand Up @@ -139,44 +140,12 @@ public void setCustomGameMode(CustomGameDto gameDto){
this.gamemode = CUSTOM;
setQuestionCategoriesForCustom(gameDto.getCategories());
}
public void setGamemode(GameMode gamemode){
public void setGameMode(GameMode gamemode){
if(gamemode == null){
gamemode = KIWI_QUEST;
}
setGamemodeParams(gamemode);
}

private void setGamemodeParams(GameMode gamemode){ //This could be moved to a GameMode entity if we have time
switch(gamemode){
case KIWI_QUEST:
setRounds(9L);
setRoundDuration(30);
break;
case FOOTBALL_SHOWDOWN:
setRounds(9L);
setRoundDuration(30);
break;
case GEO_GENIUS:
setRounds(9L);
setRoundDuration(30);
break;
case VIDEOGAME_ADVENTURE:
setRounds(9L);
setRoundDuration(30);
break;
case ANCIENT_ODYSSEY:
setRounds(9L);
setRoundDuration(30);
break;
case RANDOM:
setRounds(9L);
setRoundDuration(30);
break;
default:
setRounds(9L);
setRoundDuration(30);
}
this.gamemode = gamemode;
GameModeUtils.setGamemodeParams(this);
}

public void setQuestionCategoriesForCustom(List<QuestionCategory> questionCategoriesForCustom) {
Expand All @@ -188,18 +157,7 @@ public void setQuestionCategoriesForCustom(List<QuestionCategory> questionCatego
}

public List<QuestionCategory> getQuestionCategoriesForGamemode(){
if(gamemode == null){
gamemode = KIWI_QUEST;
}
return switch (gamemode) {
case KIWI_QUEST -> List.of(QuestionCategory.ART, QuestionCategory.MUSIC, QuestionCategory.GEOGRAPHY);
case FOOTBALL_SHOWDOWN -> List.of(QuestionCategory.SPORTS);
case GEO_GENIUS -> List.of(QuestionCategory.GEOGRAPHY);
case VIDEOGAME_ADVENTURE -> List.of(QuestionCategory.VIDEOGAMES);
case ANCIENT_ODYSSEY -> List.of(QuestionCategory.MUSIC,QuestionCategory.ART);
case RANDOM -> List.of(QuestionCategory.values());
case CUSTOM -> questionCategoriesForCustom;
};
return GameModeUtils.getQuestionCategoriesForGamemode(gamemode,questionCategoriesForCustom);
}
private boolean isLanguageSupported(String language) {
return language.equals("en") || language.equals("es");
Expand Down

0 comments on commit 871dddd

Please sign in to comment.