From 5587467a2d4b00bdd3d97ba2360ac83f2b82cbe9 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Sun, 15 Dec 2024 02:30:44 -0700 Subject: [PATCH] PlayerState.Points() --- cmd/server/games.go | 8 ++++++++ cmd/server/ws.go | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cmd/server/games.go b/cmd/server/games.go index d9b9e9b..32811df 100644 --- a/cmd/server/games.go +++ b/cmd/server/games.go @@ -73,6 +73,14 @@ func (games Games) UserName(ctx context.Context, id string) (string, error) { return result, err } +func (s PlayerState) Points() int { + points := 0 + for _, kill := range s.Kills { + points += kill.KillWord.Points + } + return points +} + func (games Games) GameByName(ctx context.Context, uid, name string) (string, error) { var result string err := games.db.Query(ctx, func(rows *sql.Rows) error { diff --git a/cmd/server/ws.go b/cmd/server/ws.go index 6e47c10..0e37efb 100644 --- a/cmd/server/ws.go +++ b/cmd/server/ws.go @@ -81,6 +81,20 @@ func (s *S) serveWS(w http.ResponseWriter, r *http.Request) error { msg["event"] = "A" items := []map[string]any{} for k, v := range gameState.Players { + if k == s.Session(ctx).ID { + continue + } + name, err := s.games.UserName(ctx, k) + if err != nil { + return err + } + items = append(items, map[string]any{ + "name": name, + "title": strconv.Itoa(v.Points()), + "tags": []map[string]any{ + map[string]any{"k": "k", "v": "1"}, + }, + }) _, _ = k, v } slices.SortFunc(items, func(a, b map[string]any) int { @@ -97,10 +111,8 @@ func (s *S) serveWS(w http.ResponseWriter, r *http.Request) error { if err != nil { return err } - points := 0 tags := []map[string]any{} for _, kill := range v.Kills { - points += kill.KillWord.Points tags = append(tags, map[string]any{ "k": kill.KillWord.Word, "v": kill.Victim, @@ -108,7 +120,7 @@ func (s *S) serveWS(w http.ResponseWriter, r *http.Request) error { } items = append(items, map[string]any{ "name": name, - "title": fmt.Sprint(points), + "title": fmt.Sprint(v.Points()), "tags": tags, }) }