Added logics for a* pathfinding , rotation calculation and anti cheat, npm update.

This commit is contained in:
2024-07-28 00:46:13 +02:00
parent 7f84c4fedf
commit f2fcc67cb6
6 changed files with 242 additions and 31 deletions

View File

@ -2,6 +2,8 @@ import { Server } from 'socket.io'
import { TSocket } from '../../utilities/Types'
import ZoneManager from '../../managers/ZoneManager'
import prisma from '../../utilities/Prisma'
import AStar from '../../utilities/Player/AStar'
import Rotation from '../../utilities/Player/Rotation'
type SocketResponseT = {
position_x: number
@ -18,24 +20,59 @@ export default function (socket: TSocket, io: Server) {
return
}
socket.character.position_x = data.position_x
socket.character.position_y = data.position_y
const start = {
x: socket.character.position_x,
y: socket.character.position_y,
g: 0,
h: 0,
f: 0
}
const end = {
x: data.position_x,
y: data.position_y,
g: 0,
h: 0,
f: 0
}
await prisma.character.update({
where: {
id: socket.character.id
},
data: {
position_x: data.position_x,
position_y: data.position_y
}
})
const grid = await ZoneManager.getGrid(socket.character.zoneId)
ZoneManager.updateCharacterInZone(socket.character.zoneId, socket.character)
console.log(socket.character)
if (grid.length === 0) {
console.log('character:move error', 'Grid not found')
return
}
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
} catch (error: any) {
const path = AStar.findPath(start, end, grid)
if (path.length > 0) {
const finalPosition = path[path.length - 1]
// Calculate the rotation
const rotation = Rotation.calculate(socket.character.position_x, socket.character.position_y, finalPosition.x, finalPosition.y)
socket.character.position_x = finalPosition.x
socket.character.position_y = finalPosition.y
socket.character.rotation = rotation // Assuming character has a rotation property
await prisma.character.update({
where: {
id: socket.character.id
},
data: {
position_x: finalPosition.x,
position_y: finalPosition.y,
rotation: rotation // Update the rotation in the database
}
})
ZoneManager.updateCharacterInZone(socket.character.zoneId, socket.character)
io.in(socket.character.zoneId.toString()).emit('character:moved', socket.character)
console.log(socket.character)
} else {
console.log('character:move error', 'No valid path found')
}
} catch (error) {
console.log('character:move error', error)
}
})