1
0
forked from noxious/server

code update

This commit is contained in:
2024-05-09 22:03:21 +02:00
parent 8356a83431
commit a2f21229d8
14 changed files with 216 additions and 45 deletions

86
src/server.ts Normal file
View File

@ -0,0 +1,86 @@
import fs from "fs";
import path from "path";
import express from 'express';
import http from 'http';
import cors from 'cors';
import {Server as HttpServer, Socket} from 'socket.io';
import config from './app/utilities/config';
import prisma from './app/utilities/prisma';
import api from "./app/utilities/api";
import ZoneManager from "./app/ZoneManager";
export class Server
{
private readonly app: express.Application;
private readonly server: any;
private readonly io: HttpServer;
/**
* 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.server = http.createServer(this.app)
this.io = new HttpServer(this.server);
}
/**
* 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.server.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 API routes
await api.addAuthRoutes(this.app);
// Load zone manager
const zoneManager = ZoneManager.getInstance();
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: Socket) {
const eventsPath = path.join(__dirname, '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();