1
0
forked from noxious/server

37 lines
1.1 KiB
TypeScript

import { BaseEvent } from '#application/base/baseEvent'
import { Map, MapCacheT } from '#entities/map'
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: MapCacheT | false) => void): Promise<void> {
try {
if (!(await this.isCharacterGM())) return
this.logger.info(`GM ${(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()
return callback(await map.cache())
} catch (error: any) {
this.logger.error('gm:map:create error', error.message)
this.socket.emit('notification', { message: 'Failed to create map.' })
return callback(false)
}
}
}