From b173a993f7bdb0d9f4317a5cdb91cd7bee549195 Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Sun, 16 Feb 2025 21:18:01 +0100 Subject: [PATCH] Map editor teleport enhancements --- src/entities/base/map.ts | 4 +-- src/entities/map.ts | 39 ++++++++++++++++++++++ src/events/gameMaster/mapEditor/request.ts | 8 ++--- src/events/gameMaster/mapEditor/update.ts | 22 ++++++------ 4 files changed, 55 insertions(+), 18 deletions(-) diff --git a/src/entities/base/map.ts b/src/entities/base/map.ts index d40431b..fbda149 100644 --- a/src/entities/base/map.ts +++ b/src/entities/base/map.ts @@ -11,7 +11,7 @@ export class BaseMap extends BaseEntity { id = randomUUID() @Property() - name!: string + name: string = '' @Property() width = 10 @@ -19,7 +19,7 @@ export class BaseMap extends BaseEntity { @Property() height = 10 - @Property({ type: 'json', nullable: true }) + @Property({ type: 'json' }) tiles: Array> = [] @Property() diff --git a/src/entities/map.ts b/src/entities/map.ts index e7f6f4e..cdb3d29 100644 --- a/src/entities/map.ts +++ b/src/entities/map.ts @@ -2,6 +2,7 @@ import { BaseMap } from '@/entities/base/map' import { Entity } from '@mikro-orm/core' export type MapCacheT = ReturnType | {} +export type MapEditorMapT = ReturnType | {} @Entity() export class Map extends BaseMap { @@ -35,4 +36,42 @@ export class Map extends BaseMap { return {} } } + + public async mapEditorObject() { + try { + await this.getPlacedMapObjects().load() + await this.getMapEffects().load() + await this.getMapEventTiles().load() + + return { + id: this.getId(), + name: this.getName(), + width: this.getWidth(), + height: this.getHeight(), + tiles: this.getTiles(), + pvp: this.getPvp(), + createdAt: this.getCreatedAt(), + updatedAt: this.getUpdatedAt(), + placedMapObjects: this.getPlacedMapObjects(), + mapEffects: this.getMapEffects(), + mapEventTiles: this.getMapEventTiles().map((mapEventTile) => ({ + id: mapEventTile.getId(), + type: mapEventTile.getType(), + positionX: mapEventTile.getPositionX(), + positionY: mapEventTile.getPositionY(), + teleport: mapEventTile.getTeleport() + ? { + toMap: mapEventTile.getTeleport()?.getToMap().getId(), + toPositionX: mapEventTile.getTeleport()?.getToPositionX(), + toPositionY: mapEventTile.getTeleport()?.getToPositionY(), + toRotation: mapEventTile.getTeleport()?.getToRotation() + } + : undefined + })) + } + } catch (error) { + console.error(error) + return {} + } + } } diff --git a/src/events/gameMaster/mapEditor/request.ts b/src/events/gameMaster/mapEditor/request.ts index dc9fe93..c1885db 100644 --- a/src/events/gameMaster/mapEditor/request.ts +++ b/src/events/gameMaster/mapEditor/request.ts @@ -1,7 +1,7 @@ import { BaseEvent } from '@/application/base/baseEvent' import { SocketEvent } from '@/application/enums' import type { UUID } from '@/application/types' -import { Map } from '@/entities/map' +import { type MapEditorMapT } from '@/entities/map' import MapRepository from '@/repositories/mapRepository' interface IPayload { @@ -13,7 +13,7 @@ export default class MapRequestEvent extends BaseEvent { this.socket.on(SocketEvent.GM_MAP_REQUEST, this.handleEvent.bind(this)) } - private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise { + private async handleEvent(data: IPayload, callback: (response: MapEditorMapT | null) => void): Promise { try { if (!(await this.isCharacterGM())) return @@ -34,9 +34,7 @@ export default class MapRequestEvent extends BaseEvent { await mapRepository.getEntityManager().populate(map, mapRepository.POPULATE_MAP_EDITOR as any) - // Remove map.mapEventTiles.teleport.toMap and add map.mapEventTiles.teleport.toMapId - - return callback(map) + return callback(await map.mapEditorObject()) } catch (error: any) { this.logger.error('gm:map:request error', error.message) return callback(null) diff --git a/src/events/gameMaster/mapEditor/update.ts b/src/events/gameMaster/mapEditor/update.ts index 1df390d..8989745 100644 --- a/src/events/gameMaster/mapEditor/update.ts +++ b/src/events/gameMaster/mapEditor/update.ts @@ -1,7 +1,7 @@ import { BaseEvent } from '@/application/base/baseEvent' import { MapEventTileType, SocketEvent } from '@/application/enums' import type { UUID } from '@/application/types' -import { Map } from '@/entities/map' +import { type MapEditorMapT } from '@/entities/map' import { MapEffect } from '@/entities/mapEffect' import { MapEventTile } from '@/entities/mapEventTile' import { MapEventTileTeleport } from '@/entities/mapEventTileTeleport' @@ -21,7 +21,7 @@ interface IPayload { positionX: number positionY: number teleport?: { - toMapId: string + toMap: string toPositionX: number toPositionY: number toRotation: number @@ -36,7 +36,7 @@ export default class MapUpdateEvent extends BaseEvent { this.socket.on(SocketEvent.GM_MAP_UPDATE, this.handleEvent.bind(this)) } - private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise { + private async handleEvent(data: IPayload, callback: (response: MapEditorMapT | null) => void): Promise { try { if (!(await this.isCharacterGM())) return @@ -80,17 +80,17 @@ export default class MapUpdateEvent extends BaseEvent { map.getMapEffects().removeAll() // Create and add new map event tiles - for (const tile of data.mapEventTiles) { - const mapEventTile = new MapEventTile().setType(tile.type).setPositionX(tile.positionX).setPositionY(tile.positionY).setMap(map) - if (tile.teleport) { - const toMap = await mapRepository.getById(tile.teleport.toMapId as UUID) + for (const eventTile of data.mapEventTiles) { + const mapEventTile = new MapEventTile().setMap(map).setType(eventTile.type).setPositionX(eventTile.positionX).setPositionY(eventTile.positionY) + if (eventTile.teleport) { + const toMap = await mapRepository.getById(eventTile.teleport.toMap as UUID) if (!toMap) continue const teleport = new MapEventTileTeleport() .setMapEventTile(mapEventTile) .setToMap(toMap) - .setToPositionX(tile.teleport.toPositionX) - .setToPositionY(tile.teleport.toPositionY) - .setToRotation(tile.teleport.toRotation) + .setToPositionX(eventTile.teleport.toPositionX) + .setToPositionY(eventTile.teleport.toPositionY) + .setToRotation(eventTile.teleport.toRotation) mapEventTile.setTeleport(teleport) } @@ -128,7 +128,7 @@ export default class MapUpdateEvent extends BaseEvent { mapManager.unloadMap(data.mapId) await mapManager.loadMap(map) - return callback(map) + return callback(await map.mapEditorObject()) } catch (error: any) { this.emitError(`gm:map:update error: ${error instanceof Error ? error.message + error.stack : String(error)}`) return callback(null)