Compare commits
4 Commits
0bade0bd4f
...
fbf512c0d2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbf512c0d2 | ||
|
|
2d87e78556 | ||
|
|
aadc23c45b | ||
|
|
d597a74a2b |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/cmd/server/server
|
||||
Binary file not shown.
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"slices"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -33,7 +34,7 @@ func NewGames(ctx context.Context, db DB) (Games, error) {
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS events (
|
||||
game_uuid TEXT,
|
||||
updated DATETIME,
|
||||
timestamp DATETIME,
|
||||
payload TEXT
|
||||
);
|
||||
`)
|
||||
@@ -78,8 +79,10 @@ type (
|
||||
|
||||
KillWords struct {
|
||||
Global string
|
||||
|
||||
Assigned time.Time
|
||||
Assignee string
|
||||
|
||||
Assignment Assignment
|
||||
}
|
||||
|
||||
@@ -100,7 +103,8 @@ type (
|
||||
EventAssignmentRotation struct {
|
||||
Killer 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 {
|
||||
return err
|
||||
}
|
||||
// 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
|
||||
return fmt.Errorf("not impl: assignment rotation: %+v", assignmentRotation)
|
||||
|
||||
if killer, ok := result.Players[assignmentRotation.Killer]; !ok {
|
||||
} else if victim, ok := result.Players[assignmentRotation.Killed]; !ok {
|
||||
} else {
|
||||
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:
|
||||
return fmt.Errorf("unknown event type %d: %s", peek.Type, payload)
|
||||
}
|
||||
@@ -173,3 +195,39 @@ func (games Games) GameState(ctx context.Context, id string) (GameState, error)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user