test UserGameState of freshly started game

main
Bel LaPointe 2024-12-15 12:52:10 -07:00
parent aba5225ed2
commit 598cb0684c
3 changed files with 40 additions and 3 deletions

View File

@ -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
}

View File

@ -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 = ""
}

View File

@ -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")
}
}
}
})