#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) this.socket.emit('character:list', characters)
} catch (error: any) { } catch (error: any) {
console.log(error)
return this.socket.emit('notification', { message: 'Character delete failed. Please try again.' }) 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) const character = await CharacterRepository.getById(this.socket.characterId as number)
if (!character) { if (!character) {
gameMasterLogger.error('gm:zone_editor:zone:update error', 'Character not found') gameMasterLogger.error('gm:zone_editor:zone:update error', 'Character not found')
callback(null) return callback(null)
return
} }
if (character.role !== 'gm') { if (character.role !== 'gm') {
gameMasterLogger.info(`User ${character.id} tried to update zone but is not a game master.`) gameMasterLogger.info(`User ${character.id} tried to update zone but is not a game master.`)
callback(null) return callback(null)
return
} }
gameMasterLogger.info(`User ${character.id} has updated zone via zone editor.`) gameMasterLogger.info(`User ${character.id} has updated zone via zone editor.`)
if (!data.zoneId) { if (!data.zoneId) {
gameMasterLogger.info(`User ${character.id} tried to update zone but did not provide a zone id.`) gameMasterLogger.info(`User ${character.id} tried to update zone but did not provide a zone id.`)
callback(null) return callback(null)
return
} }
let zone = await ZoneRepository.getById(data.zoneId) let zone = await ZoneRepository.getById(data.zoneId)
if (!zone) { if (!zone) {
gameMasterLogger.info(`User ${character.id} tried to update zone ${data.zoneId} but it does not exist.`) gameMasterLogger.info(`User ${character.id} tried to update zone ${data.zoneId} but it does not exist.`)
callback(null) return callback(null)
return
} }
// 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({ await prisma.zone.update({
where: { id: data.zoneId }, where: { id: data.zoneId },
data: { data: {
@ -137,7 +149,7 @@ export default class ZoneUpdateEvent {
zoneManager.unloadZone(data.zoneId) zoneManager.unloadZone(data.zoneId)
await zoneManager.loadZone(zone) await zoneManager.loadZone(zone)
} catch (error: any) { } 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) callback(null)
} }
} }