forked from noxious/server
More map editor work
This commit is contained in:
parent
33afef5466
commit
57b21f1499
@ -1,6 +1,6 @@
|
|||||||
import { Migration } from '@mikro-orm/migrations';
|
import { Migration } from '@mikro-orm/migrations';
|
||||||
|
|
||||||
export class Migration20250105025845 extends Migration {
|
export class Migration20250105033941 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;`);
|
@ -14,6 +14,11 @@ export abstract class BaseEntity {
|
|||||||
return this.entityManager
|
return this.entityManager
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public setEntityManager(entityManager: EntityManager) {
|
||||||
|
this.entityManager = entityManager
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
async save(): Promise<this> {
|
async save(): Promise<this> {
|
||||||
return this.execute('persist', 'save entity')
|
return this.execute('persist', 'save entity')
|
||||||
}
|
}
|
||||||
@ -22,7 +27,11 @@ export abstract class BaseEntity {
|
|||||||
return this.execute('remove', 'remove entity')
|
return this.execute('remove', 'remove entity')
|
||||||
}
|
}
|
||||||
|
|
||||||
private async execute(method: 'persist' | 'remove', actionDescription: string): Promise<this> {
|
async update(): Promise<this> {
|
||||||
|
return this.execute('merge', 'update entity')
|
||||||
|
}
|
||||||
|
|
||||||
|
private async execute(method: 'persist' | 'merge' | 'remove', actionDescription: string): Promise<this> {
|
||||||
try {
|
try {
|
||||||
const em = this.getEntityManager()
|
const em = this.getEntityManager()
|
||||||
|
|
||||||
|
@ -33,9 +33,9 @@ export class Character extends BaseEntity {
|
|||||||
@OneToMany(() => Chat, (chat) => chat.character)
|
@OneToMany(() => Chat, (chat) => chat.character)
|
||||||
chats = new Collection<Chat>(this)
|
chats = new Collection<Chat>(this)
|
||||||
|
|
||||||
// Position
|
// Position - @TODO: Update to spawn point when current map is not found
|
||||||
@ManyToOne()
|
@ManyToOne()
|
||||||
map!: Map // @TODO: Update to spawn point when current map is not found
|
map!: Map
|
||||||
|
|
||||||
@Property()
|
@Property()
|
||||||
positionX = 0
|
positionX = 0
|
||||||
|
@ -44,21 +44,12 @@ export class Map extends BaseEntity {
|
|||||||
@OneToMany(() => MapEventTile, (tile) => tile.map, { orphanRemoval: true })
|
@OneToMany(() => MapEventTile, (tile) => tile.map, { orphanRemoval: true })
|
||||||
mapEventTiles = new Collection<MapEventTile>(this)
|
mapEventTiles = new Collection<MapEventTile>(this)
|
||||||
|
|
||||||
@OneToMany(() => MapEventTileTeleport, (teleport) => teleport.toMap, { orphanRemoval: true })
|
|
||||||
mapEventTileTeleports = new Collection<MapEventTileTeleport>(this)
|
|
||||||
|
|
||||||
@OneToMany(() => PlacedMapObject, (pmo) => pmo.map, {
|
@OneToMany(() => PlacedMapObject, (pmo) => pmo.map, {
|
||||||
name: 'placedMapObjects',
|
name: 'placedMapObjects',
|
||||||
orphanRemoval: true,
|
orphanRemoval: true,
|
||||||
})
|
})
|
||||||
placedMapObjects = new Collection<PlacedMapObject>(this)
|
placedMapObjects = new Collection<PlacedMapObject>(this)
|
||||||
|
|
||||||
@OneToMany(() => Character, (character) => character.map)
|
|
||||||
characters = new Collection<Character>(this)
|
|
||||||
|
|
||||||
@OneToMany(() => Chat, (chat) => chat.map)
|
|
||||||
chats = new Collection<Chat>(this)
|
|
||||||
|
|
||||||
setId(id: UUID) {
|
setId(id: UUID) {
|
||||||
this.id = id
|
this.id = id
|
||||||
return this
|
return this
|
||||||
@ -149,15 +140,6 @@ export class Map extends BaseEntity {
|
|||||||
return this.mapEventTiles
|
return this.mapEventTiles
|
||||||
}
|
}
|
||||||
|
|
||||||
setMapEventTileTeleports(mapEventTileTeleports: Collection<MapEventTileTeleport>) {
|
|
||||||
this.mapEventTileTeleports = mapEventTileTeleports
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
getMapEventTileTeleports() {
|
|
||||||
return this.mapEventTileTeleports
|
|
||||||
}
|
|
||||||
|
|
||||||
setPlacedMapObjects(placedMapObjects: Collection<PlacedMapObject>) {
|
setPlacedMapObjects(placedMapObjects: Collection<PlacedMapObject>) {
|
||||||
this.placedMapObjects = placedMapObjects
|
this.placedMapObjects = placedMapObjects
|
||||||
return this
|
return this
|
||||||
@ -166,22 +148,4 @@ export class Map extends BaseEntity {
|
|||||||
getPlacedMapObjects() {
|
getPlacedMapObjects() {
|
||||||
return this.placedMapObjects
|
return this.placedMapObjects
|
||||||
}
|
}
|
||||||
|
|
||||||
setCharacters(characters: Collection<Character>) {
|
|
||||||
this.characters = characters
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
getCharacters() {
|
|
||||||
return this.characters
|
|
||||||
}
|
|
||||||
|
|
||||||
setChats(chats: Collection<Chat>) {
|
|
||||||
this.chats = chats
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
getChats() {
|
|
||||||
return this.chats
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,7 @@ export default class MapUpdateEvent extends BaseEvent {
|
|||||||
return callback(null)
|
return callback(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
await mapRepository.getEntityManager().populate(map, ['*'])
|
await mapRepository.getEntityManager().populate(map, ['*'])
|
||||||
|
|
||||||
// Validation logic remains the same
|
// 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)
|
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
|
||||||
map.mapEventTiles.removeAll()
|
map.getMapEventTiles().removeAll()
|
||||||
map.placedMapObjects.removeAll()
|
map.getPlacedMapObjects().removeAll()
|
||||||
map.mapEffects.removeAll()
|
map.getMapEffects().removeAll()
|
||||||
|
|
||||||
// Create and add new map event tiles
|
// Create and add new map event tiles
|
||||||
for (const tile of data.mapEventTiles) {
|
for (const tile of data.mapEventTiles) {
|
||||||
@ -90,7 +91,7 @@ export default class MapUpdateEvent extends BaseEvent {
|
|||||||
mapEventTile.setTeleport(teleport)
|
mapEventTile.setTeleport(teleport)
|
||||||
}
|
}
|
||||||
|
|
||||||
map.mapEventTiles.add(mapEventTile)
|
// map.mapEventTiles.add(mapEventTile)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and add new map objects
|
// Create and add new map objects
|
||||||
@ -108,15 +109,19 @@ export default class MapUpdateEvent extends BaseEvent {
|
|||||||
// Create and add new map effects
|
// Create and add new map effects
|
||||||
for (const effect of data.mapEffects) {
|
for (const effect of data.mapEffects) {
|
||||||
const mapEffect = new MapEffect().setEffect(effect.effect).setStrength(effect.strength).setMap(map)
|
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
|
// 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()
|
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
|
||||||
mapRepository = new MapRepository()
|
mapRepository = new MapRepository()
|
||||||
map = await mapRepository.getById(data.mapId)
|
map = await mapRepository.getById(data.mapId)
|
||||||
|
// @ts-ignore
|
||||||
await mapRepository.getEntityManager().populate(map!, ['*'])
|
await mapRepository.getEntityManager().populate(map!, ['*'])
|
||||||
|
|
||||||
if (!map) {
|
if (!map) {
|
||||||
|
@ -28,7 +28,10 @@ class MapRepository extends BaseRepository {
|
|||||||
async getById(id: UUID) {
|
async getById(id: UUID) {
|
||||||
try {
|
try {
|
||||||
const repository = this.getEntityManager().getRepository(Map)
|
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) {
|
} catch (error: any) {
|
||||||
this.logger.error(`Failed to get map by id: ${error.message}`)
|
this.logger.error(`Failed to get map by id: ${error.message}`)
|
||||||
return null
|
return null
|
||||||
|
Loading…
x
Reference in New Issue
Block a user