Added logics for a* pathfinding , rotation calculation and anti cheat, npm update.
This commit is contained in:
parent
7f84c4fedf
commit
f2fcc67cb6
30
package-lock.json
generated
30
package-lock.json
generated
@ -1203,9 +1203,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/engine.io/node_modules/debug": {
|
||||
"version": "4.3.5",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.5.tgz",
|
||||
"integrity": "sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==",
|
||||
"version": "4.3.6",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.6.tgz",
|
||||
"integrity": "sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ms": "2.1.2"
|
||||
@ -1792,9 +1792,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/nodemon/node_modules/debug": {
|
||||
"version": "4.3.5",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.5.tgz",
|
||||
"integrity": "sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==",
|
||||
"version": "4.3.6",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.6.tgz",
|
||||
"integrity": "sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
@ -2206,9 +2206,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-adapter/node_modules/debug": {
|
||||
"version": "4.3.5",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.5.tgz",
|
||||
"integrity": "sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==",
|
||||
"version": "4.3.6",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.6.tgz",
|
||||
"integrity": "sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ms": "2.1.2"
|
||||
@ -2242,9 +2242,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-parser/node_modules/debug": {
|
||||
"version": "4.3.5",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.5.tgz",
|
||||
"integrity": "sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==",
|
||||
"version": "4.3.6",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.6.tgz",
|
||||
"integrity": "sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ms": "2.1.2"
|
||||
@ -2265,9 +2265,9 @@
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/socket.io/node_modules/debug": {
|
||||
"version": "4.3.5",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.5.tgz",
|
||||
"integrity": "sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==",
|
||||
"version": "4.3.6",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.6.tgz",
|
||||
"integrity": "sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ms": "2.1.2"
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Character, Zone } from '@prisma/client'
|
||||
import ZoneRepository from '../repositories/ZoneRepository'
|
||||
import ZoneService from '../services/ZoneService'
|
||||
import zoneRepository from '../repositories/ZoneRepository'
|
||||
|
||||
type TLoadedZone = {
|
||||
zone: Zone
|
||||
@ -88,6 +89,44 @@ class ZoneManager {
|
||||
})
|
||||
return loadedZone ? loadedZone.characters : []
|
||||
}
|
||||
|
||||
public async getGrid(zoneId: number): Promise<number[][]> {
|
||||
const zone = this.loadedZones.find((z) => z.zone.id === zoneId)
|
||||
let grid: number[][] = []
|
||||
if (zone) {
|
||||
const eventTiles = await zoneRepository.getEventTiles(zoneId)
|
||||
const objects = await zoneRepository.getObjects(zoneId)
|
||||
|
||||
// Create a grid based on the event tiles
|
||||
for (let i = 0; i < zone.zone.width; i++) {
|
||||
grid.push(Array(zone.zone.height).fill(0))
|
||||
}
|
||||
|
||||
// Set the grid values based on the event tiles, these are strings
|
||||
eventTiles.forEach((eventTile) => {
|
||||
if (eventTile.type === 'BLOCK') {
|
||||
grid[eventTile.position_x][eventTile.position_y] = 0
|
||||
} else if (eventTile.type === 'WARP') {
|
||||
grid[eventTile.position_x][eventTile.position_y] = 1
|
||||
} else if (eventTile.type === 'NPC') {
|
||||
grid[eventTile.position_x][eventTile.position_y] = 2
|
||||
} else if (eventTile.type === 'ITEM') {
|
||||
grid[eventTile.position_x][eventTile.position_y] = 3
|
||||
}
|
||||
});
|
||||
|
||||
objects.forEach((object) => {
|
||||
if (object.objectId === 'blank_tile') {
|
||||
grid[object.position_x][object.position_y] = 0
|
||||
} else {
|
||||
grid[object.position_x][object.position_y] = 1
|
||||
}
|
||||
});
|
||||
|
||||
return grid
|
||||
}
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export default new ZoneManager()
|
||||
|
@ -20,7 +20,7 @@ class ZoneRepository {
|
||||
}
|
||||
}
|
||||
|
||||
async getById(id: number): Promise<Zone | null> {
|
||||
async getById(id: number) {
|
||||
try {
|
||||
return await prisma.zone.findUnique({
|
||||
where: {
|
||||
@ -44,6 +44,32 @@ class ZoneRepository {
|
||||
throw new Error(`Failed to get zone by id: ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
async getEventTiles(id: number) {
|
||||
try {
|
||||
return await prisma.zoneEventTile.findMany({
|
||||
where: {
|
||||
zoneId: id
|
||||
}
|
||||
})
|
||||
} catch (error: any) {
|
||||
// Handle error
|
||||
throw new Error(`Failed to get zone event tiles: ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
async getObjects(id: number) {
|
||||
try {
|
||||
return await prisma.zoneObject.findMany({
|
||||
where: {
|
||||
zoneId: id
|
||||
}
|
||||
})
|
||||
} catch (error: any) {
|
||||
// Handle error
|
||||
throw new Error(`Failed to get zone objects: ${error.message}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new ZoneRepository()
|
||||
|
84
src/utilities/Player/AStar.ts
Normal file
84
src/utilities/Player/AStar.ts
Normal file
@ -0,0 +1,84 @@
|
||||
// utilities/AStar.ts
|
||||
|
||||
type Node = {
|
||||
x: number
|
||||
y: number
|
||||
parent?: Node
|
||||
g: number
|
||||
h: number
|
||||
f: number
|
||||
}
|
||||
|
||||
class AStar {
|
||||
static findPath(start: Node, end: Node, grid: number[][]): Node[] {
|
||||
// Initialize the open and closed lists
|
||||
let openList: Node[] = []
|
||||
let closedList: Node[] = []
|
||||
|
||||
// Push the start node onto the open list
|
||||
openList.push(start)
|
||||
|
||||
while (openList.length > 0) {
|
||||
// Get the node with the lowest f value
|
||||
let currentNode = openList.reduce((prev, curr) => (prev.f < curr.f ? prev : curr))
|
||||
|
||||
// Move the current node to the closed list
|
||||
openList = openList.filter((node) => node !== currentNode)
|
||||
closedList.push(currentNode)
|
||||
|
||||
// Check if we've reached the end
|
||||
if (currentNode.x === end.x && currentNode.y === end.y) {
|
||||
let path: Node[] = []
|
||||
let curr = currentNode
|
||||
while (curr) {
|
||||
path.push(curr)
|
||||
curr = curr.parent as Node
|
||||
}
|
||||
return path.reverse()
|
||||
}
|
||||
|
||||
// Get the neighboring nodes
|
||||
let neighbors = this.getNeighbors(currentNode, grid)
|
||||
for (let neighbor of neighbors) {
|
||||
if (closedList.some((node) => node.x === neighbor.x && node.y === neighbor.y)) {
|
||||
continue
|
||||
}
|
||||
|
||||
neighbor.g = currentNode.g + 1
|
||||
neighbor.h = Math.abs(neighbor.x - end.x) + Math.abs(neighbor.y - end.y)
|
||||
neighbor.f = neighbor.g + neighbor.h
|
||||
neighbor.parent = currentNode
|
||||
|
||||
if (!openList.some((node) => node.x === neighbor.x && node.y === neighbor.y)) {
|
||||
openList.push(neighbor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No path found
|
||||
return []
|
||||
}
|
||||
|
||||
private static getNeighbors(node: Node, grid: number[][]): Node[] {
|
||||
let neighbors: Node[] = []
|
||||
let directions = [
|
||||
{ x: 0, y: -1 },
|
||||
{ x: 0, y: 1 },
|
||||
{ x: -1, y: 0 },
|
||||
{ x: 1, y: 0 }
|
||||
]
|
||||
|
||||
for (let direction of directions) {
|
||||
let x = node.x + direction.x
|
||||
let y = node.y + direction.y
|
||||
|
||||
if (x >= 0 && y >= 0 && x < grid.length && y < grid[0].length && grid[x][y] === 0) {
|
||||
neighbors.push({ x, y, g: 0, h: 0, f: 0 })
|
||||
}
|
||||
}
|
||||
|
||||
return neighbors
|
||||
}
|
||||
}
|
||||
|
||||
export default AStar
|
25
src/utilities/Player/Rotation.ts
Normal file
25
src/utilities/Player/Rotation.ts
Normal file
@ -0,0 +1,25 @@
|
||||
class Rotation {
|
||||
static calculate(X1: number, Y1: number, X2: number, Y2: number): number {
|
||||
let rotation = 0
|
||||
if (X1 > X2 && Y1 > Y2) {
|
||||
rotation = 7
|
||||
} else if (X1 < X2 && Y1 < Y2) {
|
||||
rotation = 3
|
||||
} else if (X1 > X2 && Y1 < Y2) {
|
||||
rotation = 5
|
||||
} else if (X1 < X2 && Y1 > Y2) {
|
||||
rotation = 1
|
||||
} else if (X1 > X2) {
|
||||
rotation = 6
|
||||
} else if (X1 < X2) {
|
||||
rotation = 2
|
||||
} else if (Y1 < Y2) {
|
||||
rotation = 4
|
||||
} else if (Y1 > Y2) {
|
||||
rotation = 0
|
||||
}
|
||||
return rotation
|
||||
}
|
||||
}
|
||||
|
||||
export default Rotation
|
Loading…
x
Reference in New Issue
Block a user