Map editor WIP

This commit is contained in:
Dennis Postma 2025-01-05 04:01:43 +01:00
parent 813ddbd8b1
commit 33afef5466
5 changed files with 20 additions and 16 deletions

View File

@ -1,6 +1,6 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20250103003053 extends Migration {
export class Migration20250105025845 extends Migration {
override async up(): Promise<void> {
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;`);

View File

@ -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<MapEffect>(this)
@OneToMany(() => MapEventTile, (tile) => tile.map)
@OneToMany(() => MapEventTile, (tile) => tile.map, { orphanRemoval: true })
mapEventTiles = new Collection<MapEventTile>(this)
@OneToMany(() => MapEventTileTeleport, (teleport) => teleport.toMap)
@OneToMany(() => MapEventTileTeleport, (teleport) => teleport.toMap, { orphanRemoval: true })
mapEventTileTeleports = new Collection<MapEventTileTeleport>(this)
@OneToMany(() => PlacedMapObject, (object) => object.map)
@OneToMany(() => PlacedMapObject, (pmo) => pmo.map, {
name: 'placedMapObjects',
orphanRemoval: true,
})
placedMapObjects = new Collection<PlacedMapObject>(this)
@OneToMany(() => Character, (character) => character.map)

View File

@ -14,7 +14,7 @@ export class PlacedMapObject extends BaseEntity {
@PrimaryKey()
id = randomUUID()
@ManyToOne({ deleteRule: 'cascade' })
@ManyToOne(()=> Map, { deleteRule: 'cascade' })
map!: Map
@ManyToOne({ deleteRule: 'cascade' })

View File

@ -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) {

View File

@ -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.`)