2.0 KiB
2.0 KiB
Image Caching Implementation
Objective
Implement a robust image caching system that downloads card images to the server when creating a draft room, ensuring all players can see images reliably via local serving.
Changes
-
Backend - Image Service (
src/server/services/CardService.ts):- Created
CardServiceclass. - Implements
cacheImageswhich downloads images from external URLs tosrc/server/public/cards. - Uses a concurrency limit (5) to avoid rate limiting.
- Checks for existence before downloading to avoid redundant work.
- Created
-
Backend - Server Setup (
src/server/index.ts):- Enabled static file serving for
/cardsendpoint mapping tosrc/server/public/cards. - Added
POST /api/cards/cacheendpoint that accepts a list of cards and triggers cache logic. - Increased JSON body limit to 50mb to handle large set payloads.
- Enabled static file serving for
-
Frontend - Lobby Manager (
LobbyManager.tsx):- Updated
handleCreateRoomworkflow. - Pre-Creation: Extracts all unique cards from generated packs.
- Cache Request: Sends list to
/api/cards/cache. - Transformation: Updates local pack data to point
imageproperty to the local server URL (/cards/{scryfallId}.jpg) instead of remote Scryfall URL. - This ensures that when
create_roomis emitted, the room state on the server (and thus all connected clients) contains valid local URLs.
- Updated
-
Fixes:
- Addressed
GameRoom.tsxcrash by replacingrequirewith dynamic imports (or static if preloaded) and fixing clipboard access. - Fixed TS imports in server index.
- Addressed
Status
- Image Caching: Functional. Creating a room now triggers a download process on the terminal.
- Local Serving: Cards should now load instantly from the server for all peers.
How to Verify
- Generate packs in Draft Management.
- Create a Room. Watch server logs for "Cached image: ..." messages.
- Join room.
- Start Draft. Images should appear.