flags implementation
This commit is contained in:
parent
a71595596a
commit
d0e045974f
@ -16,9 +16,11 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const flagSessionCookie = "app_flags_game_session"
|
||||||
|
|
||||||
func answerHandler(c *fiber.Ctx) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -77,7 +79,7 @@ func questionHandler(c *fiber.Ctx, newGame NewGameUUID) error {
|
|||||||
if newGame.used {
|
if newGame.used {
|
||||||
gameId = newGame.UUID
|
gameId = newGame.UUID
|
||||||
} else {
|
} else {
|
||||||
gameId, err = uuid.Parse(c.Cookies("app_flags_game_session"))
|
gameId, err = uuid.Parse(c.Cookies(flagSessionCookie))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -88,6 +90,11 @@ func questionHandler(c *fiber.Ctx, newGame NewGameUUID) error {
|
|||||||
return err
|
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) {
|
if (gameSession.QuestionAmount != 0) && (gameSession.QuestionAmount+1 == gameSession.QuestionCurrent) {
|
||||||
return gameEndHandler(c)
|
return gameEndHandler(c)
|
||||||
}
|
}
|
||||||
@ -158,7 +165,7 @@ func startNewGameHandler(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.Cookie(&fiber.Cookie{
|
c.Cookie(&fiber.Cookie{
|
||||||
Name: "app_flags_game_session",
|
Name: flagSessionCookie,
|
||||||
Value: row.GameID.String(),
|
Value: row.GameID.String(),
|
||||||
//Secure: true,
|
//Secure: true,
|
||||||
})
|
})
|
||||||
@ -167,7 +174,7 @@ func startNewGameHandler(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func gameEndHandler(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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -183,6 +190,13 @@ 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, "app_flags_game_session")
|
utils.ClearCookie(c, flagSessionCookie)
|
||||||
return EntryPageHandler(c)
|
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")
|
||||||
|
}
|
@ -1,12 +1,8 @@
|
|||||||
package flags
|
package flags
|
||||||
|
|
||||||
// WIP
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.tijl.dev/tijl/tijl.dev-core/modules/i18n"
|
"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"
|
||||||
@ -22,34 +18,6 @@ type CountryCode struct {
|
|||||||
Tags []string `json:"tags"`
|
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/*
|
//go:embed locales/* data/*
|
||||||
var Embed embed.FS
|
var Embed embed.FS
|
||||||
|
|
||||||
@ -115,10 +83,3 @@ func loadData() error {
|
|||||||
|
|
||||||
return nil
|
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")
|
|
||||||
}
|
|
||||||
|
@ -3,6 +3,8 @@ package flags
|
|||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,3 +38,31 @@ func shuffleSlice(slice []string, seed string) []string {
|
|||||||
|
|
||||||
return shuffled
|
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
|
||||||
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"git.tijl.dev/tijl/tijl.dev-core/internal/oidc"
|
"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/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/i18n"
|
||||||
"git.tijl.dev/tijl/tijl.dev-core/modules/web"
|
"git.tijl.dev/tijl/tijl.dev-core/modules/web"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
@ -44,7 +45,7 @@ func authHandler(c *fiber.Ctx) error {
|
|||||||
func logoutHandler(c *fiber.Ctx) error {
|
func logoutHandler(c *fiber.Ctx) error {
|
||||||
_, err := user.GetSession(c)
|
_, err := user.GetSession(c)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.ClearCookie("session", "state")
|
utils.ClearCookie(c, "session")
|
||||||
return c.Redirect("/login", http.StatusOK)
|
return c.Redirect("/login", http.StatusOK)
|
||||||
} else {
|
} else {
|
||||||
return c.Redirect("/login", http.StatusUnauthorized)
|
return c.Redirect("/login", http.StatusUnauthorized)
|
||||||
|
Loading…
Reference in New Issue
Block a user