stub GameReset event

main
Bel LaPointe 2024-12-15 14:09:54 -07:00
parent 57dd66e510
commit 54116131b3
1 changed files with 24 additions and 8 deletions

View File

@ -228,6 +228,10 @@ type (
KillWord KillWord KillWord KillWord
AllKillWords AllKillWords AllKillWords AllKillWords
} }
EventGameReset struct {
Type EventType
Timestamp time.Time
}
AllKillWords map[string]KillWords AllKillWords map[string]KillWords
) )
@ -236,6 +240,7 @@ const (
PlayerLeave PlayerLeave
GameComplete GameComplete
AssignmentRotation AssignmentRotation
GameReset
) )
type Event interface{ event() } type Event interface{ event() }
@ -244,6 +249,7 @@ func (EventPlayerJoin) event() {}
func (EventPlayerLeave) event() {} func (EventPlayerLeave) event() {}
func (EventGameComplete) event() {} func (EventGameComplete) event() {}
func (EventAssignmentRotation) event() {} func (EventAssignmentRotation) event() {}
func (EventGameReset) event() {}
func EventWithTime(event Event, t time.Time) Event { func EventWithTime(event Event, t time.Time) Event {
switch e := event.(type) { switch e := event.(type) {
@ -259,6 +265,9 @@ func EventWithTime(event Event, t time.Time) Event {
case EventAssignmentRotation: case EventAssignmentRotation:
e.Timestamp = t e.Timestamp = t
event = e event = e
case EventGameReset:
e.Timestamp = t
event = e
} }
return event return event
} }
@ -309,6 +318,10 @@ func parseEvent(b []byte, timestamp time.Time) (Event, error) {
var v EventAssignmentRotation var v EventAssignmentRotation
err := json.Unmarshal(b, &v) err := json.Unmarshal(b, &v)
return EventWithTime(v, timestamp), err 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) 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 { for _, event := range events {
switch event.(type) { switch e := event.(type) {
case EventPlayerJoin: case EventPlayerJoin:
playerJoin := event.(EventPlayerJoin) result.Players[e.ID] = PlayerState{}
result.Players[playerJoin.ID] = PlayerState{}
case EventPlayerLeave: case EventPlayerLeave:
playerLeave := event.(EventPlayerLeave) delete(result.Players, e.ID)
delete(result.Players, playerLeave.ID)
case EventGameComplete: case EventGameComplete:
gameComplete := event.(EventGameComplete) result.Completed = e.Timestamp
result.Completed = gameComplete.Timestamp
case EventAssignmentRotation: case EventAssignmentRotation:
result.Started = true result.Started = true
assignmentRotation := event.(EventAssignmentRotation) assignmentRotation := e
if killer, ok := result.Players[assignmentRotation.Killer]; !ok { if killer, ok := result.Players[assignmentRotation.Killer]; !ok {
} else if _, ok := result.Players[assignmentRotation.Victim]; !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 player.KillWords = v
result.Players[k] = player result.Players[k] = player
} }
case EventGameReset:
return GameState{}, fmt.Errorf("not impl")
default: default:
return GameState{}, fmt.Errorf("unknown event type %T", event) 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) 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 { func (words KillWords) Empty() bool {
return words.Global == (KillWord{}) && words.Assigned.IsZero() && words.Assignee == "" && words.Assignment.Empty() return words.Global == (KillWord{}) && words.Assigned.IsZero() && words.Assignee == "" && words.Assignment.Empty()
} }