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" "github.com/gofiber/fiber/v2" "github.com/google/uuid" ) const flagSessionCookie string = "app_flags_game_session" func answerHandler(c *fiber.Ctx) error { gameId, err := uuid.Parse(c.Cookies(flagSessionCookie)) if err != nil { return err } answer := c.FormValue("answer") gameSession, err := db.Queries.GetFlagsGame(context.TODO(), gameId) if err != nil { return err } err, countries := filterCountriesByTags(gameSession.Tags) if err != nil { return err } shuffledCountries := shuffleSlice(countries, gameSession.GameSeed.UUID.String()) correctAnswer := shuffledCountries[gameSession.QuestionCurrent-1] if answer == correctAnswer { db.Queries.UpdateFlagsGame(context.TODO(), queries.UpdateFlagsGameParams{ GameID: gameId, QuestionCurrent: gameSession.QuestionCurrent + 1, }) db.Queries.UpsertGameAnswer(context.TODO(), queries.UpsertGameAnswerParams{ GameID: gameId, Question: gameSession.QuestionCurrent, Errors: 0, }) } else { db.Queries.UpsertGameAnswer(context.TODO(), queries.UpsertGameAnswerParams{ GameID: gameId, Question: gameSession.QuestionCurrent, Errors: 1, }) } err = db.Queries.UpdateQuestionCorrect(context.TODO(), gameId) if err != nil { return err } return questionHandler(c, NewGameUUID{}) } type NewGameUUID struct { uuid.UUID used bool } func questionHandler(c *fiber.Ctx, newGame NewGameUUID) error { var gameId uuid.UUID var err error if newGame.used { gameId = newGame.UUID } else { gameId, err = uuid.Parse(c.Cookies(flagSessionCookie)) if err != nil { return err } } gameSession, err := db.Queries.GetFlagsGame(context.TODO(), gameId) if err != nil { return err } 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) { return gameEndHandler(c) } err, countries := filterCountriesByTags(gameSession.Tags) if err != nil { return err } if int(gameSession.QuestionCurrent) == len(countries) { return gameEndHandler(c) } shuffledCountries := shuffleSlice(countries, gameSession.GameSeed.UUID.String()) shuffledAnswers := shuffleSlice(countries, gameSession.GameSeed.UUID.String()+string(gameSession.QuestionCurrent)) shuffledAnswers = shuffledAnswers[0:4] // 4 random aswers 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 && int(gameSession.QuestionAmount) < len(countries) { shuffledCountries = shuffledCountries[0:gameSession.QuestionAmount] } log.Debug().Interface("shuffledCountries", shuffledCountries).Interface("shuffledAnswers", shuffledAnswers).Msg("data") flag, err := emoji.CountryFlag(shuffledCountries[gameSession.QuestionCurrent-1]) if err != nil { return err } data := *web.Common(c) data["Title"] = "tmp" data["Answers"] = shuffledAnswers data["Flag"] = flag data["Errors"] = gameSession.QuestionsErrors return c.Render("apps/flags/question", data, "layouts/base") } func getGameData() { } func startNewGameHandler(c *fiber.Ctx) error { values := c.Request().PostArgs().PeekMulti("tags") var selectedTags []string for _, v := range values { 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 } var Quid = sql.NullString{} uid, err := user.GetSession(c) if err == nil { Quid.Valid = true Quid.String = uid } row, err := db.Queries.CreateFlagsGame(context.TODO(), queries.CreateFlagsGameParams{ Uid: Quid, Tags: selectedTags, QuestionAmount: int32(maxQuestions), }) if err != nil { return err } c.Cookie(&fiber.Cookie{ Name: flagSessionCookie, Value: row.GameID.String(), //Secure: true, }) return questionHandler(c, NewGameUUID{used: true, UUID: row.GameID}) } func gameEndHandler(c *fiber.Ctx) error { gameId, err := uuid.Parse(c.Cookies(flagSessionCookie)) if err != nil { return err } gameSession, err := db.Queries.GetFlagsGame(context.TODO(), gameId) if err != nil { return err } data := *web.Common(c) data["Title"] = "tmp" data["Errors"] = gameSession.QuestionsErrors return c.Render("apps/flags/end", data, "layouts/base") } func stopGameHandler(c *fiber.Ctx) error { // exit game utils.ClearCookie(c, flagSessionCookie) return gameStartHandler(c) } func gameStartHandler(c *fiber.Ctx) error { data := *web.Common(c) data["Title"] = "tmp" data["SupportedTags"] = supportedTags return c.Render("apps/flags/start", data, "layouts/base") }