From 57b21f149971161a5903718f21eacddc5677b46b Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Sun, 5 Jan 2025 04:52:16 +0100 Subject: [PATCH] More map editor work --- ...05025845.ts => Migration20250105033941.ts} | 2 +- src/application/base/baseEntity.ts | 11 +++++- src/entities/character.ts | 4 +-- src/entities/map.ts | 36 ------------------- src/events/gameMaster/mapEditor/update.ts | 15 +++++--- src/repositories/mapRepository.ts | 5 ++- 6 files changed, 27 insertions(+), 46 deletions(-) rename migrations/{Migration20250105025845.ts => Migration20250105033941.ts} (99%) diff --git a/migrations/Migration20250105025845.ts b/migrations/Migration20250105033941.ts similarity index 99% rename from migrations/Migration20250105025845.ts rename to migrations/Migration20250105033941.ts index c6a67c7..d22c686 100644 --- a/migrations/Migration20250105025845.ts +++ b/migrations/Migration20250105033941.ts @@ -1,6 +1,6 @@ import { Migration } from '@mikro-orm/migrations'; -export class Migration20250105025845 extends Migration { +export class Migration20250105033941 extends Migration { override async up(): Promise { this.addSql(`create table \`map\` (\`id\` varchar(255) not null, \`name\` varchar(255) not null, \`width\` int not null default 10, \`height\` int not null default 10, \`tiles\` json null, \`pvp\` tinyint(1) not null default false, \`created_at\` datetime not null, \`updated_at\` datetime not null, primary key (\`id\`)) default character set utf8mb4 engine = InnoDB;`); diff --git a/src/application/base/baseEntity.ts b/src/application/base/baseEntity.ts index e1a81f7..65fee38 100644 --- a/src/application/base/baseEntity.ts +++ b/src/application/base/baseEntity.ts @@ -14,6 +14,11 @@ export abstract class BaseEntity { return this.entityManager } + public setEntityManager(entityManager: EntityManager) { + this.entityManager = entityManager + return this + } + async save(): Promise { return this.execute('persist', 'save entity') } @@ -22,7 +27,11 @@ export abstract class BaseEntity { return this.execute('remove', 'remove entity') } - private async execute(method: 'persist' | 'remove', actionDescription: string): Promise { + async update(): Promise { + return this.execute('merge', 'update entity') + } + + private async execute(method: 'persist' | 'merge' | 'remove', actionDescription: string): Promise { try { const em = this.getEntityManager() diff --git a/src/entities/character.ts b/src/entities/character.ts index de40721..054b632 100644 --- a/src/entities/character.ts +++ b/src/entities/character.ts @@ -33,9 +33,9 @@ export class Character extends BaseEntity { @OneToMany(() => Chat, (chat) => chat.character) chats = new Collection(this) - // Position + // Position - @TODO: Update to spawn point when current map is not found @ManyToOne() - map!: Map // @TODO: Update to spawn point when current map is not found + map!: Map @Property() positionX = 0 diff --git a/src/entities/map.ts b/src/entities/map.ts index 0432173..0c55814 100644 --- a/src/entities/map.ts +++ b/src/entities/map.ts @@ -44,21 +44,12 @@ export class Map extends BaseEntity { @OneToMany(() => MapEventTile, (tile) => tile.map, { orphanRemoval: true }) mapEventTiles = new Collection(this) - @OneToMany(() => MapEventTileTeleport, (teleport) => teleport.toMap, { orphanRemoval: true }) - mapEventTileTeleports = new Collection(this) - @OneToMany(() => PlacedMapObject, (pmo) => pmo.map, { name: 'placedMapObjects', orphanRemoval: true, }) placedMapObjects = new Collection(this) - @OneToMany(() => Character, (character) => character.map) - characters = new Collection(this) - - @OneToMany(() => Chat, (chat) => chat.map) - chats = new Collection(this) - setId(id: UUID) { this.id = id return this @@ -149,15 +140,6 @@ export class Map extends BaseEntity { return this.mapEventTiles } - setMapEventTileTeleports(mapEventTileTeleports: Collection) { - this.mapEventTileTeleports = mapEventTileTeleports - return this - } - - getMapEventTileTeleports() { - return this.mapEventTileTeleports - } - setPlacedMapObjects(placedMapObjects: Collection) { this.placedMapObjects = placedMapObjects return this @@ -166,22 +148,4 @@ export class Map extends BaseEntity { getPlacedMapObjects() { return this.placedMapObjects } - - setCharacters(characters: Collection) { - this.characters = characters - return this - } - - getCharacters() { - return this.characters - } - - setChats(chats: Collection) { - this.chats = chats - return this - } - - getChats() { - return this.chats - } } diff --git a/src/events/gameMaster/mapEditor/update.ts b/src/events/gameMaster/mapEditor/update.ts index a68fb6c..c3608ce 100644 --- a/src/events/gameMaster/mapEditor/update.ts +++ b/src/events/gameMaster/mapEditor/update.ts @@ -56,6 +56,7 @@ export default class MapUpdateEvent extends BaseEvent { return callback(null) } + // @ts-ignore await mapRepository.getEntityManager().populate(map, ['*']) // Validation logic remains the same @@ -72,9 +73,9 @@ export default class MapUpdateEvent extends BaseEvent { data.placedMapObjects = data.placedMapObjects.filter((obj) => obj.positionX >= 0 && obj.positionX < data.width && obj.positionY >= 0 && obj.positionY < data.height) // Clear existing collections - map.mapEventTiles.removeAll() - map.placedMapObjects.removeAll() - map.mapEffects.removeAll() + map.getMapEventTiles().removeAll() + map.getPlacedMapObjects().removeAll() + map.getMapEffects().removeAll() // Create and add new map event tiles for (const tile of data.mapEventTiles) { @@ -90,7 +91,7 @@ export default class MapUpdateEvent extends BaseEvent { mapEventTile.setTeleport(teleport) } - map.mapEventTiles.add(mapEventTile) + // map.mapEventTiles.add(mapEventTile) } // Create and add new map objects @@ -108,15 +109,19 @@ export default class MapUpdateEvent extends BaseEvent { // Create and add new map effects for (const effect of data.mapEffects) { const mapEffect = new MapEffect().setEffect(effect.effect).setStrength(effect.strength).setMap(map) - map.mapEffects.add(mapEffect) + // map.mapEffects.add(mapEffect) } + console.log(map.getPlacedMapObjects().count()) + // Update map properties + // map.setEntityManager(mapRepository.getEntityManager()) await map.setName(data.name).setWidth(data.width).setHeight(data.height).setTiles(data.tiles).setPvp(data.pvp).setUpdatedAt(new Date()).save() // Reload map from database to get fresh data mapRepository = new MapRepository() map = await mapRepository.getById(data.mapId) + // @ts-ignore await mapRepository.getEntityManager().populate(map!, ['*']) if (!map) { diff --git a/src/repositories/mapRepository.ts b/src/repositories/mapRepository.ts index 4091359..7c8fc13 100644 --- a/src/repositories/mapRepository.ts +++ b/src/repositories/mapRepository.ts @@ -28,7 +28,10 @@ class MapRepository extends BaseRepository { async getById(id: UUID) { try { const repository = this.getEntityManager().getRepository(Map) - return await repository.findOne({ id }) + const result = await repository.findOne({ id }) + if (result) result.setEntityManager(this.getEntityManager()) + + return result } catch (error: any) { this.logger.error(`Failed to get map by id: ${error.message}`) return null