import { BaseEvent } from '#application/base/baseEvent' import { Zone } from '#entities/zone' import ZoneRepository from '#repositories/zoneRepository' type Payload = { name: string width: number height: number } export default class ZoneCreateEvent extends BaseEvent { public listen(): void { this.socket.on('gm:zone_editor:zone:create', this.handleEvent.bind(this)) } private async handleEvent(data: Payload, callback: (response: Zone[]) => void): Promise { try { if (!(await this.isCharacterGM())) return this.logger.info(`User ${(await this.getCharacter())!.getId()} has created a new zone via zone editor.`) const zone = new Zone() await zone .setName(data.name) .setWidth(data.width) .setHeight(data.height) .setTiles(Array.from({ length: data.height }, () => Array.from({ length: data.width }, () => 'blank_tile'))) .save() const zoneList = await ZoneRepository.getAll() callback(zoneList) } catch (error: any) { this.logger.error('gm:zone_editor:zone:create error', error.message) this.socket.emit('notification', { message: 'Failed to create zone.' }) callback([]) } } }