diff --git a/game/store.go b/game/store.go index cd5ddc6..9e7aae8 100644 --- a/game/store.go +++ b/game/store.go @@ -6,10 +6,6 @@ import ( "sync" ) -type PubSub interface { - Publish(subject string, data []byte) error -} - type PlayerSession struct { Player *Player } @@ -25,8 +21,8 @@ type Persister interface { type GameStore struct { games map[string]*GameInstance gamesMu sync.RWMutex - persister Persister - pubsub PubSub + persister Persister + notifyFunc func(gameID string) } func NewGameStore() *GameStore { @@ -39,14 +35,14 @@ func (gs *GameStore) SetPersister(p Persister) { gs.persister = p } -func (gs *GameStore) SetPubSub(ps PubSub) { - gs.pubsub = ps +func (gs *GameStore) SetNotifyFunc(f func(gameID string)) { + gs.notifyFunc = f } func (gs *GameStore) makeNotify(gameID string) func() { return func() { - if gs.pubsub != nil { - gs.pubsub.Publish("game."+gameID, nil) + if gs.notifyFunc != nil { + gs.notifyFunc(gameID) } } } diff --git a/main.go b/main.go index 848326d..fa6bce1 100644 --- a/main.go +++ b/main.go @@ -81,8 +81,12 @@ func main() { subFS, _ := fs.Sub(assets, "assets") v.StaticFS("/assets/", subFS) - store.SetPubSub(v.PubSub()) - snakeStore.SetPubSub(v.PubSub()) + store.SetNotifyFunc(func(gameID string) { + v.PubSub().Publish("game."+gameID, nil) + }) + snakeStore.SetNotifyFunc(func(gameID string) { + v.PubSub().Publish("snake."+gameID, nil) + }) // Home page - tabbed lobby v.Page("/", func(c *via.Context) { diff --git a/snake/store.go b/snake/store.go index ea90557..fac2574 100644 --- a/snake/store.go +++ b/snake/store.go @@ -6,10 +6,6 @@ import ( "sync" ) -type PubSub interface { - Publish(subject string, data []byte) error -} - type Persister interface { SaveSnakeGame(sg *SnakeGame) error LoadSnakeGame(id string) (*SnakeGame, error) @@ -21,8 +17,8 @@ type Persister interface { type SnakeStore struct { games map[string]*SnakeGameInstance gamesMu sync.RWMutex - persister Persister - pubsub PubSub + persister Persister + notifyFunc func(gameID string) } func NewSnakeStore() *SnakeStore { @@ -35,14 +31,14 @@ func (ss *SnakeStore) SetPersister(p Persister) { ss.persister = p } -func (ss *SnakeStore) SetPubSub(ps PubSub) { - ss.pubsub = ps +func (ss *SnakeStore) SetNotifyFunc(f func(gameID string)) { + ss.notifyFunc = f } func (ss *SnakeStore) makeNotify(gameID string) func() { return func() { - if ss.pubsub != nil { - ss.pubsub.Publish("snake."+gameID, nil) + if ss.notifyFunc != nil { + ss.notifyFunc(gameID) } } }