refactor out incomplete msg
parent
3a83fe7c17
commit
e8817f9e74
126
cmd/server/ws.go
126
cmd/server/ws.go
|
|
@ -67,9 +67,72 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error {
|
|||
}
|
||||
|
||||
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"
|
||||
if gameState.Completed.IsZero() {
|
||||
msg["event"] = "A"
|
||||
items := []map[string]any{}
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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"]))
|
||||
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 {
|
||||
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
|
||||
return nil, io.EOF
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue