forked from noxious/server
Almost finalised refactoring
This commit is contained in:
38
src/events/gameMaster/assetManager/mapObject/remove.ts
Normal file
38
src/events/gameMaster/assetManager/mapObject/remove.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import fs from 'fs'
|
||||
import Storage from '#application/storage'
|
||||
import { BaseEvent } from '#application/base/baseEvent'
|
||||
import MapObjectRepository from '#repositories/mapObjectRepository'
|
||||
import { UUID } from '#application/types'
|
||||
|
||||
interface IPayload {
|
||||
mapObjectId: UUID
|
||||
}
|
||||
|
||||
export default class MapObjectRemoveEvent extends BaseEvent {
|
||||
public listen(): void {
|
||||
this.socket.on('gm:mapObject:remove', this.handleEvent.bind(this))
|
||||
}
|
||||
|
||||
private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> {
|
||||
if (!(await this.isCharacterGM())) return
|
||||
|
||||
try {
|
||||
// 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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user