40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import fs from 'fs'
|
|
import { BaseEvent } from '@/application/base/baseEvent'
|
|
import { SocketEvent } from '@/application/enums'
|
|
import Storage from '@/application/storage'
|
|
import type { UUID } from '@/application/types'
|
|
import MapObjectRepository from '@/repositories/mapObjectRepository'
|
|
|
|
interface IPayload {
|
|
mapObjectId: UUID
|
|
}
|
|
|
|
export default class MapObjectRemoveEvent extends BaseEvent {
|
|
public listen(): void {
|
|
this.socket.on(SocketEvent.GM_MAPOBJECT_REMOVE, this.handleEvent.bind(this))
|
|
}
|
|
|
|
private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> {
|
|
try {
|
|
if (!(await this.isCharacterGM())) return
|
|
// remove the tile from the disk
|
|
const finalFilePath = Storage.getPublicPath('map_objects', data.mapObjectId + '.png')
|
|
fs.unlink(finalFilePath, async (err) => {
|
|
if (err) {
|
|
this.logger.error(`Error deleting object ${data.mapObjectId}: ${err.message}`)
|
|
callback(false)
|
|
return
|
|
}
|
|
|
|
const mapObjectRepository = new MapObjectRepository()
|
|
await (await mapObjectRepository.getById(data.mapObjectId))?.delete()
|
|
|
|
return callback(true)
|
|
})
|
|
} catch (error) {
|
|
this.logger.error(`Error deleting object ${data.mapObjectId}: ${error instanceof Error ? error.message : String(error)}`)
|
|
return callback(false)
|
|
}
|
|
}
|
|
}
|