PlayerState.Points()

main
Bel LaPointe 2024-12-15 02:30:44 -07:00
parent 2a635bc8b5
commit 5587467a2d
2 changed files with 23 additions and 3 deletions

View File

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

View File

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