fix possible infinite loop in gacha rolls

This commit is contained in:
wish
2023-03-30 23:32:10 +11:00
parent 84c3944e19
commit a042cef5b1

View File

@@ -358,6 +358,9 @@ func getRandomEntries(entries []GachaEntry, rolls int, isBox bool) ([]GachaEntry
totalWeight += entries[i].Weight totalWeight += entries[i].Weight
} }
for { for {
if rolls == len(chosen) {
break
}
if !isBox { if !isBox {
result := rand.Float64() * totalWeight result := rand.Float64() * totalWeight
for _, entry := range entries { for _, entry := range entries {
@@ -373,9 +376,6 @@ func getRandomEntries(entries []GachaEntry, rolls int, isBox bool) ([]GachaEntry
entries[result] = entries[len(entries)-1] entries[result] = entries[len(entries)-1]
entries = entries[:len(entries)-1] entries = entries[:len(entries)-1]
} }
if rolls == len(chosen) {
break
}
} }
return chosen, nil return chosen, nil
} }