flags
This commit is contained in:
parent
4a268ef8c7
commit
d246f5e270
1
go.mod
1
go.mod
@ -20,6 +20,7 @@ require (
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/brotli v1.1.0 // indirect
|
github.com/andybalholm/brotli v1.1.0 // indirect
|
||||||
|
github.com/enescakir/emoji v1.0.0 // indirect
|
||||||
github.com/go-jose/go-jose/v4 v4.0.2 // indirect
|
github.com/go-jose/go-jose/v4 v4.0.2 // indirect
|
||||||
github.com/gofiber/template v1.8.3 // indirect
|
github.com/gofiber/template v1.8.3 // indirect
|
||||||
github.com/gofiber/utils v1.1.0 // indirect
|
github.com/gofiber/utils v1.1.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -21,6 +21,8 @@ github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKoh
|
|||||||
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
|
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
|
||||||
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||||
|
github.com/enescakir/emoji v1.0.0 h1:W+HsNql8swfCQFtioDGDHCHri8nudlK1n5p2rHCJoog=
|
||||||
|
github.com/enescakir/emoji v1.0.0/go.mod h1:Bt1EKuLnKDTYpLALApstIkAjdDrS/8IAgTkKp+WKFD0=
|
||||||
github.com/go-jose/go-jose/v4 v4.0.2 h1:R3l3kkBds16bO7ZFAEEcofK0MkrAJt3jlJznWZG0nvk=
|
github.com/go-jose/go-jose/v4 v4.0.2 h1:R3l3kkBds16bO7ZFAEEcofK0MkrAJt3jlJznWZG0nvk=
|
||||||
github.com/go-jose/go-jose/v4 v4.0.2/go.mod h1:WVf9LFMHh/QVrmqrOfqun0C45tMe3RoiKJMPvgWwLfY=
|
github.com/go-jose/go-jose/v4 v4.0.2/go.mod h1:WVf9LFMHh/QVrmqrOfqun0C45tMe3RoiKJMPvgWwLfY=
|
||||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
|
120
internal/apps/flags/game.go
Normal file
120
internal/apps/flags/game.go
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
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)
|
||||||
|
}
|
@ -1 +0,0 @@
|
|||||||
package flags
|
|
@ -1,269 +1,269 @@
|
|||||||
{
|
{
|
||||||
"Asia": "Asia",
|
"Asia": "Asia",
|
||||||
"MiddleEast": "Middle East",
|
"MiddleEast": "Middle East",
|
||||||
"SoutheastAsia": "Southeast Asia",
|
"SoutheastAsia": "Southeast Asia",
|
||||||
"CentralAsia": "Central Asia",
|
"CentralAsia": "Central Asia",
|
||||||
"Europe": "Europe",
|
"Europe": "Europe",
|
||||||
"Balkans": "Balkans",
|
"Balkans": "Balkans",
|
||||||
"NorthAmerica": "North America",
|
"NorthAmerica": "North America",
|
||||||
"SouthAmerica": "South America",
|
"SouthAmerica": "South America",
|
||||||
"CentralAmerica": "Central America",
|
"CentralAmerica": "Central America",
|
||||||
"Caribbean": "Caribbean",
|
"Caribbean": "Caribbean",
|
||||||
"Africa": "Africa",
|
"Africa": "Africa",
|
||||||
"NorthAfrica": "North Africa",
|
"NorthAfrica": "North Africa",
|
||||||
"SouthernAfrica": "Southern Africa",
|
"SouthernAfrica": "Southern Africa",
|
||||||
"WestAfrica": "West Africa",
|
"WestAfrica": "West Africa",
|
||||||
"EastAfrica": "East Africa",
|
"EastAfrica": "East Africa",
|
||||||
"CentralAfrica": "Central Africa",
|
"CentralAfrica": "Central Africa",
|
||||||
"Oceania": "Oceania",
|
"Oceania": "Oceania",
|
||||||
"BD": "Bangladesh",
|
"BD": "Bangladesh",
|
||||||
"BE": "Belgium",
|
"BE": "Belgium",
|
||||||
"BF": "Burkina Faso",
|
"BF": "Burkina Faso",
|
||||||
"BG": "Bulgaria",
|
"BG": "Bulgaria",
|
||||||
"BA": "Bosnia and Herzegovina",
|
"BA": "Bosnia and Herzegovina",
|
||||||
"BB": "Barbados",
|
"BB": "Barbados",
|
||||||
"WF": "Wallis and Futuna",
|
"WF": "Wallis and Futuna",
|
||||||
"BL": "Saint Barthelemy",
|
"BL": "Saint Barthelemy",
|
||||||
"BM": "Bermuda",
|
"BM": "Bermuda",
|
||||||
"BN": "Brunei",
|
"BN": "Brunei",
|
||||||
"BO": "Bolivia",
|
"BO": "Bolivia",
|
||||||
"BH": "Bahrain",
|
"BH": "Bahrain",
|
||||||
"BI": "Burundi",
|
"BI": "Burundi",
|
||||||
"BJ": "Benin",
|
"BJ": "Benin",
|
||||||
"BT": "Bhutan",
|
"BT": "Bhutan",
|
||||||
"JM": "Jamaica",
|
"JM": "Jamaica",
|
||||||
"BV": "Bouvet Island",
|
"BV": "Bouvet Island",
|
||||||
"BW": "Botswana",
|
"BW": "Botswana",
|
||||||
"WS": "Samoa",
|
"WS": "Samoa",
|
||||||
"BQ": "Caribbean Netherlands",
|
"BQ": "Caribbean Netherlands",
|
||||||
"BR": "Brazil",
|
"BR": "Brazil",
|
||||||
"BS": "Bahamas",
|
"BS": "Bahamas",
|
||||||
"JE": "Jersey",
|
"JE": "Jersey",
|
||||||
"BY": "Belarus",
|
"BY": "Belarus",
|
||||||
"BZ": "Belize",
|
"BZ": "Belize",
|
||||||
"RU": "Russia",
|
"RU": "Russia",
|
||||||
"RW": "Rwanda",
|
"RW": "Rwanda",
|
||||||
"RS": "Serbia",
|
"RS": "Serbia",
|
||||||
"TL": "East Timor",
|
"TL": "East Timor",
|
||||||
"RE": "Reunion",
|
"RE": "Reunion",
|
||||||
"TM": "Turkmenistan",
|
"TM": "Turkmenistan",
|
||||||
"TJ": "Tajikistan",
|
"TJ": "Tajikistan",
|
||||||
"RO": "Romania",
|
"RO": "Romania",
|
||||||
"TK": "Tokelau",
|
"TK": "Tokelau",
|
||||||
"GW": "Guinea-Bissau",
|
"GW": "Guinea-Bissau",
|
||||||
"GU": "Guam",
|
"GU": "Guam",
|
||||||
"GT": "Guatemala",
|
"GT": "Guatemala",
|
||||||
"GS": "South Georgia and the South Sandwich Islands",
|
"GS": "South Georgia and the South Sandwich Islands",
|
||||||
"GR": "Greece",
|
"GR": "Greece",
|
||||||
"GQ": "Equatorial Guinea",
|
"GQ": "Equatorial Guinea",
|
||||||
"GP": "Guadeloupe",
|
"GP": "Guadeloupe",
|
||||||
"JP": "Japan",
|
"JP": "Japan",
|
||||||
"GY": "Guyana",
|
"GY": "Guyana",
|
||||||
"GG": "Guernsey",
|
"GG": "Guernsey",
|
||||||
"GF": "French Guiana",
|
"GF": "French Guiana",
|
||||||
"GE": "Georgia",
|
"GE": "Georgia",
|
||||||
"GD": "Grenada",
|
"GD": "Grenada",
|
||||||
"GB": "United Kingdom",
|
"GB": "United Kingdom",
|
||||||
"GA": "Gabon",
|
"GA": "Gabon",
|
||||||
"SV": "El Salvador",
|
"SV": "El Salvador",
|
||||||
"GN": "Guinea",
|
"GN": "Guinea",
|
||||||
"GM": "Gambia",
|
"GM": "Gambia",
|
||||||
"GL": "Greenland",
|
"GL": "Greenland",
|
||||||
"GI": "Gibraltar",
|
"GI": "Gibraltar",
|
||||||
"GH": "Ghana",
|
"GH": "Ghana",
|
||||||
"OM": "Oman",
|
"OM": "Oman",
|
||||||
"TN": "Tunisia",
|
"TN": "Tunisia",
|
||||||
"JO": "Jordan",
|
"JO": "Jordan",
|
||||||
"HR": "Croatia",
|
"HR": "Croatia",
|
||||||
"HT": "Haiti",
|
"HT": "Haiti",
|
||||||
"HU": "Hungary",
|
"HU": "Hungary",
|
||||||
"HK": "Hong Kong",
|
"HK": "Hong Kong",
|
||||||
"HN": "Honduras",
|
"HN": "Honduras",
|
||||||
"HM": "Heard Island and McDonald Islands",
|
"HM": "Heard Island and McDonald Islands",
|
||||||
"VE": "Venezuela",
|
"VE": "Venezuela",
|
||||||
"PR": "Puerto Rico",
|
"PR": "Puerto Rico",
|
||||||
"PS": "Palestine",
|
"PS": "Palestine",
|
||||||
"PW": "Palau",
|
"PW": "Palau",
|
||||||
"PT": "Portugal",
|
"PT": "Portugal",
|
||||||
"SJ": "Svalbard and Jan Mayen",
|
"SJ": "Svalbard and Jan Mayen",
|
||||||
"PY": "Paraguay",
|
"PY": "Paraguay",
|
||||||
"IQ": "Iraq",
|
"IQ": "Iraq",
|
||||||
"PA": "Panama",
|
"PA": "Panama",
|
||||||
"PF": "French Polynesia",
|
"PF": "French Polynesia",
|
||||||
"PG": "Papua New Guinea",
|
"PG": "Papua New Guinea",
|
||||||
"PE": "Peru",
|
"PE": "Peru",
|
||||||
"PK": "Pakistan",
|
"PK": "Pakistan",
|
||||||
"PH": "Philippines",
|
"PH": "Philippines",
|
||||||
"PN": "Pitcairn",
|
"PN": "Pitcairn",
|
||||||
"PL": "Poland",
|
"PL": "Poland",
|
||||||
"PM": "Saint Pierre and Miquelon",
|
"PM": "Saint Pierre and Miquelon",
|
||||||
"ZM": "Zambia",
|
"ZM": "Zambia",
|
||||||
"EH": "Western Sahara",
|
"EH": "Western Sahara",
|
||||||
"EE": "Estonia",
|
"EE": "Estonia",
|
||||||
"EG": "Egypt",
|
"EG": "Egypt",
|
||||||
"ZA": "South Africa",
|
"ZA": "South Africa",
|
||||||
"EC": "Ecuador",
|
"EC": "Ecuador",
|
||||||
"IT": "Italy",
|
"IT": "Italy",
|
||||||
"VN": "Vietnam",
|
"VN": "Vietnam",
|
||||||
"SB": "Solomon Islands",
|
"SB": "Solomon Islands",
|
||||||
"ET": "Ethiopia",
|
"ET": "Ethiopia",
|
||||||
"SO": "Somalia",
|
"SO": "Somalia",
|
||||||
"ZW": "Zimbabwe",
|
"ZW": "Zimbabwe",
|
||||||
"SA": "Saudi Arabia",
|
"SA": "Saudi Arabia",
|
||||||
"ES": "Spain",
|
"ES": "Spain",
|
||||||
"ER": "Eritrea",
|
"ER": "Eritrea",
|
||||||
"ME": "Montenegro",
|
"ME": "Montenegro",
|
||||||
"MD": "Moldova",
|
"MD": "Moldova",
|
||||||
"MG": "Madagascar",
|
"MG": "Madagascar",
|
||||||
"MF": "Saint Martin",
|
"MF": "Saint Martin",
|
||||||
"MA": "Morocco",
|
"MA": "Morocco",
|
||||||
"MC": "Monaco",
|
"MC": "Monaco",
|
||||||
"UZ": "Uzbekistan",
|
"UZ": "Uzbekistan",
|
||||||
"MM": "Myanmar",
|
"MM": "Myanmar",
|
||||||
"ML": "Mali",
|
"ML": "Mali",
|
||||||
"MO": "Macao",
|
"MO": "Macao",
|
||||||
"MN": "Mongolia",
|
"MN": "Mongolia",
|
||||||
"MH": "Marshall Islands",
|
"MH": "Marshall Islands",
|
||||||
"MK": "Macedonia",
|
"MK": "Macedonia",
|
||||||
"MU": "Mauritius",
|
"MU": "Mauritius",
|
||||||
"MT": "Malta",
|
"MT": "Malta",
|
||||||
"MW": "Malawi",
|
"MW": "Malawi",
|
||||||
"MV": "Maldives",
|
"MV": "Maldives",
|
||||||
"MQ": "Martinique",
|
"MQ": "Martinique",
|
||||||
"MP": "Northern Mariana Islands",
|
"MP": "Northern Mariana Islands",
|
||||||
"MS": "Montserrat",
|
"MS": "Montserrat",
|
||||||
"MR": "Mauritania",
|
"MR": "Mauritania",
|
||||||
"IM": "Isle of Man",
|
"IM": "Isle of Man",
|
||||||
"UG": "Uganda",
|
"UG": "Uganda",
|
||||||
"TZ": "Tanzania",
|
"TZ": "Tanzania",
|
||||||
"MY": "Malaysia",
|
"MY": "Malaysia",
|
||||||
"MX": "Mexico",
|
"MX": "Mexico",
|
||||||
"IL": "Israel",
|
"IL": "Israel",
|
||||||
"FR": "France",
|
"FR": "France",
|
||||||
"IO": "British Indian Ocean Territory",
|
"IO": "British Indian Ocean Territory",
|
||||||
"SH": "Saint Helena",
|
"SH": "Saint Helena",
|
||||||
"FI": "Finland",
|
"FI": "Finland",
|
||||||
"FJ": "Fiji",
|
"FJ": "Fiji",
|
||||||
"FK": "Falkland Islands",
|
"FK": "Falkland Islands",
|
||||||
"FM": "Micronesia",
|
"FM": "Micronesia",
|
||||||
"FO": "Faroe Islands",
|
"FO": "Faroe Islands",
|
||||||
"NI": "Nicaragua",
|
"NI": "Nicaragua",
|
||||||
"NL": "Netherlands",
|
"NL": "Netherlands",
|
||||||
"NO": "Norway",
|
"NO": "Norway",
|
||||||
"NA": "Namibia",
|
"NA": "Namibia",
|
||||||
"VU": "Vanuatu",
|
"VU": "Vanuatu",
|
||||||
"NC": "New Caledonia",
|
"NC": "New Caledonia",
|
||||||
"NE": "Niger",
|
"NE": "Niger",
|
||||||
"NF": "Norfolk Island",
|
"NF": "Norfolk Island",
|
||||||
"NG": "Nigeria",
|
"NG": "Nigeria",
|
||||||
"NZ": "New Zealand",
|
"NZ": "New Zealand",
|
||||||
"NP": "Nepal",
|
"NP": "Nepal",
|
||||||
"NR": "Nauru",
|
"NR": "Nauru",
|
||||||
"NU": "Niue",
|
"NU": "Niue",
|
||||||
"CK": "Cook Islands",
|
"CK": "Cook Islands",
|
||||||
"XK": "Kosovo",
|
"XK": "Kosovo",
|
||||||
"CI": "Ivory Coast",
|
"CI": "Ivory Coast",
|
||||||
"CH": "Switzerland",
|
"CH": "Switzerland",
|
||||||
"CO": "Colombia",
|
"CO": "Colombia",
|
||||||
"CN": "China",
|
"CN": "China",
|
||||||
"CM": "Cameroon",
|
"CM": "Cameroon",
|
||||||
"CL": "Chile",
|
"CL": "Chile",
|
||||||
"CC": "Cocos Islands",
|
"CC": "Cocos Islands",
|
||||||
"CA": "Canada",
|
"CA": "Canada",
|
||||||
"CG": "Republic of the Congo",
|
"CG": "Republic of the Congo",
|
||||||
"CF": "Central African Republic",
|
"CF": "Central African Republic",
|
||||||
"CD": "Democratic Republic of the Congo",
|
"CD": "Democratic Republic of the Congo",
|
||||||
"CZ": "Czech Republic",
|
"CZ": "Czech Republic",
|
||||||
"CY": "Cyprus",
|
"CY": "Cyprus",
|
||||||
"CX": "Christmas Island",
|
"CX": "Christmas Island",
|
||||||
"CR": "Costa Rica",
|
"CR": "Costa Rica",
|
||||||
"CW": "Curacao",
|
"CW": "Curacao",
|
||||||
"CV": "Cape Verde",
|
"CV": "Cape Verde",
|
||||||
"CU": "Cuba",
|
"CU": "Cuba",
|
||||||
"SZ": "Swaziland",
|
"SZ": "Swaziland",
|
||||||
"SY": "Syria",
|
"SY": "Syria",
|
||||||
"SX": "Sint Maarten",
|
"SX": "Sint Maarten",
|
||||||
"KG": "Kyrgyzstan",
|
"KG": "Kyrgyzstan",
|
||||||
"KE": "Kenya",
|
"KE": "Kenya",
|
||||||
"SS": "South Sudan",
|
"SS": "South Sudan",
|
||||||
"SR": "Suriname",
|
"SR": "Suriname",
|
||||||
"KI": "Kiribati",
|
"KI": "Kiribati",
|
||||||
"KH": "Cambodia",
|
"KH": "Cambodia",
|
||||||
"KN": "Saint Kitts and Nevis",
|
"KN": "Saint Kitts and Nevis",
|
||||||
"KM": "Comoros",
|
"KM": "Comoros",
|
||||||
"ST": "Sao Tome and Principe",
|
"ST": "Sao Tome and Principe",
|
||||||
"SK": "Slovakia",
|
"SK": "Slovakia",
|
||||||
"KR": "South Korea",
|
"KR": "South Korea",
|
||||||
"SI": "Slovenia",
|
"SI": "Slovenia",
|
||||||
"KP": "North Korea",
|
"KP": "North Korea",
|
||||||
"KW": "Kuwait",
|
"KW": "Kuwait",
|
||||||
"SN": "Senegal",
|
"SN": "Senegal",
|
||||||
"SM": "San Marino",
|
"SM": "San Marino",
|
||||||
"SL": "Sierra Leone",
|
"SL": "Sierra Leone",
|
||||||
"SC": "Seychelles",
|
"SC": "Seychelles",
|
||||||
"KZ": "Kazakhstan",
|
"KZ": "Kazakhstan",
|
||||||
"KY": "Cayman Islands",
|
"KY": "Cayman Islands",
|
||||||
"SG": "Singapore",
|
"SG": "Singapore",
|
||||||
"SE": "Sweden",
|
"SE": "Sweden",
|
||||||
"SD": "Sudan",
|
"SD": "Sudan",
|
||||||
"DO": "Dominican Republic",
|
"DO": "Dominican Republic",
|
||||||
"DM": "Dominica",
|
"DM": "Dominica",
|
||||||
"DJ": "Djibouti",
|
"DJ": "Djibouti",
|
||||||
"DK": "Denmark",
|
"DK": "Denmark",
|
||||||
"VG": "British Virgin Islands",
|
"VG": "British Virgin Islands",
|
||||||
"DE": "Germany",
|
"DE": "Germany",
|
||||||
"YE": "Yemen",
|
"YE": "Yemen",
|
||||||
"DZ": "Algeria",
|
"DZ": "Algeria",
|
||||||
"US": "United States",
|
"US": "United States",
|
||||||
"UY": "Uruguay",
|
"UY": "Uruguay",
|
||||||
"YT": "Mayotte",
|
"YT": "Mayotte",
|
||||||
"UM": "United States Minor Outlying Islands",
|
"UM": "United States Minor Outlying Islands",
|
||||||
"LB": "Lebanon",
|
"LB": "Lebanon",
|
||||||
"LC": "Saint Lucia",
|
"LC": "Saint Lucia",
|
||||||
"LA": "Laos",
|
"LA": "Laos",
|
||||||
"TV": "Tuvalu",
|
"TV": "Tuvalu",
|
||||||
"TW": "Taiwan",
|
"TW": "Taiwan",
|
||||||
"TT": "Trinidad and Tobago",
|
"TT": "Trinidad and Tobago",
|
||||||
"TR": "Turkey",
|
"TR": "Turkey",
|
||||||
"LK": "Sri Lanka",
|
"LK": "Sri Lanka",
|
||||||
"LI": "Liechtenstein",
|
"LI": "Liechtenstein",
|
||||||
"LV": "Latvia",
|
"LV": "Latvia",
|
||||||
"TO": "Tonga",
|
"TO": "Tonga",
|
||||||
"LT": "Lithuania",
|
"LT": "Lithuania",
|
||||||
"LU": "Luxembourg",
|
"LU": "Luxembourg",
|
||||||
"LR": "Liberia",
|
"LR": "Liberia",
|
||||||
"LS": "Lesotho",
|
"LS": "Lesotho",
|
||||||
"TH": "Thailand",
|
"TH": "Thailand",
|
||||||
"TF": "French Southern Territories",
|
"TF": "French Southern Territories",
|
||||||
"TG": "Togo",
|
"TG": "Togo",
|
||||||
"TD": "Chad",
|
"TD": "Chad",
|
||||||
"TC": "Turks and Caicos Islands",
|
"TC": "Turks and Caicos Islands",
|
||||||
"LY": "Libya",
|
"LY": "Libya",
|
||||||
"VA": "Vatican",
|
"VA": "Vatican",
|
||||||
"VC": "Saint Vincent and the Grenadines",
|
"VC": "Saint Vincent and the Grenadines",
|
||||||
"AE": "United Arab Emirates",
|
"AE": "United Arab Emirates",
|
||||||
"AD": "Andorra",
|
"AD": "Andorra",
|
||||||
"AG": "Antigua and Barbuda",
|
"AG": "Antigua and Barbuda",
|
||||||
"AF": "Afghanistan",
|
"AF": "Afghanistan",
|
||||||
"AI": "Anguilla",
|
"AI": "Anguilla",
|
||||||
"VI": "U.S. Virgin Islands",
|
"VI": "U.S. Virgin Islands",
|
||||||
"IS": "Iceland",
|
"IS": "Iceland",
|
||||||
"IR": "Iran",
|
"IR": "Iran",
|
||||||
"AM": "Armenia",
|
"AM": "Armenia",
|
||||||
"AL": "Albania",
|
"AL": "Albania",
|
||||||
"AO": "Angola",
|
"AO": "Angola",
|
||||||
"AQ": "Antarctica",
|
"AQ": "Antarctica",
|
||||||
"AS": "American Samoa",
|
"AS": "American Samoa",
|
||||||
"AR": "Argentina",
|
"AR": "Argentina",
|
||||||
"AU": "Australia",
|
"AU": "Australia",
|
||||||
"AT": "Austria",
|
"AT": "Austria",
|
||||||
"AW": "Aruba",
|
"AW": "Aruba",
|
||||||
"IN": "India",
|
"IN": "India",
|
||||||
"AX": "Aland Islands",
|
"AX": "Aland Islands",
|
||||||
"AZ": "Azerbaijan",
|
"AZ": "Azerbaijan",
|
||||||
"IE": "Ireland",
|
"IE": "Ireland",
|
||||||
"ID": "Indonesia",
|
"ID": "Indonesia",
|
||||||
"UA": "Ukraine",
|
"UA": "Ukraine",
|
||||||
"QA": "Qatar",
|
"QA": "Qatar",
|
||||||
"MZ": "Mozambique"
|
"MZ": "Mozambique"
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,11 @@ package flags
|
|||||||
// WIP
|
// WIP
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"embed"
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strconv"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"git.tijl.dev/tijl/tijl.dev-core/internal/queries"
|
|
||||||
"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/i18n"
|
||||||
log "git.tijl.dev/tijl/tijl.dev-core/modules/logger"
|
log "git.tijl.dev/tijl/tijl.dev-core/modules/logger"
|
||||||
"git.tijl.dev/tijl/tijl.dev-core/modules/web"
|
"git.tijl.dev/tijl/tijl.dev-core/modules/web"
|
||||||
@ -25,6 +22,34 @@ 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
|
||||||
|
|
||||||
@ -38,47 +63,26 @@ func Setup() {
|
|||||||
|
|
||||||
web.RegisterAppSetupFunc(func(a *fiber.App) {
|
web.RegisterAppSetupFunc(func(a *fiber.App) {
|
||||||
a.Get("/app/flags", func(c *fiber.Ctx) error {
|
a.Get("/app/flags", func(c *fiber.Ctx) error {
|
||||||
data := *web.Common(c)
|
|
||||||
data["Title"] = "tmp"
|
if c.Cookies("app_flags_game_session") != "" {
|
||||||
data["SupportedTags"] = supportedTags
|
return questionHandler(c, NewGameUUID{})
|
||||||
return c.Render("apps/flags/start", data, "layouts/base")
|
} else {
|
||||||
|
return EntryPageHandler(c)
|
||||||
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
a.Post("/app/flags", func(c *fiber.Ctx) error {
|
a.Post("/app/flags", func(c *fiber.Ctx) error {
|
||||||
|
|
||||||
recType := c.FormValue("type")
|
recType := c.FormValue("type")
|
||||||
|
|
||||||
if recType == "start" {
|
switch recType {
|
||||||
|
case "start":
|
||||||
values := c.Request().PostArgs().PeekMulti("tags")
|
return startNewGameHandler(c)
|
||||||
var selectedTags []string
|
case "answer":
|
||||||
for _, v := range values {
|
return c.SendString("wip")
|
||||||
selectedTags = append(selectedTags, string(v))
|
case "cancel":
|
||||||
}
|
return stopGameHandler(c)
|
||||||
|
|
||||||
maxQuestions, err := strconv.Atoi(c.FormValue("max_questions"))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = db.Queries.CreateFlagsGame(context.TODO(), queries.CreateFlagsGameParams{
|
|
||||||
Uid: sql.NullString{},
|
|
||||||
Tags: selectedTags,
|
|
||||||
QuestionAmount: int32(maxQuestions),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Cookie(&fiber.Cookie{
|
|
||||||
Name: "app_flags_game_session",
|
|
||||||
Value: "",
|
|
||||||
})
|
|
||||||
|
|
||||||
data := *web.Common(c)
|
|
||||||
data["Title"] = "tmp"
|
|
||||||
return c.Render("apps/flags/question", data, "layouts/base")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -111,3 +115,10 @@ 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")
|
||||||
|
}
|
||||||
|
38
internal/apps/flags/util.go
Normal file
38
internal/apps/flags/util.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package flags
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/binary"
|
||||||
|
"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
func shuffleSliceRandom(slice []string) []string {
|
||||||
|
shuffled := make([]string, len(slice))
|
||||||
|
copy(shuffled, slice)
|
||||||
|
|
||||||
|
for i := len(shuffled) - 1; i > 0; i-- {
|
||||||
|
j := rand.Intn(i + 1)
|
||||||
|
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
return shuffled
|
||||||
|
}
|
||||||
|
|
||||||
|
func shuffleSlice(slice []string, seed string) []string {
|
||||||
|
hasher := sha256.New()
|
||||||
|
hasher.Write([]byte(seed))
|
||||||
|
hash := hasher.Sum(nil)
|
||||||
|
|
||||||
|
seedInt := int64(binary.LittleEndian.Uint64(hash[:8]))
|
||||||
|
|
||||||
|
r := rand.New(rand.NewSource(seedInt))
|
||||||
|
|
||||||
|
shuffled := make([]string, len(slice))
|
||||||
|
copy(shuffled, slice)
|
||||||
|
|
||||||
|
r.Shuffle(len(shuffled), func(i, j int) {
|
||||||
|
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
|
||||||
|
})
|
||||||
|
|
||||||
|
return shuffled
|
||||||
|
}
|
@ -85,7 +85,8 @@ func HandleCallback(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
redirect := c.Cookies("internal_redirect")
|
redirect := c.Cookies("internal_redirect")
|
||||||
c.ClearCookie("internal_redirect", "state")
|
utils.ClearCookie(c, "internal_redirect")
|
||||||
|
utils.ClearCookie(c, "state")
|
||||||
if redirect != "" {
|
if redirect != "" {
|
||||||
return c.Redirect(redirect, http.StatusFound)
|
return c.Redirect(redirect, http.StatusFound)
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ type AppFlagsGame struct {
|
|||||||
type AppFlagsGamesAnswer struct {
|
type AppFlagsGamesAnswer struct {
|
||||||
GameID uuid.UUID
|
GameID uuid.UUID
|
||||||
Question int32
|
Question int32
|
||||||
Correct bool
|
Errors int32
|
||||||
}
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
|
15
internal/utils/cookie.go
Normal file
15
internal/utils/cookie.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ClearCookie(c *fiber.Ctx, key string) {
|
||||||
|
c.Cookie(&fiber.Cookie{
|
||||||
|
Name: key,
|
||||||
|
Value: "",
|
||||||
|
Expires: time.Now().Add(-1 * time.Hour),
|
||||||
|
})
|
||||||
|
}
|
@ -4,7 +4,7 @@ CREATE TABLE app_flags_games (
|
|||||||
uid VARCHAR DEFAULT NULL,
|
uid VARCHAR DEFAULT NULL,
|
||||||
tags VARCHAR[] NOT NULL,
|
tags VARCHAR[] NOT NULL,
|
||||||
question_amount INT NOT NULL,
|
question_amount INT NOT NULL,
|
||||||
question_current INT DEFAULT 0 NOT NULL,
|
question_current INT DEFAULT 1 NOT NULL,
|
||||||
question_correct INT DEFAULT 0 NOT NULL,
|
question_correct INT DEFAULT 0 NOT NULL,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||||
last_activity TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
last_activity TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||||
@ -14,7 +14,7 @@ CREATE TABLE app_flags_games (
|
|||||||
CREATE TABLE app_flags_games_answers (
|
CREATE TABLE app_flags_games_answers (
|
||||||
game_id UUID NOT NULL,
|
game_id UUID NOT NULL,
|
||||||
question INT NOT NULL,
|
question INT NOT NULL,
|
||||||
correct BOOLEAN NOT NULL,
|
errors INT NOT NULL,
|
||||||
FOREIGN KEY (game_id) REFERENCES app_flags_games (game_id),
|
FOREIGN KEY (game_id) REFERENCES app_flags_games (game_id),
|
||||||
CONSTRAINT app_flags_games_answers_unique UNIQUE (game_id, question)
|
CONSTRAINT app_flags_games_answers_unique UNIQUE (game_id, question)
|
||||||
);
|
);
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
<div class="container">
|
||||||
|
|
||||||
|
<div>
|
||||||
|
|
||||||
|
<div class="flex justify-center p-4 mt-4">
|
||||||
|
<span style="font-size: 105px;">{{.Flag}}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div hx-boost="true" class="flex justify-center flex-wrap">
|
||||||
|
{{range .Answers}}
|
||||||
|
<form method="post" class="m-1">
|
||||||
|
<input class="hidden" name="type" value="answer" />
|
||||||
|
<input class="hidden" name="answer" value="{{.}}" />
|
||||||
|
<button type="submit" class="btn btn-lg rounded-2xl">{{index $.T .}}</button>
|
||||||
|
</form>
|
||||||
|
{{end}}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form hx-boost="true" method="post">
|
||||||
|
<input class="hidden" name="type" value="cancel" />
|
||||||
|
<button type="submit" class="btn hover:btn-error">Stop</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
@ -11,10 +11,10 @@
|
|||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<label class="cursor-pointer label">
|
<!--<label class="cursor-pointer label">
|
||||||
<span class="label-text">Aantal vragen</span>
|
<span class="label-text">Aantal vragen</span>
|
||||||
<input name="tags" value="{{.}}" type="checkbox" class="checkbox checkbox-primary" />
|
<input name="tags" value="{{.}}" type="checkbox" class="checkbox checkbox-primary" />
|
||||||
</label>
|
</label>-->
|
||||||
|
|
||||||
<input value="0" name="max_questions" type="number" class="input input-bordered" />
|
<input value="0" name="max_questions" type="number" class="input input-bordered" />
|
||||||
<button type="submit" class="btn btn-primary">Submit</button>
|
<button type="submit" class="btn btn-primary">Submit</button>
|
||||||
|
Loading…
Reference in New Issue
Block a user