import { SocketEvent } from '#application/enums'; import type { MapCacheT } from '#entities/map' import { BaseEvent } from '#application/base/baseEvent' import { Map } from '#entities/map' type Payload = { name: string width: number height: number } export default class MapCreateEvent extends BaseEvent { public listen(): void { this.socket.on(SocketEvent.GM_MAP_CREATE, this.handleEvent.bind(this)) } private async handleEvent(data: Payload, callback: (response: MapCacheT | false) => void): Promise { try { if (!(await this.isCharacterGM())) return this.logger.info(`GM ${(await this.getCharacter())!.getId()} has created a new map via map editor.`) if (data.name === '') { this.socket.emit(SocketEvent.NOTIFICATION, { title: 'Error', message: 'Map name cannot be empty.' }) return callback(false) } if (data.width < 1 || data.height < 1) { this.socket.emit(SocketEvent.NOTIFICATION, { title: 'Error', message: 'Map width and height must be greater than 0.' }) return callback(false) } const map = new Map() await map .setName(data.name) .setWidth(data.width) .setHeight(data.height) .setTiles(Array.from({ length: data.height }, () => Array.from({ length: data.width }, () => 'blank_tile'))) .save() return callback(await map.cache()) } catch (error: any) { this.logger.error('gm:map:create error', error.message) this.socket.emit(SocketEvent.NOTIFICATION, { message: 'Failed to create map.' }) return callback(false) } } }