flags implementation
Some checks failed
build / build (push) Successful in 27s
release-tag / release-image (push) Has been cancelled

This commit is contained in:
Tijl 2024-08-26 16:57:28 +02:00
parent a71595596a
commit d0e045974f
Signed by: tijl
GPG Key ID: DAE24BFCD722F053
4 changed files with 51 additions and 45 deletions

View File

@ -16,9 +16,11 @@ import (
"github.com/google/uuid"
)
const flagSessionCookie = "app_flags_game_session"
func answerHandler(c *fiber.Ctx) error {
gameId, err := uuid.Parse(c.Cookies("app_flags_game_session"))
gameId, err := uuid.Parse(c.Cookies(flagSessionCookie))
if err != nil {
return err
}
@ -77,7 +79,7 @@ func questionHandler(c *fiber.Ctx, newGame NewGameUUID) error {
if newGame.used {
gameId = newGame.UUID
} else {
gameId, err = uuid.Parse(c.Cookies("app_flags_game_session"))
gameId, err = uuid.Parse(c.Cookies(flagSessionCookie))
if err != nil {
return err
}
@ -88,6 +90,11 @@ func questionHandler(c *fiber.Ctx, newGame NewGameUUID) error {
return err
}
uid, err := user.GetSession(c)
if uid != gameSession.Uid.String {
utils.ClearCookie(c, flagSessionCookie)
}
if (gameSession.QuestionAmount != 0) && (gameSession.QuestionAmount+1 == gameSession.QuestionCurrent) {
return gameEndHandler(c)
}
@ -158,7 +165,7 @@ func startNewGameHandler(c *fiber.Ctx) error {
}
c.Cookie(&fiber.Cookie{
Name: "app_flags_game_session",
Name: flagSessionCookie,
Value: row.GameID.String(),
//Secure: true,
})
@ -167,7 +174,7 @@ func startNewGameHandler(c *fiber.Ctx) error {
}
func gameEndHandler(c *fiber.Ctx) error {
gameId, err := uuid.Parse(c.Cookies("app_flags_game_session"))
gameId, err := uuid.Parse(c.Cookies(flagSessionCookie))
if err != nil {
return err
}
@ -183,6 +190,13 @@ func gameEndHandler(c *fiber.Ctx) error {
}
func stopGameHandler(c *fiber.Ctx) error { // exit game
utils.ClearCookie(c, "app_flags_game_session")
utils.ClearCookie(c, flagSessionCookie)
return EntryPageHandler(c)
}
func EntryPageHandler(c *fiber.Ctx) error {
data := *web.Common(c)
data["Title"] = "tmp"
data["SupportedTags"] = supportedTags
return c.Render("apps/flags/start", data, "layouts/base")
}

View File

@ -1,12 +1,8 @@
package flags
// WIP
import (
"embed"
"encoding/json"
"errors"
"fmt"
"git.tijl.dev/tijl/tijl.dev-core/modules/i18n"
log "git.tijl.dev/tijl/tijl.dev-core/modules/logger"
@ -22,34 +18,6 @@ type CountryCode struct {
Tags []string `json:"tags"`
}
func filterCountriesByTags(tags []string) (error, []string) {
var result []string
for _, tag := range tags {
isSupported := false
for _, supportedTag := range supportedTags {
if tag == supportedTag {
isSupported = true
}
}
if isSupported == false {
return errors.New(fmt.Sprintf("unsupported tag %v", tag)), result
}
}
for _, country := range countryCodes {
for _, tag := range tags {
for _, cTag := range country.Tags {
if cTag == tag {
result = append(result, country.Code)
}
}
}
}
return nil, result
}
//go:embed locales/* data/*
var Embed embed.FS
@ -115,10 +83,3 @@ func loadData() error {
return nil
}
func EntryPageHandler(c *fiber.Ctx) error {
data := *web.Common(c)
data["Title"] = "tmp"
data["SupportedTags"] = supportedTags
return c.Render("apps/flags/start", data, "layouts/base")
}

View File

@ -3,6 +3,8 @@ package flags
import (
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
"math/rand"
)
@ -36,3 +38,31 @@ func shuffleSlice(slice []string, seed string) []string {
return shuffled
}
func filterCountriesByTags(tags []string) (error, []string) {
var result []string
for _, tag := range tags {
isSupported := false
for _, supportedTag := range supportedTags {
if tag == supportedTag {
isSupported = true
}
}
if isSupported == false {
return errors.New(fmt.Sprintf("unsupported tag %v", tag)), result
}
}
for _, country := range countryCodes {
for _, tag := range tags {
for _, cTag := range country.Tags {
if cTag == tag {
result = append(result, country.Code)
}
}
}
}
return nil, result
}

View File

@ -5,6 +5,7 @@ import (
"git.tijl.dev/tijl/tijl.dev-core/internal/oidc"
"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/i18n"
"git.tijl.dev/tijl/tijl.dev-core/modules/web"
"github.com/gofiber/fiber/v2"
@ -44,7 +45,7 @@ func authHandler(c *fiber.Ctx) error {
func logoutHandler(c *fiber.Ctx) error {
_, err := user.GetSession(c)
if err == nil {
c.ClearCookie("session", "state")
utils.ClearCookie(c, "session")
return c.Redirect("/login", http.StatusOK)
} else {
return c.Redirect("/login", http.StatusUnauthorized)