diff --git a/go.mod b/go.mod index 285c21f..bc4c47e 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 599d273..0887ef2 100644 --- a/go.sum +++ b/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= diff --git a/internal/apps/flags/game.go b/internal/apps/flags/game.go new file mode 100644 index 0000000..405f956 --- /dev/null +++ b/internal/apps/flags/game.go @@ -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) +} diff --git a/internal/apps/flags/handlers.go b/internal/apps/flags/handlers.go deleted file mode 100644 index 0cfcbd5..0000000 --- a/internal/apps/flags/handlers.go +++ /dev/null @@ -1 +0,0 @@ -package flags diff --git a/internal/apps/flags/locales/en.json b/internal/apps/flags/locales/en.json index 1daeaa4..3c0ce1a 100644 --- a/internal/apps/flags/locales/en.json +++ b/internal/apps/flags/locales/en.json @@ -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" } diff --git a/internal/apps/flags/main.go b/internal/apps/flags/main.go index f750b61..48a90fc 100644 --- a/internal/apps/flags/main.go +++ b/internal/apps/flags/main.go @@ -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") +} diff --git a/internal/apps/flags/util.go b/internal/apps/flags/util.go new file mode 100644 index 0000000..0272234 --- /dev/null +++ b/internal/apps/flags/util.go @@ -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 +} diff --git a/internal/oidc/handler.go b/internal/oidc/handler.go index 8042c10..b4c4f58 100644 --- a/internal/oidc/handler.go +++ b/internal/oidc/handler.go @@ -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) } diff --git a/internal/queries/models.go b/internal/queries/models.go index 29410a8..22ded5e 100644 --- a/internal/queries/models.go +++ b/internal/queries/models.go @@ -26,7 +26,7 @@ type AppFlagsGame struct { type AppFlagsGamesAnswer struct { GameID uuid.UUID Question int32 - Correct bool + Errors int32 } type Session struct { diff --git a/internal/utils/cookie.go b/internal/utils/cookie.go new file mode 100644 index 0000000..8891611 --- /dev/null +++ b/internal/utils/cookie.go @@ -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), + }) +} diff --git a/migrations/00000002_flags.up.sql b/migrations/00000002_flags.up.sql index 3778bae..e57107e 100644 --- a/migrations/00000002_flags.up.sql +++ b/migrations/00000002_flags.up.sql @@ -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) ); diff --git a/web/views/apps/flags/question.html b/web/views/apps/flags/question.html index e69de29..7b34081 100644 --- a/web/views/apps/flags/question.html +++ b/web/views/apps/flags/question.html @@ -0,0 +1,26 @@ +
+ +
+ +
+ {{.Flag}} +
+ +
+ {{range .Answers}} +
+ + + +
+ {{end}} +
+ +
+ + +
+ +
+ +
diff --git a/web/views/apps/flags/start.html b/web/views/apps/flags/start.html index 67657bd..38587c6 100644 --- a/web/views/apps/flags/start.html +++ b/web/views/apps/flags/start.html @@ -11,10 +11,10 @@ {{end}} -