Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e68e4b48f5 |
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
8
main.go
8
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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user