diff --git a/cmd/server/games.go b/cmd/server/games.go index 1c834d1..f474422 100644 --- a/cmd/server/games.go +++ b/cmd/server/games.go @@ -378,6 +378,22 @@ func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string, return games.createEvent(ctx, id, event) } +func (words KillWords) Privates() []KillWord { + a := slices.Clone(words.Assignment.Private) + slices.SortFunc(a, func(a, b KillWord) int { + return strings.Compare(a.Word, b.Word) + }) + return a +} + +func (words KillWords) Publics() []KillWord { + a := slices.Clone(words.Assignment.Public) + slices.SortFunc(a, func(a, b KillWord) int { + return strings.Compare(a.Word, b.Word) + }) + return a +} + func (prev AllKillWords) ShuffleAssignees(killer, victim, word string) AllKillWords { m := prev.withoutAssignees() diff --git a/cmd/server/ws.go b/cmd/server/ws.go index 7dfc70e..c032c41 100644 --- a/cmd/server/ws.go +++ b/cmd/server/ws.go @@ -156,7 +156,7 @@ type inProgressMsgItem struct { type inProgressMsgItemTag struct { K string `json:"k"` - V string `json:"v"` + V int `json:"v,string"` } func (ws WS) inProgressMsgItems(ctx context.Context, ugs *UserGameServer, gameState GameState) ([]inProgressMsgItem, error) { @@ -180,44 +180,44 @@ func (ws WS) inProgressMsgItems(ctx context.Context, ugs *UserGameServer, gameSt } func (ws WS) inProgressMsgItem(ctx context.Context, ugs *UserGameServer, gameState GameState, uid string) (*inProgressMsgItem, error) { - v := gameState.Players[uid] - - if uid == ugs.Session.ID { + if isSelf := uid == ugs.Session.ID; isSelf { return nil, nil } - name, err := ws.games.UserName(ctx, uid) - if err != nil { - return nil, err - } + v := gameState.Players[uid] + self := gameState.Players[ugs.Session.ID] tags := []inProgressMsgItemTag{} - if self := gameState.Players[ugs.Session.ID]; self.KillWords.Assignee == uid { - for _, private := range v.KillWords.Assignment.Private { + + if hasBeenKilledWithGlobal := slices.ContainsFunc(self.Kills, func(a Kill) bool { + return a.KillWord.Word == self.KillWords.Global.Word && a.Victim == uid + }); !hasBeenKilledWithGlobal { + tags = append(tags, inProgressMsgItemTag{ + K: self.KillWords.Global.Word, + V: self.KillWords.Global.Points, + }) + } + + for _, public := range v.KillWords.Publics() { + tags = append(tags, inProgressMsgItemTag{ + K: public.Word, + V: public.Points, + }) + } + + if isAssigned := self.KillWords.Assignee == uid; isAssigned { + for _, private := range v.KillWords.Privates() { tags = append(tags, inProgressMsgItemTag{ K: private.Word, - V: strconv.Itoa(private.Points), + V: private.Points, }) } } - for _, public := range v.KillWords.Assignment.Public { - tags = append(tags, inProgressMsgItemTag{ - K: public.Word, - V: strconv.Itoa(public.Points), - }) - } - if self := gameState.Players[ugs.Session.ID]; !slices.ContainsFunc(self.Kills, func(a Kill) bool { - return a.Victim == uid - }) { - tags = append(tags, inProgressMsgItemTag{ - K: self.KillWords.Global.Word, - V: strconv.Itoa(self.KillWords.Global.Points), - }) - } + name, err := ws.games.UserName(ctx, uid) return &inProgressMsgItem{ Name: name, Title: strconv.Itoa(v.Points()), Tags: tags, - }, nil + }, err }