Compare commits

..

10 Commits

Author SHA1 Message Date
Bel LaPointe
617785ad51 f u wannabe cors 2024-12-18 19:45:45 -07:00
Bel LaPointe
d2fa707628 tests pass but LOTS of todos 2024-12-16 17:29:27 -07:00
bel
421331eb71 i have a plan but also realize event state computation CANNOT create events because itll evalulate many times 2024-12-15 22:28:26 -07:00
Bel LaPointe
c6ffa12354 grr how do trial 2024-12-15 19:03:35 -07:00
Bel LaPointe
58fae19522 stub codename trial, accuse events 2024-12-15 18:43:15 -07:00
Bel LaPointe
4abac89472 use codename.Consumed instead of kills to know whether codename is available for murdering 2024-12-15 18:36:19 -07:00
Bel LaPointe
15078a626d Global-1 to Codename+200 2024-12-15 18:25:47 -07:00
Bel LaPointe
02dc21c124 todo 2024-12-15 17:53:38 -07:00
Bel LaPointe
4d9abef04c todo 2024-12-15 16:40:59 -07:00
Bel LaPointe
557e1ec6d4 accept ?uuid=X instead of cookie 2024-12-15 14:39:14 -07:00
8 changed files with 212 additions and 62 deletions

View File

@@ -11,6 +11,7 @@ import (
"slices"
"strings"
"time"
"unicode"
"github.com/google/uuid"
)
@@ -172,7 +173,10 @@ type (
ID string
Started bool
Completed time.Time
Players map[string]PlayerState
Players map[string]PlayerState
Trial Trial
}
PlayerState struct {
@@ -187,7 +191,7 @@ type (
}
KillWords struct {
Global KillWord
Codename Codename
Assigned time.Time
Assignee string
@@ -195,6 +199,11 @@ type (
Assignment Assignment
}
Codename struct {
KillWord KillWord
Consumed bool
}
Assignment struct {
Public []KillWord
Private []KillWord
@@ -205,6 +214,12 @@ type (
Points int
}
Trial struct {
Prosecutor string
Defendant string
Word string
}
EventType int
EventPlayerJoin struct {
@@ -234,6 +249,24 @@ type (
Timestamp time.Time
ID string
}
EventCodenameAccusal struct {
Type EventType
Timestamp time.Time
Prosecutor string
Defendant string
Word string
}
EventCodenameTrial struct {
Type EventType
Timestamp time.Time
Guilty bool
}
EventNotification struct {
Type EventType
Timestamp time.Time
Recipient string
Message string
}
AllKillWords map[string]KillWords
)
@@ -243,6 +276,9 @@ const (
GameComplete
AssignmentRotation
GameReset
CodenameAccusal
CodenameTrial
Notification
)
type Event interface{ event() }
@@ -252,6 +288,9 @@ func (EventPlayerLeave) event() {}
func (EventGameComplete) event() {}
func (EventAssignmentRotation) event() {}
func (EventGameReset) event() {}
func (EventCodenameAccusal) event() {}
func (EventCodenameTrial) event() {}
func (EventNotification) event() {}
func EventWithTime(event Event, t time.Time) Event {
switch e := event.(type) {
@@ -270,6 +309,15 @@ func EventWithTime(event Event, t time.Time) Event {
case EventGameReset:
e.Timestamp = t
event = e
case EventCodenameAccusal:
e.Timestamp = t
event = e
case EventCodenameTrial:
e.Timestamp = t
event = e
case EventNotification:
e.Timestamp = t
event = e
}
return event
}
@@ -324,6 +372,18 @@ func parseEvent(b []byte, timestamp time.Time) (Event, error) {
var v EventGameReset
err := json.Unmarshal(b, &v)
return EventWithTime(v, timestamp), err
case CodenameAccusal:
var v EventCodenameAccusal
err := json.Unmarshal(b, &v)
return EventWithTime(v, timestamp), err
case CodenameTrial:
var v EventCodenameTrial
err := json.Unmarshal(b, &v)
return EventWithTime(v, timestamp), err
case Notification:
var v EventNotification
err := json.Unmarshal(b, &v)
return EventWithTime(v, timestamp), err
}
return nil, fmt.Errorf("unknown event type %d: %s", peek.Type, b)
}
@@ -369,6 +429,46 @@ func (games Games) GameState(ctx context.Context, id string) (GameState, error)
player.KillWords = v
result.Players[k] = player
}
case EventCodenameAccusal:
if actual := result.Players[e.Defendant].KillWords.Codename; !actual.Consumed {
result.Trial.Prosecutor = e.Prosecutor
result.Trial.Defendant = e.Defendant
result.Trial.Word = e.Word
if !basicallyTheSame(actual.KillWord.Word, e.Word) {
} else if err := games.CreateEventCodenameTrial(ctx, id, true); err != nil { // TODO cannot be in State loop
return GameState{}, err
}
}
case EventCodenameTrial:
if result.Trial == (Trial{}) {
} else if e.Guilty {
if err := games.CreateEventNotification(ctx, id, fmt.Sprintf(`%s revealed %s is %s and collected %s's bounty.`, result.Trial.Prosecutor, result.Trial.Defendant, result.Trial.Word, result.Trial.Defendant)); err != nil { // TODO not in this loop
return GameState{}, err
}
return GameState{}, fmt.Errorf("not impl: trial: guilty: %+v", e)
} else {
v := result.Players[result.Trial.Prosecutor]
v.KillWords.Codename.Consumed = true
v.Kills = append(v.Kills, Kill{
Timestamp: e.Timestamp,
Victim: result.Trial.Defendant,
KillWord: KillWord{
Word: result.Trial.Word,
Points: -200,
},
})
result.Players[result.Trial.Prosecutor] = v
v = result.Players[result.Trial.Defendant]
v.KillWords.Codename.KillWord.Word = "" // TODO
return GameState{}, fmt.Errorf("creating state CANNOT create events because it will eval every loop")
if err := games.CreateEventNotification(ctx, id, fmt.Sprintf(`%s accused the innocent %s of being %s. %s will get a new codename.`, result.Trial.Prosecutor, result.Trial.Defendant, result.Trial.Word, result.Trial.Defendant)); err != nil {
return GameState{}, err
}
}
result.Trial = Trial{}
case EventGameReset:
return games.GameState(ctx, e.ID)
default:
@@ -379,6 +479,20 @@ func (games Games) GameState(ctx context.Context, id string) (GameState, error)
return result, err
}
func basicallyTheSame(a, b string) bool {
simplify := func(s string) string {
s = strings.TrimSpace(strings.ToLower(s))
s2 := ""
for _, c := range s {
if unicode.IsLetter(c) {
s2 = fmt.Sprintf("%s%c", s2, c)
}
}
return s2
}
return simplify(a) == simplify(b)
}
func (games Games) CreateGame(ctx context.Context, name string) (string, error) {
var exists string
if err := games.db.Query(ctx,
@@ -440,10 +554,7 @@ func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string,
},
}
prevAllKillWords := make(AllKillWords)
for k, v := range state.Players {
prevAllKillWords[k] = v.KillWords
}
prevAllKillWords := state.AllKillWords()
event.AllKillWords = prevAllKillWords.ShuffleAssignees(killer, victim, word)
event.AllKillWords = event.AllKillWords.FillKillWords()
@@ -451,6 +562,14 @@ func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string,
return games.createEvent(ctx, id, event)
}
func (state GameState) AllKillWords() AllKillWords {
m := make(AllKillWords)
for k, v := range state.Players {
m[k] = v.KillWords
}
return m
}
func (games Games) CreateEventGameReset(ctx context.Context, gid string) error {
state, err := games.GameState(ctx, gid)
if err != nil {
@@ -478,7 +597,7 @@ func (games Games) CreateEventGameReset(ctx context.Context, gid string) error {
}
func (words KillWords) Empty() bool {
return words.Global == (KillWord{}) && words.Assigned.IsZero() && words.Assignee == "" && words.Assignment.Empty()
return words.Codename == (Codename{}) && words.Assigned.IsZero() && words.Assignee == "" && words.Assignment.Empty()
}
func (words KillWords) Privates() []KillWord {
@@ -549,7 +668,7 @@ func (m AllKillWords) withoutAssignees() AllKillWords {
result := make(AllKillWords)
for k := range m {
result[k] = KillWords{
Global: m[k].Global,
Codename: m[k].Codename,
Assigned: now,
Assignee: "",
Assignment: m[k].Assignment,
@@ -598,7 +717,7 @@ func (m AllKillWords) FillKillWords() AllKillWords {
}
func (m AllKillWords) fillKillWords(
poolGlobal []string,
poolCodename []string,
nPublic int,
poolPublic []string,
nPrivate int,
@@ -607,8 +726,8 @@ func (m AllKillWords) fillKillWords(
result := maps.Clone(m)
m = result
for k, v := range m {
if v.Global.Word == "" {
v.Global = KillWord{Word: m.unusedGlobal(poolGlobal), Points: -1}
if v.Codename.KillWord.Word == "" {
v.Codename = Codename{KillWord: KillWord{Word: m.unusedCodename(poolCodename), Points: 200}}
m[k] = v
}
if len(v.Assignment.Public) == 0 {
@@ -629,11 +748,11 @@ func (m AllKillWords) fillKillWords(
return m
}
func (m AllKillWords) unusedGlobal(pool []string) string {
func (m AllKillWords) unusedCodename(pool []string) string {
inUse := func() []string {
result := []string{}
for _, killWords := range m {
result = append(result, killWords.Global.Word)
result = append(result, killWords.Codename.KillWord.Word)
}
return result
}
@@ -692,6 +811,23 @@ func (games Games) CreateEventGameComplete(ctx context.Context, id string) error
return games.createEvent(ctx, id, EventGameComplete{Type: GameComplete})
}
func (games Games) CreateEventCodenameAccusal(ctx context.Context, gid, prosecutor, defendant, codename string) error {
return fmt.Errorf("not impl: x accused y")
return fmt.Errorf("not impl: x caught by y")
}
func (games Games) CreateEventCodenameTrial(ctx context.Context, gid string, guilty bool) error {
return fmt.Errorf("not impl: x found guilty/notguilty")
}
func (games Games) CreateEventNotification(ctx context.Context, gid, msg string) error {
return games.CreateEventNotificationTo(ctx, gid, "", msg)
}
func (games Games) CreateEventNotificationTo(ctx context.Context, gid, uid, msg string) error {
return fmt.Errorf("not impl: simple")
}
func (games Games) createEvent(ctx context.Context, id string, v any) error {
payload, err := json.Marshal(v)
if err != nil {
@@ -706,7 +842,3 @@ func (games Games) createEvent(ctx context.Context, id string, v any) error {
) VALUES (?, ?, ?)
`, id, time.Now(), payload)
}
func (games *Games) Reset(ctx context.Context, gid string) error {
return games.CreateEventGameReset(ctx, gid)
}

View File

@@ -128,8 +128,8 @@ func TestGames(t *testing.T) {
if v.Players[p].Points() != 0 {
t.Error("nonzero points after zero kills:", v.Players[p].Points())
}
if v.Players[p].KillWords.Global.Word == "" {
t.Error(p, "no killwords.global")
if v.Players[p].KillWords.Codename.KillWord.Word == "" {
t.Error(p, "no killwords.Codename")
} else if v.Players[p].KillWords.Assigned.IsZero() {
t.Error(p, "no killwords.assigned")
} else if v.Players[p].KillWords.Assignee == "" {
@@ -150,7 +150,7 @@ func TestGames(t *testing.T) {
t.Fatal("state.Completed is zero")
}
if err := games.Reset(ctx, id); err != nil {
if err := games.CreateEventGameReset(ctx, id); err != nil {
t.Fatal(err)
} else if state, err := games.GameState(ctx, id); err != nil {
t.Fatal(err)
@@ -199,10 +199,10 @@ func TestParseEvent(t *testing.T) {
},
AllKillWords: map[string]KillWords{
"x": KillWords{
Global: KillWord{
Codename: Codename{KillWord: KillWord{
Word: "a",
Points: -1,
},
Points: 200,
}},
Assignee: "z",
Assigned: now,
Assignment: Assignment{
@@ -260,48 +260,48 @@ func TestAllKillWordsFill(t *testing.T) {
}{
"full": {
given: KillWords{
Global: kw(-1, "global"),
Codename: Codename{KillWord: kw(200, "global")},
Assignment: ass("pub", "pri"),
},
expect: KillWords{
Global: kw(-1, "global"),
Codename: Codename{KillWord: kw(200, "global")},
Assignment: ass("pub", "pri"),
},
},
"no ass": {
given: KillWords{
Global: kw(-1, "global"),
Codename: Codename{KillWord: kw(200, "global")},
Assignment: Assignment{},
},
expect: KillWords{
Global: kw(-1, "global"),
Codename: Codename{KillWord: kw(200, "global")},
Assignment: ass("filled-public", "filled-private"),
},
},
"no pub": {
given: KillWords{
Global: kw(-1, "global"),
Codename: Codename{KillWord: kw(200, "global")},
Assignment: ass("", "pri"),
},
expect: KillWords{
Global: kw(-1, "global"),
Codename: Codename{KillWord: kw(200, "global")},
Assignment: ass("filled-public", "pri"),
},
},
"no pri": {
given: KillWords{
Global: kw(-1, "global"),
Codename: Codename{KillWord: kw(200, "global")},
Assignment: ass("pub", ""),
},
expect: KillWords{
Global: kw(-1, "global"),
Codename: Codename{KillWord: kw(200, "global")},
Assignment: ass("pub", "filled-private"),
},
},
"empty": {
given: KillWords{},
expect: KillWords{
Global: kw(-1, "filled-global"),
Codename: Codename{KillWord: kw(200, "filled-global")},
Assignment: ass("filled-public", "filled-private"),
},
},
@@ -310,7 +310,7 @@ func TestAllKillWordsFill(t *testing.T) {
Assignment: ass("pub", "pri"),
},
expect: KillWords{
Global: kw(-1, "filled-global"),
Codename: Codename{KillWord: kw(200, "filled-global")},
Assignment: ass("pub", "pri"),
},
},
@@ -354,7 +354,7 @@ func TestAllKillWordsUnused(t *testing.T) {
t.Error("empty playerbase didnt think only option was unused")
}
if got := akw.unusedGlobal([]string{"x"}); got != "x" {
if got := akw.unusedCodename([]string{"x"}); got != "x" {
t.Error("empty playerbase didnt think only option was unused")
}
})
@@ -363,7 +363,7 @@ func TestAllKillWordsUnused(t *testing.T) {
t.Run("private", func(t *testing.T) {
akw := make(AllKillWords)
akw["k"] = KillWords{
Global: KillWord{Word: "x"},
Codename: Codename{KillWord: KillWord{Word: "x"}},
Assignment: Assignment{
Private: []KillWord{{}, {Word: "y"}},
Public: []KillWord{{}, {Word: "x"}},
@@ -377,13 +377,13 @@ func TestAllKillWordsUnused(t *testing.T) {
t.Run("global", func(t *testing.T) {
akw := make(AllKillWords)
akw["k"] = KillWords{
Global: KillWord{Word: "y"},
Codename: Codename{KillWord: KillWord{Word: "y"}},
Assignment: Assignment{
Private: []KillWord{{}, {Word: "x"}},
Public: []KillWord{{}, {Word: "x"}},
},
}
got := akw.unusedGlobal([]string{"x", "y"})
got := akw.unusedCodename([]string{"x", "y"})
if got != "x" {
t.Error("didnt return only unused option")
}
@@ -391,7 +391,7 @@ func TestAllKillWordsUnused(t *testing.T) {
t.Run("public", func(t *testing.T) {
akw := make(AllKillWords)
akw["k"] = KillWords{
Global: KillWord{Word: "x"},
Codename: Codename{KillWord: KillWord{Word: "x"}},
Assignment: Assignment{
Private: []KillWord{{}, {Word: "x"}},
Public: []KillWord{{}, {Word: "y"}},

View File

@@ -59,13 +59,19 @@ type Session struct {
}
func (s *S) injectContext(w http.ResponseWriter, r *http.Request) error {
id, err := r.Cookie("uuid")
if err != nil || id.Value == "" {
id := r.Header.Get("uuid")
if id == "" {
c, _ := r.Cookie("uuid")
if c != nil {
id = c.Value
}
}
if id == "" {
return io.EOF
}
ctx := r.Context()
ctx = context.WithValue(ctx, "session", Session{
ID: id.Value,
ID: id,
})
*r = *r.WithContext(ctx)
return nil

View File

@@ -95,8 +95,8 @@ func (ugs *UserGameServer) listen(ctx context.Context, reader func(context.Conte
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 codename := gameState.Players[killer].KillWords.Codename.KillWord; codename.Word == word {
points = codename.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 {
@@ -116,7 +116,7 @@ func (ugs *UserGameServer) listen(ctx context.Context, reader func(context.Conte
if gameState, err := ugs.games.GameState(ctx, ugs.ID); err != nil {
return err
} else if gameState.Completed.IsZero() {
} else if err := ugs.games.Reset(ctx, ugs.ID); err != nil {
} else if err := ugs.games.CreateEventGameReset(ctx, ugs.ID); err != nil {
return err
}
} else {
@@ -151,7 +151,7 @@ func (ugs *UserGameServer) State(ctx context.Context) (UserGameState, error) {
if isSelf := k == ugs.Session.ID; isSelf {
v.KillWords.Assignment = Assignment{}
} else {
v.KillWords.Global = KillWord{}
v.KillWords.Codename = Codename{}
v.KillWords.Assignee = ""
for i := range v.Kills {
v.Kills[i].Victim = ""

View File

@@ -83,8 +83,8 @@ func TestUserGameServer(t *testing.T) {
}
if isSelf := pid == ugs.Session.ID; isSelf {
if p.KillWords.Global.Word == "" || p.KillWords.Global.Points == 0 {
t.Error("self global missing field")
if p.KillWords.Codename.KillWord.Word == "" || p.KillWords.Codename.KillWord.Points == 0 {
t.Error("self codename missing field")
}
if p.KillWords.Assignee == "" {
t.Error("assignee is empty")
@@ -96,8 +96,8 @@ func TestUserGameServer(t *testing.T) {
t.Error("self knows its own private")
}
} else {
if !p.KillWords.Global.Empty() {
t.Error("can see not self global")
if !p.KillWords.Codename.KillWord.Empty() {
t.Error("can see not self Codename")
}
if p.KillWords.Assignee != "" {
t.Error("can see other player's assignee")
@@ -167,8 +167,8 @@ func TestUserGameServer(t *testing.T) {
} else if kill.KillWord.Word == "" {
t.Errorf("dont know own kill word")
}
if p.KillWords.Global.Word == "" || p.KillWords.Global.Points == 0 {
t.Error("self global missing field")
if p.KillWords.Codename.KillWord.Word == "" || p.KillWords.Codename.KillWord.Points == 0 {
t.Error("self Codename missing field")
}
if p.KillWords.Assignee == "" {
t.Error("assignee is empty")
@@ -190,8 +190,8 @@ func TestUserGameServer(t *testing.T) {
} else if kill.KillWord.Word != "" {
t.Errorf("know other's kill word")
}
if !p.KillWords.Global.Empty() {
t.Error("can see not self global")
if !p.KillWords.Codename.KillWord.Empty() {
t.Error("can see not self Codename")
}
if p.KillWords.Assignee != "" {
t.Error("can see other player's assignee")
@@ -248,8 +248,8 @@ func TestUserGameServer(t *testing.T) {
} else if kill.KillWord.Word == "" {
t.Errorf("dont know own kill word")
}
if p.KillWords.Global.Word == "" || p.KillWords.Global.Points == 0 {
t.Error("self global missing field")
if p.KillWords.Codename.KillWord.Word == "" || p.KillWords.Codename.KillWord.Points == 0 {
t.Error("self Codename missing field")
}
if p.KillWords.Assignee == "" {
t.Error("assignee is empty")
@@ -271,8 +271,8 @@ func TestUserGameServer(t *testing.T) {
} else if kill.KillWord.Word == "" {
t.Errorf("dont know other's kill word")
}
if p.KillWords.Global.Empty() {
t.Error("cannot see not self global")
if p.KillWords.Codename.KillWord.Empty() {
t.Error("cannot see not self Codename")
}
if p.KillWords.Assignee == "" {
t.Error("cannot see other player's assignee")

View File

@@ -191,10 +191,8 @@ func (ws WS) inProgressMsgItem(ctx context.Context, ugs *UserGameServer, gameSta
tags := []inProgressMsgItemTag{}
if hasBeenKilledWithGlobal := slices.ContainsFunc(self.Kills, func(a Kill) bool {
return a.Victim == uid && a.KillWord.Word == self.KillWords.Global.Word
}); !hasBeenKilledWithGlobal {
tags = append(tags, newInProgressMsgItemTag(self.KillWords.Global))
if canKillWithCodename := !self.KillWords.Codename.Consumed; canKillWithCodename {
tags = append(tags, newInProgressMsgItemTag(self.KillWords.Codename.KillWord))
}
for _, killWord := range append(

View File

@@ -146,7 +146,9 @@ func (s *S) Session(ctx context.Context) Session {
func (s *S) serveWS(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
c, err := websocket.Accept(w, r, nil)
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
InsecureSkipVerify: true,
})
if err != nil {
return err
}

View File

@@ -1,4 +1,14 @@
todo:
- global+public+private to public+private+CODENAME
- accuse event which either ends in a successful codename call worth 100% points +
disables codename OR ends in a failed codename call costing 50% codename points
- report system
- how to generate word lists??
- '"handler" system; there are both assassinations AND tenet-friendship-codeword systems
in-flight'
- dont like other players points; just order by points so winner is at top of list
- comeback/rebound system
- kingkiller system, increased bounty for those who havent died recently + high scorers
- test ws flow
- notifications system with dismissal server-side so users see X got a kill
- play mp3 on kill + shuffle
@@ -14,3 +24,5 @@ done:
ts: Sun Dec 15 14:28:29 MST 2024
- todo: quit
ts: Sun Dec 15 14:28:34 MST 2024
- todo: '"handler" system??'
ts: Sun Dec 15 16:43:34 MST 2024