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]

View File

@@ -231,11 +231,9 @@ func TestGachaItemStruct(t *testing.T) {
}
}
func TestGetRandomEntries_EmptyEntriesZeroRolls(t *testing.T) {
// Note: getRandomEntries with empty entries and rolls > 0 causes infinite loop.
// Only test the valid case of 0 rolls with empty entries.
func TestGetRandomEntries_EmptyEntries(t *testing.T) {
entries := []GachaEntry{}
result, err := getRandomEntries(entries, 0, false)
result, err := getRandomEntries(entries, 5, false)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
@@ -244,6 +242,57 @@ func TestGetRandomEntries_EmptyEntriesZeroRolls(t *testing.T) {
}
}
func TestGetRandomEntries_EmptyEntriesBoxMode(t *testing.T) {
entries := []GachaEntry{}
result, err := getRandomEntries(entries, 5, true)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(result) != 0 {
t.Errorf("expected empty result, got %d entries", len(result))
}
}
func TestGetRandomEntries_NegativeRolls(t *testing.T) {
entries := []GachaEntry{{ID: 1, Weight: 1.0}}
result, err := getRandomEntries(entries, -5, false)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(result) != 0 {
t.Errorf("expected empty result, got %d entries", len(result))
}
}
func TestGetRandomEntries_ZeroTotalWeight(t *testing.T) {
entries := []GachaEntry{
{ID: 1, Weight: 0},
{ID: 2, Weight: 0},
}
result, err := getRandomEntries(entries, 5, false)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(result) != 0 {
t.Errorf("expected empty result with zero weight, got %d entries", len(result))
}
}
func TestGetRandomEntries_BoxModeMoreRollsThanEntries(t *testing.T) {
entries := []GachaEntry{
{ID: 1, Weight: 1.0},
{ID: 2, Weight: 1.0},
}
// Request 5 rolls but only 2 entries - should return only 2
result, err := getRandomEntries(entries, 5, true)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(result) != 2 {
t.Errorf("expected 2 results (all available), got %d", len(result))
}
}
func TestGetRandomEntries_ZeroRolls(t *testing.T) {
entries := []GachaEntry{
{ID: 1, Weight: 1.0},