feat: Enhance card metadata handling, implement persistent Scryfall caching, and update pack generation logic for new booster structure.
Some checks failed
Build and Deploy / build (push) Failing after 55s
Some checks failed
Build and Deploy / build (push) Failing after 55s
This commit is contained in:
@@ -111,7 +111,8 @@ export const CubeManager: React.FC<CubeManagerProps> = ({ packs, setPacks, onGoT
|
||||
// --- Effects ---
|
||||
useEffect(() => {
|
||||
if (rawScryfallData) {
|
||||
const result = generatorService.processCards(rawScryfallData, filters);
|
||||
// Use local images: true
|
||||
const result = generatorService.processCards(rawScryfallData, filters, true);
|
||||
setProcessedData(result);
|
||||
}
|
||||
}, [filters, rawScryfallData]);
|
||||
@@ -189,6 +190,20 @@ export const CubeManager: React.FC<CubeManagerProps> = ({ packs, setPacks, onGoT
|
||||
}
|
||||
|
||||
setRawScryfallData(expandedCards);
|
||||
|
||||
// Cache to server
|
||||
if (expandedCards.length > 0) {
|
||||
setProgress('Loading...');
|
||||
// Deduplicate for shipping to server
|
||||
const uniqueCards = Array.from(new Map(expandedCards.map(c => [c.id, c])).values());
|
||||
|
||||
await fetch('/api/cards/cache', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ cards: uniqueCards }) // Send full metadata
|
||||
});
|
||||
}
|
||||
|
||||
setLoading(false);
|
||||
setProgress('');
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ export const LobbyManager: React.FC<LobbyManagerProps> = ({ generatedPacks }) =>
|
||||
|
||||
// Transform packs to use local URLs
|
||||
// Note: For multiplayer, clients need to access this URL.
|
||||
const baseUrl = `${window.location.protocol}//${window.location.host}/cards`;
|
||||
const baseUrl = `${window.location.protocol}//${window.location.host}/cards/images`;
|
||||
|
||||
const updatedPacks = generatedPacks.map(pack => ({
|
||||
...pack,
|
||||
|
||||
@@ -79,7 +79,7 @@ export interface PackGenerationSettings {
|
||||
|
||||
export class PackGeneratorService {
|
||||
|
||||
processCards(cards: ScryfallCard[], filters: { ignoreBasicLands: boolean, ignoreCommander: boolean, ignoreTokens: boolean }): { pools: ProcessedPools, sets: SetsMap } {
|
||||
processCards(cards: ScryfallCard[], filters: { ignoreBasicLands: boolean, ignoreCommander: boolean, ignoreTokens: boolean }, useLocalImages: boolean = false): { pools: ProcessedPools, sets: SetsMap } {
|
||||
const pools: ProcessedPools = { commons: [], uncommons: [], rares: [], mythics: [], lands: [], tokens: [] };
|
||||
const setsMap: SetsMap = {};
|
||||
|
||||
@@ -104,7 +104,9 @@ export class PackGeneratorService {
|
||||
typeLine: typeLine,
|
||||
layout: layout,
|
||||
colors: cardData.colors || [],
|
||||
image: cardData.image_uris?.normal || cardData.card_faces?.[0]?.image_uris?.normal || '',
|
||||
image: useLocalImages
|
||||
? `${window.location.origin}/cards/images/${cardData.id}.jpg`
|
||||
: (cardData.image_uris?.normal || cardData.card_faces?.[0]?.image_uris?.normal || ''),
|
||||
set: cardData.set_name,
|
||||
setCode: cardData.set,
|
||||
setType: setType,
|
||||
|
||||
@@ -28,7 +28,7 @@ const PORT = process.env.PORT || 3000;
|
||||
|
||||
app.use(express.json({ limit: '50mb' })); // Increase limit for large card lists
|
||||
|
||||
// Serve static images
|
||||
// Serve static images (Nested)
|
||||
app.use('/cards', express.static(path.join(__dirname, 'public/cards')));
|
||||
|
||||
// API Routes
|
||||
@@ -51,9 +51,10 @@ app.post('/api/cards/cache', async (req: Request, res: Response) => {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Caching images for ${cards.length} cards...`);
|
||||
const count = await cardService.cacheImages(cards);
|
||||
res.json({ success: true, downloaded: count });
|
||||
console.log(`Caching images and metadata for ${cards.length} cards...`);
|
||||
const imgCount = await cardService.cacheImages(cards);
|
||||
const metaCount = await cardService.cacheMetadata(cards);
|
||||
res.json({ success: true, downloadedImages: imgCount, savedMetadata: metaCount });
|
||||
} catch (err: any) {
|
||||
console.error('Error in cache route:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
|
||||
@@ -8,9 +8,18 @@ const __dirname = path.dirname(__filename);
|
||||
const CARDS_DIR = path.join(__dirname, '../public/cards');
|
||||
|
||||
export class CardService {
|
||||
private imagesDir: string;
|
||||
private metadataDir: string;
|
||||
|
||||
constructor() {
|
||||
if (!fs.existsSync(CARDS_DIR)) {
|
||||
fs.mkdirSync(CARDS_DIR, { recursive: true });
|
||||
this.imagesDir = path.join(CARDS_DIR, 'images');
|
||||
this.metadataDir = path.join(CARDS_DIR, 'metadata');
|
||||
|
||||
if (!fs.existsSync(this.imagesDir)) {
|
||||
fs.mkdirSync(this.imagesDir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(this.metadataDir)) {
|
||||
fs.mkdirSync(this.metadataDir, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +47,7 @@ export class CardService {
|
||||
|
||||
if (!imageUrl) continue;
|
||||
|
||||
const filePath = path.join(CARDS_DIR, `${uuid}.jpg`);
|
||||
const filePath = path.join(this.imagesDir, `${uuid}.jpg`);
|
||||
|
||||
if (fs.existsSync(filePath)) {
|
||||
// Already cached
|
||||
@@ -67,4 +76,21 @@ export class CardService {
|
||||
|
||||
return downloadedCount;
|
||||
}
|
||||
|
||||
async cacheMetadata(cards: any[]): Promise<number> {
|
||||
let cachedCount = 0;
|
||||
for (const card of cards) {
|
||||
if (!card.id) continue;
|
||||
const filePath = path.join(this.metadataDir, `${card.id}.json`);
|
||||
if (!fs.existsSync(filePath)) {
|
||||
try {
|
||||
fs.writeFileSync(filePath, JSON.stringify(card, null, 2));
|
||||
cachedCount++;
|
||||
} catch (e) {
|
||||
console.error(`Failed to save metadata for ${card.id}`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return cachedCount;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user