Map editor teleport enhancements

This commit is contained in:
Dennis Postma 2025-02-16 21:18:01 +01:00
parent f3e0d6e03a
commit b173a993f7
4 changed files with 55 additions and 18 deletions

View File

@ -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<Array<string>> = []
@Property()

View File

@ -2,6 +2,7 @@ import { BaseMap } from '@/entities/base/map'
import { Entity } from '@mikro-orm/core'
export type MapCacheT = ReturnType<Map['cache']> | {}
export type MapEditorMapT = ReturnType<Map['mapEditorObject']> | {}
@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 {}
}
}
}

View File

@ -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<void> {
private async handleEvent(data: IPayload, callback: (response: MapEditorMapT | null) => void): Promise<void> {
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)

View File

@ -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<void> {
private async handleEvent(data: IPayload, callback: (response: MapEditorMapT | null) => void): Promise<void> {
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)