import { BaseEvent } from '@/application/base/baseEvent' import { SocketEvent } from '@/application/enums' import type { UUID } from '@/application/types' import { Map } from '@/entities/map' import MapRepository from '@/repositories/mapRepository' interface IPayload { mapId: UUID } export default class MapRequestEvent extends BaseEvent { public listen(): void { this.socket.on(SocketEvent.GM_MAP_REQUEST, this.handleEvent.bind(this)) } private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise { try { if (!(await this.isCharacterGM())) return this.logger.info(`User ${(await this.getCharacter())!.getId()} has requested map via map editor.`) if (!data.mapId) { this.logger.info(`User ${(await this.getCharacter())!.getId()} tried to request map but did not provide a map id.`) return callback(null) } const mapRepository = new MapRepository() const map = await mapRepository.getById(data.mapId) if (!map) { this.logger.info(`User ${(await this.getCharacter())!.getId()} tried to request map ${data.mapId} but it does not exist.`) return callback(null) } await mapRepository.getEntityManager().populate(map, mapRepository.POPULATE_MAP_EDITOR as any) return callback(map) } catch (error: any) { this.logger.error('gm:map:request error', error.message) return callback(null) } } }