refactor: extract shared player, session, and chat packages #5

Merged
ryan merged 14 commits from refactor/shared-player-session-chat into main 2026-03-03 08:50:13 +00:00
Owner

Summary

Three incremental refactors to reduce duplication and decouple chat from game logic:

  • Shared player package — Extracted player.ID type and player.GenerateID() from identical definitions in game and snake packages. Both now import from player/.
  • Session helpers — Added sessions.GetPlayerID(), GetUserID(), GetNickname() replacing the inline 10-line player-identity-resolution pattern duplicated across every handler in both game features.
  • Standalone chat package — Extracted chat.Room (NATS pub/sub + in-memory buffer), chat.Message (unified wire format), DB persistence helpers, and a single parameterized templ component. Replaces duplicated ChatMessage types, Chat() templ components, chatAutoScroll() scripts, color functions, and manual buffer management in both c4game and snakegame.

Stats

17 files changed, 412 insertions, 390 deletions (3 new packages, 2 deleted templ files)

## Summary Three incremental refactors to reduce duplication and decouple chat from game logic: - **Shared `player` package** — Extracted `player.ID` type and `player.GenerateID()` from identical definitions in `game` and `snake` packages. Both now import from `player/`. - **Session helpers** — Added `sessions.GetPlayerID()`, `GetUserID()`, `GetNickname()` replacing the inline 10-line player-identity-resolution pattern duplicated across every handler in both game features. - **Standalone `chat` package** — Extracted `chat.Room` (NATS pub/sub + in-memory buffer), `chat.Message` (unified wire format), DB persistence helpers, and a single parameterized templ component. Replaces duplicated `ChatMessage` types, `Chat()` templ components, `chatAutoScroll()` scripts, color functions, and manual buffer management in both c4game and snakegame. ## Stats 17 files changed, 412 insertions, 390 deletions (3 new packages, 2 deleted templ files)
ryan added 3 commits 2026-03-03 05:20:54 +00:00
Both game and snake packages had identical PlayerID types and the snake
package imported game.GenerateID. Now both use player.ID and
player.GenerateID from the shared player package.
Add GetPlayerID, GetUserID, GetNickname to the sessions package.
Remove the inline player-ID-from-session pattern duplicated across
every handler in c4game and snakegame, and the local getPlayerID
helper in snakegame.
refactor: extract standalone chat package from game-specific handlers
Some checks failed
CI / Deploy / test (pull_request) Failing after 11s
CI / Deploy / lint (pull_request) Successful in 25s
CI / Deploy / deploy (pull_request) Has been skipped
10de5d21ad
Create chat/ package with Message type, Room (NATS pub/sub + buffer),
DB persistence helpers, and a unified templ component parameterized by
Config (CSS prefix, post URL, color function, key propagation).

Both c4game and snakegame now use chat.Room for message management and
chatcomponents.Chat for rendering, eliminating the duplicated
ChatMessage types, chat templ components, chatAutoScroll scripts,
color functions, and inline buffer management.
ryan added 1 commit 2026-03-03 05:47:07 +00:00
fix: use format string for datastar.PostSSE in chat component
All checks were successful
CI / Deploy / test (pull_request) Successful in 16s
CI / Deploy / lint (pull_request) Successful in 24s
CI / Deploy / deploy (pull_request) Has been skipped
f71acfc73e
PostSSE requires a constant format string; pass "%s" with the URL
as an argument instead of passing the URL directly.
ryan added 1 commit 2026-03-03 06:31:07 +00:00
refactor: rename game package to connect4, drop Game prefix from types
All checks were successful
CI / Deploy / test (pull_request) Successful in 16s
CI / Deploy / lint (pull_request) Successful in 25s
CI / Deploy / deploy (pull_request) Has been skipped
38eb9ee398
Rename game/ -> connect4/ to avoid c4/game stutter. Drop redundant
Game prefix from exported types (GameStore -> Store, GameInstance ->
Instance, GameStatus -> Status). Rename NATS subjects from game.{id}
to connect4.{id}. URL routes unchanged.
ryan added 1 commit 2026-03-03 06:41:27 +00:00
refactor: rename Go module from c4 to games
All checks were successful
CI / Deploy / test (pull_request) Successful in 14s
CI / Deploy / lint (pull_request) Successful in 25s
CI / Deploy / deploy (pull_request) Has been skipped
c6885a069b
Rename module path github.com/ryanhamamura/c4 to
github.com/ryanhamamura/games across go.mod, all source files,
and golangci config.
ryan added 1 commit 2026-03-03 07:16:19 +00:00
refactor: rename remaining c4 references to games
All checks were successful
CI / Deploy / test (pull_request) Successful in 16s
CI / Deploy / lint (pull_request) Successful in 25s
CI / Deploy / deploy (pull_request) Has been skipped
6d43bdea16
Update binary name, DB path, session cookie, deploy scripts, systemd
service, Docker config, CI workflow, and .dockerignore. Remove stale
Claude command and settings files.
ryan added 1 commit 2026-03-03 07:25:08 +00:00
refactor: integrate chat persistence into Room
All checks were successful
CI / Deploy / test (pull_request) Successful in 14s
CI / Deploy / lint (pull_request) Successful in 25s
CI / Deploy / deploy (pull_request) Has been skipped
2cfd42b606
Move SaveMessage/LoadMessages logic into Room as private methods.
NewPersistentRoom auto-loads history and auto-saves on Send, removing
the need for handlers to coordinate persistence separately.
ryan added 1 commit 2026-03-03 07:30:51 +00:00
refactor: replace hardcoded NATS subjects with typed helpers
Some checks failed
CI / Deploy / test (pull_request) Successful in 14s
CI / Deploy / lint (pull_request) Failing after 21s
CI / Deploy / deploy (pull_request) Has been skipped
fb6c0e3d90
Add GameSubject/ChatSubject helpers to connect4 and snake packages,
eliminating magic string concatenation from handlers and main.go.
ryan added 1 commit 2026-03-03 07:34:50 +00:00
refactor: drop redundant WithSelectorID from SSE patches
Some checks failed
CI / Deploy / test (pull_request) Successful in 14s
CI / Deploy / lint (pull_request) Failing after 22s
CI / Deploy / deploy (pull_request) Has been skipped
42211439c9
All templ components already have id attributes on their root elements,
which PatchElementTempl uses automatically.
ryan added 1 commit 2026-03-03 07:43:30 +00:00
refactor: patch entire game content instead of individual components
Some checks failed
CI / Deploy / test (pull_request) Successful in 14s
CI / Deploy / lint (pull_request) Failing after 21s
CI / Deploy / deploy (pull_request) Has been skipped
0808c4d972
Extract GameContent from GamePage so the SSE handler can patch a single
element and let DOM morphing diff the changes, replacing the per-component
sendGameComponents helper.
ryan added 1 commit 2026-03-03 08:34:26 +00:00
refactor: patch entire game content for snake SSE handler
Some checks failed
CI / Deploy / test (pull_request) Successful in 16s
CI / Deploy / lint (pull_request) Failing after 20s
CI / Deploy / deploy (pull_request) Has been skipped
4faf4f73b0
Same approach as connect4 — extract GameContent component and patch it
as a single element, letting DOM morphing handle the diff.
ryan added 1 commit 2026-03-03 08:40:15 +00:00
refactor: replace session key strings with consts
Some checks failed
CI / Deploy / test (pull_request) Successful in 13s
CI / Deploy / lint (pull_request) Failing after 20s
CI / Deploy / deploy (pull_request) Has been skipped
dcf76bb773
Define KeyPlayerID, KeyUserID, and KeyNickname in the sessions package
and use them across all handlers to avoid duplicated magic strings.
ryan added 1 commit 2026-03-03 08:48:19 +00:00
fix: satisfy staticcheck comment style for exported consts
All checks were successful
CI / Deploy / test (pull_request) Successful in 14s
CI / Deploy / lint (pull_request) Successful in 25s
CI / Deploy / deploy (pull_request) Has been skipped
718e0c55c9
ryan merged commit d77e4af1e2 into main 2026-03-03 08:50:13 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ryan/games#5