diff --git a/src/commands/alert.ts b/src/commands/alert.ts index 353d098..ee3302c 100644 --- a/src/commands/alert.ts +++ b/src/commands/alert.ts @@ -2,8 +2,12 @@ import { Server } from 'socket.io' type CommandInput = string[] -module.exports = function (input: CommandInput, io: Server) { - const message: string = input.join(' ') ?? null - if (!message) return console.log('message is required') - io.emit('notification', { message: message }) -} +export default class AlertCommand { + constructor(private readonly io: Server) {} + + public execute(input: CommandInput): void { + const message: string = input.join(' ') ?? null + if (!message) return console.log('message is required') + this.io.emit('notification', { message: message }) + } +} \ No newline at end of file diff --git a/src/commands/listZones.ts b/src/commands/listZones.ts index 5d98aff..4d1d2da 100644 --- a/src/commands/listZones.ts +++ b/src/commands/listZones.ts @@ -3,6 +3,10 @@ import ZoneManager from '../managers/zoneManager' type CommandInput = string[] -module.exports = function (input: CommandInput, io: Server) { - console.log(ZoneManager.getLoadedZones()) -} +export default class ListZonesCommand { + constructor(private readonly io: Server) {} + + public execute(input: CommandInput): void { + console.log(ZoneManager.getLoadedZones()) + } +} \ No newline at end of file diff --git a/src/commands/tiles.ts b/src/commands/tiles.ts index 36aa9fa..ca15b63 100644 --- a/src/commands/tiles.ts +++ b/src/commands/tiles.ts @@ -2,43 +2,48 @@ import path from 'path' import fs from 'fs' import sharp from 'sharp' import { commandLogger } from '../utilities/logger' +import { Server } from 'socket.io' -module.exports = async function () { - // Get all tiles - const tilesDir = path.join(process.cwd(), 'public', 'tiles'); - const tiles = fs.readdirSync(tilesDir).filter((file) => file.endsWith('.png')); +export default class TilesCommand { + constructor(private readonly io: Server) {} - // Create output directory if it doesn't exist - if (!fs.existsSync(tilesDir)) { - fs.mkdirSync(tilesDir, { recursive: true }); - } + public async execute(): Promise { + // Get all tiles + const tilesDir = path.join(process.cwd(), 'public', 'tiles'); + const tiles = fs.readdirSync(tilesDir).filter((file) => file.endsWith('.png')); - for (const tile of tiles) { - // Check if tile is already 66x34 - const metadata = await sharp(path.join(tilesDir, tile)).metadata(); - if (metadata.width === 66 && metadata.height === 34) { - commandLogger.info(`Tile ${tile} already processed`); - continue; + // Create output directory if it doesn't exist + if (!fs.existsSync(tilesDir)) { + fs.mkdirSync(tilesDir, { recursive: true }); } - const inputPath = path.join(tilesDir, tile); - const outputPath = path.join(tilesDir, tile); + for (const tile of tiles) { + // Check if tile is already 66x34 + const metadata = await sharp(path.join(tilesDir, tile)).metadata(); + if (metadata.width === 66 && metadata.height === 34) { + commandLogger.info(`Tile ${tile} already processed`); + continue; + } - try { - await sharp(inputPath) - .resize({ - width: 66, - height: 34, - fit: 'fill', - kernel: 'nearest' - }) - .toFile(outputPath); + const inputPath = path.join(tilesDir, tile); + const outputPath = path.join(tilesDir, tile); - commandLogger.info(`Processed: ${tile}`); - } catch (error) { - console.error(`Error processing ${tile}:`, error); + try { + await sharp(inputPath) + .resize({ + width: 66, + height: 34, + fit: 'fill', + kernel: 'nearest' + }) + .toFile(outputPath); + + commandLogger.info(`Processed: ${tile}`); + } catch (error) { + console.error(`Error processing ${tile}:`, error); + } } - } - commandLogger.info('Tile processing completed.'); + commandLogger.info('Tile processing completed.'); + } } \ No newline at end of file diff --git a/src/managers/commandManager.ts b/src/managers/commandManager.ts index e19b9e8..2d8bb6d 100644 --- a/src/managers/commandManager.ts +++ b/src/managers/commandManager.ts @@ -3,9 +3,10 @@ import * as fs from 'fs' import * as path from 'path' import { Server } from 'socket.io' import { commandLogger } from '../utilities/logger' +import config from '../utilities/config' class CommandManager { - private commands: Map = new Map() + private commands: Map = new Map() private rl: readline.Interface private io: Server | null = null private rlClosed: boolean = false @@ -40,7 +41,9 @@ class CommandManager { private async processCommand(command: string): Promise { const [cmd, ...args] = command.trim().split(' ') 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 { this.handleUnknownCommand(cmd) } @@ -72,24 +75,30 @@ class CommandManager { private async loadCommand(commandsDir: string, file: string) { try { - const ext = path.extname(file) - const commandName = path.basename(file, ext) + const extension = config.ENV === 'development' ? '.ts' : '.js' + const commandName = path.basename(file, extension) const commandPath = path.join(commandsDir, file) + + // Use dynamic import const module = await import(commandPath) - this.registerCommand(commandName, module.default) + if (typeof module.default === 'function') { + this.registerCommand(commandName, module.default) + } else { + commandLogger.warn(`Unrecognized export in ${file}`) + } } catch (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)) { commandLogger.warn(`Command '${name}' is already registered. Overwriting...`) } - this.commands.set(name, command) + this.commands.set(name, CommandClass) commandLogger.info(`Registered command: ${name}`) } } -export default new CommandManager() +export default new CommandManager() \ No newline at end of file