Renamed zone > map

This commit is contained in:
2025-01-02 17:31:24 +01:00
parent 887da447e0
commit 11041fec83
54 changed files with 871 additions and 895 deletions

View File

@ -0,0 +1,38 @@
import { BaseEvent } from '#application/base/baseEvent'
import { Map } from '#entities/map'
import MapRepository from '#repositories/mapRepository'
type Payload = {
name: string
width: number
height: number
}
export default class MapCreateEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map_editor:map:create', this.handleEvent.bind(this))
}
private async handleEvent(data: Payload, callback: (response: Map[]) => void): Promise<void> {
try {
if (!(await this.isCharacterGM())) return
this.logger.info(`User ${(await this.getCharacter())!.getId()} has created a new map via map editor.`)
const map = new Map()
await map
.setName(data.name)
.setWidth(data.width)
.setHeight(data.height)
.setTiles(Array.from({ length: data.height }, () => Array.from({ length: data.width }, () => 'blank_tile')))
.save()
const mapList = await MapRepository.getAll()
return callback(mapList)
} catch (error: any) {
this.logger.error('gm:map_editor:map:create error', error.message)
this.socket.emit('notification', { message: 'Failed to create map.' })
return callback([])
}
}
}

View File

@ -0,0 +1,29 @@
import { BaseEvent } from '#application/base/baseEvent'
import { UUID } from '#application/types'
import MapRepository from '#repositories/mapRepository'
type Payload = {
mapId: UUID
}
export default class MapDeleteEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map_editor:map:delete', this.handleEvent.bind(this))
}
private async handleEvent(data: Payload, callback: (response: boolean) => void): Promise<void> {
if (!(await this.isCharacterGM())) return
try {
this.logger.info(`Deleting map ${data.mapId}`)
await (await MapRepository.getById(data.mapId))?.delete()
this.logger.info(`Map ${data.mapId} deleted successfully.`)
return callback(true)
} catch (error: unknown) {
this.logger.error('gm:map_editor:map:delete error', error)
return callback(false)
}
}
}

View File

@ -0,0 +1,25 @@
import { BaseEvent } from '#application/base/baseEvent'
import { Map } from '#entities/map'
import MapRepository from '#repositories/mapRepository'
interface IPayload {}
export default class MapListEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map_editor:map:list', this.handleEvent.bind(this))
}
private async handleEvent(data: IPayload, callback: (response: Map[]) => void): Promise<void> {
try {
if (!(await this.isCharacterGM())) return
this.logger.info(`User ${(await this.getCharacter())!.getId()} has created a new map via map editor.`)
const maps = await MapRepository.getAll()
return callback(maps)
} catch (error: any) {
this.logger.error('gm:map_editor:map:list error', error.message)
return callback([])
}
}
}

View File

@ -0,0 +1,39 @@
import { BaseEvent } from '#application/base/baseEvent'
import { UUID } from '#application/types'
import { Map } from '#entities/map'
import MapRepository from '#repositories/mapRepository'
interface IPayload {
mapId: UUID
}
export default class MapRequestEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map_editor:map:request', this.handleEvent.bind(this))
}
private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise<void> {
try {
if (!(await this.isCharacterGM())) return
this.logger.info(`User ${(await this.getCharacter())!.getId()} has requested map via map editor.`)
if (!data.mapId) {
this.logger.info(`User ${(await this.getCharacter())!.getId()} tried to request map but did not provide a map id.`)
return callback(null)
}
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)
}
return callback(map)
} catch (error: any) {
this.logger.error('gm:map_editor:map:request error', error.message)
return callback(null)
}
}
}

View File

@ -0,0 +1,132 @@
import { BaseEvent } from '#application/base/baseEvent'
import { MapEventTileType } from '#application/enums'
import { UUID } from '#application/types'
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'
interface IPayload {
mapId: UUID
name: string
width: number
height: number
tiles: string[][]
pvp: boolean
mapEventTiles: {
type: MapEventTileType
positionX: number
positionY: number
teleport?: {
toMapId: UUID
toPositionX: number
toPositionY: number
toRotation: number
}
}[]
mapEffects: {
effect: string
strength: number
}[]
mapObjects: MapObject[]
}
export default class MapUpdateEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map_editor:map:update', this.handleEvent.bind(this))
}
private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise<void> {
try {
if (!(await this.isCharacterGM())) return
const character = await this.getCharacter()
this.logger.info(`User ${character!.getId()} has updated map via map editor.`)
if (!data.mapId) {
this.logger.info(`User ${character!.getId()} tried to update map but did not provide a map id.`)
return callback(null)
}
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)
}
// Validation logic remains the same
if (data.tiles.length > data.height) {
data.tiles = data.tiles.slice(0, data.height)
}
for (let i = 0; i < data.tiles.length; i++) {
if (data.tiles[i].length > data.width) {
data.tiles[i] = data.tiles[i].slice(0, data.width)
}
}
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)
// Clear existing collections
map.mapEventTiles.removeAll()
map.mapObjects.removeAll()
map.mapEffects.removeAll()
// Create and add new map event tiles
for (const tile of data.mapEventTiles) {
const mapEventTile = new MapEventTile().setType(tile.type).setPositionX(tile.positionX).setPositionY(tile.positionY).setMap(map)
if (tile.teleport) {
const teleport = new MapEventTileTeleport()
.setToMap((await MapRepository.getById(tile.teleport.toMapId))!)
.setToPositionX(tile.teleport.toPositionX)
.setToPositionY(tile.teleport.toPositionY)
.setToRotation(tile.teleport.toRotation)
mapEventTile.setTeleport(teleport)
}
map.mapEventTiles.add(mapEventTile)
}
// 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)
}
// 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)
}
// Update map properties
await map.setName(data.name).setWidth(data.width).setHeight(data.height).setTiles(data.tiles).setPvp(data.pvp).setUpdatedAt(new Date()).update()
// Reload map from database to get fresh data
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 after update.`)
return callback(null)
}
// Reload map for players
mapManager.unloadMap(data.mapId)
await mapManager.loadMap(map)
return callback(map)
} catch (error: any) {
this.logger.error(`gm:map_editor:map:update error: ${error instanceof Error ? error.message : String(error)}`)
return callback(null)
}
}
}