Disconnect fix, move improvements, baseEvent improvement
This commit is contained in:
parent
e40a56825a
commit
deac2892fb
@ -28,6 +28,7 @@ export abstract class BaseEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected async getCharacter(): Promise<Character | null> {
|
protected async getCharacter(): Promise<Character | null> {
|
||||||
|
if (!this.socket.characterId) return null
|
||||||
const characterRepository = new CharacterRepository()
|
const characterRepository = new CharacterRepository()
|
||||||
return characterRepository.getById(this.socket.characterId)
|
return characterRepository.getById(this.socket.characterId)
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import MapManager from '@/managers/mapManager'
|
|||||||
|
|
||||||
export default class DisconnectEvent extends BaseEvent {
|
export default class DisconnectEvent extends BaseEvent {
|
||||||
public listen(): void {
|
public listen(): void {
|
||||||
this.socket.on(SocketEvent.DISCONNECT, this.handleEvent.bind(this))
|
this.socket.on('disconnect', this.handleEvent.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handleEvent(): Promise<void> {
|
private async handleEvent(): Promise<void> {
|
||||||
@ -16,7 +16,7 @@ export default class DisconnectEvent extends BaseEvent {
|
|||||||
|
|
||||||
this.io.emit(SocketEvent.USER_DISCONNECT, this.socket.userId)
|
this.io.emit(SocketEvent.USER_DISCONNECT, this.socket.userId)
|
||||||
|
|
||||||
const mapCharacter = MapManager.getCharacterById(this.socket.characterId)
|
const mapCharacter = MapManager.getCharacterById(this.socket.characterId!)
|
||||||
if (!mapCharacter) {
|
if (!mapCharacter) {
|
||||||
this.logger.info('User disconnected but had no character set')
|
this.logger.info('User disconnected but had no character set')
|
||||||
return
|
return
|
||||||
|
@ -9,41 +9,33 @@ import TeleportService from '@/services/characterTeleportService'
|
|||||||
|
|
||||||
export default class CharacterMove extends BaseEvent {
|
export default class CharacterMove extends BaseEvent {
|
||||||
private readonly characterService = CharacterService
|
private readonly characterService = CharacterService
|
||||||
private readonly MOVEMENT_THROTTLE = 230 // Minimum time between movement requests
|
private readonly STEP_DELAY = 150 // Milliseconds between each tile movement
|
||||||
|
private readonly THROTTLE_DELAY = 230 // Minimum time between movement requests
|
||||||
|
private movementTimeout: NodeJS.Timeout | null = null
|
||||||
|
|
||||||
public listen(): void {
|
public listen(): void {
|
||||||
this.socket.on(SocketEvent.MAP_CHARACTER_MOVE, this.handleEvent.bind(this))
|
this.socket.on(SocketEvent.MAP_CHARACTER_MOVE, this.handleEvent.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handleEvent({ positionX, positionY }: { positionX: number; positionY: number }): Promise<void> {
|
private async handleEvent({ positionX, positionY }: { positionX: number; positionY: number }): Promise<void> {
|
||||||
const mapCharacter = MapManager.getCharacterById(this.socket.characterId)
|
try {
|
||||||
|
const mapCharacter = MapManager.getCharacterById(this.socket.characterId!)
|
||||||
if (!mapCharacter?.getCharacter()) {
|
if (!mapCharacter?.getCharacter()) {
|
||||||
this.logger.error('map:character:move error: Character not found or not initialized')
|
this.logger.error('map:character:move error: Character not found or not initialized')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isThrottled(`movement_${this.socket.characterId}`, this.MOVEMENT_THROTTLE)) {
|
// Cancel any ongoing movement
|
||||||
// Only cancel current movement if the new target is different
|
this.cancelCurrentMovement(mapCharacter)
|
||||||
if (mapCharacter.isMoving && (Math.floor(positionX) !== Math.floor(mapCharacter.character.positionX) || Math.floor(positionY) !== Math.floor(mapCharacter.character.positionY))) {
|
|
||||||
mapCharacter.isMoving = false
|
// Throttle rapid movement requests
|
||||||
mapCharacter.currentPath = null
|
if (this.isThrottled(`movement_${this.socket.characterId}`, this.THROTTLE_DELAY)) {
|
||||||
// this.finalizeMovement(mapCharacter)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If already moving, cancel current movement
|
// Calculate path to target position
|
||||||
if (mapCharacter.isMoving && mapCharacter.currentPath && mapCharacter.currentPath.length > 2) {
|
const path = await this.characterService.calculatePath(mapCharacter.character, Math.floor(positionX), Math.floor(positionY))
|
||||||
mapCharacter.isMoving = false
|
|
||||||
mapCharacter.currentPath = null
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate target position is within reasonable range
|
|
||||||
const currentX = mapCharacter.character.positionX
|
|
||||||
const currentY = mapCharacter.character.positionY
|
|
||||||
const distance = Math.sqrt(Math.pow(positionX - currentX, 2) + Math.pow(positionY - currentY, 2))
|
|
||||||
|
|
||||||
const path = await this.characterService.calculatePath(mapCharacter.character, positionX, positionY)
|
|
||||||
if (!path?.length) {
|
if (!path?.length) {
|
||||||
this.io.in(mapCharacter.character.map.id).emit(SocketEvent.MAP_CHARACTER_MOVEERROR, 'No valid path found')
|
this.io.in(mapCharacter.character.map.id).emit(SocketEvent.MAP_CHARACTER_MOVEERROR, 'No valid path found')
|
||||||
return
|
return
|
||||||
@ -52,11 +44,24 @@ export default class CharacterMove extends BaseEvent {
|
|||||||
// Start new movement
|
// Start new movement
|
||||||
mapCharacter.isMoving = true
|
mapCharacter.isMoving = true
|
||||||
mapCharacter.currentPath = path
|
mapCharacter.currentPath = path
|
||||||
await this.moveAlongPath(mapCharacter, path)
|
await this.moveAlongPath(mapCharacter)
|
||||||
|
} catch (error: any) {
|
||||||
|
this.logger.error('map:character:move error: ' + error.message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async moveAlongPath(mapCharacter: MapCharacter, path: Array<{ positionX: number; positionY: number }>): Promise<void> {
|
private cancelCurrentMovement(mapCharacter: MapCharacter): void {
|
||||||
|
if (mapCharacter.isMoving) {
|
||||||
|
mapCharacter.isMoving = false
|
||||||
|
mapCharacter.currentPath = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async moveAlongPath(mapCharacter: MapCharacter): Promise<void> {
|
||||||
const character = mapCharacter.getCharacter()
|
const character = mapCharacter.getCharacter()
|
||||||
|
const path = mapCharacter.currentPath
|
||||||
|
|
||||||
|
if (!path?.length) return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (let i = 0; i < path.length - 1; i++) {
|
for (let i = 0; i < path.length - 1; i++) {
|
||||||
@ -64,47 +69,42 @@ export default class CharacterMove extends BaseEvent {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const [start, end] = [path[i], path[i + 1]]
|
const [currentTile, nextTile] = [path[i], path[i + 1]]
|
||||||
|
|
||||||
if (!start || !end) {
|
if (!currentTile || !nextTile) {
|
||||||
this.logger.error('Invalid path step detected')
|
this.logger.error('Invalid movement step detected')
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i !== 0) {
|
// Validate step
|
||||||
await this.characterService.applyMovementDelay()
|
if (!this.isValidStep(currentTile, nextTile)) {
|
||||||
}
|
this.logger.error('Invalid movement step detected')
|
||||||
|
|
||||||
// Validate each step
|
|
||||||
if (Math.abs(end.positionX - start.positionX) > 1 || Math.abs(end.positionY - start.positionY) > 1) {
|
|
||||||
this.logger.error('Invalid path step detected')
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
character.setRotation(CharacterService.calculateRotation(start.positionX, start.positionY, end.positionX, end.positionY))
|
// Update character rotation
|
||||||
|
character.setRotation(CharacterService.calculateRotation(currentTile.positionX, currentTile.positionY, nextTile.positionX, nextTile.positionY))
|
||||||
|
|
||||||
const mapEventTileRepository = new MapEventTileRepository()
|
// Check for map events at the next tile
|
||||||
const mapEventTile = await mapEventTileRepository.getEventTileByMapIdAndPosition(character.getMap().getId(), Math.floor(end.positionX), Math.floor(end.positionY))
|
const mapEventTile = await this.checkMapEvents(character, nextTile)
|
||||||
|
if (mapEventTile) {
|
||||||
if (mapEventTile?.type === 'BLOCK') break
|
if (mapEventTile.type === 'BLOCK') break
|
||||||
if (mapEventTile?.type === 'TELEPORT' && mapEventTile.teleport) {
|
if (mapEventTile.type === 'TELEPORT' && mapEventTile.teleport) {
|
||||||
await this.handleTeleportMapEventTile(mapEventTile as MapEventTileWithTeleport)
|
await this.handleTeleportMapEventTile(mapEventTile as MapEventTileWithTeleport)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update position first
|
// Move character to next tile
|
||||||
character.setPositionX(end.positionX).setPositionY(end.positionY)
|
character.setPositionX(nextTile.positionX).setPositionY(nextTile.positionY)
|
||||||
|
|
||||||
// Then emit with the same properties
|
// Broadcast movement
|
||||||
this.io.in(character.map.id).emit(SocketEvent.MAP_CHARACTER_MOVE, {
|
this.broadcastMovement(character, true)
|
||||||
characterId: character.id,
|
|
||||||
positionX: character.getPositionX(),
|
|
||||||
positionY: character.getPositionY(),
|
|
||||||
rotation: character.getRotation(),
|
|
||||||
isMoving: true
|
|
||||||
})
|
|
||||||
|
|
||||||
await this.characterService.applyMovementDelay()
|
// Apply movement delay between steps
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, this.STEP_DELAY))
|
||||||
|
if (i < path.length - 2) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (mapCharacter.isMoving && mapCharacter.currentPath === path) {
|
if (mapCharacter.isMoving && mapCharacter.currentPath === path) {
|
||||||
@ -113,25 +113,52 @@ export default class CharacterMove extends BaseEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private isValidStep(current: { positionX: number; positionY: number }, next: { positionX: number; positionY: number }): boolean {
|
||||||
|
return Math.abs(next.positionX - current.positionX) <= 1 && Math.abs(next.positionY - current.positionY) <= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkMapEvents(character: any, nextTile: { positionX: number; positionY: number }) {
|
||||||
|
const mapEventTileRepository = new MapEventTileRepository()
|
||||||
|
return mapEventTileRepository.getEventTileByMapIdAndPosition(character.getMap().getId(), Math.floor(nextTile.positionX), Math.floor(nextTile.positionY))
|
||||||
|
}
|
||||||
|
|
||||||
private async handleTeleportMapEventTile(mapEventTile: MapEventTileWithTeleport): Promise<void> {
|
private async handleTeleportMapEventTile(mapEventTile: MapEventTileWithTeleport): Promise<void> {
|
||||||
if (mapEventTile.getTeleport()) {
|
const teleport = mapEventTile.getTeleport()
|
||||||
await TeleportService.teleportCharacter(this.socket.characterId, {
|
if (teleport) {
|
||||||
targetMapId: mapEventTile.getTeleport()!.getToMap().getId(),
|
await TeleportService.teleportCharacter(this.socket.characterId!, {
|
||||||
targetX: mapEventTile.getTeleport()!.getToPositionX(),
|
targetMapId: teleport.getToMap().getId(),
|
||||||
targetY: mapEventTile.getTeleport()!.getToPositionY(),
|
targetX: teleport.getToPositionX(),
|
||||||
rotation: mapEventTile.getTeleport()!.getToRotation()
|
targetY: teleport.getToPositionY(),
|
||||||
|
rotation: teleport.getToRotation()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private finalizeMovement(mapCharacter: MapCharacter): void {
|
private broadcastMovement(character: any, isMoving: boolean): void {
|
||||||
mapCharacter.isMoving = false
|
this.io.in(character.map.id).emit(SocketEvent.MAP_CHARACTER_MOVE, {
|
||||||
this.io.in(mapCharacter.character.map.id).emit(SocketEvent.MAP_CHARACTER_MOVE, {
|
characterId: character.id,
|
||||||
characterId: mapCharacter.character.id,
|
positionX: character.getPositionX(),
|
||||||
positionX: mapCharacter.character.positionX,
|
positionY: character.getPositionY(),
|
||||||
positionY: mapCharacter.character.positionY,
|
rotation: character.getRotation(),
|
||||||
rotation: mapCharacter.character.rotation,
|
isMoving
|
||||||
isMoving: mapCharacter.isMoving
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private finalizeMovement(mapCharacter: MapCharacter): void {
|
||||||
|
// Clear any existing timeout
|
||||||
|
if (this.movementTimeout) {
|
||||||
|
clearTimeout(this.movementTimeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set new timeout
|
||||||
|
this.movementTimeout = setTimeout(() => {
|
||||||
|
// Only finalize if there are no pending movements
|
||||||
|
if (mapCharacter.currentPath === null) {
|
||||||
|
mapCharacter.isMoving = false
|
||||||
|
this.broadcastMovement(mapCharacter.character, false)
|
||||||
|
}
|
||||||
|
}, this.STEP_DELAY)
|
||||||
|
|
||||||
|
mapCharacter.currentPath = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user