diff --git a/migrations/Migration20250103003053.ts b/migrations/Migration20250105025845.ts similarity index 96% rename from migrations/Migration20250103003053.ts rename to migrations/Migration20250105025845.ts index b555b9e..c6a67c7 100644 --- a/migrations/Migration20250103003053.ts +++ b/migrations/Migration20250105025845.ts @@ -1,6 +1,6 @@ import { Migration } from '@mikro-orm/migrations'; -export class Migration20250103003053 extends Migration { +export class Migration20250105025845 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;`); @@ -23,7 +23,7 @@ export class Migration20250103003053 extends Migration { this.addSql(`create table \`sprite\` (\`id\` varchar(255) not null, \`name\` varchar(255) not null, \`created_at\` datetime not null, \`updated_at\` datetime not null, primary key (\`id\`)) default character set utf8mb4 engine = InnoDB;`); - this.addSql(`create table \`item\` (\`id\` varchar(255) not null, \`name\` varchar(255) not null, \`description\` varchar(255) null, \`item_type\` enum('WEAPON', 'HELMET', 'CHEST', 'LEGS', 'BOOTS', 'GLOVES', 'RING', 'NECKLACE') not null, \`stackable\` tinyint(1) not null default false, \`rarity\` enum('COMMON', 'UNCOMMON', 'RARE', 'EPIC', 'LEGENDARY') not null default 'COMMON', \`sprite_id\` varchar(255) null, \`created_at\` datetime not null, \`updated_at\` datetime not null, primary key (\`id\`)) default character set utf8mb4 engine = InnoDB;`); + this.addSql(`create table \`item\` (\`id\` varchar(255) not null, \`name\` varchar(255) not null, \`description\` varchar(255) not null default '', \`item_type\` enum('WEAPON', 'HELMET', 'CHEST', 'LEGS', 'BOOTS', 'GLOVES', 'RING', 'NECKLACE') not null, \`stackable\` tinyint(1) not null default false, \`rarity\` enum('COMMON', 'UNCOMMON', 'RARE', 'EPIC', 'LEGENDARY') not null default 'COMMON', \`sprite_id\` varchar(255) null, \`created_at\` datetime not null, \`updated_at\` datetime not null, primary key (\`id\`)) default character set utf8mb4 engine = InnoDB;`); this.addSql(`alter table \`item\` add index \`item_sprite_id_index\`(\`sprite_id\`);`); this.addSql(`create table \`character_type\` (\`id\` varchar(255) not null, \`name\` varchar(255) not null, \`gender\` enum('MALE', 'FEMALE') not null, \`race\` enum('HUMAN', 'ELF', 'DWARF', 'ORC', 'GOBLIN') not null, \`is_selectable\` tinyint(1) not null default false, \`sprite_id\` varchar(255) null, \`created_at\` datetime not null, \`updated_at\` datetime not null, primary key (\`id\`)) default character set utf8mb4 engine = InnoDB;`); diff --git a/src/entities/map.ts b/src/entities/map.ts index 657711d..0432173 100644 --- a/src/entities/map.ts +++ b/src/entities/map.ts @@ -38,16 +38,19 @@ export class Map extends BaseEntity { @Property() updatedAt = new Date() - @OneToMany(() => MapEffect, (effect) => effect.map) + @OneToMany(() => MapEffect, (effect) => effect.map, { orphanRemoval: true }) mapEffects = new Collection(this) - @OneToMany(() => MapEventTile, (tile) => tile.map) + @OneToMany(() => MapEventTile, (tile) => tile.map, { orphanRemoval: true }) mapEventTiles = new Collection(this) - @OneToMany(() => MapEventTileTeleport, (teleport) => teleport.toMap) + @OneToMany(() => MapEventTileTeleport, (teleport) => teleport.toMap, { orphanRemoval: true }) mapEventTileTeleports = new Collection(this) - @OneToMany(() => PlacedMapObject, (object) => object.map) + @OneToMany(() => PlacedMapObject, (pmo) => pmo.map, { + name: 'placedMapObjects', + orphanRemoval: true, + }) placedMapObjects = new Collection(this) @OneToMany(() => Character, (character) => character.map) diff --git a/src/entities/placedMapObject.ts b/src/entities/placedMapObject.ts index 72e7ed8..b1a6b90 100644 --- a/src/entities/placedMapObject.ts +++ b/src/entities/placedMapObject.ts @@ -14,7 +14,7 @@ export class PlacedMapObject extends BaseEntity { @PrimaryKey() id = randomUUID() - @ManyToOne({ deleteRule: 'cascade' }) + @ManyToOne(()=> Map, { deleteRule: 'cascade' }) map!: Map @ManyToOne({ deleteRule: 'cascade' }) diff --git a/src/events/gameMaster/mapEditor/request.ts b/src/events/gameMaster/mapEditor/request.ts index 67f9c03..d9211ef 100644 --- a/src/events/gameMaster/mapEditor/request.ts +++ b/src/events/gameMaster/mapEditor/request.ts @@ -32,7 +32,7 @@ export default class MapRequestEvent extends BaseEvent { } // Populate map with mapEventTiles and placedMapObjects - await mapRepository.getEntityManager().populate(map, ['mapEventTiles', 'placedMapObjects']) + await mapRepository.getEntityManager().populate(map, ['mapEffects', 'mapEventTiles', 'placedMapObjects', 'placedMapObjects.mapObject']) return callback(map) } catch (error: any) { diff --git a/src/events/gameMaster/mapEditor/update.ts b/src/events/gameMaster/mapEditor/update.ts index a930fc3..a68fb6c 100644 --- a/src/events/gameMaster/mapEditor/update.ts +++ b/src/events/gameMaster/mapEditor/update.ts @@ -27,10 +27,7 @@ interface IPayload { toRotation: number } }[] - mapEffects: { - effect: string - strength: number - }[] + mapEffects: MapEffect[] placedMapObjects: PlacedMapObject[] } @@ -51,13 +48,16 @@ export default class MapUpdateEvent extends BaseEvent { return callback(null) } - let map = await MapRepository.getById(data.mapId) + let mapRepository = new MapRepository() + let map = await mapRepository.getById(data.mapId) if (!map) { this.logger.info(`User ${character!.getId()} tried to update map ${data.mapId} but it does not exist.`) return callback(null) } + await mapRepository.getEntityManager().populate(map, ['*']) + // Validation logic remains the same if (data.tiles.length > data.height) { data.tiles = data.tiles.slice(0, data.height) @@ -69,7 +69,6 @@ export default class MapUpdateEvent extends BaseEvent { } data.mapEventTiles = data.mapEventTiles.filter((tile) => tile.positionX >= 0 && tile.positionX < data.width && tile.positionY >= 0 && tile.positionY < data.height) - data.placedMapObjects = data.placedMapObjects.filter((obj) => obj.positionX >= 0 && obj.positionX < data.width && obj.positionY >= 0 && obj.positionY < data.height) // Clear existing collections @@ -83,7 +82,7 @@ export default class MapUpdateEvent extends BaseEvent { if (tile.teleport) { const teleport = new MapEventTileTeleport() - .setToMap(await MapRepository.getById(tile.teleport.toMapId)) + .setToMap((await mapRepository.getById(tile.teleport.toMapId))!) .setToPositionX(tile.teleport.toPositionX) .setToPositionY(tile.teleport.toPositionY) .setToRotation(tile.teleport.toRotation) @@ -116,7 +115,9 @@ export default class MapUpdateEvent extends BaseEvent { 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 - map = await MapRepository.getById(data.mapId) + mapRepository = new MapRepository() + map = await mapRepository.getById(data.mapId) + await mapRepository.getEntityManager().populate(map!, ['*']) if (!map) { this.logger.info(`User ${character!.getId()} tried to update map ${data.mapId} but it does not exist after update.`)