import { BaseEvent } from '#application/base/baseEvent' import { Map } from '#entities/map' import MapRepository from '#repositories/mapRepository' type Payload = { name: string width: number height: number } export default class MapCreateEvent extends BaseEvent { public listen(): void { this.socket.on('gm:map:create', this.handleEvent.bind(this)) } private async handleEvent(data: Payload, callback: (response: Map[]) => void): Promise { try { if (!(await this.isCharacterGM())) return this.logger.info(`User ${(await this.getCharacter())!.getId()} has created a new map via map editor.`) 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() const mapList = await MapRepository.getAll() return callback(mapList) } catch (error: any) { this.logger.error('gm:map:create error', error.message) this.socket.emit('notification', { message: 'Failed to create map.' }) return callback([]) } } }