changes + add uploader
This commit is contained in:
parent
06c6057c7d
commit
b8d23ee68f
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user