forked from noxious/server
Map editor WIP
This commit is contained in:
parent
813ddbd8b1
commit
33afef5466
@ -1,6 +1,6 @@
|
|||||||
import { Migration } from '@mikro-orm/migrations';
|
import { Migration } from '@mikro-orm/migrations';
|
||||||
|
|
||||||
export class Migration20250103003053 extends Migration {
|
export class Migration20250105025845 extends Migration {
|
||||||
|
|
||||||
override async up(): Promise<void> {
|
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;`);
|
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 \`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(`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;`);
|
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;`);
|
@ -38,16 +38,19 @@ export class Map extends BaseEntity {
|
|||||||
@Property()
|
@Property()
|
||||||
updatedAt = new Date()
|
updatedAt = new Date()
|
||||||
|
|
||||||
@OneToMany(() => MapEffect, (effect) => effect.map)
|
@OneToMany(() => MapEffect, (effect) => effect.map, { orphanRemoval: true })
|
||||||
mapEffects = new Collection<MapEffect>(this)
|
mapEffects = new Collection<MapEffect>(this)
|
||||||
|
|
||||||
@OneToMany(() => MapEventTile, (tile) => tile.map)
|
@OneToMany(() => MapEventTile, (tile) => tile.map, { orphanRemoval: true })
|
||||||
mapEventTiles = new Collection<MapEventTile>(this)
|
mapEventTiles = new Collection<MapEventTile>(this)
|
||||||
|
|
||||||
@OneToMany(() => MapEventTileTeleport, (teleport) => teleport.toMap)
|
@OneToMany(() => MapEventTileTeleport, (teleport) => teleport.toMap, { orphanRemoval: true })
|
||||||
mapEventTileTeleports = new Collection<MapEventTileTeleport>(this)
|
mapEventTileTeleports = new Collection<MapEventTileTeleport>(this)
|
||||||
|
|
||||||
@OneToMany(() => PlacedMapObject, (object) => object.map)
|
@OneToMany(() => PlacedMapObject, (pmo) => pmo.map, {
|
||||||
|
name: 'placedMapObjects',
|
||||||
|
orphanRemoval: true,
|
||||||
|
})
|
||||||
placedMapObjects = new Collection<PlacedMapObject>(this)
|
placedMapObjects = new Collection<PlacedMapObject>(this)
|
||||||
|
|
||||||
@OneToMany(() => Character, (character) => character.map)
|
@OneToMany(() => Character, (character) => character.map)
|
||||||
|
@ -14,7 +14,7 @@ export class PlacedMapObject extends BaseEntity {
|
|||||||
@PrimaryKey()
|
@PrimaryKey()
|
||||||
id = randomUUID()
|
id = randomUUID()
|
||||||
|
|
||||||
@ManyToOne({ deleteRule: 'cascade' })
|
@ManyToOne(()=> Map, { deleteRule: 'cascade' })
|
||||||
map!: Map
|
map!: Map
|
||||||
|
|
||||||
@ManyToOne({ deleteRule: 'cascade' })
|
@ManyToOne({ deleteRule: 'cascade' })
|
||||||
|
@ -32,7 +32,7 @@ export default class MapRequestEvent extends BaseEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Populate map with mapEventTiles and placedMapObjects
|
// 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)
|
return callback(map)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
@ -27,10 +27,7 @@ interface IPayload {
|
|||||||
toRotation: number
|
toRotation: number
|
||||||
}
|
}
|
||||||
}[]
|
}[]
|
||||||
mapEffects: {
|
mapEffects: MapEffect[]
|
||||||
effect: string
|
|
||||||
strength: number
|
|
||||||
}[]
|
|
||||||
placedMapObjects: PlacedMapObject[]
|
placedMapObjects: PlacedMapObject[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,13 +48,16 @@ export default class MapUpdateEvent extends BaseEvent {
|
|||||||
return callback(null)
|
return callback(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
let map = await MapRepository.getById(data.mapId)
|
let mapRepository = new MapRepository()
|
||||||
|
let map = await mapRepository.getById(data.mapId)
|
||||||
|
|
||||||
if (!map) {
|
if (!map) {
|
||||||
this.logger.info(`User ${character!.getId()} tried to update map ${data.mapId} but it does not exist.`)
|
this.logger.info(`User ${character!.getId()} tried to update map ${data.mapId} but it does not exist.`)
|
||||||
return callback(null)
|
return callback(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await mapRepository.getEntityManager().populate(map, ['*'])
|
||||||
|
|
||||||
// Validation logic remains the same
|
// Validation logic remains the same
|
||||||
if (data.tiles.length > data.height) {
|
if (data.tiles.length > data.height) {
|
||||||
data.tiles = data.tiles.slice(0, 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.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)
|
data.placedMapObjects = data.placedMapObjects.filter((obj) => obj.positionX >= 0 && obj.positionX < data.width && obj.positionY >= 0 && obj.positionY < data.height)
|
||||||
|
|
||||||
// Clear existing collections
|
// Clear existing collections
|
||||||
@ -83,7 +82,7 @@ export default class MapUpdateEvent extends BaseEvent {
|
|||||||
|
|
||||||
if (tile.teleport) {
|
if (tile.teleport) {
|
||||||
const teleport = new MapEventTileTeleport()
|
const teleport = new MapEventTileTeleport()
|
||||||
.setToMap(await MapRepository.getById(tile.teleport.toMapId))
|
.setToMap((await mapRepository.getById(tile.teleport.toMapId))!)
|
||||||
.setToPositionX(tile.teleport.toPositionX)
|
.setToPositionX(tile.teleport.toPositionX)
|
||||||
.setToPositionY(tile.teleport.toPositionY)
|
.setToPositionY(tile.teleport.toPositionY)
|
||||||
.setToRotation(tile.teleport.toRotation)
|
.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()
|
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
|
// 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) {
|
if (!map) {
|
||||||
this.logger.info(`User ${character!.getId()} tried to update map ${data.mapId} but it does not exist after update.`)
|
this.logger.info(`User ${character!.getId()} tried to update map ${data.mapId} but it does not exist after update.`)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user