From 3a83fe7c1710325a90b2fcd75336031396704409 Mon Sep 17 00:00:00 2001
From: Bel LaPointe <153096461+breel-render@users.noreply.github.com>
Date: Sun, 15 Dec 2024 11:28:42 -0700
Subject: [PATCH] refactor out ws.unstartedMsg
---
cmd/server/ws.go | 50 +++++++++++++++++++++++++++---------------------
1 file changed, 28 insertions(+), 22 deletions(-)
diff --git a/cmd/server/ws.go b/cmd/server/ws.go
index 09d7e6d..f4b9bf4 100644
--- a/cmd/server/ws.go
+++ b/cmd/server/ws.go
@@ -66,18 +66,7 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error {
return err
}
- msg := map[string]any{
- "help": strings.Join([]string{
- "CARD ASSASSINS (Mobile Ed.)",
- "",
- "1. Get any target to say any of his or her kill words.",
- "2. Click the word to collect points.",
- "3. Review new kill words.",
- "",
- "The game ends when everyone has been assassinated.",
- }, "
"),
- }
-
+ var msg map[string]any
if gameState.Started {
msg["page"] = "B"
if gameState.Completed.IsZero() {
@@ -153,18 +142,35 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error {
msg["items"] = items
}
} else {
- msg["page"] = "A"
- items := []map[string]any{}
- for k := range gameState.Players {
- name, err := ws.games.UserName(ctx, k)
- if err != nil {
- return err
- }
- items = append(items, map[string]any{"name": name})
- }
- msg["items"] = items
+ msg, err = ws.unstartedMsg(ctx, gameState)
+ }
+ if err != nil {
+ return err
}
+ msg["help"] = strings.Join([]string{
+ "CARD ASSASSINS (Mobile Ed.)",
+ "",
+ "1. Get any target to say any of his or her kill words.",
+ "2. Click the word to collect points.",
+ "3. Review new kill words.",
+ "",
+ "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)
+ if err != nil {
+ return nil, err
+ }
+ items = append(items, map[string]any{"name": name})
+ }
+ msg["items"] = items
+ return msg, nil
+}