diff --git a/cmd/server/ws.go b/cmd/server/ws.go index f4b9bf4..be83a2e 100644 --- a/cmd/server/ws.go +++ b/cmd/server/ws.go @@ -67,82 +67,12 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error { } var msg map[string]any - if gameState.Started { - msg["page"] = "B" - if gameState.Completed.IsZero() { - msg["event"] = "A" - items := []map[string]any{} - for k, v := range gameState.Players { - if k == ws.Session(ctx).ID { - continue - } - - name, err := ws.games.UserName(ctx, k) - if err != nil { - return err - } - - tags := []map[string]any{} - if self := gameState.Players[ws.Session(ctx).ID]; self.KillWords.Assignee == k { - for _, private := range v.KillWords.Assignment.Private { - tags = append(tags, map[string]any{ - "k": private.Word, - "v": private.Points, - }) - } - } - for _, public := range v.KillWords.Assignment.Public { - tags = append(tags, map[string]any{ - "k": public.Word, - "v": public.Points, - }) - } - if self := gameState.Players[ws.Session(ctx).ID]; !slices.ContainsFunc(self.Kills, func(a Kill) bool { - return a.Victim == k - }) { - tags = append(tags, map[string]any{ - "k": self.KillWords.Global.Word, - "v": self.KillWords.Global.Points, - }) - } - - items = append(items, map[string]any{ - "name": name, - "title": strconv.Itoa(v.Points()), - "tags": tags, - }) - } - slices.SortFunc(items, func(a, b map[string]any) int { - an, _ := strconv.Atoi(fmt.Sprint(a["title"])) - bn, _ := strconv.Atoi(fmt.Sprint(b["title"])) - return an - bn - }) - return io.EOF - } else { - msg["event"] = "B" - items := []map[string]any{} - for k, v := range gameState.Players { - name, err := ws.games.UserName(ctx, k) - if err != nil { - return err - } - tags := []map[string]any{} - for _, kill := range v.Kills { - tags = append(tags, map[string]any{ - "k": kill.KillWord.Word, - "v": kill.Victim, - }) - } - items = append(items, map[string]any{ - "name": name, - "title": fmt.Sprint(v.Points()), - "tags": tags, - }) - } - msg["items"] = items - } - } else { + if unstarted := !gameState.Started; unstarted { msg, err = ws.unstartedMsg(ctx, gameState) + } else if complete := !gameState.Completed.IsZero(); complete { + msg, err = ws.completeMsg(ctx, gameState) + } else { + msg, err = ws.incompleteMsg(ctx, gameState) } if err != nil { return err @@ -174,3 +104,81 @@ func (ws WS) unstartedMsg(ctx context.Context, gameState GameState) (msg map[str 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) + if err != nil { + return nil, err + } + tags := []map[string]any{} + for _, kill := range v.Kills { + tags = append(tags, map[string]any{ + "k": kill.KillWord.Word, + "v": kill.Victim, + }) + } + items = append(items, map[string]any{ + "name": name, + "title": fmt.Sprint(v.Points()), + "tags": tags, + }) + } + msg["items"] = items + return msg, nil +} + +func (ws WS) incompleteMsg(ctx context.Context, 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 { + continue + } + + name, err := ws.games.UserName(ctx, k) + if err != nil { + return nil, err + } + + tags := []map[string]any{} + if self := gameState.Players[ws.Session(ctx).ID]; self.KillWords.Assignee == k { + for _, private := range v.KillWords.Assignment.Private { + tags = append(tags, map[string]any{ + "k": private.Word, + "v": private.Points, + }) + } + } + for _, public := range v.KillWords.Assignment.Public { + tags = append(tags, map[string]any{ + "k": public.Word, + "v": public.Points, + }) + } + if self := gameState.Players[ws.Session(ctx).ID]; !slices.ContainsFunc(self.Kills, func(a Kill) bool { + return a.Victim == k + }) { + tags = append(tags, map[string]any{ + "k": self.KillWords.Global.Word, + "v": self.KillWords.Global.Points, + }) + } + + items = append(items, map[string]any{ + "name": name, + "title": strconv.Itoa(v.Points()), + "tags": tags, + }) + } + slices.SortFunc(items, func(a, b map[string]any) int { + an, _ := strconv.Atoi(fmt.Sprint(a["title"])) + bn, _ := strconv.Atoi(fmt.Sprint(b["title"])) + return an - bn + }) + return nil, io.EOF +}