Files
FaroeGames 6b693cc3c6 Add Legends: Arceus tracker and bundle offline sprites on dev
Add standalone Pokémon Legends: Arceus POC tracker (Hisui 242 Pokédex, Research Tasks & RL10 mode, Satchel MP & Save Mythicals toggles, Noble/story battles, saves/movesets), bundle local sprite packs across all three trackers into Git/Docker/Nginx with attribution credits, and update Hub landing page & unified server/tests.
2026-11-17 17:53:59 -07:00

86 lines
2.6 KiB
JavaScript

import http from 'node:http';
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const MIME_TYPES = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.mjs': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
};
async function dirExists(dirPath) {
try {
const stat = await fs.stat(dirPath);
return stat.isDirectory();
} catch {
return false;
}
}
export async function createServer({ rootDir = __dirname } = {}) {
const resolvedRoot = path.resolve(rootDir);
return http.createServer(async (req, res) => {
try {
const rawPath = (req.url || '/').split('?')[0];
let pathname = decodeURIComponent(rawPath);
if (pathname === '/api/assets-status') {
const hasLocalSprites = await dirExists(path.join(resolvedRoot, 'assets/sprites/pokemon'));
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify({ hasLocalSprites }));
return;
}
if (pathname === '/') {
pathname = '/index.html';
}
const fullPath = path.resolve(resolvedRoot, `.${pathname}`);
if (!fullPath.startsWith(resolvedRoot + path.sep) && fullPath !== resolvedRoot) {
res.writeHead(403, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Forbidden');
return;
}
const data = await fs.readFile(fullPath);
const ext = path.extname(fullPath).toLowerCase();
res.writeHead(200, {
'Content-Type': MIME_TYPES[ext] || 'application/octet-stream',
'Cache-Control': 'no-cache',
});
res.end(data);
} catch (err) {
if (err && (err.code === 'ENOENT' || err.code === 'EISDIR')) {
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Not Found');
return;
}
res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Internal Server Error');
}
});
}
const isDirectRun =
process.argv[1] && path.resolve(process.argv[1]) === path.resolve(__filename);
if (isDirectRun) {
const PORT = Number(process.env.PORT || 4175);
const HOST = process.env.HOST || '127.0.0.1';
const server = await createServer();
server.listen(PORT, HOST, () => {
console.log(`Legends: Arceus Oak Tracker running at http://${HOST}:${PORT}`);
});
}