1
0
forked from noxious/server

Pathfinding spam fix, persistence character location without saving every step

This commit is contained in:
Zaxiure 2024-09-09 22:22:06 +02:00
parent 293b2be919
commit 592496861a
No known key found for this signature in database
3 changed files with 55 additions and 26 deletions

View File

@ -6,6 +6,9 @@ import prisma from '../../utilities/prisma'
import { ZoneEventTile, ZoneEventTileTeleport } from '@prisma/client'
import Rotation from '../../utilities/character/rotation'
import logger from '../../utilities/logger'
import CharacterManager from '../../managers/characterManager'
import zoneManager from '../../managers/zoneManager'
import ZoneManager from '../../managers/zoneManager'
type ZoneEventTileWithTeleport = ZoneEventTile & {
teleport: ZoneEventTileTeleport
@ -29,22 +32,32 @@ export default class CharacterMove {
}
private async handleCharacterMove({ positionX, positionY }: { positionX: number; positionY: number }): Promise<void> {
const { character } = this.socket
let character = CharacterManager.getCharacterFromSocket(this.socket);
if (!character) {
logger.error('character:move error', 'Character not found')
return
}
if(!character) {
logger.error('character:move error', 'character has not been initialized?');
return;
}
const path = await this.characterMoveService.calculatePath(character, positionX, positionY)
if (!path) {
this.io.in(character.zoneId.toString()).emit('character:moveError', 'No valid path found')
return
}
if(!character.isMoving && character.resetMovement) {
character.resetMovement = false;
}
if (character.isMoving && !character.resetMovement) {
character.resetMovement = true
this.nextPath[character.id] = path
} else {
}
if(!character.isMoving && !character.resetMovement) {
character.isMoving = true;
await this.moveAlongPath(character, path)
}
}
@ -58,16 +71,9 @@ export default class CharacterMove {
// break
// }
if (character.isMoving && character.resetMovement) {
character.isMoving = false
character.resetMovement = false
const nextPath = this.nextPath[character.id]
this.moveAlongPath(character, nextPath)
break
}
if (character.resetMovement) {
if (!character.isMoving) {
character.isMoving = true
break
}
character.rotation = Rotation.calculate(start.x, start.y, end.x, end.y)
@ -98,14 +104,19 @@ export default class CharacterMove {
}
}
await this.characterMoveService.updatePosition(character, end)
this.characterMoveService.updatePosition(character, end)
this.io.in(character.zoneId.toString()).emit('character:move', character)
await this.characterMoveService.applyMovementDelay()
}
if(character.resetMovement) {
character.resetMovement = false;
await this.moveAlongPath(character, this.nextPath[character.id]);
} else {
this.finalizeMovement(character)
}
}
private async handleZoneEventTile(zoneEventTile: ZoneEventTileWithTeleport): Promise<void> {
const { character } = this.socket
@ -122,7 +133,7 @@ export default class CharacterMove {
}
private finalizeMovement(character: ExtendedCharacter): void {
character.isMoving = false
character.isMoving = false;
this.io.in(character.zoneId.toString()).emit('character:move', character)
}
}

View File

@ -1,5 +1,6 @@
import { ExtendedCharacter } from '../utilities/types'
import { ExtendedCharacter, TSocket } from '../utilities/types'
import { Zone } from '@prisma/client'
import prisma from '../utilities/prisma'
class CharacterManager {
private characters!: ExtendedCharacter[];
@ -12,10 +13,27 @@ class CharacterManager {
this.characters = [...this.characters, character]
}
public removeCharacter(character: ExtendedCharacter) {
public async removeCharacter(character: ExtendedCharacter) {
await prisma.character.update({
where: { id: character.id },
data: {
positionX: character.positionX,
positionY: character.positionY,
rotation: character.rotation,
zoneId: character.zoneId
}
})
this.characters = this.characters.filter(x => x.id !== character.id);
}
public getCharacter(characterId: number) {
return this.characters.find((x) => x.id === characterId);
}
public getCharacterFromSocket(socket: TSocket) {
return this.characters.find((x) => x.id === socket?.character?.id);
}
public getCharactersInZone(zone: Zone) {
return this.characters.filter(x => x.zoneId === zone.id);
}

View File

@ -6,7 +6,7 @@ import Rotation from '../../utilities/character/rotation'
import logger from '../../utilities/logger'
export class CharacterMoveService {
public async updatePosition(character: ExtendedCharacter, position: { x: number; y: number }, newZoneId?: number): Promise<void> {
public updatePosition(character: ExtendedCharacter, position: { x: number; y: number }, newZoneId?: number) {
Object.assign(character, {
positionX: position.x,
positionY: position.y,
@ -14,15 +14,15 @@ export class CharacterMoveService {
zoneId: newZoneId || character.zoneId
})
await prisma.character.update({
where: { id: character.id },
data: {
positionX: position.x,
positionY: position.y,
rotation: character.rotation,
zoneId: newZoneId
}
})
// await prisma.character.update({
// where: { id: character.id },
// data: {
// positionX: position.x,
// positionY: position.y,
// rotation: character.rotation,
// zoneId: newZoneId
// }
// })
}
public async calculatePath(character: ExtendedCharacter, targetX: number, targetY: number): Promise<Array<{ x: number; y: number }> | null> {