changes + add uploader
All checks were successful
build / build (push) Successful in 26s
release-tag / release-image (push) Successful in 16m57s

This commit is contained in:
Tijl 2024-08-29 12:54:38 +02:00
parent 06c6057c7d
commit b8d23ee68f
Signed by: tijl
GPG Key ID: DAE24BFCD722F053
2 changed files with 31 additions and 10 deletions

View File

@ -122,20 +122,28 @@ func questionHandler(c *fiber.Ctx, newGame NullableUUID, prevError NullableStrin
shuffledCountries := shuffleSlice(countries, gameSession.GameSeed.String()) shuffledCountries := shuffleSlice(countries, gameSession.GameSeed.String())
correctAnswer := shuffledCountries[gameSession.QuestionCurrent-1] correctAnswer := shuffledCountries[gameSession.QuestionCurrent-1]
// Filter out the correct answer from the countries list
filteredCountries := filterSlice(countries, correctAnswer) filteredCountries := filterSlice(countries, correctAnswer)
// Shuffle the filtered countries to get random answers
shuffledAnswers := shuffleSlice(filteredCountries, gameSession.GameSeed.String()+string(gameSession.QuestionCurrent)) shuffledAnswers := shuffleSlice(filteredCountries, gameSession.GameSeed.String()+string(gameSession.QuestionCurrent))
var correctAnswerData CountryCode
// Select 4 random answers from the shuffled list 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] shuffledAnswers = shuffledAnswers[0:4]
// Add the correct answer to the list
shuffledAnswers = append(shuffledAnswers, correctAnswer) 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)) shuffledAnswers = shuffleSlice(shuffledAnswers, gameSession.GameSeed.String()+string(gameSession.QuestionCurrent))
if gameSession.QuestionAmount != 0 && int(gameSession.QuestionAmount) < len(countries) { if gameSession.QuestionAmount != 0 && int(gameSession.QuestionAmount) < len(countries) {

View File

@ -76,3 +76,16 @@ func filterSlice(slice []string, value string) []string {
} }
return result 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
}