import { createConnection } from "mysql2/promise"; import { readdir, access } from "fs/promises"; import { join } from "path"; import "dotenv/config"; const GAMES_DIR = "/home/ubuntu/webdev-static-assets/games"; async function main() { const conn = await createConnection(process.env.DATABASE_URL); // Get all game folders that have index.html const gameFolders = await readdir(GAMES_DIR); const playableGames = []; for (const folder of gameFolders) { try { await access(join(GAMES_DIR, folder, "index.html")); playableGames.push(folder); } catch { // no index.html } } console.log(`Found ${playableGames.length} playable games`); // Update each game's gameUrl in the database let updated = 0; let notFound = 0; for (const folder of playableGames) { const gameUrl = `/games/${folder}/index.html`; const [result] = await conn.execute( "UPDATE all_games SET gameUrl = ? WHERE gameId = ?", [gameUrl, folder] ); if (result.affectedRows > 0) { updated++; } else { notFound++; // Try partial match - some game IDs might be lowercase or have different casing const [result2] = await conn.execute( "UPDATE all_games SET gameUrl = ? WHERE LOWER(gameId) = LOWER(?)", [gameUrl, folder] ); if (result2.affectedRows > 0) { updated++; notFound--; } } } console.log(`Updated: ${updated}, Not found in DB: ${notFound}`); // Verify const [rows] = await conn.execute("SELECT COUNT(*) as cnt FROM all_games WHERE gameUrl IS NOT NULL"); console.log(`Games with gameUrl in DB: ${rows[0].cnt}`); // Show sample const [samples] = await conn.execute("SELECT gameId, gameName, gameUrl FROM all_games WHERE gameUrl IS NOT NULL LIMIT 5"); console.log("Sample:", samples); await conn.end(); } main().catch(console.error);