refactor out incomplete msg

main
Bel LaPointe 2024-12-15 11:32:23 -07:00
parent 3a83fe7c17
commit e8817f9e74
1 changed files with 83 additions and 75 deletions

View File

@ -67,9 +67,72 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error {
} }
var msg map[string]any var msg map[string]any
if gameState.Started { 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
}
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.",
}, "<br>")
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
}
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["page"] = "B"
if gameState.Completed.IsZero() {
msg["event"] = "A" msg["event"] = "A"
items := []map[string]any{} items := []map[string]any{}
for k, v := range gameState.Players { for k, v := range gameState.Players {
@ -79,7 +142,7 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error {
name, err := ws.games.UserName(ctx, k) name, err := ws.games.UserName(ctx, k)
if err != nil { if err != nil {
return err return nil, err
} }
tags := []map[string]any{} tags := []map[string]any{}
@ -117,60 +180,5 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error {
bn, _ := strconv.Atoi(fmt.Sprint(b["title"])) bn, _ := strconv.Atoi(fmt.Sprint(b["title"]))
return an - bn return an - bn
}) })
return io.EOF return nil, 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 {
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.",
}, "<br>")
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
} }