mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-28 18:42:39 +01:00
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:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user