fix(gacha): prevent infinite loop in getRandomEntries

Add guards for edge cases:
- Empty entries with rolls > 0
- Zero or negative rolls
- Zero total weight in non-box mode
- Box mode with more rolls than available entries

Previously these cases caused infinite loops or panics.
This commit is contained in:
Houmgaor
2026-02-05 08:51:09 +01:00
parent a11ee6d9eb
commit 6ccbc24a4a
2 changed files with 62 additions and 4 deletions

View File

@@ -353,10 +353,16 @@ func addGachaItem(s *Session, items []GachaItem) {
func getRandomEntries(entries []GachaEntry, rolls int, isBox bool) ([]GachaEntry, error) {
var chosen []GachaEntry
if len(entries) == 0 || rolls <= 0 {
return chosen, nil
}
var totalWeight float64
for i := range entries {
totalWeight += entries[i].Weight
}
if !isBox && totalWeight <= 0 {
return chosen, nil
}
for {
if rolls == len(chosen) {
break
@@ -371,6 +377,9 @@ func getRandomEntries(entries []GachaEntry, rolls int, isBox bool) ([]GachaEntry
}
}
} else {
if len(entries) == 0 {
break
}
result := rand.Intn(len(entries))
chosen = append(chosen, entries[result])
entries[result] = entries[len(entries)-1]