shuffle v01.config to split out state and meta

This commit is contained in:
Bel LaPointe
2023-03-27 10:54:02 -06:00
parent ecb719a97a
commit f647a03467
6 changed files with 130 additions and 116 deletions

View File

@@ -27,19 +27,19 @@ func TestPatchConfig(t *testing.T) {
"replace entire doc": {
was: config{
Feedback: configFeedback{Addr: "a", TTSURL: "a"},
Users: map[string]configUser{"a": configUser{Player: 1, Message: "a"}},
Users: map[string]configUser{"a": configUser{State: configUserState{Player: 1, Message: "a"}}},
Players: []configPlayer{configPlayer{Transformation: transformation{"a": "a"}}},
Quiet: true,
},
patch: `[{"op": "replace", "path": "", "value": {
"Feedback": {"Addr": "b", "TTSURL": "b"},
"Users": {"b": {"Player": 2, "Message": "b"}},
"Users": {"b": {"State":{"Player": 2, "Message": "b"}}},
"Players": [{"Transformation": {"b": "b"}}],
"Quiet": false
}}]`,
want: config{
Feedback: configFeedback{Addr: "b", TTSURL: "b"},
Users: map[string]configUser{"b": configUser{Player: 2, Message: "b"}},
Users: map[string]configUser{"b": configUser{State: configUserState{Player: 2, Message: "b"}}},
Players: []configPlayer{configPlayer{Transformation: transformation{"b": "b"}}},
Quiet: false,
},
@@ -130,9 +130,11 @@ func TestServeGM(t *testing.T) {
}
v01.cfg.Users = map[string]configUser{
"bel": configUser{
Player: 3,
LastTSMS: time.Now().Add(-1*time.Minute).UnixNano() / int64(time.Millisecond),
LastLag: int64(time.Second / time.Millisecond),
State: configUserState{Player: 3},
Meta: configUserMeta{
LastTSMS: time.Now().Add(-1*time.Minute).UnixNano() / int64(time.Millisecond),
LastLag: int64(time.Second / time.Millisecond),
},
},
"zach": configUser{},
"chase": configUser{},
@@ -175,24 +177,24 @@ func TestServeGM(t *testing.T) {
v01 := NewV01(ctx, nil)
v01.cfg.Quiet = false
v01.cfg.Users = map[string]configUser{
"bel": configUser{
"bel": configUser{State: configUserState{
Alias: "driver",
Message: "if someone else says 'driver', then you get to play",
},
"broadcast": configUser{
}},
"broadcast": configUser{State: configUserState{
Message: ":)",
},
}},
}
do(v01, "/gm/rpc/broadcastSomeoneSaidAlias", "")
if !v01.cfg.Quiet {
t.Error(v01.cfg.Quiet)
}
if v := v01.cfg.Users["bel"]; v.Alias != "" {
t.Error(v.Alias)
} else if v.Message != "driver" {
t.Error(v.Message)
if v := v01.cfg.Users["bel"]; v.State.Alias != "" {
t.Error(v.State.Alias)
} else if v.State.Message != "driver" {
t.Error(v.State.Message)
}
if bc := v01.cfg.Users["broadcast"]; bc.Message == ":)" {
if bc := v01.cfg.Users["broadcast"]; bc.State.Message == ":)" {
t.Error(bc)
}
})
@@ -210,7 +212,7 @@ func TestServeGM(t *testing.T) {
t.Run("not enough", func(t *testing.T) {
v01 := NewV01(ctx, nil)
v01.cfg.Users = map[string]configUser{
"zach": configUser{Player: 0},
"zach": configUser{State: configUserState{Player: 0}},
}
resp := do(v01, "/gm/rpc/fillNonPlayerAliases", "[]")
if resp.Code != http.StatusBadRequest {
@@ -221,19 +223,19 @@ func TestServeGM(t *testing.T) {
t.Run("happy", func(t *testing.T) {
v01 := NewV01(ctx, nil)
v01.cfg.Users = map[string]configUser{
"bel": configUser{Player: 1},
"zach": configUser{Player: 0},
"bel": configUser{State: configUserState{Player: 1}},
"zach": configUser{State: configUserState{Player: 0}},
}
do(v01, "/gm/rpc/fillNonPlayerAliases", "[qt]")
if v := v01.cfg.Users["bel"]; v.Alias != "" {
t.Error(v.Alias)
} else if v.Player != 1 {
t.Error(v.Player)
if v := v01.cfg.Users["bel"]; v.State.Alias != "" {
t.Error(v.State.Alias)
} else if v.State.Player != 1 {
t.Error(v.State.Player)
}
if v := v01.cfg.Users["zach"]; v.Alias != "qt" {
t.Error(v.Alias)
} else if v.Player != 0 {
t.Error(v.Player)
if v := v01.cfg.Users["zach"]; v.State.Alias != "qt" {
t.Error(v.State.Alias)
} else if v.State.Player != 0 {
t.Error(v.State.Player)
}
})
})
@@ -248,8 +250,8 @@ func TestServeGM(t *testing.T) {
if resp.Code != http.StatusBadRequest {
t.Error(resp)
}
if v01.cfg.Users["bel"].Message != "" {
t.Error(v01.cfg.Users["bel"].Message)
if v01.cfg.Users["bel"].State.Message != "" {
t.Error(v01.cfg.Users["bel"].State.Message)
}
})
@@ -257,8 +259,8 @@ func TestServeGM(t *testing.T) {
v01 := NewV01(ctx, nil)
v01.cfg.Users = map[string]configUser{"bel": {}, "zach": {}}
do(v01, "/gm/rpc/vote?user=bel&payload=zach", "")
if v01.cfg.Users["bel"].Message != "//zach" {
t.Error(v01.cfg.Users["bel"].Message)
if v01.cfg.Users["bel"].State.Vote != "zach" {
t.Error(v01.cfg.Users["bel"].State.Vote)
}
})
@@ -281,7 +283,7 @@ func TestServeGM(t *testing.T) {
t.Run("get mid vote", func(t *testing.T) {
v01 := NewV01(ctx, nil)
v01.cfg.Users = map[string]configUser{"bel": {Message: "driver//zach"}}
v01.cfg.Users = map[string]configUser{"bel": {State: configUserState{Vote: "zach", Message: "driver"}}}
resp := do(v01, "/gm/rpc/vote", "", "GET")
var result result
if err := yaml.Unmarshal(resp.Body.Bytes(), &result); err != nil {
@@ -317,14 +319,14 @@ func TestServeGM(t *testing.T) {
t.Run("happy", func(t *testing.T) {
v01 := NewV01(ctx, nil)
v01.cfg.Users = map[string]configUser{
"bel": configUser{Message: "driver//zach", Player: 1},
"zach": configUser{Message: "pizza//bel"},
"bill": configUser{Message: "//bel", Player: 2},
"bel": configUser{State: configUserState{Vote: "zach", Message: "driver", Player: 1}},
"zach": configUser{State: configUserState{Vote: "bel", Message: "pizza"}},
"bill": configUser{State: configUserState{Vote: "bel", Message: "", Player: 2}},
}
resp := do(v01, "/gm/rpc/elect?alias=pizza", "")
var result result
if err := yaml.Unmarshal(resp.Body.Bytes(), &result); err != nil {
t.Error(err)
t.Errorf("%s => %v", resp.Body.Bytes(), err)
}
if len(result) != 2 {
t.Error(result)
@@ -333,13 +335,13 @@ func TestServeGM(t *testing.T) {
} else if result["zach"] != 1 {
t.Error(result)
}
if v01.cfg.Users["bel"].Player != 0 {
t.Error(v01.cfg.Users["bel"].Player)
} else if v01.cfg.Users["zach"].Player != 1 {
t.Error(v01.cfg.Users["zach"].Player)
if v01.cfg.Users["bel"].State.Player != 0 {
t.Error(v01.cfg.Users["bel"].State.Player)
} else if v01.cfg.Users["zach"].State.Player != 1 {
t.Error(v01.cfg.Users["zach"].State.Player)
}
if v01.cfg.Users["broadcast"].Message != `bel is now player 0 and zach is now player 1` {
t.Error(v01.cfg.Users["broadcast"].Message)
if v01.cfg.Users["broadcast"].State.Message != `bel is now player 0 and zach is now player 1` {
t.Error(v01.cfg.Users["broadcast"].State.Message)
}
})
@@ -347,9 +349,9 @@ func TestServeGM(t *testing.T) {
v01 := NewV01(ctx, nil)
v01.cfg.Players = []configPlayer{{}}
v01.cfg.Users = map[string]configUser{
"bel": configUser{Message: "driver//zach", Player: 1},
"zach": configUser{Message: "//bel"},
"bill": configUser{Message: "//bel"},
"bel": configUser{State: configUserState{Vote: "zach", Message: "driver", Player: 1}},
"zach": configUser{State: configUserState{Vote: "bel", Message: ""}},
"bill": configUser{State: configUserState{Vote: "bel", Message: ""}},
}
resp := do(v01, "/gm/rpc/elect?alias=driver", "")
var result result
@@ -363,12 +365,12 @@ func TestServeGM(t *testing.T) {
} else if result["zach"] != 1 {
t.Error(result)
}
if !strings.HasSuffix(v01.cfg.Users["broadcast"].Message, `is now player 1`) || strings.Contains(v01.cfg.Users["broadcast"].Message, ",") {
t.Error(v01.cfg.Users["broadcast"].Message)
if !strings.HasSuffix(v01.cfg.Users["broadcast"].State.Message, `is now player 1`) || strings.Contains(v01.cfg.Users["broadcast"].State.Message, ",") {
t.Error(v01.cfg.Users["broadcast"].State.Message)
}
assignments := map[int]int{}
for _, v := range v01.cfg.Users {
assignments[v.Player] = assignments[v.Player] + 1
assignments[v.State.Player] = assignments[v.State.Player] + 1
}
if len(assignments) != 2 {
t.Error(assignments)
@@ -383,8 +385,8 @@ func TestServeGM(t *testing.T) {
v01 := NewV01(ctx, nil)
v01.cfg.Players = []configPlayer{{}}
v01.cfg.Users = map[string]configUser{
"bel": configUser{Message: "driver//zach", Player: 1},
"zach": configUser{Message: "pizza//bel"},
"bel": configUser{State: configUserState{Vote: "zach", Message: "driver", Player: 1}},
"zach": configUser{State: configUserState{Vote: "bel", Message: "pizza"}},
}
resp := do(v01, "/gm/rpc/elect?alias=pizza", "")
var result result
@@ -398,12 +400,12 @@ func TestServeGM(t *testing.T) {
} else if result["zach"] != 1 {
t.Error(result)
}
if !strings.HasSuffix(v01.cfg.Users["broadcast"].Message, `is now player 1`) || strings.Contains(v01.cfg.Users["broadcast"].Message, ",") {
t.Error(v01.cfg.Users["broadcast"].Message)
if !strings.HasSuffix(v01.cfg.Users["broadcast"].State.Message, `is now player 1`) || strings.Contains(v01.cfg.Users["broadcast"].State.Message, ",") {
t.Error(v01.cfg.Users["broadcast"].State.Message)
}
assignments := map[int]int{}
for _, v := range v01.cfg.Users {
assignments[v.Player] = assignments[v.Player] + 1
assignments[v.State.Player] = assignments[v.State.Player] + 1
}
if len(assignments) != 2 {
t.Error(assignments)
@@ -421,8 +423,8 @@ func TestServeGM(t *testing.T) {
for i := 0; i < 100; i++ {
v01.cfg.Quiet = true
v01.cfg.Users = map[string]configUser{
"bel": configUser{Player: 1},
"zach": configUser{Player: 2},
"bel": configUser{State: configUserState{Player: 1}},
"zach": configUser{State: configUserState{Player: 2}},
}
v01.cfg.Players = []configPlayer{{}, {}}
do(v01, "/gm/rpc/shuffle", "")
@@ -433,9 +435,9 @@ func TestServeGM(t *testing.T) {
t.Error(v01.cfg.Users)
} else if len(v01.cfg.Players) != 2 {
t.Error(v01.cfg.Users)
} else if bp := v01.cfg.Users["bel"].Player; bp != 1 && bp != 2 {
} else if bp := v01.cfg.Users["bel"].State.Player; bp != 1 && bp != 2 {
t.Error(bp)
} else if zp := v01.cfg.Users["zach"].Player; zp != 1 && zp != 2 {
} else if zp := v01.cfg.Users["zach"].State.Player; zp != 1 && zp != 2 {
t.Error(zp)
} else if bp == zp {
t.Error(bp, zp)
@@ -468,7 +470,7 @@ func TestServeGM(t *testing.T) {
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.Users[strconv.Itoa(i)] = configUser{State: configUserState{Player: i}}
}
}
v01.cfg.Players = make([]configPlayer, c.players)
@@ -490,8 +492,8 @@ func TestServeGM(t *testing.T) {
}
assignments := map[int]int{}
for _, v := range v01.cfg.Users {
if v.Player > 0 {
assignments[v.Player] = assignments[v.Player] + 1
if v.State.Player > 0 {
assignments[v.State.Player] = assignments[v.State.Player] + 1
}
}
lesser := c.users
@@ -515,7 +517,7 @@ func TestServeGM(t *testing.T) {
v01 := NewV01(ctx, nil)
v01.cfg.Quiet = true
v01.cfg.Users = map[string]configUser{
"bel": configUser{Player: 1},
"bel": configUser{State: configUserState{Player: 1}},
}
resp := do(v01, "/gm/rpc/swap?a=bel&b=bel", "")
if resp.Code != http.StatusConflict {
@@ -542,8 +544,8 @@ func TestServeGM(t *testing.T) {
v01 := NewV01(ctx, nil)
v01.cfg.Quiet = true
v01.cfg.Users = map[string]configUser{
"bel": configUser{Player: 1},
"zach": configUser{Player: 2},
"bel": configUser{State: configUserState{Player: 1}},
"zach": configUser{State: configUserState{Player: 2}},
}
resp := do(v01, "/gm/rpc/swap?a=bel&b=zach", "")
if resp.Code != http.StatusOK {
@@ -552,9 +554,9 @@ func TestServeGM(t *testing.T) {
if v01.cfg.Quiet {
t.Error(v01.cfg.Quiet)
}
if v01.cfg.Users["bel"].Player != 2 {
if v01.cfg.Users["bel"].State.Player != 2 {
t.Error(v01.cfg.Users["bel"])
} else if v01.cfg.Users["zach"].Player != 1 {
} else if v01.cfg.Users["zach"].State.Player != 1 {
t.Error(v01.cfg.Users["zach"])
}
})