Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e68e4b48f5 | ||
|
|
91b5f2b80c |
@@ -6,10 +6,6 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type PubSub interface {
|
||||
Publish(subject string, data []byte) error
|
||||
}
|
||||
|
||||
type PlayerSession struct {
|
||||
Player *Player
|
||||
}
|
||||
@@ -26,7 +22,7 @@ type GameStore struct {
|
||||
games map[string]*GameInstance
|
||||
gamesMu sync.RWMutex
|
||||
persister Persister
|
||||
pubsub PubSub
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@@ -6,7 +6,7 @@ require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
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
|
||||
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/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.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/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
|
||||
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(),
|
||||
SessionManager: sessionManager,
|
||||
Plugins: []via.Plugin{DaisyUIPlugin},
|
||||
ContextSuspendAfter: 5 * time.Minute,
|
||||
ContextTTL: 30 * time.Minute,
|
||||
})
|
||||
|
||||
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)
|
||||
@@ -22,7 +18,7 @@ type SnakeStore struct {
|
||||
games map[string]*SnakeGameInstance
|
||||
gamesMu sync.RWMutex
|
||||
persister Persister
|
||||
pubsub PubSub
|
||||
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