1
0
forked from noxious/server
Files
noxious_server/src/events/map/characterAttack.ts
2025-02-16 21:22:49 +01:00

35 lines
1.1 KiB
TypeScript

import { BaseEvent } from '@/application/base/baseEvent'
import { SocketEvent } from '@/application/enums'
import CharacterAttackService from '@/services/characterAttackService'
export default class CharacterMove extends BaseEvent {
private readonly characterAttackService = CharacterAttackService
public listen(): void {
this.socket.on(SocketEvent.MAP_CHARACTER_ATTACK, this.handleEvent.bind(this))
}
private async handleEvent(data: any, callback: (response: any) => void): Promise<void> {
try {
if (!this.socket.characterId) {
this.logger.error('map:character:attack error: Character not found or not initialized')
return
}
// Don't attack if the character is already moving
if (this.getMapCharacter()?.isMoving) return
const throttleKey = `attack_${this.socket.characterId}`
if (this.isThrottled(throttleKey, 1000)) {
return
}
// Start attack
await this.characterAttackService.attack(this.socket.characterId!)
} catch (error) {
this.logger.error('map:character:attack error', error)
return callback(false)
}
}
}