diff --git a/api/src/main/java/lab/en2b/quizapi/commons/utils/GameModeUtils.java b/api/src/main/java/lab/en2b/quizapi/commons/utils/GameModeUtils.java new file mode 100644 index 00000000..4e6c2886 --- /dev/null +++ b/api/src/main/java/lab/en2b/quizapi/commons/utils/GameModeUtils.java @@ -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 getQuestionCategoriesForGamemode(GameMode gamemode, List 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); + } + } +} diff --git a/api/src/main/java/lab/en2b/quizapi/game/Game.java b/api/src/main/java/lab/en2b/quizapi/game/Game.java index a75c9ca5..19097219 100644 --- a/api/src/main/java/lab/en2b/quizapi/game/Game.java +++ b/api/src/main/java/lab/en2b/quizapi/game/Game.java @@ -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; @@ -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){ @@ -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 questionCategoriesForCustom) { @@ -188,18 +157,7 @@ public void setQuestionCategoriesForCustom(List questionCatego } public List 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");