Update command manager and commands to OOP
This commit is contained in:
parent
3ec4bc2557
commit
6ac827630a
@ -2,8 +2,12 @@ import { Server } from 'socket.io'
|
|||||||
|
|
||||||
type CommandInput = string[]
|
type CommandInput = string[]
|
||||||
|
|
||||||
module.exports = function (input: CommandInput, io: Server) {
|
export default class AlertCommand {
|
||||||
|
constructor(private readonly io: Server) {}
|
||||||
|
|
||||||
|
public execute(input: CommandInput): void {
|
||||||
const message: string = input.join(' ') ?? null
|
const message: string = input.join(' ') ?? null
|
||||||
if (!message) return console.log('message is required')
|
if (!message) return console.log('message is required')
|
||||||
io.emit('notification', { message: message })
|
this.io.emit('notification', { message: message })
|
||||||
|
}
|
||||||
}
|
}
|
@ -3,6 +3,10 @@ import ZoneManager from '../managers/zoneManager'
|
|||||||
|
|
||||||
type CommandInput = string[]
|
type CommandInput = string[]
|
||||||
|
|
||||||
module.exports = function (input: CommandInput, io: Server) {
|
export default class ListZonesCommand {
|
||||||
|
constructor(private readonly io: Server) {}
|
||||||
|
|
||||||
|
public execute(input: CommandInput): void {
|
||||||
console.log(ZoneManager.getLoadedZones())
|
console.log(ZoneManager.getLoadedZones())
|
||||||
|
}
|
||||||
}
|
}
|
@ -2,8 +2,12 @@ import path from 'path'
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import sharp from 'sharp'
|
import sharp from 'sharp'
|
||||||
import { commandLogger } from '../utilities/logger'
|
import { commandLogger } from '../utilities/logger'
|
||||||
|
import { Server } from 'socket.io'
|
||||||
|
|
||||||
module.exports = async function () {
|
export default class TilesCommand {
|
||||||
|
constructor(private readonly io: Server) {}
|
||||||
|
|
||||||
|
public async execute(): Promise<void> {
|
||||||
// Get all tiles
|
// Get all tiles
|
||||||
const tilesDir = path.join(process.cwd(), 'public', 'tiles');
|
const tilesDir = path.join(process.cwd(), 'public', 'tiles');
|
||||||
const tiles = fs.readdirSync(tilesDir).filter((file) => file.endsWith('.png'));
|
const tiles = fs.readdirSync(tilesDir).filter((file) => file.endsWith('.png'));
|
||||||
@ -41,4 +45,5 @@ module.exports = async function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
commandLogger.info('Tile processing completed.');
|
commandLogger.info('Tile processing completed.');
|
||||||
|
}
|
||||||
}
|
}
|
@ -3,9 +3,10 @@ import * as fs from 'fs'
|
|||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import { Server } from 'socket.io'
|
import { Server } from 'socket.io'
|
||||||
import { commandLogger } from '../utilities/logger'
|
import { commandLogger } from '../utilities/logger'
|
||||||
|
import config from '../utilities/config'
|
||||||
|
|
||||||
class CommandManager {
|
class CommandManager {
|
||||||
private commands: Map<string, Function> = new Map()
|
private commands: Map<string, any> = new Map()
|
||||||
private rl: readline.Interface
|
private rl: readline.Interface
|
||||||
private io: Server | null = null
|
private io: Server | null = null
|
||||||
private rlClosed: boolean = false
|
private rlClosed: boolean = false
|
||||||
@ -40,7 +41,9 @@ class CommandManager {
|
|||||||
private async processCommand(command: string): Promise<void> {
|
private async processCommand(command: string): Promise<void> {
|
||||||
const [cmd, ...args] = command.trim().split(' ')
|
const [cmd, ...args] = command.trim().split(' ')
|
||||||
if (this.commands.has(cmd)) {
|
if (this.commands.has(cmd)) {
|
||||||
this.commands.get(cmd)?.(args, this.io as Server)
|
const CommandClass = this.commands.get(cmd)
|
||||||
|
const commandInstance = new CommandClass(this.io as Server)
|
||||||
|
await commandInstance.execute(args)
|
||||||
} else {
|
} else {
|
||||||
this.handleUnknownCommand(cmd)
|
this.handleUnknownCommand(cmd)
|
||||||
}
|
}
|
||||||
@ -72,22 +75,28 @@ class CommandManager {
|
|||||||
|
|
||||||
private async loadCommand(commandsDir: string, file: string) {
|
private async loadCommand(commandsDir: string, file: string) {
|
||||||
try {
|
try {
|
||||||
const ext = path.extname(file)
|
const extension = config.ENV === 'development' ? '.ts' : '.js'
|
||||||
const commandName = path.basename(file, ext)
|
const commandName = path.basename(file, extension)
|
||||||
const commandPath = path.join(commandsDir, file)
|
const commandPath = path.join(commandsDir, file)
|
||||||
|
|
||||||
|
// Use dynamic import
|
||||||
const module = await import(commandPath)
|
const module = await import(commandPath)
|
||||||
|
|
||||||
|
if (typeof module.default === 'function') {
|
||||||
this.registerCommand(commandName, module.default)
|
this.registerCommand(commandName, module.default)
|
||||||
|
} else {
|
||||||
|
commandLogger.warn(`Unrecognized export in ${file}`)
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
commandLogger.error(`Failed to load command: ${file}: ${error}`)
|
commandLogger.error(`Failed to load command: ${file}: ${error}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerCommand(name: string, command: (args: string[], io: Server) => void) {
|
private registerCommand(name: string, CommandClass: any) {
|
||||||
if (this.commands.has(name)) {
|
if (this.commands.has(name)) {
|
||||||
commandLogger.warn(`Command '${name}' is already registered. Overwriting...`)
|
commandLogger.warn(`Command '${name}' is already registered. Overwriting...`)
|
||||||
}
|
}
|
||||||
this.commands.set(name, command)
|
this.commands.set(name, CommandClass)
|
||||||
commandLogger.info(`Registered command: ${name}`)
|
commandLogger.info(`Registered command: ${name}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user