forked from noxious/server
Improved folder structures for better maintainability
This commit is contained in:
98
src/managers/CommandManager.ts
Normal file
98
src/managers/CommandManager.ts
Normal file
@ -0,0 +1,98 @@
|
||||
import * as readline from 'readline';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { Server } from 'socket.io';
|
||||
|
||||
class CommandManager {
|
||||
private commands: Map<string, Function> = new Map();
|
||||
private rl: readline.Interface;
|
||||
private io: Server | null = null;
|
||||
private rlClosed: boolean = false;
|
||||
|
||||
constructor() {
|
||||
this.rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
this.rl.on('close', () => {
|
||||
this.rlClosed = true;
|
||||
});
|
||||
}
|
||||
|
||||
public async boot(io: Server) {
|
||||
this.io = io;
|
||||
await this.loadCommands();
|
||||
console.log('[✅] Command manager loaded');
|
||||
this.startPrompt();
|
||||
}
|
||||
|
||||
private startPrompt() {
|
||||
if (this.rlClosed) return;
|
||||
|
||||
this.rl.question('> ', (command: string) => {
|
||||
this.processCommand(command);
|
||||
this.startPrompt();
|
||||
});
|
||||
}
|
||||
|
||||
private async processCommand(command: string): Promise<void> {
|
||||
const [cmd, ...args] = command.trim().split(' ');
|
||||
if (this.commands.has(cmd)) {
|
||||
this.commands.get(cmd)?.(args, this.io as Server);
|
||||
} else {
|
||||
this.handleUnknownCommand(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
private handleUnknownCommand(command: string) {
|
||||
switch (command) {
|
||||
case 'exit':
|
||||
console.log('Goodbye!');
|
||||
this.rl.close();
|
||||
break;
|
||||
default:
|
||||
console.error(`Unknown command: ${command}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async loadCommands() {
|
||||
const commandsDir = path.resolve(__dirname, 'commands');
|
||||
try {
|
||||
const files: string[] = await fs.promises.readdir(commandsDir);
|
||||
|
||||
for (const file of files) {
|
||||
await this.loadCommand(commandsDir, file);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[❌] Failed to read commands directory:', error);
|
||||
}
|
||||
}
|
||||
|
||||
private async loadCommand(commandsDir: string, file: string) {
|
||||
try {
|
||||
const ext = path.extname(file);
|
||||
const commandName = path.basename(file, ext);
|
||||
const commandPath = path.join(commandsDir, file);
|
||||
const module = await import(commandPath);
|
||||
|
||||
this.registerCommand(commandName, module.default);
|
||||
} catch (error) {
|
||||
console.error('[❌] Failed to load command:', file, error);
|
||||
}
|
||||
}
|
||||
|
||||
private registerCommand(
|
||||
name: string,
|
||||
command: (args: string[], io: Server) => void
|
||||
) {
|
||||
if (this.commands.has(name)) {
|
||||
console.warn(`Command '${name}' is already registered. Overwriting...`);
|
||||
}
|
||||
this.commands.set(name, command);
|
||||
console.log(`Registered command: ${name}`);
|
||||
}
|
||||
}
|
||||
|
||||
export default new CommandManager();
|
37
src/managers/UserManager.ts
Normal file
37
src/managers/UserManager.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import {User} from "@prisma/client";
|
||||
|
||||
type TLoggedInUsers = {
|
||||
users: User[];
|
||||
}
|
||||
|
||||
class UserManager {
|
||||
private loggedInUsers: TLoggedInUsers[] = [];
|
||||
|
||||
// Method to initialize user manager
|
||||
public async boot() {
|
||||
console.log('[✅] User manager loaded');
|
||||
}
|
||||
|
||||
// Function that adds user to logged in users
|
||||
public loginUser(user: User) {
|
||||
this.loggedInUsers.push({
|
||||
users: [user]
|
||||
});
|
||||
}
|
||||
|
||||
// Function that checks if a user is already logged in
|
||||
public findUser(user: User) {
|
||||
return this.loggedInUsers.find((loggedInUser) => {
|
||||
return loggedInUser.users.includes(user);
|
||||
});
|
||||
}
|
||||
|
||||
// Function that lists all logged in users
|
||||
public listUsers() {
|
||||
return this.loggedInUsers.map((loggedInUser) => {
|
||||
return loggedInUser.users;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default new UserManager();
|
93
src/managers/ZoneManager.ts
Normal file
93
src/managers/ZoneManager.ts
Normal file
@ -0,0 +1,93 @@
|
||||
import {Character, Zone} from "@prisma/client";
|
||||
import ZoneRepository from "../repositories/ZoneRepository";
|
||||
import ZoneService from "../services/ZoneService";
|
||||
|
||||
type TLoadedZone = {
|
||||
zone: Zone;
|
||||
characters: Character[];
|
||||
}
|
||||
|
||||
class ZoneManager {
|
||||
private loadedZones: TLoadedZone[] = [];
|
||||
|
||||
// Method to initialize zone manager
|
||||
public async boot() {
|
||||
if (!await ZoneRepository.getById(1)) {
|
||||
const zoneService = new ZoneService();
|
||||
await zoneService.createDemoZone();
|
||||
}
|
||||
|
||||
const zones = await ZoneRepository.getAll();
|
||||
|
||||
for (const zone of zones) {
|
||||
this.loadZone(zone);
|
||||
}
|
||||
|
||||
console.log('[✅] Zone manager loaded');
|
||||
}
|
||||
|
||||
// Method to handle individual zone loading
|
||||
public loadZone(zone: Zone) {
|
||||
this.loadedZones.push({
|
||||
zone,
|
||||
characters: []
|
||||
});
|
||||
console.log(`[✅] Zone ID ${zone.id} loaded`);
|
||||
}
|
||||
|
||||
// Method to handle individual zone unloading
|
||||
public unloadZone(zoneId: number) {
|
||||
this.loadedZones = this.loadedZones.filter((loadedZone) => {
|
||||
return loadedZone.zone.id !== zoneId;
|
||||
});
|
||||
console.log(`[❌] Zone ID ${zoneId} unloaded`);
|
||||
}
|
||||
|
||||
// Getter for loaded zones
|
||||
public getLoadedZones(): TLoadedZone[] {
|
||||
return this.loadedZones;
|
||||
}
|
||||
|
||||
public addCharacterToZone(zoneId: number, character: Character) {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId;
|
||||
});
|
||||
if (loadedZone) {
|
||||
loadedZone.characters.push(character);
|
||||
}
|
||||
}
|
||||
|
||||
public removeCharacterFromZone(zoneId: number, character: Character) {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId;
|
||||
});
|
||||
if (loadedZone) {
|
||||
loadedZone.characters = loadedZone.characters.filter((loadedCharacter) => {
|
||||
return loadedCharacter.id !== character.id;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public updateCharacterInZone(zoneId: number, character: Character) {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId;
|
||||
});
|
||||
if (loadedZone) {
|
||||
const characterIndex = loadedZone.characters.findIndex((loadedCharacter) => {
|
||||
return loadedCharacter.id === character.id;
|
||||
});
|
||||
if (characterIndex !== -1) {
|
||||
loadedZone.characters[characterIndex] = character;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public getCharactersInZone(zoneId: number): Character[] {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId;
|
||||
});
|
||||
return loadedZone ? loadedZone.characters : [];
|
||||
}
|
||||
}
|
||||
|
||||
export default new ZoneManager;
|
Reference in New Issue
Block a user