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

309 lines
8.1 KiB
Go
Raw Normal View History

2024-08-26 14:22:24 +02:00
package flags
import (
2024-09-01 13:15:56 +02:00
"context"
"database/sql"
"net/http"
"strconv"
"time"
"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"
"git.tijl.dev/tijl/tijl.dev-core/modules/web"
"github.com/enescakir/emoji"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
2024-08-26 14:22:24 +02:00
)
2024-08-26 17:43:36 +02:00
const flagSessionCookie string = "app_flags_game_session"
2024-08-26 16:57:28 +02:00
2024-08-26 21:49:37 +02:00
type NullableUUID struct {
2024-09-01 13:15:56 +02:00
uuid.UUID
valid bool
2024-08-26 21:49:37 +02:00
}
type NullableString struct {
2024-09-01 13:15:56 +02:00
String string
valid bool
2024-08-26 21:49:37 +02:00
}
2024-08-26 14:22:24 +02:00
func answerHandler(c *fiber.Ctx) error {
2024-08-26 16:46:28 +02:00
2024-09-01 13:15:56 +02:00
gameId, err := uuid.Parse(c.Cookies(flagSessionCookie))
if err != nil {
return err
}
answer := c.FormValue("answer")
gameSession, err := db.Queries.AppFlagsGetGame(context.TODO(), gameId)
if err != nil {
return err
}
err, countries := filterCountriesByTags(gameSession.Tags)
if err != nil {
return err
}
shuffledCountries := shuffleSlice(countries, gameSession.GameSeed.String())
correctAnswer := shuffledCountries[gameSession.QuestionCurrent-1]
if answer == correctAnswer {
db.Queries.AppFlagsUpdateGame(context.TODO(), queries.AppFlagsUpdateGameParams{
GameID: gameId,
QuestionCurrent: gameSession.QuestionCurrent + 1,
})
db.Queries.AppFlagsUpsertGameAnswer(context.TODO(), queries.AppFlagsUpsertGameAnswerParams{
GameID: gameId,
Question: gameSession.QuestionCurrent,
Errors: 0,
})
} else {
db.Queries.AppFlagsUpsertGameAnswer(context.TODO(), queries.AppFlagsUpsertGameAnswerParams{
GameID: gameId,
Question: gameSession.QuestionCurrent,
Errors: 1,
})
}
err = db.Queries.AppFlagsUpdateQuestionCorrect(context.TODO(), gameId)
if err != nil {
return err
}
if answer == correctAnswer {
return questionHandler(c, NullableUUID{}, NullableString{})
} else {
return questionHandler(c, NullableUUID{}, NullableString{valid: true, String: answer})
}
2024-08-26 14:22:24 +02:00
}
2024-08-26 21:49:37 +02:00
func questionHandler(c *fiber.Ctx, newGame NullableUUID, prevError NullableString) error {
2024-08-26 14:22:24 +02:00
2024-09-01 13:15:56 +02:00
var gameId uuid.UUID
var err error
if newGame.valid {
gameId = newGame.UUID
} else {
gameId, err = uuid.Parse(c.Cookies(flagSessionCookie))
if err != nil {
return err
}
}
gameSession, err := db.Queries.AppFlagsGetGame(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.String())
correctAnswer := shuffledCountries[gameSession.QuestionCurrent-1]
filteredCountries := filterSlice(countries, correctAnswer)
shuffledAnswers := shuffleSlice(filteredCountries, gameSession.GameSeed.String()+string(gameSession.QuestionCurrent))
var correctAnswerData CountryCode
for _, country := range countryCodes {
if country.Code == correctAnswer {
correctAnswerData = country
break
}
}
var filteredAnswers []string
for _, shuffledAnswer := range shuffledAnswers {
for _, country := range countryCodes {
if shuffledAnswer == country.Code {
if hasCommonTag(correctAnswerData.Tags, country.Tags) {
filteredAnswers = append(filteredAnswers, country.Code)
}
}
}
}
shuffledAnswers = filteredAnswers
shuffledAnswers = shuffledAnswers[0:4]
shuffledAnswers = append(shuffledAnswers, correctAnswer)
shuffledAnswers = shuffleSlice(shuffledAnswers, gameSession.GameSeed.String()+string(gameSession.QuestionCurrent))
if gameSession.QuestionAmount != 0 && int(gameSession.QuestionAmount) < len(countries) {
shuffledCountries = shuffledCountries[0:gameSession.QuestionAmount]
}
var timeleft = []string{}
if gameSession.Seconds != 0 {
timeleft = append(timeleft, strconv.Itoa(int(gameSession.Seconds)-int(time.Since(gameSession.CreatedAt).Seconds())))
}
flag, err := emoji.CountryFlag(shuffledCountries[gameSession.QuestionCurrent-1])
if err != nil {
return err
}
data := *web.Common(c)
data["Title"] = "tmp"
data["TimeLeft"] = timeleft
data["QuestionsLeft"] = len(shuffledCountries) - int(gameSession.QuestionCurrent) + 1
data["Answers"] = shuffledAnswers
data["Flag"] = flag
data["Errors"] = gameSession.QuestionsErrors
data["PreviousError"] = prevError.String
data["ShortcutKeys"] = []string{"d", "f", "h", "j", "k"}
2024-09-04 23:03:23 +02:00
return web.Render(c, "apps/flags/question", data, "layouts/base")
2024-08-26 14:22:24 +02:00
}
2024-08-26 20:28:20 +02:00
func sharedGameHandler(c *fiber.Ctx) error {
2024-09-01 13:15:56 +02:00
shareKey := c.FormValue("sharekey")
data, err := db.Queries.AppFlagsGetSharedData(context.TODO(), shareKey)
if err != nil {
return err
}
2024-08-26 20:28:20 +02:00
2024-09-01 13:15:56 +02:00
return setupGame(c, data.Tags, int(data.Questions), int(data.Seconds), NullableUUID{valid: true, UUID: data.GameSeed})
2024-08-26 16:46:28 +02:00
}
2024-08-26 14:22:24 +02:00
func startNewGameHandler(c *fiber.Ctx) error {
2024-09-01 13:15:56 +02:00
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
}
seconds, err := strconv.Atoi(c.FormValue("seconds"))
if err != nil {
return err
}
if c.FormValue("share") != "" {
return createSharedGameData(c, selectedTags, maxQuestions, seconds)
}
return setupGame(c, selectedTags, maxQuestions, seconds, NullableUUID{})
2024-08-26 20:28:20 +02:00
}
func createSharedGameData(c *fiber.Ctx, tags []string, maxQuestions int, seconds int) error {
2024-09-01 13:15:56 +02:00
uid, err := user.GetSession(c)
if err != nil {
return c.SendStatus(http.StatusUnauthorized)
}
shareKey := utils.RandString(4)
_, err = db.Queries.AppFlagsNewSharedData(context.TODO(), queries.AppFlagsNewSharedDataParams{
Uid: uid,
ShareKey: shareKey,
Tags: tags,
Questions: int32(maxQuestions),
Seconds: int32(seconds),
GameSeed: uuid.New(),
})
if err != nil {
return err
}
data := *web.Common(c)
data["Title"] = "tmp"
data["ShareKey"] = shareKey
2024-09-04 23:03:23 +02:00
return web.Render(c, "apps/flags/shared", data, "layouts/base")
2024-08-26 20:28:20 +02:00
}
2024-08-26 21:49:37 +02:00
func setupGame(c *fiber.Ctx, tags []string, maxQuestions int, seconds int, gameSeed NullableUUID) error {
2024-09-01 13:15:56 +02:00
var Quid = sql.NullString{}
uid, err := user.GetSession(c)
if err == nil {
Quid.Valid = true
Quid.String = uid
}
createGameParams := queries.AppFlagsCreateGameWithSeedParams{
Uid: Quid,
Tags: tags,
QuestionAmount: int32(maxQuestions),
Seconds: int32(seconds),
GameSeed: uuid.New(),
}
if gameSeed.valid {
createGameParams.GameSeed = gameSeed.UUID
}
gameID, err := db.Queries.AppFlagsCreateGameWithSeed(context.TODO(), createGameParams)
if err != nil {
return err
}
c.Cookie(&fiber.Cookie{
Name: flagSessionCookie,
Value: gameID.String(),
//Secure: true,
})
return questionHandler(c, NullableUUID{valid: true, UUID: gameID}, NullableString{})
2024-08-26 14:22:24 +02:00
}
2024-08-26 16:46:28 +02:00
func gameEndHandler(c *fiber.Ctx) error {
2024-09-01 13:15:56 +02:00
gameId, err := uuid.Parse(c.Cookies(flagSessionCookie))
if err != nil {
return err
}
gameSession, err := db.Queries.AppFlagsGetGame(context.TODO(), gameId)
if err != nil {
return err
}
data := *web.Common(c)
data["Title"] = "tmp"
data["Errors"] = gameSession.QuestionsErrors
2024-09-04 23:03:23 +02:00
return web.Render(c, "apps/flags/end", data, "layouts/base")
2024-08-26 16:46:28 +02:00
}
func stopGameHandler(c *fiber.Ctx) error { // exit game
2024-09-01 13:15:56 +02:00
utils.ClearCookie(c, flagSessionCookie)
return gameStartHandler(c)
2024-08-26 14:22:24 +02:00
}
2024-08-26 16:57:28 +02:00
2024-08-26 17:43:36 +02:00
func gameStartHandler(c *fiber.Ctx) error {
2024-09-01 13:15:56 +02:00
data := *web.Common(c)
data["Title"] = "Flags Game | tijl.dev"
data["SupportedTags"] = supportedTags
2024-09-04 23:03:23 +02:00
return web.Render(c, "apps/flags/start", data, "layouts/base")
2024-08-26 16:57:28 +02:00
}