refactor: deduplicate persistence, add upsert queries, throttle snake saves
Some checks failed
CI / Deploy / test (pull_request) Failing after 15s
CI / Deploy / lint (pull_request) Failing after 23s
CI / Deploy / deploy (pull_request) Has been skipped

- Replace Create+Get+Update with UpsertGame/UpsertSnakeGame queries
- Extract free functions (saveGame, loadGame, etc.) from duplicated
  receiver methods on Store and Instance types
- Remove duplicate generateID from snake package, reuse game.GenerateID
- Throttle snake game DB writes to every 2s instead of every tick
- Fix double-lock in c4game chat handler
- Update all code for sqlc pointer types (*string instead of sql.NullString)
This commit is contained in:
Ryan Hamamura
2026-03-02 16:56:29 -10:00
parent 9c3f659e96
commit bc6488f063
14 changed files with 318 additions and 494 deletions

View File

@@ -5,50 +5,56 @@
package repository
import (
"database/sql"
"time"
)
type ChatMessage struct {
ID int64
GameID string
Nickname string
Color int64
Message string
CreatedAt int64
ID int64 `db:"id" json:"id"`
GameID string `db:"game_id" json:"game_id"`
Nickname string `db:"nickname" json:"nickname"`
Color int64 `db:"color" json:"color"`
Message string `db:"message" json:"message"`
CreatedAt int64 `db:"created_at" json:"created_at"`
}
type Game struct {
ID string
Board string
CurrentTurn int64
Status int64
WinnerUserID sql.NullString
WinningCells sql.NullString
CreatedAt sql.NullTime
UpdatedAt sql.NullTime
RematchGameID sql.NullString
GameType string
GridWidth sql.NullInt64
GridHeight sql.NullInt64
MaxPlayers int64
GameMode int64
Score int64
SnakeSpeed int64
ID string `db:"id" json:"id"`
Board string `db:"board" json:"board"`
CurrentTurn int64 `db:"current_turn" json:"current_turn"`
Status int64 `db:"status" json:"status"`
WinnerUserID *string `db:"winner_user_id" json:"winner_user_id"`
WinningCells *string `db:"winning_cells" json:"winning_cells"`
CreatedAt *time.Time `db:"created_at" json:"created_at"`
UpdatedAt *time.Time `db:"updated_at" json:"updated_at"`
RematchGameID *string `db:"rematch_game_id" json:"rematch_game_id"`
GameType string `db:"game_type" json:"game_type"`
GridWidth *int64 `db:"grid_width" json:"grid_width"`
GridHeight *int64 `db:"grid_height" json:"grid_height"`
MaxPlayers int64 `db:"max_players" json:"max_players"`
GameMode int64 `db:"game_mode" json:"game_mode"`
Score int64 `db:"score" json:"score"`
SnakeSpeed int64 `db:"snake_speed" json:"snake_speed"`
}
type GamePlayer struct {
GameID string
UserID sql.NullString
GuestPlayerID sql.NullString
Nickname string
Color int64
Slot int64
CreatedAt sql.NullTime
GameID string `db:"game_id" json:"game_id"`
UserID *string `db:"user_id" json:"user_id"`
GuestPlayerID *string `db:"guest_player_id" json:"guest_player_id"`
Nickname string `db:"nickname" json:"nickname"`
Color int64 `db:"color" json:"color"`
Slot int64 `db:"slot" json:"slot"`
CreatedAt *time.Time `db:"created_at" json:"created_at"`
}
type Session struct {
Token string `db:"token" json:"token"`
Data []byte `db:"data" json:"data"`
Expiry float64 `db:"expiry" json:"expiry"`
}
type User struct {
ID string
Username string
PasswordHash string
CreatedAt sql.NullTime
ID string `db:"id" json:"id"`
Username string `db:"username" json:"username"`
PasswordHash string `db:"password_hash" json:"password_hash"`
CreatedAt *time.Time `db:"created_at" json:"created_at"`
}