diff --git a/cmd/server/games.go b/cmd/server/games.go index 07783c3..5c371ac 100644 --- a/cmd/server/games.go +++ b/cmd/server/games.go @@ -228,6 +228,10 @@ type ( KillWord KillWord AllKillWords AllKillWords } + EventGameReset struct { + Type EventType + Timestamp time.Time + } AllKillWords map[string]KillWords ) @@ -236,6 +240,7 @@ const ( PlayerLeave GameComplete AssignmentRotation + GameReset ) type Event interface{ event() } @@ -244,6 +249,7 @@ func (EventPlayerJoin) event() {} func (EventPlayerLeave) event() {} func (EventGameComplete) event() {} func (EventAssignmentRotation) event() {} +func (EventGameReset) event() {} func EventWithTime(event Event, t time.Time) Event { switch e := event.(type) { @@ -259,6 +265,9 @@ func EventWithTime(event Event, t time.Time) Event { case EventAssignmentRotation: e.Timestamp = t event = e + case EventGameReset: + e.Timestamp = t + event = e } return event } @@ -309,6 +318,10 @@ func parseEvent(b []byte, timestamp time.Time) (Event, error) { var v EventAssignmentRotation err := json.Unmarshal(b, &v) return EventWithTime(v, timestamp), err + case GameReset: + var v EventGameReset + err := json.Unmarshal(b, &v) + return EventWithTime(v, timestamp), err } return nil, fmt.Errorf("unknown event type %d: %s", peek.Type, b) } @@ -322,19 +335,16 @@ func (games Games) GameState(ctx context.Context, id string) (GameState, error) } for _, event := range events { - switch event.(type) { + switch e := event.(type) { case EventPlayerJoin: - playerJoin := event.(EventPlayerJoin) - result.Players[playerJoin.ID] = PlayerState{} + result.Players[e.ID] = PlayerState{} case EventPlayerLeave: - playerLeave := event.(EventPlayerLeave) - delete(result.Players, playerLeave.ID) + delete(result.Players, e.ID) case EventGameComplete: - gameComplete := event.(EventGameComplete) - result.Completed = gameComplete.Timestamp + result.Completed = e.Timestamp case EventAssignmentRotation: result.Started = true - assignmentRotation := event.(EventAssignmentRotation) + assignmentRotation := e if killer, ok := result.Players[assignmentRotation.Killer]; !ok { } else if _, ok := result.Players[assignmentRotation.Victim]; !ok { @@ -357,6 +367,8 @@ func (games Games) GameState(ctx context.Context, id string) (GameState, error) player.KillWords = v result.Players[k] = player } + case EventGameReset: + return GameState{}, fmt.Errorf("not impl") default: return GameState{}, fmt.Errorf("unknown event type %T", event) } @@ -437,6 +449,10 @@ func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string, return games.createEvent(ctx, id, event) } +func (games Games) CreateEventGameReset(ctx context.Context, id string, killer, victim, word string, points int) error { + return fmt.Errorf("not impl") +} + func (words KillWords) Empty() bool { return words.Global == (KillWord{}) && words.Assigned.IsZero() && words.Assignee == "" && words.Assignment.Empty() }