ws blindly pushes public, private and lets UserGameServer limit visibility of user
parent
c1933dc180
commit
c760dac44b
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -98,6 +99,36 @@ func (ugs *UserGameServer) listen(ctx context.Context, reader func(context.Conte
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ugs *UserGameServer) State(ctx context.Context) (GameState, error) {
|
type UserGameState GameState
|
||||||
return ugs.games.GameState(ctx, ugs.ID)
|
|
||||||
|
func (ugs *UserGameServer) State(ctx context.Context) (UserGameState, error) {
|
||||||
|
gameState, err := ugs.games.GameState(ctx, ugs.ID)
|
||||||
|
if err != nil {
|
||||||
|
return UserGameState{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if complete := !gameState.Completed.IsZero(); complete {
|
||||||
|
return UserGameState(gameState), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
self := gameState.Players[ugs.Session.ID]
|
||||||
|
for i := range self.Kills {
|
||||||
|
self.Kills[i].KillWord.Points = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range gameState.Players {
|
||||||
|
if isSelf := k == ugs.Session.ID; isSelf {
|
||||||
|
self.KillWords.Assignment = Assignment{}
|
||||||
|
} else {
|
||||||
|
v.KillWords.Assignee = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if assignedToSomeoneElse := self.KillWords.Assignee != k; assignedToSomeoneElse {
|
||||||
|
v.KillWords.Assignment.Private = v.KillWords.Assignment.Private[:0]
|
||||||
|
}
|
||||||
|
|
||||||
|
gameState.Players[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
return UserGameState(gameState), io.EOF
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error {
|
||||||
|
|
||||||
var msg map[string]any
|
var msg map[string]any
|
||||||
if unstarted := !gameState.Started; unstarted {
|
if unstarted := !gameState.Started; unstarted {
|
||||||
msg, err = ws.unstartedMsg(ctx, gameState)
|
msg, err = ws.unstartedMsg(ctx, ugs, gameState)
|
||||||
} else if complete := !gameState.Completed.IsZero(); complete {
|
} else if complete := !gameState.Completed.IsZero(); complete {
|
||||||
msg, err = ws.completeMsg(ctx, gameState)
|
msg, err = ws.completeMsg(ctx, gameState)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -92,23 +92,27 @@ func (ws WS) Push(ctx context.Context, ugs *UserGameServer) error {
|
||||||
return ws.c.Write(ctx, 1, msgB)
|
return ws.c.Write(ctx, 1, msgB)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws WS) unstartedMsg(ctx context.Context, gameState GameState) (msg map[string]any, _ error) {
|
func (ws WS) unstartedMsg(ctx context.Context, ugs *UserGameServer, gameState UserGameState) (msg map[string]any, _ error) {
|
||||||
msg["page"] = "A"
|
msg["page"] = "A"
|
||||||
|
|
||||||
items := []map[string]any{}
|
items := []map[string]any{}
|
||||||
for k := range gameState.Players {
|
for k := range gameState.Players {
|
||||||
|
if k == ugs.Session.ID {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
name, err := ws.games.UserName(ctx, k)
|
name, err := ws.games.UserName(ctx, k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
items = append(items, map[string]any{"name": name})
|
items = append(items, map[string]any{"id": k, "name": name})
|
||||||
}
|
}
|
||||||
msg["items"] = items
|
msg["items"] = items
|
||||||
|
|
||||||
return msg, nil
|
return msg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws WS) completeMsg(ctx context.Context, gameState GameState) (msg map[string]any, _ error) {
|
func (ws WS) completeMsg(ctx context.Context, gameState UserGameState) (msg map[string]any, _ error) {
|
||||||
msg["page"] = "B"
|
msg["page"] = "B"
|
||||||
msg["event"] = "B"
|
msg["event"] = "B"
|
||||||
|
|
||||||
|
|
@ -135,7 +139,7 @@ func (ws WS) completeMsg(ctx context.Context, gameState GameState) (msg map[stri
|
||||||
return msg, nil
|
return msg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws WS) inProgressMsg(ctx context.Context, ugs *UserGameServer, gameState GameState) (msg map[string]any, _ error) {
|
func (ws WS) inProgressMsg(ctx context.Context, ugs *UserGameServer, gameState UserGameState) (msg map[string]any, _ error) {
|
||||||
msg["page"] = "B"
|
msg["page"] = "B"
|
||||||
msg["event"] = "A"
|
msg["event"] = "A"
|
||||||
|
|
||||||
|
|
@ -159,7 +163,7 @@ type inProgressMsgItemTag struct {
|
||||||
V int `json:"v,string"`
|
V int `json:"v,string"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws WS) inProgressMsgItems(ctx context.Context, ugs *UserGameServer, gameState GameState) ([]inProgressMsgItem, error) {
|
func (ws WS) inProgressMsgItems(ctx context.Context, ugs *UserGameServer, gameState UserGameState) ([]inProgressMsgItem, error) {
|
||||||
items := []inProgressMsgItem{}
|
items := []inProgressMsgItem{}
|
||||||
for k := range gameState.Players {
|
for k := range gameState.Players {
|
||||||
item, err := ws.inProgressMsgItem(ctx, ugs, gameState, k)
|
item, err := ws.inProgressMsgItem(ctx, ugs, gameState, k)
|
||||||
|
|
@ -179,39 +183,26 @@ func (ws WS) inProgressMsgItems(ctx context.Context, ugs *UserGameServer, gameSt
|
||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws WS) inProgressMsgItem(ctx context.Context, ugs *UserGameServer, gameState GameState, uid string) (*inProgressMsgItem, error) {
|
func (ws WS) inProgressMsgItem(ctx context.Context, ugs *UserGameServer, gameState UserGameState, uid string) (*inProgressMsgItem, error) {
|
||||||
if isSelf := uid == ugs.Session.ID; isSelf {
|
if isSelf := uid == ugs.Session.ID; isSelf {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
v := gameState.Players[uid]
|
|
||||||
self := gameState.Players[ugs.Session.ID]
|
self := gameState.Players[ugs.Session.ID]
|
||||||
|
v := gameState.Players[uid]
|
||||||
|
|
||||||
tags := []inProgressMsgItemTag{}
|
tags := []inProgressMsgItemTag{}
|
||||||
|
|
||||||
if hasBeenKilledWithGlobal := slices.ContainsFunc(self.Kills, func(a Kill) bool {
|
if hasBeenKilledWithGlobal := slices.ContainsFunc(self.Kills, func(a Kill) bool {
|
||||||
return a.KillWord.Word == self.KillWords.Global.Word && a.Victim == uid
|
return a.Victim == uid && a.KillWord.Word == self.KillWords.Global.Word
|
||||||
}); !hasBeenKilledWithGlobal {
|
}); !hasBeenKilledWithGlobal {
|
||||||
tags = append(tags, inProgressMsgItemTag{
|
tags = append(tags, newInProgressMsgItemTag(self.KillWords.Global))
|
||||||
K: self.KillWords.Global.Word,
|
|
||||||
V: self.KillWords.Global.Points,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, public := range v.KillWords.Publics() {
|
for _, killWord := range append(
|
||||||
tags = append(tags, inProgressMsgItemTag{
|
v.KillWords.Publics(),
|
||||||
K: public.Word,
|
v.KillWords.Privates()...,
|
||||||
V: public.Points,
|
) {
|
||||||
})
|
tags = append(tags, newInProgressMsgItemTag(killWord))
|
||||||
}
|
|
||||||
|
|
||||||
if isAssigned := self.KillWords.Assignee == uid; isAssigned {
|
|
||||||
for _, private := range v.KillWords.Privates() {
|
|
||||||
tags = append(tags, inProgressMsgItemTag{
|
|
||||||
K: private.Word,
|
|
||||||
V: private.Points,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
name, err := ws.games.UserName(ctx, uid)
|
name, err := ws.games.UserName(ctx, uid)
|
||||||
|
|
@ -221,3 +212,10 @@ func (ws WS) inProgressMsgItem(ctx context.Context, ugs *UserGameServer, gameSta
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newInProgressMsgItemTag(kw KillWord) inProgressMsgItemTag {
|
||||||
|
return inProgressMsgItemTag{
|
||||||
|
K: kw.Word,
|
||||||
|
V: kw.Points,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue