#154: Remove tiles, event tiles, and objects that are out of the zone's width and height before saving zone

This commit is contained in:
Dennis Postma 2024-11-14 23:46:26 +01:00
parent 821e742527
commit fda8cc532e
2 changed files with 21 additions and 10 deletions

View File

@ -30,7 +30,6 @@ export default class CharacterDeleteEvent {
this.socket.emit('character:list', characters)
} catch (error: any) {
console.log(error)
return this.socket.emit('notification', { message: 'Character delete failed. Please try again.' })
}
}

View File

@ -47,32 +47,44 @@ export default class ZoneUpdateEvent {
const character = await CharacterRepository.getById(this.socket.characterId as number)
if (!character) {
gameMasterLogger.error('gm:zone_editor:zone:update error', 'Character not found')
callback(null)
return
return callback(null)
}
if (character.role !== 'gm') {
gameMasterLogger.info(`User ${character.id} tried to update zone but is not a game master.`)
callback(null)
return
return callback(null)
}
gameMasterLogger.info(`User ${character.id} has updated zone via zone editor.`)
if (!data.zoneId) {
gameMasterLogger.info(`User ${character.id} tried to update zone but did not provide a zone id.`)
callback(null)
return
return callback(null)
}
let zone = await ZoneRepository.getById(data.zoneId)
if (!zone) {
gameMasterLogger.info(`User ${character.id} tried to update zone ${data.zoneId} but it does not exist.`)
callback(null)
return
return callback(null)
}
// If tiles are larger than the zone, remove the extra tiles
if (data.tiles.length > data.height) {
data.tiles = data.tiles.slice(0, data.height)
}
for (let i = 0; i < data.tiles.length; i++) {
if (data.tiles[i].length > data.width) {
data.tiles[i] = data.tiles[i].slice(0, data.width)
}
}
// If zone event tiles are placed outside the zone's bounds, remove these
data.zoneEventTiles = data.zoneEventTiles.filter((tile) => tile.positionX >= 0 && tile.positionX < data.width && tile.positionY >= 0 && tile.positionY < data.height)
// If zone objects are placed outside the zone's bounds, remove these
data.zoneObjects = data.zoneObjects.filter((obj) => obj.positionX >= 0 && obj.positionX < data.width && obj.positionY >= 0 && obj.positionY < data.height)
await prisma.zone.update({
where: { id: data.zoneId },
data: {
@ -137,7 +149,7 @@ export default class ZoneUpdateEvent {
zoneManager.unloadZone(data.zoneId)
await zoneManager.loadZone(zone)
} catch (error: any) {
gameMasterLogger.error('gm:zone_editor:zone:update error', error.message)
gameMasterLogger.error(`gm:zone_editor:zone:update error: ${error instanceof Error ? error.message : String(error)}`)
callback(null)
}
}