- 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)
48 lines
1.5 KiB
SQL
48 lines
1.5 KiB
SQL
-- name: UpsertGame :exec
|
|
INSERT INTO games (id, board, current_turn, status, game_type, winner_user_id, winning_cells, rematch_game_id)
|
|
VALUES (?, ?, ?, ?, 'connect4', ?, ?, ?)
|
|
ON CONFLICT(id) DO UPDATE SET
|
|
board = excluded.board,
|
|
current_turn = excluded.current_turn,
|
|
status = excluded.status,
|
|
winner_user_id = excluded.winner_user_id,
|
|
winning_cells = excluded.winning_cells,
|
|
rematch_game_id = excluded.rematch_game_id,
|
|
updated_at = CURRENT_TIMESTAMP;
|
|
|
|
-- name: GetGame :one
|
|
SELECT * FROM games WHERE id = ?;
|
|
|
|
-- name: DeleteGame :exec
|
|
DELETE FROM games WHERE id = ?;
|
|
|
|
-- name: GetActiveGames :many
|
|
SELECT * FROM games WHERE game_type = 'connect4' AND status < 2;
|
|
|
|
-- name: CreateGamePlayer :exec
|
|
INSERT INTO game_players (game_id, user_id, guest_player_id, nickname, color, slot)
|
|
VALUES (?, ?, ?, ?, ?, ?);
|
|
|
|
-- name: GetGamePlayers :many
|
|
SELECT * FROM game_players WHERE game_id = ?;
|
|
|
|
-- name: GetGamesByUserID :many
|
|
SELECT g.* FROM games g
|
|
JOIN game_players gp ON g.id = gp.game_id
|
|
WHERE gp.user_id = ?
|
|
ORDER BY g.updated_at DESC;
|
|
|
|
-- name: GetUserActiveGames :many
|
|
SELECT
|
|
g.id,
|
|
g.status,
|
|
g.current_turn,
|
|
g.updated_at,
|
|
gp_user.color as my_color,
|
|
gp_opponent.nickname as opponent_nickname
|
|
FROM games g
|
|
JOIN game_players gp_user ON g.id = gp_user.game_id AND gp_user.user_id = ?
|
|
LEFT JOIN game_players gp_opponent ON g.id = gp_opponent.game_id AND gp_opponent.slot != gp_user.slot
|
|
WHERE g.game_type = 'connect4' AND g.status < 2
|
|
ORDER BY g.updated_at DESC;
|