Almost finalised refactoring

This commit is contained in:
2025-01-03 14:35:02 +01:00
parent fecdf222d7
commit a40b71140a
35 changed files with 257 additions and 410 deletions

View File

@ -10,7 +10,7 @@ type Payload = {
export default class MapCreateEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map_editor:map:create', this.handleEvent.bind(this))
this.socket.on('gm:map:create', this.handleEvent.bind(this))
}
private async handleEvent(data: Payload, callback: (response: Map[]) => void): Promise<void> {
@ -30,7 +30,7 @@ export default class MapCreateEvent extends BaseEvent {
const mapList = await MapRepository.getAll()
return callback(mapList)
} catch (error: any) {
this.logger.error('gm:map_editor:map:create error', error.message)
this.logger.error('gm:map:create error', error.message)
this.socket.emit('notification', { message: 'Failed to create map.' })
return callback([])
}

View File

@ -8,7 +8,7 @@ type Payload = {
export default class MapDeleteEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map_editor:map:delete', this.handleEvent.bind(this))
this.socket.on('gm:map:delete', this.handleEvent.bind(this))
}
private async handleEvent(data: Payload, callback: (response: boolean) => void): Promise<void> {
@ -22,7 +22,7 @@ export default class MapDeleteEvent extends BaseEvent {
this.logger.info(`Map ${data.mapId} deleted successfully.`)
return callback(true)
} catch (error: unknown) {
this.logger.error('gm:map_editor:map:delete error', error)
this.logger.error('gm:map:delete error', error)
return callback(false)
}
}

View File

@ -6,7 +6,7 @@ interface IPayload {}
export default class MapListEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map_editor:map:list', this.handleEvent.bind(this))
this.socket.on('gm:map:list', this.handleEvent.bind(this))
}
private async handleEvent(data: IPayload, callback: (response: Map[]) => void): Promise<void> {
@ -18,7 +18,7 @@ export default class MapListEvent extends BaseEvent {
const maps = await MapRepository.getAll()
return callback(maps)
} catch (error: any) {
this.logger.error('gm:map_editor:map:list error', error.message)
this.logger.error('gm:map:list error', error.message)
return callback([])
}
}

View File

@ -2,6 +2,7 @@ import { BaseEvent } from '#application/base/baseEvent'
import { UUID } from '#application/types'
import { Map } from '#entities/map'
import MapRepository from '#repositories/mapRepository'
import Database from '#application/database'
interface IPayload {
mapId: UUID
@ -9,7 +10,7 @@ interface IPayload {
export default class MapRequestEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map_editor:map:request', this.handleEvent.bind(this))
this.socket.on('gm:map:request', this.handleEvent.bind(this))
}
private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise<void> {
@ -25,14 +26,18 @@ export default class MapRequestEvent extends BaseEvent {
const map = await MapRepository.getById(data.mapId)
if (!map) {
this.logger.info(`User ${(await this.getCharacter())!.getId()} tried to request map ${data.mapId} but it does not exist.`)
return callback(null)
}
console.log(map)
await Database.getEntityManager().populate(map, ['mapEventTiles', 'placedMapObjects'])
return callback(map)
} catch (error: any) {
this.logger.error('gm:map_editor:map:request error', error.message)
this.logger.error('gm:map:request error', error.message)
return callback(null)
}
}

View File

@ -5,9 +5,9 @@ import { Map } from '#entities/map'
import { MapEffect } from '#entities/mapEffect'
import { MapEventTile } from '#entities/mapEventTile'
import { MapEventTileTeleport } from '#entities/mapEventTileTeleport'
import { MapObject } from '#entities/mapObject'
import mapManager from '#managers/mapManager'
import MapRepository from '#repositories/mapRepository'
import { PlacedMapObject } from '#entities/placedMapObject'
interface IPayload {
mapId: UUID
@ -31,12 +31,12 @@ interface IPayload {
effect: string
strength: number
}[]
mapObjects: MapObject[]
placedMapObjects: PlacedMapObject[]
}
export default class MapUpdateEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map_editor:map:update', this.handleEvent.bind(this))
this.socket.on('gm:map:update', this.handleEvent.bind(this))
}
private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise<void> {
@ -70,11 +70,11 @@ 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.mapObjects = data.mapObjects.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
map.mapEventTiles.removeAll()
map.mapObjects.removeAll()
map.placedMapObjects.removeAll()
map.mapEffects.removeAll()
// Create and add new map event tiles
@ -95,16 +95,14 @@ export default class MapUpdateEvent extends BaseEvent {
}
// Create and add new map objects
for (const object of data.mapObjects) {
const mapObject = new MapObject().setMapObject(object.mapObject).setDepth(object.depth).setIsRotated(object.isRotated).setPositionX(object.positionX).setPositionY(object.positionY).setMap(map)
map.mapObjects.add(mapObject)
for (const object of data.placedMapObjects) {
const mapObject = new PlacedMapObject().setMapObject(object.mapObject).setDepth(object.depth).setIsRotated(object.isRotated).setPositionX(object.positionX).setPositionY(object.positionY).setMap(map)
map.placedMapObjects.add(mapObject)
}
// 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)
}
@ -125,7 +123,7 @@ export default class MapUpdateEvent extends BaseEvent {
return callback(map)
} catch (error: any) {
this.logger.error(`gm:map_editor:map:update error: ${error instanceof Error ? error.message : String(error)}`)
this.logger.error(`gm:mapObject:update error: ${error instanceof Error ? error.message : String(error)}`)
return callback(null)
}
}