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