refactor: replace hardcoded NATS subjects with typed helpers
Add GameSubject/ChatSubject helpers to connect4 and snake packages, eliminating magic string concatenation from handlers and main.go.
This commit is contained in:
@@ -6,6 +6,12 @@ import (
|
|||||||
"github.com/ryanhamamura/games/player"
|
"github.com/ryanhamamura/games/player"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NATS subject helpers.
|
||||||
|
const SubjectPrefix = "connect4"
|
||||||
|
|
||||||
|
func GameSubject(gameID string) string { return SubjectPrefix + "." + gameID }
|
||||||
|
func ChatSubject(gameID string) string { return SubjectPrefix + ".chat." + gameID }
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
ID player.ID
|
ID player.ID
|
||||||
UserID *string // UUID for authenticated users, nil for guests
|
UserID *string // UUID for authenticated users, nil for guests
|
||||||
|
|||||||
@@ -111,14 +111,14 @@ func HandleGameEvents(store *connect4.Store, nc *nats.Conn, sm *scs.SessionManag
|
|||||||
))
|
))
|
||||||
|
|
||||||
chatCfg := c4ChatConfig(gameID)
|
chatCfg := c4ChatConfig(gameID)
|
||||||
room := chat.NewPersistentRoom(nc, "connect4.chat."+gameID, queries, gameID)
|
room := chat.NewPersistentRoom(nc, connect4.ChatSubject(gameID), queries, gameID)
|
||||||
|
|
||||||
// Send initial render
|
// Send initial render
|
||||||
sendGameComponents(sse, gi, myColor, room, chatCfg)
|
sendGameComponents(sse, gi, myColor, room, chatCfg)
|
||||||
|
|
||||||
// Subscribe to game state updates
|
// Subscribe to game state updates
|
||||||
gameCh := make(chan *nats.Msg, 64)
|
gameCh := make(chan *nats.Msg, 64)
|
||||||
gameSub, err := nc.ChanSubscribe("connect4."+gameID, gameCh)
|
gameSub, err := nc.ChanSubscribe(connect4.GameSubject(gameID), gameCh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -228,7 +228,7 @@ func HandleSendChat(store *connect4.Store, nc *nats.Conn, sm *scs.SessionManager
|
|||||||
Message: signals.ChatMsg,
|
Message: signals.ChatMsg,
|
||||||
Time: time.Now().UnixMilli(),
|
Time: time.Now().UnixMilli(),
|
||||||
}
|
}
|
||||||
room := chat.NewPersistentRoom(nc, "connect4.chat."+gameID, queries, gameID)
|
room := chat.NewPersistentRoom(nc, connect4.ChatSubject(gameID), queries, gameID)
|
||||||
room.Send(msg)
|
room.Send(msg)
|
||||||
|
|
||||||
sse := datastar.NewSSE(w, r)
|
sse := datastar.NewSSE(w, r)
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ func HandleSnakeEvents(snakeStore *snake.SnakeStore, nc *nats.Conn, sm *scs.Sess
|
|||||||
|
|
||||||
// Subscribe to game updates via NATS
|
// Subscribe to game updates via NATS
|
||||||
gameCh := make(chan *nats.Msg, 64)
|
gameCh := make(chan *nats.Msg, 64)
|
||||||
gameSub, err := nc.ChanSubscribe("snake."+gameID, gameCh)
|
gameSub, err := nc.ChanSubscribe(snake.GameSubject(gameID), gameCh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -126,7 +126,7 @@ func HandleSnakeEvents(snakeStore *snake.SnakeStore, nc *nats.Conn, sm *scs.Sess
|
|||||||
var room *chat.Room
|
var room *chat.Room
|
||||||
|
|
||||||
if sg.Mode == snake.ModeMultiplayer {
|
if sg.Mode == snake.ModeMultiplayer {
|
||||||
room = chat.NewRoom(nc, "snake.chat."+gameID)
|
room = chat.NewRoom(nc, snake.ChatSubject(gameID))
|
||||||
chatCh, chatSub, err = room.Subscribe()
|
chatCh, chatSub, err = room.Subscribe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -247,7 +247,7 @@ func HandleSendChat(snakeStore *snake.SnakeStore, nc *nats.Conn, sm *scs.Session
|
|||||||
Message: signals.ChatMsg,
|
Message: signals.ChatMsg,
|
||||||
}
|
}
|
||||||
|
|
||||||
room := chat.NewRoom(nc, "snake.chat."+gameID)
|
room := chat.NewRoom(nc, snake.ChatSubject(gameID))
|
||||||
room.Send(msg)
|
room.Send(msg)
|
||||||
|
|
||||||
sse := datastar.NewSSE(w, r)
|
sse := datastar.NewSSE(w, r)
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -73,12 +73,12 @@ func run(ctx context.Context) error {
|
|||||||
// Game stores
|
// Game stores
|
||||||
store := connect4.NewStore(queries)
|
store := connect4.NewStore(queries)
|
||||||
store.SetNotifyFunc(func(gameID string) {
|
store.SetNotifyFunc(func(gameID string) {
|
||||||
nc.Publish("connect4."+gameID, nil) //nolint:errcheck // best-effort notification
|
nc.Publish(connect4.GameSubject(gameID), nil) //nolint:errcheck // best-effort notification
|
||||||
})
|
})
|
||||||
|
|
||||||
snakeStore := snake.NewSnakeStore(queries)
|
snakeStore := snake.NewSnakeStore(queries)
|
||||||
snakeStore.SetNotifyFunc(func(gameID string) {
|
snakeStore.SetNotifyFunc(func(gameID string) {
|
||||||
nc.Publish("snake."+gameID, nil) //nolint:errcheck // best-effort notification
|
nc.Publish(snake.GameSubject(gameID), nil) //nolint:errcheck // best-effort notification
|
||||||
})
|
})
|
||||||
|
|
||||||
// Router
|
// Router
|
||||||
|
|||||||
@@ -7,6 +7,12 @@ import (
|
|||||||
"github.com/ryanhamamura/games/player"
|
"github.com/ryanhamamura/games/player"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NATS subject helpers.
|
||||||
|
const SubjectPrefix = "snake"
|
||||||
|
|
||||||
|
func GameSubject(gameID string) string { return SubjectPrefix + "." + gameID }
|
||||||
|
func ChatSubject(gameID string) string { return SubjectPrefix + ".chat." + gameID }
|
||||||
|
|
||||||
type Direction int
|
type Direction int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
Reference in New Issue
Block a user