/** * Complete Game Migration Script * 1. Backs up existing games * 2. Removes ALL existing slot games from database * 3. Integrates new HTML5 PixiJS games from AI game builder * * Run with: node server/migrations/complete-game-migration.mjs */ import mysql from 'mysql2/promise'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); // New HTML5 PixiJS games created with AI game builder const NEW_PIXIJS_GAMES = [ { name: 'Fishing Fortune', slug: 'fishing-fortune', provider: 'CoinKrazy', rtp: 96.5, volatility: 'medium', description: 'Cast your line in this underwater adventure', imageUrl: '/games/images/fishing-fortune.png', }, { name: "Dragon's Hoard", slug: 'dragons-hoard', provider: 'CoinKrazy', rtp: 96.2, volatility: 'high', description: 'Guard the dragon\'s treasure in this epic slot', imageUrl: '/games/images/dragons-hoard.png', }, { name: 'Cosmic Reels', slug: 'cosmic-reels', provider: 'CoinKrazy', rtp: 96.8, volatility: 'medium', description: 'Spin through the cosmos for cosmic wins', imageUrl: '/games/images/cosmic-reels.png', }, { name: 'Lucky Spin', slug: 'lucky-spin', provider: 'CoinKrazy', rtp: 96.0, volatility: 'low', description: 'Simple, classic slot with big potential', imageUrl: '/games/images/lucky-spin.png', }, { name: 'Treasure Quest', slug: 'treasure-quest', provider: 'CoinKrazy', rtp: 97.0, volatility: 'high', description: 'Hunt for treasure in this adventure slot', imageUrl: '/games/images/treasure-quest.png', }, { name: 'Neon Nights', slug: 'neon-nights', provider: 'CoinKrazy', rtp: 95.8, volatility: 'medium', description: 'Experience the glow of neon in this modern slot', imageUrl: '/games/images/neon-nights.png', }, { name: 'Golden Pharaoh', slug: 'golden-pharaoh', provider: 'CoinKrazy', rtp: 96.4, volatility: 'medium', description: 'Uncover ancient Egyptian riches', imageUrl: '/games/images/golden-pharaoh.png', }, { name: 'Jungle Wild', slug: 'jungle-wild', provider: 'CoinKrazy', rtp: 96.1, volatility: 'high', description: 'Venture into the wild jungle for big wins', imageUrl: '/games/images/jungle-wild.png', }, { name: 'Starlight Dreams', slug: 'starlight-dreams', provider: 'CoinKrazy', rtp: 96.6, volatility: 'low', description: 'Dream big with starlight wins', imageUrl: '/games/images/starlight-dreams.png', }, { name: 'Mystic Forest', slug: 'mystic-forest', provider: 'CoinKrazy', rtp: 96.3, volatility: 'medium', description: 'Explore the mysteries of the enchanted forest', imageUrl: '/games/images/mystic-forest.png', }, ]; async function backupGames(conn) { console.log('\nšŸ“¦ Creating backup of existing games...'); try { const [games] = await conn.query('SELECT * FROM casino_games'); const backupDir = path.join(__dirname, '../backups'); if (!fs.existsSync(backupDir)) { fs.mkdirSync(backupDir, { recursive: true }); } const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const backupFile = path.join(backupDir, `games-backup-${timestamp}.json`); fs.writeFileSync(backupFile, JSON.stringify(games, null, 2)); console.log(`āœ“ Backup created: ${backupFile}`); console.log(`āœ“ Total games backed up: ${games.length}`); return games.length; } catch (error) { console.error('Error creating backup:', error.message); throw error; } } async function removeAllExistingGames(conn) { console.log('\nšŸ—‘ļø Removing all existing games from database...'); try { // Get count before deletion const [countBefore] = await conn.query('SELECT COUNT(*) as count FROM casino_games'); console.log(`Games before cleanup: ${countBefore[0].count}`); // Delete all games const [result] = await conn.query('DELETE FROM casino_games'); console.log(`āœ“ Deleted ${result.affectedRows} games`); // Verify deletion const [countAfter] = await conn.query('SELECT COUNT(*) as count FROM casino_games'); console.log(`Games after cleanup: ${countAfter[0].count}`); if (countAfter[0].count === 0) { console.log('āœ“ Database successfully cleared'); return true; } else { throw new Error('Database cleanup verification failed'); } } catch (error) { console.error('Error removing games:', error.message); throw error; } } async function insertNewGames(conn) { console.log('\n✨ Inserting new HTML5 PixiJS games...'); try { let insertedCount = 0; for (const game of NEW_PIXIJS_GAMES) { const query = ` INSERT INTO casino_games (title, slug, provider, category, rtp, volatility, description, thumbnailUrl, isActive, createdAt) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, NOW()) `; await conn.query(query, [ game.name, game.slug, game.provider, 'slots', game.rtp, game.volatility, game.description, game.imageUrl, ]); insertedCount++; console.log(`āœ“ Inserted: ${game.name}`); } console.log(`\nāœ“ Successfully inserted ${insertedCount} new games`); // Verify insertion const [countResult] = await conn.query('SELECT COUNT(*) as count FROM casino_games'); console.log(`Total games in database: ${countResult[0].count}`); return insertedCount; } catch (error) { console.error('Error inserting new games:', error.message); throw error; } } async function verifyMigration(conn) { console.log('\nšŸ” Verifying migration...'); try { // Check total games const [totalResult] = await conn.query('SELECT COUNT(*) as count FROM casino_games'); console.log(`Total games: ${totalResult[0].count}`); // Check provider distribution const [providerResult] = await conn.query(` SELECT provider, COUNT(*) as count FROM casino_games GROUP BY provider `); console.log('\nGames by provider:'); for (const row of providerResult) { console.log(` - ${row.provider}: ${row.count} games`); } // Check all are CoinKrazy games const [coinKrazyResult] = await conn.query(` SELECT COUNT(*) as count FROM casino_games WHERE provider = 'CoinKrazy' `); if (coinKrazyResult[0].count === NEW_PIXIJS_GAMES.length) { console.log(`\nāœ“ All ${NEW_PIXIJS_GAMES.length} CoinKrazy games are present`); return true; } else { throw new Error('Game count mismatch'); } } catch (error) { console.error('Error verifying migration:', error.message); throw error; } } async function runMigration() { const conn = await mysql.createConnection(process.env.DATABASE_URL); try { console.log('šŸš€ Starting complete game migration...'); console.log('='.repeat(60)); // Step 1: Backup const backupCount = await backupGames(conn); // Step 2: Remove all existing games await removeAllExistingGames(conn); // Step 3: Insert new games const insertedCount = await insertNewGames(conn); // Step 4: Verify migration const verified = await verifyMigration(conn); console.log('\n' + '='.repeat(60)); if (verified) { console.log('āœ… MIGRATION COMPLETED SUCCESSFULLY'); console.log(` - Backed up: ${backupCount} games`); console.log(` - Removed: ${backupCount} games`); console.log(` - Inserted: ${insertedCount} new games`); console.log(' - All games are now CoinKrazy HTML5 PixiJS games'); } else { throw new Error('Migration verification failed'); } } catch (error) { console.error('\nāŒ MIGRATION FAILED:', error.message); process.exit(1); } finally { await conn.end(); } } runMigration().catch(console.error);