tijl.dev-core/internal/apps/flags/game.go

121 lines
3.2 KiB
Go
Raw Normal View History

2024-08-26 14:22:24 +02:00
package flags
import (
"context"
"database/sql"
"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"
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"
)
func answerHandler(c *fiber.Ctx) error {
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("app_flags_game_session"))
if err != nil {
return err
}
}
gameSession, err := db.Queries.GetFlagsGame(context.TODO(), gameId)
if err != nil {
return err
}
if (gameSession.QuestionAmount != 0) && (gameSession.QuestionAmount+1 == gameSession.QuestionCurrent) {
// TODO: game is over
return c.SendString("game is over")
}
err, countries := filterCountriesByTags(gameSession.Tags)
if err != nil {
return err
}
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 {
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
return c.Render("apps/flags/question", data, "layouts/base")
}
func startNewGameHandler(c *fiber.Ctx) error {
values := c.Request().PostArgs().PeekMulti("tags")
var selectedTags []string
for _, v := range values {
selectedTags = append(selectedTags, string(v))
}
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: "app_flags_game_session",
Value: row.GameID.String(),
Secure: true,
})
return questionHandler(c, NewGameUUID{used: true, UUID: row.GameID})
}
func stopGameHandler(c *fiber.Ctx) error {
utils.ClearCookie(c, "app_flags_game_session")
return EntryPageHandler(c)
}