impl ws kill

main
Bel LaPointe 2024-12-15 13:56:59 -07:00
parent 706d55631b
commit f1282f588d
1 changed files with 29 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"slices"
"time" "time"
) )
@ -79,7 +80,34 @@ func (ugs *UserGameServer) listen(ctx context.Context, reader func(context.Conte
return err return err
} }
} else if killOccurred := m["k"] != ""; killOccurred { } else if killOccurred := m["k"] != ""; killOccurred {
return fmt.Errorf("not impl: a kill occurred: %+v", m) victimName := m["name"]
word := m["k"]
if word == "" {
return fmt.Errorf("expected .k")
}
killer := ugs.Session.ID
victim, err := ugs.games.UserByName(ctx, ugs.ID, victimName)
if err != nil {
return err
}
var points int
if gameState, err := ugs.games.GameState(ctx, ugs.ID); err != nil {
return err
} else if global := gameState.Players[killer].KillWords.Global; global.Word == word {
points = global.Points
} else if matches := slices.DeleteFunc(gameState.Players[victim].KillWords.Publics(), func(kw KillWord) bool { return kw.Word != word }); len(matches) > 0 {
points = matches[0].Points
} else if matches := slices.DeleteFunc(gameState.Players[victim].KillWords.Privates(), func(kw KillWord) bool { return kw.Word != word }); len(matches) > 0 {
points = matches[0].Points
} else {
return fmt.Errorf("refusing unexpected .k")
}
if err := ugs.games.CreateEventAssignmentRotation(ctx, ugs.ID, killer, victim, word, points); err != nil {
return err
}
} else if isRename := m["name"] != ""; isRename { } else if isRename := m["name"] != ""; isRename {
if err := ugs.games.UpdateUserName(ctx, ugs.Session.ID, m["name"]); err != nil { if err := ugs.games.UpdateUserName(ctx, ugs.Session.ID, m["name"]); err != nil {
return err return err