NPM update, removed CRUD functions from object repository, added prettier
This commit is contained in:
@ -1,98 +1,95 @@
|
||||
import * as readline from 'readline';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { Server } from 'socket.io';
|
||||
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;
|
||||
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
|
||||
});
|
||||
constructor() {
|
||||
this.rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
})
|
||||
|
||||
this.rl.on('close', () => {
|
||||
this.rlClosed = true;
|
||||
});
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
public async boot(io: Server) {
|
||||
this.io = io;
|
||||
await this.loadCommands();
|
||||
console.log('[✅] Command manager loaded');
|
||||
this.startPrompt();
|
||||
private handleUnknownCommand(command: string) {
|
||||
switch (command) {
|
||||
case 'exit':
|
||||
console.log('Goodbye!')
|
||||
this.rl.close()
|
||||
break
|
||||
default:
|
||||
console.error(`Unknown command: ${command}`)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
private startPrompt() {
|
||||
if (this.rlClosed) return;
|
||||
private async loadCommands() {
|
||||
const commandsDir = path.resolve(__dirname, 'commands')
|
||||
try {
|
||||
const files: string[] = await fs.promises.readdir(commandsDir)
|
||||
|
||||
this.rl.question('> ', (command: string) => {
|
||||
this.processCommand(command);
|
||||
this.startPrompt();
|
||||
});
|
||||
for (const file of files) {
|
||||
await this.loadCommand(commandsDir, file)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[❌] Failed to read commands directory:', error)
|
||||
}
|
||||
}
|
||||
|
||||
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 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 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}`);
|
||||
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();
|
||||
export default new CommandManager()
|
||||
|
@ -1,37 +1,37 @@
|
||||
import {User} from "@prisma/client";
|
||||
import { User } from '@prisma/client'
|
||||
|
||||
type TLoggedInUsers = {
|
||||
users: User[];
|
||||
users: User[]
|
||||
}
|
||||
|
||||
class UserManager {
|
||||
private loggedInUsers: TLoggedInUsers[] = [];
|
||||
private loggedInUsers: TLoggedInUsers[] = []
|
||||
|
||||
// Method to initialize user manager
|
||||
public async boot() {
|
||||
console.log('[✅] User manager loaded');
|
||||
}
|
||||
// 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 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 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;
|
||||
});
|
||||
}
|
||||
// Function that lists all logged in users
|
||||
public listUsers() {
|
||||
return this.loggedInUsers.map((loggedInUser) => {
|
||||
return loggedInUser.users
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default new UserManager();
|
||||
export default new UserManager()
|
||||
|
@ -1,93 +1,93 @@
|
||||
import {Character, Zone} from "@prisma/client";
|
||||
import ZoneRepository from "../repositories/ZoneRepository";
|
||||
import ZoneService from "../services/ZoneService";
|
||||
import { Character, Zone } from '@prisma/client'
|
||||
import ZoneRepository from '../repositories/ZoneRepository'
|
||||
import ZoneService from '../services/ZoneService'
|
||||
|
||||
type TLoadedZone = {
|
||||
zone: Zone;
|
||||
characters: Character[];
|
||||
zone: Zone
|
||||
characters: Character[]
|
||||
}
|
||||
|
||||
class ZoneManager {
|
||||
private loadedZones: TLoadedZone[] = [];
|
||||
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 initialize zone manager
|
||||
public async boot() {
|
||||
if (!(await ZoneRepository.getById(1))) {
|
||||
const zoneService = new ZoneService()
|
||||
await zoneService.createDemoZone()
|
||||
}
|
||||
|
||||
// Method to handle individual zone loading
|
||||
public loadZone(zone: Zone) {
|
||||
this.loadedZones.push({
|
||||
zone,
|
||||
characters: []
|
||||
});
|
||||
console.log(`[✅] Zone ID ${zone.id} loaded`);
|
||||
const zones = await ZoneRepository.getAll()
|
||||
|
||||
for (const zone of zones) {
|
||||
this.loadZone(zone)
|
||||
}
|
||||
|
||||
// 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`);
|
||||
}
|
||||
console.log('[✅] Zone manager loaded')
|
||||
}
|
||||
|
||||
// Getter for loaded zones
|
||||
public getLoadedZones(): TLoadedZone[] {
|
||||
return this.loadedZones;
|
||||
}
|
||||
// Method to handle individual zone loading
|
||||
public loadZone(zone: Zone) {
|
||||
this.loadedZones.push({
|
||||
zone,
|
||||
characters: []
|
||||
})
|
||||
console.log(`[✅] Zone ID ${zone.id} loaded`)
|
||||
}
|
||||
|
||||
public addCharacterToZone(zoneId: number, character: Character) {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId;
|
||||
});
|
||||
if (loadedZone) {
|
||||
loadedZone.characters.push(character);
|
||||
}
|
||||
}
|
||||
// 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`)
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
// Getter for loaded zones
|
||||
public getLoadedZones(): TLoadedZone[] {
|
||||
return this.loadedZones
|
||||
}
|
||||
|
||||
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 addCharacterToZone(zoneId: number, character: Character) {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId
|
||||
})
|
||||
if (loadedZone) {
|
||||
loadedZone.characters.push(character)
|
||||
}
|
||||
}
|
||||
|
||||
public getCharactersInZone(zoneId: number): Character[] {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId;
|
||||
});
|
||||
return loadedZone ? loadedZone.characters : [];
|
||||
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;
|
||||
export default new ZoneManager()
|
||||
|
Reference in New Issue
Block a user