diff --git a/cmd/server/games.go b/cmd/server/games.go index d9328ae..c7ac6f1 100644 --- a/cmd/server/games.go +++ b/cmd/server/games.go @@ -18,23 +18,23 @@ func NewGames(ctx context.Context, db DB) (Games, error) { err := db.Exec(ctx, ` CREATE TABLE IF NOT EXISTS users ( uuid TEXT, - updated DATETIME, + updated DATETIME, name TEXT ); CREATE TABLE IF NOT EXISTS games ( uuid TEXT, - updated DATETIME, + updated DATETIME, name TEXT, - completed DATETIME + completed DATETIME ); CREATE TABLE IF NOT EXISTS players ( user_uuid TEXT, game_uuid TEXT, - updated DATETIME + updated DATETIME ); CREATE TABLE IF NOT EXISTS events ( game_uuid TEXT, - updated DATETIME, + timestamp DATETIME, payload TEXT ); `) @@ -181,10 +181,6 @@ func (games Games) GameState(ctx context.Context, id string) (GameState, error) player.KillWords = v result.Players[k] = player } - // 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 nil default: return fmt.Errorf("unknown event type %d: %s", peek.Type, payload) @@ -194,24 +190,44 @@ func (games Games) GameState(ctx context.Context, id string) (GameState, error) }, ` SELECT timestamp, payload FROM events - WHERE game_uuid=? + WHERE game_uuid=? `, id) return result, err } -func (games Games) CreateEventPlayerJoin(ctx context.Context, id string, playerJoin EventPlayerJoin) error { - return io.EOF +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, playerLeave EventPlayerLeave) error { - return io.EOF +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 io.EOF + 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) }