From 598cb0684c38006e8d656aeaddc85abd6adaf67e Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Sun, 15 Dec 2024 12:52:10 -0700 Subject: [PATCH] test UserGameState of freshly started game --- cmd/server/games.go | 4 ++++ cmd/server/usergameserver.go | 4 ++++ cmd/server/usergameserver_test.go | 35 ++++++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/cmd/server/games.go b/cmd/server/games.go index 2c79b4e..a241214 100644 --- a/cmd/server/games.go +++ b/cmd/server/games.go @@ -77,6 +77,10 @@ func (games Games) UserName(ctx context.Context, id string) (string, error) { return result, err } +func (a KillWord) Empty() bool { + return a == (KillWord{}) +} + func (a Assignment) Empty() bool { return len(a.Public) == 0 && len(a.Private) == 0 } diff --git a/cmd/server/usergameserver.go b/cmd/server/usergameserver.go index a5f4203..856a5a6 100644 --- a/cmd/server/usergameserver.go +++ b/cmd/server/usergameserver.go @@ -114,11 +114,15 @@ func (ugs *UserGameServer) State(ctx context.Context) (UserGameState, error) { for i := range self.Kills { self.Kills[i].KillWord.Points = 0 } + self.KillWords.Assignment.Public = nil + self.KillWords.Assignment.Private = nil + gameState.Players[ugs.Session.ID] = self for k, v := range gameState.Players { if isSelf := k == ugs.Session.ID; isSelf { self.KillWords.Assignment = Assignment{} } else { + v.KillWords.Global = KillWord{} v.KillWords.Assignee = "" } diff --git a/cmd/server/usergameserver_test.go b/cmd/server/usergameserver_test.go index 829f887..60a66d6 100644 --- a/cmd/server/usergameserver_test.go +++ b/cmd/server/usergameserver_test.go @@ -71,16 +71,45 @@ func TestUserGameServer(t *testing.T) { } for _, pid := range pids { - if p, ok := state.Players[pid]; !ok { + p, ok := state.Players[pid] + if !ok { t.Error(pid, "not in players") } else if p.Empty() { t.Error(pid, p) + } else if len(p.Kills) > 0 { + t.Error(pid, "has a kill") + } else if p.KillWords.Assigned.IsZero() { + t.Error("assigned is zero") } if isSelf := pid == ugs.Session.ID; isSelf { - t.Error("not impl") + if p.KillWords.Global.Word == "" || p.KillWords.Global.Points == 0 { + t.Error("self global missing field") + } + if p.KillWords.Assignee == "" { + t.Error("assignee is empty") + } + if len(p.KillWords.Assignment.Public) > 0 { + t.Error("self knows its own public") + } + if len(p.KillWords.Assignment.Private) > 0 { + t.Error("self knows its own private") + } } else { - t.Error("not impl") + if !p.KillWords.Global.Empty() { + t.Error("can see not self global") + } + if p.KillWords.Assignee != "" { + t.Error("can see other player's assignee") + } + if len(p.KillWords.Assignment.Public) == 0 { + t.Error("cannot see other player's public") + } + if state.Players[ugs.Session.ID].KillWords.Assignee == pid && len(p.KillWords.Assignment.Private) == 0 { + t.Error("cannot see assignee's private") + } else if state.Players[ugs.Session.ID].KillWords.Assignee != pid && len(p.KillWords.Assignment.Private) > 0 { + t.Error("can see not assignee's private") + } } } })