From c2de456291b713bb96b486b39523f0d2793e99a4 Mon Sep 17 00:00:00 2001 From: tijl Date: Mon, 26 Aug 2024 17:43:36 +0200 Subject: [PATCH] flags --- internal/apps/flags/handlers.go | 24 ++++++++++++++++++++---- internal/apps/flags/locales/en.json | 1 + internal/apps/flags/locales/nl.json | 1 + internal/apps/flags/main.go | 2 +- locales/nl.json | 3 ++- web/views/apps/flags/start.html | 7 +++++-- web/views/projects.html | 2 +- 7 files changed, 31 insertions(+), 9 deletions(-) diff --git a/internal/apps/flags/handlers.go b/internal/apps/flags/handlers.go index 0c88d36..0798582 100644 --- a/internal/apps/flags/handlers.go +++ b/internal/apps/flags/handlers.go @@ -3,12 +3,14 @@ package flags import ( "context" "database/sql" + "net/http" "strconv" "git.tijl.dev/tijl/tijl.dev-core/internal/queries" "git.tijl.dev/tijl/tijl.dev-core/internal/user" "git.tijl.dev/tijl/tijl.dev-core/internal/utils" "git.tijl.dev/tijl/tijl.dev-core/modules/db" + "git.tijl.dev/tijl/tijl.dev-core/modules/i18n" log "git.tijl.dev/tijl/tijl.dev-core/modules/logger" "git.tijl.dev/tijl/tijl.dev-core/modules/web" "github.com/enescakir/emoji" @@ -16,7 +18,7 @@ import ( "github.com/google/uuid" ) -const flagSessionCookie = "app_flags_game_session" +const flagSessionCookie string = "app_flags_game_session" func answerHandler(c *fiber.Ctx) error { @@ -93,6 +95,7 @@ func questionHandler(c *fiber.Ctx, newGame NewGameUUID) error { uid, err := user.GetSession(c) if uid != gameSession.Uid.String { utils.ClearCookie(c, flagSessionCookie) + return gameStartHandler(c) } if (gameSession.QuestionAmount != 0) && (gameSession.QuestionAmount+1 == gameSession.QuestionCurrent) { @@ -114,7 +117,7 @@ func questionHandler(c *fiber.Ctx, newGame NewGameUUID) error { shuffledAnswers = append(shuffledAnswers, shuffledCountries[gameSession.QuestionCurrent-1]) // add correct answer shuffledAnswers = shuffleSlice(shuffledAnswers, gameSession.GameSeed.UUID.String()+string(gameSession.QuestionCurrent)) // shuffle again - if gameSession.QuestionAmount != 0 { + if gameSession.QuestionAmount != 0 && int(gameSession.QuestionAmount) < len(countries) { shuffledCountries = shuffledCountries[0:gameSession.QuestionAmount] } @@ -143,6 +146,19 @@ func startNewGameHandler(c *fiber.Ctx) error { selectedTags = append(selectedTags, string(v)) } + if len(selectedTags) < 1 { + return c.Status(http.StatusBadRequest).SendString(i18n.GetTranslations(i18n.GetLanguage(c))["select_more_countries"]) + } + + err, countries := filterCountriesByTags(selectedTags) + if err != nil { + return err + } + + if len(countries) < 6 { + return c.Status(http.StatusBadRequest).SendString(i18n.GetTranslations(i18n.GetLanguage(c))["select_more_countries"]) + } + maxQuestions, err := strconv.Atoi(c.FormValue("max_questions")) if err != nil { return err @@ -191,10 +207,10 @@ func gameEndHandler(c *fiber.Ctx) error { func stopGameHandler(c *fiber.Ctx) error { // exit game utils.ClearCookie(c, flagSessionCookie) - return EntryPageHandler(c) + return gameStartHandler(c) } -func EntryPageHandler(c *fiber.Ctx) error { +func gameStartHandler(c *fiber.Ctx) error { data := *web.Common(c) data["Title"] = "tmp" data["SupportedTags"] = supportedTags diff --git a/internal/apps/flags/locales/en.json b/internal/apps/flags/locales/en.json index 3c0ce1a..fd320d6 100644 --- a/internal/apps/flags/locales/en.json +++ b/internal/apps/flags/locales/en.json @@ -1,4 +1,5 @@ { + "select_more_countries": "Too few countries", "Asia": "Asia", "MiddleEast": "Middle East", "SoutheastAsia": "Southeast Asia", diff --git a/internal/apps/flags/locales/nl.json b/internal/apps/flags/locales/nl.json index da27f8f..29a302f 100644 --- a/internal/apps/flags/locales/nl.json +++ b/internal/apps/flags/locales/nl.json @@ -1,4 +1,5 @@ { + "select_more_countries": "Te weinig landen", "Asia": "Aziƫ", "MiddleEast": "Midden-Oosten", "SoutheastAsia": "Zuidoost-Aziƫ", diff --git a/internal/apps/flags/main.go b/internal/apps/flags/main.go index 1c9abd6..b4d6c4c 100644 --- a/internal/apps/flags/main.go +++ b/internal/apps/flags/main.go @@ -35,7 +35,7 @@ func Setup() { if c.Cookies("app_flags_game_session") != "" { return questionHandler(c, NewGameUUID{}) } else { - return EntryPageHandler(c) + return gameStartHandler(c) } }) diff --git a/locales/nl.json b/locales/nl.json index 8603d1a..c331f1c 100644 --- a/locales/nl.json +++ b/locales/nl.json @@ -12,5 +12,6 @@ "settings": "Instellingen", "page_not_found": "Pagina niet gevonden", "logged_in": "Je bent nu ingelogd!", - "services": "Services" + "services": "Services", + "error_long": "Er ging iets fout" } diff --git a/web/views/apps/flags/start.html b/web/views/apps/flags/start.html index 38587c6..733ffa1 100644 --- a/web/views/apps/flags/start.html +++ b/web/views/apps/flags/start.html @@ -1,5 +1,8 @@
-
+
+
@@ -16,7 +19,7 @@ --> - +
diff --git a/web/views/projects.html b/web/views/projects.html index bdd4eda..78484fd 100644 --- a/web/views/projects.html +++ b/web/views/projects.html @@ -1 +1 @@ -
soon
+
flags