diff --git a/src/device/input/parse/v01/server.go b/src/device/input/parse/v01/server.go index a861c5c..30b0312 100644 --- a/src/device/input/parse/v01/server.go +++ b/src/device/input/parse/v01/server.go @@ -205,13 +205,15 @@ func (v01 *V01) serveGMShuffle(w http.ResponseWriter, r *http.Request) { poolSize = altSize } pool := make([]int, poolSize) - for i := range v01.cfg.Players { - pool[i] = i + 1 - } - for i := 0; i < 30; i++ { - l := rand.Int() % poolSize - r := rand.Int() % poolSize - pool[l], pool[r] = pool[r], pool[l] + if poolSize > 0 { + for i := range v01.cfg.Players { + pool[i] = i + 1 + } + for i := 0; i < 30; i++ { + l := rand.Int() % poolSize + r := rand.Int() % poolSize + pool[l], pool[r] = pool[r], pool[l] + } } i := 0 msg := []string{} diff --git a/src/device/input/parse/v01/server_test.go b/src/device/input/parse/v01/server_test.go index 3561bd6..35d4ddb 100644 --- a/src/device/input/parse/v01/server_test.go +++ b/src/device/input/parse/v01/server_test.go @@ -7,6 +7,7 @@ import ( "net/http/httptest" "os" "path" + "strconv" "strings" "sync" "testing" @@ -205,7 +206,7 @@ func TestServeGM(t *testing.T) { }) t.Run("shuffle", func(t *testing.T) { - t.Run("2u 2p", func(t *testing.T) { + t.Run("many 2u 2p", func(t *testing.T) { v01 := NewV01(ctx, nil) for i := 0; i < 100; i++ { v01.cfg.Quiet = true @@ -231,6 +232,55 @@ func TestServeGM(t *testing.T) { } } }) + + cases := map[string]struct { + users int + usersAssigned int + players int + }{ + "empty": {}, + } + + for name, d := range cases { + c := d + t.Run(name, func(t *testing.T) { + v01 := NewV01(ctx, nil) + v01.cfg.Quiet = true + v01.cfg.Users = map[string]configUser{} + for i := 0; i < c.users; i++ { + v01.cfg.Users[strconv.Itoa(i)] = configUser{} + if i < c.usersAssigned { + v01.cfg.Users[strconv.Itoa(i)] = configUser{Player: i} + } + } + v01.cfg.Players = make([]configPlayer, c.players) + + do(v01, "/gm/rpc/shuffle", "") + if v01.cfg.Quiet { + t.Error(v01.cfg.Quiet) + } + + if len(v01.cfg.Users) != c.users+1 { + t.Error(v01.cfg.Users) + } else if len(v01.cfg.Players) != c.players { + t.Error(v01.cfg.Users) + } + for i := 0; i < c.users; i++ { + if _, ok := v01.cfg.Users[strconv.Itoa(i)]; !ok { + t.Error(i) + } + } + assignments := map[int]struct{}{} + for _, v := range v01.cfg.Users { + if v.Player > 0 { + assignments[v.Player] = struct{}{} + } + } + if len(assignments) != c.usersAssigned { + t.Error(assignments) + } + }) + } }) t.Run("swap", func(t *testing.T) {