forked from noxious/server
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 {
|
||||||
const message: string = input.join(' ') ?? null
|
constructor(private readonly io: Server) {}
|
||||||
if (!message) return console.log('message is required')
|
|
||||||
io.emit('notification', { message: message })
|
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 })
|
||||||
|
}
|
||||||
}
|
}
|
@ -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 {
|
||||||
console.log(ZoneManager.getLoadedZones())
|
constructor(private readonly io: Server) {}
|
||||||
|
|
||||||
|
public execute(input: CommandInput): void {
|
||||||
|
console.log(ZoneManager.getLoadedZones())
|
||||||
|
}
|
||||||
}
|
}
|
@ -2,43 +2,48 @@ 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 {
|
||||||
// Get all tiles
|
constructor(private readonly io: Server) {}
|
||||||
const tilesDir = path.join(process.cwd(), 'public', 'tiles');
|
|
||||||
const tiles = fs.readdirSync(tilesDir).filter((file) => file.endsWith('.png'));
|
|
||||||
|
|
||||||
// Create output directory if it doesn't exist
|
public async execute(): Promise<void> {
|
||||||
if (!fs.existsSync(tilesDir)) {
|
// Get all tiles
|
||||||
fs.mkdirSync(tilesDir, { recursive: true });
|
const tilesDir = path.join(process.cwd(), 'public', 'tiles');
|
||||||
}
|
const tiles = fs.readdirSync(tilesDir).filter((file) => file.endsWith('.png'));
|
||||||
|
|
||||||
for (const tile of tiles) {
|
// Create output directory if it doesn't exist
|
||||||
// Check if tile is already 66x34
|
if (!fs.existsSync(tilesDir)) {
|
||||||
const metadata = await sharp(path.join(tilesDir, tile)).metadata();
|
fs.mkdirSync(tilesDir, { recursive: true });
|
||||||
if (metadata.width === 66 && metadata.height === 34) {
|
|
||||||
commandLogger.info(`Tile ${tile} already processed`);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const inputPath = path.join(tilesDir, tile);
|
for (const tile of tiles) {
|
||||||
const outputPath = path.join(tilesDir, tile);
|
// 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 {
|
const inputPath = path.join(tilesDir, tile);
|
||||||
await sharp(inputPath)
|
const outputPath = path.join(tilesDir, tile);
|
||||||
.resize({
|
|
||||||
width: 66,
|
|
||||||
height: 34,
|
|
||||||
fit: 'fill',
|
|
||||||
kernel: 'nearest'
|
|
||||||
})
|
|
||||||
.toFile(outputPath);
|
|
||||||
|
|
||||||
commandLogger.info(`Processed: ${tile}`);
|
try {
|
||||||
} catch (error) {
|
await sharp(inputPath)
|
||||||
console.error(`Error processing ${tile}:`, error);
|
.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.');
|
||||||
|
}
|
||||||
}
|
}
|
@ -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)
|
||||||
|
|
||||||
this.registerCommand(commandName, module.default)
|
if (typeof module.default === 'function') {
|
||||||
|
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