Compare commits

...

4 Commits

Author SHA1 Message Date
bel
fbf512c0d2 create event assignment rotation TODO 2024-12-15 00:05:24 -07:00
bel
2d87e78556 stub create events 2024-12-14 23:48:59 -07:00
bel
aadc23c45b assignmentRotation impl 2024-12-14 23:45:47 -07:00
bel
d597a74a2b gitignore 2024-12-14 23:41:28 -07:00
3 changed files with 76 additions and 17 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/cmd/server/server

Binary file not shown.

View File

@@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"slices"
"time" "time"
) )
@@ -17,23 +18,23 @@ func NewGames(ctx context.Context, db DB) (Games, error) {
err := db.Exec(ctx, ` err := db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
uuid TEXT, uuid TEXT,
updated DATETIME, updated DATETIME,
name TEXT name TEXT
); );
CREATE TABLE IF NOT EXISTS games ( CREATE TABLE IF NOT EXISTS games (
uuid TEXT, uuid TEXT,
updated DATETIME, updated DATETIME,
name TEXT, name TEXT,
completed DATETIME completed DATETIME
); );
CREATE TABLE IF NOT EXISTS players ( CREATE TABLE IF NOT EXISTS players (
user_uuid TEXT, user_uuid TEXT,
game_uuid TEXT, game_uuid TEXT,
updated DATETIME updated DATETIME
); );
CREATE TABLE IF NOT EXISTS events ( CREATE TABLE IF NOT EXISTS events (
game_uuid TEXT, game_uuid TEXT,
updated DATETIME, timestamp DATETIME,
payload TEXT payload TEXT
); );
`) `)
@@ -77,9 +78,11 @@ type (
} }
KillWords struct { KillWords struct {
Global string Global string
Assigned time.Time
Assignee string Assigned time.Time
Assignee string
Assignment Assignment Assignment Assignment
} }
@@ -98,9 +101,10 @@ type (
} }
EventGameComplete struct{} EventGameComplete struct{}
EventAssignmentRotation struct { EventAssignmentRotation struct {
Killer string Killer string
Killed string Killed string
Assignments map[string]Assignment KillWord string
KillWords map[string]KillWords
} }
) )
@@ -155,11 +159,29 @@ func (games Games) GameState(ctx context.Context, id string) (GameState, error)
if err := json.Unmarshal(payload, &assignmentRotation); err != nil { if err := json.Unmarshal(payload, &assignmentRotation); err != nil {
return err return err
} }
// TODO gather current assignees
// TODO get victim's target if killer, ok := result.Players[assignmentRotation.Killer]; !ok {
// TODO assign victim's target to killer } else if victim, ok := result.Players[assignmentRotation.Killed]; !ok {
// TODO randomize everyone else so not the same as before } else {
return fmt.Errorf("not impl: assignment rotation: %+v", assignmentRotation) killer.Kills = append(killer.Kills, Kill{
Timestamp: timestamp,
Victim: assignmentRotation.Killed,
Public: slices.Contains(victim.KillWords.Assignment.Public, assignmentRotation.KillWord),
})
result.Players[assignmentRotation.Killer] = killer
}
for k, v := range result.Players {
v.KillWords = KillWords{}
result.Players[k] = v
}
for k, v := range assignmentRotation.KillWords {
player := result.Players[k]
player.KillWords = v
result.Players[k] = player
}
return nil
default: default:
return fmt.Errorf("unknown event type %d: %s", peek.Type, payload) return fmt.Errorf("unknown event type %d: %s", peek.Type, payload)
} }
@@ -168,8 +190,44 @@ func (games Games) GameState(ctx context.Context, id string) (GameState, error)
}, ` }, `
SELECT timestamp, payload SELECT timestamp, payload
FROM events FROM events
WHERE game_uuid=? WHERE game_uuid=?
`, id) `, id)
return result, err return result, err
} }
func (games Games) CreateEventPlayerJoin(ctx context.Context, id string, player string) error {
return games.createEvent(ctx, id, EventPlayerJoin{ID: player})
}
func (games Games) CreateEventPlayerLeave(ctx context.Context, id string, player string) error {
return games.createEvent(ctx, id, EventPlayerLeave{ID: player})
}
func (games Games) CreateEventGameComplete(ctx context.Context, id string) error {
return games.createEvent(ctx, id, EventGameComplete{})
}
func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string, killer, killed, killWord string) error {
// TODO gather current assignees
// TODO get victim's target
// TODO assign victim's target to killer
// TODO randomize everyone else so not the same as before AND not self
return io.EOF
return games.createEvent(ctx, id, v)
}
func (games Games) createEvent(ctx context.Context, id string, v any) error {
payload, err := json.Marshal(any)
if err != nil {
panic(err)
}
return games.db.Exec(ctx, `
INSERT INTO events (
game_uuid,
timestamp,
payload
) VALUES (?, ?, ?)
`, id, time.Now(), payload)
}