flags
Some checks are pending
build / build (push) Waiting to run
release-tag / release-image (push) Waiting to run

This commit is contained in:
Tijl 2024-08-26 17:43:36 +02:00
parent d0e045974f
commit c2de456291
Signed by: tijl
GPG Key ID: DAE24BFCD722F053
7 changed files with 31 additions and 9 deletions

View File

@ -3,12 +3,14 @@ package flags
import ( import (
"context" "context"
"database/sql" "database/sql"
"net/http"
"strconv" "strconv"
"git.tijl.dev/tijl/tijl.dev-core/internal/queries" "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/user"
"git.tijl.dev/tijl/tijl.dev-core/internal/utils" "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/db"
"git.tijl.dev/tijl/tijl.dev-core/modules/i18n"
log "git.tijl.dev/tijl/tijl.dev-core/modules/logger" log "git.tijl.dev/tijl/tijl.dev-core/modules/logger"
"git.tijl.dev/tijl/tijl.dev-core/modules/web" "git.tijl.dev/tijl/tijl.dev-core/modules/web"
"github.com/enescakir/emoji" "github.com/enescakir/emoji"
@ -16,7 +18,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
const flagSessionCookie = "app_flags_game_session" const flagSessionCookie string = "app_flags_game_session"
func answerHandler(c *fiber.Ctx) error { func answerHandler(c *fiber.Ctx) error {
@ -93,6 +95,7 @@ func questionHandler(c *fiber.Ctx, newGame NewGameUUID) error {
uid, err := user.GetSession(c) uid, err := user.GetSession(c)
if uid != gameSession.Uid.String { if uid != gameSession.Uid.String {
utils.ClearCookie(c, flagSessionCookie) utils.ClearCookie(c, flagSessionCookie)
return gameStartHandler(c)
} }
if (gameSession.QuestionAmount != 0) && (gameSession.QuestionAmount+1 == gameSession.QuestionCurrent) { 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 = append(shuffledAnswers, shuffledCountries[gameSession.QuestionCurrent-1]) // add correct answer
shuffledAnswers = shuffleSlice(shuffledAnswers, gameSession.GameSeed.UUID.String()+string(gameSession.QuestionCurrent)) // shuffle again 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] shuffledCountries = shuffledCountries[0:gameSession.QuestionAmount]
} }
@ -143,6 +146,19 @@ func startNewGameHandler(c *fiber.Ctx) error {
selectedTags = append(selectedTags, string(v)) 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")) maxQuestions, err := strconv.Atoi(c.FormValue("max_questions"))
if err != nil { if err != nil {
return err return err
@ -191,10 +207,10 @@ func gameEndHandler(c *fiber.Ctx) error {
func stopGameHandler(c *fiber.Ctx) error { // exit game func stopGameHandler(c *fiber.Ctx) error { // exit game
utils.ClearCookie(c, flagSessionCookie) 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 := *web.Common(c)
data["Title"] = "tmp" data["Title"] = "tmp"
data["SupportedTags"] = supportedTags data["SupportedTags"] = supportedTags

View File

@ -1,4 +1,5 @@
{ {
"select_more_countries": "Too few countries",
"Asia": "Asia", "Asia": "Asia",
"MiddleEast": "Middle East", "MiddleEast": "Middle East",
"SoutheastAsia": "Southeast Asia", "SoutheastAsia": "Southeast Asia",

View File

@ -1,4 +1,5 @@
{ {
"select_more_countries": "Te weinig landen",
"Asia": "Azië", "Asia": "Azië",
"MiddleEast": "Midden-Oosten", "MiddleEast": "Midden-Oosten",
"SoutheastAsia": "Zuidoost-Azië", "SoutheastAsia": "Zuidoost-Azië",

View File

@ -35,7 +35,7 @@ func Setup() {
if c.Cookies("app_flags_game_session") != "" { if c.Cookies("app_flags_game_session") != "" {
return questionHandler(c, NewGameUUID{}) return questionHandler(c, NewGameUUID{})
} else { } else {
return EntryPageHandler(c) return gameStartHandler(c)
} }
}) })

View File

@ -12,5 +12,6 @@
"settings": "Instellingen", "settings": "Instellingen",
"page_not_found": "Pagina niet gevonden", "page_not_found": "Pagina niet gevonden",
"logged_in": "Je bent nu ingelogd!", "logged_in": "Je bent nu ingelogd!",
"services": "Services" "services": "Services",
"error_long": "Er ging iets fout"
} }

View File

@ -1,5 +1,8 @@
<div hx-boost="true"> <div hx-boost="true">
<form method="post"> <div id="error-message" class="text-red-500 mb-4"></div>
<form method="post" hx-on="htmx:responseError:
document.getElementById('error-message').innerHTML = '{{.T.error_long}}: ' + event.detail.xhr.responseText;
htmx.trigger(this, 'htmx:swap', { target: '#error-message', swap: 'innerHTML' });">
<input type="text" class="hidden" name="type" value="start" /> <input type="text" class="hidden" name="type" value="start" />
<div> <div>
@ -16,7 +19,7 @@
<input name="tags" value="{{.}}" type="checkbox" class="checkbox checkbox-primary" /> <input name="tags" value="{{.}}" type="checkbox" class="checkbox checkbox-primary" />
</label>--> </label>-->
<input value="0" name="max_questions" type="number" class="input input-bordered" /> <input value="0" placeholder="max questions" name="max_questions" type="number" class="input input-bordered" />
<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" class="btn btn-primary">Submit</button>
</form> </form>
</div> </div>

View File

@ -1 +1 @@
<div>soon</div> <div><a hx-boost="true" href="/app/flags">flags</a></div>