From 8d0ded9ee98a64c015efa4959cf523b4249284b9 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Sun, 15 Dec 2024 11:35:00 -0700 Subject: [PATCH] refactors --- cmd/server/ws.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/server/ws.go b/cmd/server/ws.go index be83a2e..47eb53b 100644 --- a/cmd/server/ws.go +++ b/cmd/server/ws.go @@ -72,7 +72,7 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error { } else if complete := !gameState.Completed.IsZero(); complete { msg, err = ws.completeMsg(ctx, gameState) } else { - msg, err = ws.incompleteMsg(ctx, gameState) + msg, err = ws.inProgressMsg(ctx, ugs, gameState) } if err != nil { return err @@ -87,12 +87,14 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error { "", "The game ends when everyone has been assassinated.", }, "
") + msgB, _ := json.Marshal(msg) return ws.c.Write(ctx, 1, msgB) } func (ws WS) unstartedMsg(ctx context.Context, gameState GameState) (msg map[string]any, _ error) { msg["page"] = "A" + items := []map[string]any{} for k := range gameState.Players { name, err := ws.games.UserName(ctx, k) @@ -102,12 +104,14 @@ func (ws WS) unstartedMsg(ctx context.Context, gameState GameState) (msg map[str items = append(items, map[string]any{"name": name}) } msg["items"] = items + return msg, nil } func (ws WS) completeMsg(ctx context.Context, gameState GameState) (msg map[string]any, _ error) { msg["page"] = "B" msg["event"] = "B" + items := []map[string]any{} for k, v := range gameState.Players { name, err := ws.games.UserName(ctx, k) @@ -131,12 +135,13 @@ func (ws WS) completeMsg(ctx context.Context, gameState GameState) (msg map[stri return msg, nil } -func (ws WS) incompleteMsg(ctx context.Context, gameState GameState) (msg map[string]any, _ error) { +func (ws WS) inProgressMsg(ctx context.Context, ugs *UserGameServer, gameState GameState) (msg map[string]any, _ error) { msg["page"] = "B" msg["event"] = "A" + items := []map[string]any{} for k, v := range gameState.Players { - if k == ws.Session(ctx).ID { + if k == ugs.Session.ID { continue } @@ -146,7 +151,7 @@ func (ws WS) incompleteMsg(ctx context.Context, gameState GameState) (msg map[st } tags := []map[string]any{} - if self := gameState.Players[ws.Session(ctx).ID]; self.KillWords.Assignee == k { + if self := gameState.Players[ugs.Session.ID]; self.KillWords.Assignee == k { for _, private := range v.KillWords.Assignment.Private { tags = append(tags, map[string]any{ "k": private.Word, @@ -160,7 +165,7 @@ func (ws WS) incompleteMsg(ctx context.Context, gameState GameState) (msg map[st "v": public.Points, }) } - if self := gameState.Players[ws.Session(ctx).ID]; !slices.ContainsFunc(self.Kills, func(a Kill) bool { + if self := gameState.Players[ugs.Session.ID]; !slices.ContainsFunc(self.Kills, func(a Kill) bool { return a.Victim == k }) { tags = append(tags, map[string]any{