Add PostgreSQL persistence for game state and player data #137
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Replaces in-memory storage with PostgreSQL to persist game state and user data across server restarts. Games and player statistics now survive crashes and deployments.
Database Layer
users(discord_id, wallet, stats),games(uuid, state, jsonb data),game_playersjoin tablemigrate.pyhelper script for common operationsGame Persistence
Player Statistics
end_hand()Configuration
POSTGRES_*variables orDATABASE_URLconnection stringUSE_DATABASE=falsefor memory-only mode (backward compatible)Docker & Docs
.env.examplewith all configuration optionsOriginal prompt
Goal
Add PostgreSQL persistence to preserve all game state and user data across server restarts, replacing the current in-memory storage.
Requirements
1. Database Schema
Create a PostgreSQL database schema with the following tables:
users table:
id(primary key, serial)discord_id(text, unique, not null) - Discord user IDname(text, not null) - Player display namewallet(decimal, default 0) - Player's money balancegames_played(integer, default 0)games_won(integer, default 0)games_lost(integer, default 0)games_tied(integer, default 0)created_at(timestamp with time zone, default now())updated_at(timestamp with time zone, default now())games table:
id(primary key, serial)game_id(uuid, unique, not null) - The game UUID currently used in codeguild_id(bigint) - Discord guild IDchannel_id(bigint) - Discord channel IDstate(text, not null) - Game state: 'waiting', 'active', 'finished'game_data(jsonb) - Serialized game state including:current_player_idxtime_last_hand_endedtime_last_eventplayers_waiting(list of player names)dealer_hand(list of cards)deck(list of cards)discards(list of cards)created_at(timestamp with time zone, default now())updated_at(timestamp with time zone, default now())game_players table: (join table for active players in a game)
id(primary key, serial)game_id(integer, foreign key to games.id)user_id(integer, foreign key to users.id)position(integer) - Order in the players listhand(jsonb) - Current hand as list of cardsjoined_at(timestamp with time zone, default now())2. Database Connection Management
DATABASE_URL(full connection string, e.g.,postgresql://user:pass@host:port/dbname)POSTGRES_HOST,POSTGRES_PORT,POSTGRES_DB,POSTGRES_USER,POSTGRES_PASSWORDpsycopg2orasyncpgfor database connectivity3. Migration System
4. Update Player Management
Modify
cardgames/player.py:PlayerRegistryto load/save players from PostgreSQLget_player()to query the database5. Update Game State Management
Modify
cardgames/blackjack.py:Modify
cardgames/casino.py:new_game()to persist game to database6. Update Bot Game Tracking
Modify
bot.py:BlackjackGameto store database game ID7. Game State Persistence Triggers
Automatically persist game state after:
8. Startup Recovery
server.pystartup:bot.pystartup:9. Statistics Tracking
Update
cardgames/blackjack.pyend_hand()method:10. Dependencies
Add to
requirements.txt:psycopg2-binary(orasyncpgif using async)alembic(optional, for migrations)sqlalchemy(optional, if using ORM approach)11. Configuration
Add database configuration to compose files:
compose.yml12. Documentation
Update
README.md:Implementation Notes
This pull request was created from Copilot chat.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.