Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e68e4b48f5 | ||
|
|
91b5f2b80c |
@@ -6,10 +6,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PubSub interface {
|
|
||||||
Publish(subject string, data []byte) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type PlayerSession struct {
|
type PlayerSession struct {
|
||||||
Player *Player
|
Player *Player
|
||||||
}
|
}
|
||||||
@@ -26,7 +22,7 @@ type GameStore struct {
|
|||||||
games map[string]*GameInstance
|
games map[string]*GameInstance
|
||||||
gamesMu sync.RWMutex
|
gamesMu sync.RWMutex
|
||||||
persister Persister
|
persister Persister
|
||||||
pubsub PubSub
|
notifyFunc func(gameID string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGameStore() *GameStore {
|
func NewGameStore() *GameStore {
|
||||||
@@ -39,14 +35,14 @@ func (gs *GameStore) SetPersister(p Persister) {
|
|||||||
gs.persister = p
|
gs.persister = p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gs *GameStore) SetPubSub(ps PubSub) {
|
func (gs *GameStore) SetNotifyFunc(f func(gameID string)) {
|
||||||
gs.pubsub = ps
|
gs.notifyFunc = f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gs *GameStore) makeNotify(gameID string) func() {
|
func (gs *GameStore) makeNotify(gameID string) func() {
|
||||||
return func() {
|
return func() {
|
||||||
if gs.pubsub != nil {
|
if gs.notifyFunc != nil {
|
||||||
gs.pubsub.Publish("game."+gameID, nil)
|
gs.notifyFunc(gameID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -6,7 +6,7 @@ require (
|
|||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
github.com/pressly/goose/v3 v3.26.0
|
github.com/pressly/goose/v3 v3.26.0
|
||||||
github.com/ryanhamamura/via v0.21.2
|
github.com/ryanhamamura/via v0.23.0
|
||||||
golang.org/x/crypto v0.47.0
|
golang.org/x/crypto v0.47.0
|
||||||
modernc.org/sqlite v1.44.0
|
modernc.org/sqlite v1.44.0
|
||||||
)
|
)
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -80,6 +80,8 @@ github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
|
|||||||
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
||||||
github.com/ryanhamamura/via v0.21.2 h1:osR6peY/mZSl9SNPeEv6IvzGU0akkQfQzQJgA74+7mk=
|
github.com/ryanhamamura/via v0.21.2 h1:osR6peY/mZSl9SNPeEv6IvzGU0akkQfQzQJgA74+7mk=
|
||||||
github.com/ryanhamamura/via v0.21.2/go.mod h1:rpJewNVG6tgginZN7Be3qqRuol70+v1sFCKD4UjHsQo=
|
github.com/ryanhamamura/via v0.21.2/go.mod h1:rpJewNVG6tgginZN7Be3qqRuol70+v1sFCKD4UjHsQo=
|
||||||
|
github.com/ryanhamamura/via v0.23.0 h1:0e7nytisazcWq7uxs6T27GM3FwzosCMenkxJd+78Lko=
|
||||||
|
github.com/ryanhamamura/via v0.23.0/go.mod h1:rpJewNVG6tgginZN7Be3qqRuol70+v1sFCKD4UjHsQo=
|
||||||
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
|
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
|
||||||
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
|
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
|
||||||
github.com/starfederation/datastar-go v1.0.3 h1:DnzgsJ6tDHDM6y5Nxsk0AGW/m8SyKch2vQg3P1xGTcU=
|
github.com/starfederation/datastar-go v1.0.3 h1:DnzgsJ6tDHDM6y5Nxsk0AGW/m8SyKch2vQg3P1xGTcU=
|
||||||
|
|||||||
10
main.go
10
main.go
@@ -76,15 +76,17 @@ func main() {
|
|||||||
ServerAddress: ":" + port(),
|
ServerAddress: ":" + port(),
|
||||||
SessionManager: sessionManager,
|
SessionManager: sessionManager,
|
||||||
Plugins: []via.Plugin{DaisyUIPlugin},
|
Plugins: []via.Plugin{DaisyUIPlugin},
|
||||||
ContextSuspendAfter: 5 * time.Minute,
|
|
||||||
ContextTTL: 30 * time.Minute,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
subFS, _ := fs.Sub(assets, "assets")
|
subFS, _ := fs.Sub(assets, "assets")
|
||||||
v.StaticFS("/assets/", subFS)
|
v.StaticFS("/assets/", subFS)
|
||||||
|
|
||||||
store.SetPubSub(v.PubSub())
|
store.SetNotifyFunc(func(gameID string) {
|
||||||
snakeStore.SetPubSub(v.PubSub())
|
v.PubSub().Publish("game."+gameID, nil)
|
||||||
|
})
|
||||||
|
snakeStore.SetNotifyFunc(func(gameID string) {
|
||||||
|
v.PubSub().Publish("snake."+gameID, nil)
|
||||||
|
})
|
||||||
|
|
||||||
// Home page - tabbed lobby
|
// Home page - tabbed lobby
|
||||||
v.Page("/", func(c *via.Context) {
|
v.Page("/", func(c *via.Context) {
|
||||||
|
|||||||
@@ -6,10 +6,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PubSub interface {
|
|
||||||
Publish(subject string, data []byte) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type Persister interface {
|
type Persister interface {
|
||||||
SaveSnakeGame(sg *SnakeGame) error
|
SaveSnakeGame(sg *SnakeGame) error
|
||||||
LoadSnakeGame(id string) (*SnakeGame, error)
|
LoadSnakeGame(id string) (*SnakeGame, error)
|
||||||
@@ -22,7 +18,7 @@ type SnakeStore struct {
|
|||||||
games map[string]*SnakeGameInstance
|
games map[string]*SnakeGameInstance
|
||||||
gamesMu sync.RWMutex
|
gamesMu sync.RWMutex
|
||||||
persister Persister
|
persister Persister
|
||||||
pubsub PubSub
|
notifyFunc func(gameID string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSnakeStore() *SnakeStore {
|
func NewSnakeStore() *SnakeStore {
|
||||||
@@ -35,14 +31,14 @@ func (ss *SnakeStore) SetPersister(p Persister) {
|
|||||||
ss.persister = p
|
ss.persister = p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SnakeStore) SetPubSub(ps PubSub) {
|
func (ss *SnakeStore) SetNotifyFunc(f func(gameID string)) {
|
||||||
ss.pubsub = ps
|
ss.notifyFunc = f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SnakeStore) makeNotify(gameID string) func() {
|
func (ss *SnakeStore) makeNotify(gameID string) func() {
|
||||||
return func() {
|
return func() {
|
||||||
if ss.pubsub != nil {
|
if ss.notifyFunc != nil {
|
||||||
ss.pubsub.Publish("snake."+gameID, nil)
|
ss.notifyFunc(gameID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user