import fs from "fs"; import path from "path"; import express, {Application} from 'express'; import {createServer as httpServer} from 'http'; import {addAuthRoutes} from './app/utilities/Http'; import cors from 'cors'; import {Server as SocketServer} from 'socket.io'; import {TSocket} from "./app/types/TSocket"; import config from './app/utilities/Config'; import prisma from './app/utilities/Prisma'; import ZoneManager from "./app/ZoneManager"; import UserManager from "./app/UserManager"; import {Authentication} from "./app/middleware/Authentication"; export class Server { private readonly app: Application; private readonly http: any; private readonly io: SocketServer; /** * Creates an instance of GameServer. */ constructor() { this.app = express(); this.app.use(cors()); this.app.use(express.json()); this.app.use(express.urlencoded({ extended: true })); this.http = httpServer(this.app) this.io = new SocketServer(this.http); this.io.use(Authentication) } /** * Start the server */ public async start() { // Print logo const art = fs.readFileSync(path.join(__dirname, 'app', 'logo.txt'), 'utf8'); console.log('\x1b[31m%s\x1b[0m', art + '\n'); // Check prisma connection try { await prisma.$connect(); console.log('[✅] Database connected'); } catch (error: any) { throw new Error(`[❌] Database connection failed: ${error.message}`); } // Start the server try { await this.http.listen(config.PORT); console.log('[✅] Socket.IO running on port', config.PORT); } catch (error: any) { throw new Error(`[❌] Socket.IO failed to start: ${error.message}`); } // Add http API routes await addAuthRoutes(this.app); // Load user manager await UserManager.boot(); // Load zone manager await ZoneManager.boot(); // Listen for socket connections this.io.on('connection', this.handleConnection.bind(this)); } /** * Handle socket connection * @param socket * @private */ private async handleConnection(socket: TSocket) { const eventsPath = path.join(__dirname, 'app', 'events'); try { const files: string[] = await fs.promises.readdir(eventsPath); for (const file of files) { const module = await import(path.join(eventsPath, file)); module.default(socket, this.io); } } catch (error: any) { throw new Error('[❌] Failed to load event handlers: ' + error.message); } } } // Start the server const server = new Server(); server.start();