Attempt to fix performance :(((
This commit is contained in:
@ -4,6 +4,7 @@ import { Character } from '#entities/character'
|
||||
import MapManager from '#managers/mapManager'
|
||||
import SocketManager from '#managers/socketManager'
|
||||
import MapCharacter from '#models/mapCharacter'
|
||||
import MapRepository from '#repositories/mapRepository'
|
||||
|
||||
interface TeleportOptions {
|
||||
targetMapId: UUID
|
||||
@ -18,24 +19,23 @@ class TeleportService {
|
||||
private readonly logger = Logger.type(LoggerType.GAME)
|
||||
|
||||
public async teleportCharacter(characterId: UUID, options: TeleportOptions): Promise<boolean> {
|
||||
const { targetMapId, targetX, targetY, rotation = 0, isInitialJoin = false, character } = options
|
||||
|
||||
const mapRepository = new MapRepository()
|
||||
const socket = SocketManager.getSocketByCharacterId(characterId)
|
||||
const targetMap = MapManager.getMapById(targetMapId)
|
||||
const targetMap = MapManager.getMapById(options.targetMapId)
|
||||
|
||||
if (!socket || !targetMap) {
|
||||
this.logger.error(`Teleport failed - Missing socket or target map for character ${characterId}`)
|
||||
return false
|
||||
}
|
||||
|
||||
if (isInitialJoin && !character) {
|
||||
if (options.isInitialJoin && !options.character) {
|
||||
this.logger.error('Initial join requires character data')
|
||||
return false
|
||||
}
|
||||
|
||||
const existingCharacter = !isInitialJoin && MapManager.getCharacterById(characterId)
|
||||
const mapCharacter = isInitialJoin
|
||||
? new MapCharacter(character!)
|
||||
const existingCharacter = !options.isInitialJoin && MapManager.getCharacterById(characterId)
|
||||
const mapCharacter = options.isInitialJoin
|
||||
? new MapCharacter(options.character!)
|
||||
: existingCharacter ||
|
||||
(() => {
|
||||
this.logger.error(`Teleport failed - Character ${characterId} not found in MapManager`)
|
||||
@ -56,16 +56,19 @@ class TeleportService {
|
||||
}
|
||||
|
||||
// Update character position and map
|
||||
await mapCharacter.character.setPositionX(targetX).setPositionY(targetY).setRotation(rotation).setMap(targetMap.getMap()).save()
|
||||
await mapCharacter.getCharacter().setPositionX(options.targetX).setPositionY(options.targetY).setRotation(options.rotation ?? 0).setMap(targetMap.getMap()).save()
|
||||
|
||||
// Join new map
|
||||
socket.join(targetMapId)
|
||||
targetMap.addCharacter(mapCharacter.character)
|
||||
socket.join(options.targetMapId)
|
||||
targetMap.addCharacter(mapCharacter.getCharacter())
|
||||
|
||||
const map = await mapRepository.getById(options.targetMapId)
|
||||
await mapRepository.getEntityManager().populate(map!, mapRepository.POPULATE_TELEPORT as any)
|
||||
|
||||
// Notify clients
|
||||
io.in(targetMapId).emit('map:character:join', mapCharacter)
|
||||
io.in(options.targetMapId).emit('map:character:join', mapCharacter)
|
||||
socket.emit('map:character:teleport', {
|
||||
map: targetMap.getMap(),
|
||||
map: map,
|
||||
characters: targetMap.getCharactersInMap()
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user