From b8d23ee68fcbbbdf6727ca78423d5ed0e6c33676 Mon Sep 17 00:00:00 2001 From: tijl Date: Thu, 29 Aug 2024 12:54:38 +0200 Subject: [PATCH] changes + add uploader --- internal/apps/flags/handlers.go | 28 ++++++++++++++++++---------- internal/apps/flags/util.go | 13 +++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/internal/apps/flags/handlers.go b/internal/apps/flags/handlers.go index 8d2bc26..4ce4631 100644 --- a/internal/apps/flags/handlers.go +++ b/internal/apps/flags/handlers.go @@ -122,20 +122,28 @@ func questionHandler(c *fiber.Ctx, newGame NullableUUID, prevError NullableStrin shuffledCountries := shuffleSlice(countries, gameSession.GameSeed.String()) correctAnswer := shuffledCountries[gameSession.QuestionCurrent-1] - - // Filter out the correct answer from the countries list filteredCountries := filterSlice(countries, correctAnswer) - - // Shuffle the filtered countries to get random answers shuffledAnswers := shuffleSlice(filteredCountries, gameSession.GameSeed.String()+string(gameSession.QuestionCurrent)) - - // Select 4 random answers from the shuffled list + var correctAnswerData CountryCode + for _, country := range countryCodes { + if country.Code == correctAnswer { + correctAnswerData = country + break + } + } + var filteredAnswers []string + for _, shuffledAnswer := range shuffledAnswers { + for _, country := range countryCodes { + if shuffledAnswer == country.Code { + if hasCommonTag(correctAnswerData.Tags, country.Tags) { + filteredAnswers = append(filteredAnswers, country.Code) + } + } + } + } + shuffledAnswers = filteredAnswers shuffledAnswers = shuffledAnswers[0:4] - - // Add the correct answer to the list shuffledAnswers = append(shuffledAnswers, correctAnswer) - - // Shuffle the answers again to randomize the position of the correct answer shuffledAnswers = shuffleSlice(shuffledAnswers, gameSession.GameSeed.String()+string(gameSession.QuestionCurrent)) if gameSession.QuestionAmount != 0 && int(gameSession.QuestionAmount) < len(countries) { diff --git a/internal/apps/flags/util.go b/internal/apps/flags/util.go index 573b4aa..04be727 100644 --- a/internal/apps/flags/util.go +++ b/internal/apps/flags/util.go @@ -76,3 +76,16 @@ func filterSlice(slice []string, value string) []string { } return result } + +func hasCommonTag(tags1, tags2 []string) bool { + tagSet := make(map[string]struct{}) + for _, tag := range tags1 { + tagSet[tag] = struct{}{} + } + for _, tag := range tags2 { + if _, exists := tagSet[tag]; exists { + return true + } + } + return false +}