forked from noxious/server
Attempt fix memory leak
This commit is contained in:
parent
26405433a8
commit
7da0323153
1
.prettierignore
Normal file
1
.prettierignore
Normal file
@ -0,0 +1 @@
|
||||
migrations
|
@ -99,36 +99,34 @@ export default class CharacterMove extends BaseEvent {
|
||||
|
||||
if (!path?.length) return
|
||||
|
||||
try {
|
||||
let lastMoveTime = Date.now()
|
||||
let lastMoveTime = Date.now()
|
||||
let currentTile, nextTile
|
||||
|
||||
try {
|
||||
for (let i = 0; i < path.length - 1; i++) {
|
||||
if (!mapCharacter.isMoving || mapCharacter.currentPath !== path) {
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure minimum time between moves
|
||||
const currentTime = Date.now()
|
||||
const timeSinceLastMove = currentTime - lastMoveTime
|
||||
// Ensure minimum time between moves using a single Date.now() call
|
||||
const timeSinceLastMove = Date.now() - lastMoveTime
|
||||
if (timeSinceLastMove < this.STEP_DELAY) {
|
||||
await new Promise((resolve) => setTimeout(resolve, this.STEP_DELAY - timeSinceLastMove))
|
||||
}
|
||||
|
||||
const [currentTile, nextTile] = [path[i], path[i + 1]]
|
||||
currentTile = path[i]
|
||||
nextTile = path[i + 1]
|
||||
|
||||
if (!currentTile || !nextTile) {
|
||||
if (!currentTile || !nextTile || !this.isValidStep(currentTile, nextTile)) {
|
||||
this.logger.error('Invalid movement step detected')
|
||||
break
|
||||
}
|
||||
|
||||
// Validate step
|
||||
if (!this.isValidStep(currentTile, nextTile)) {
|
||||
this.logger.error('Invalid movement step detected')
|
||||
break
|
||||
}
|
||||
|
||||
// Update character rotation
|
||||
character.setRotation(CharacterService.calculateRotation(currentTile.positionX, currentTile.positionY, nextTile.positionX, nextTile.positionY))
|
||||
// Update character rotation and position in a single operation
|
||||
character
|
||||
.setRotation(CharacterService.calculateRotation(currentTile.positionX, currentTile.positionY, nextTile.positionX, nextTile.positionY))
|
||||
.setPositionX(nextTile.positionX)
|
||||
.setPositionY(nextTile.positionY)
|
||||
|
||||
// Check for map events at the next tile
|
||||
const mapEventTile = await this.checkMapEvents(character, nextTile)
|
||||
@ -140,9 +138,6 @@ export default class CharacterMove extends BaseEvent {
|
||||
}
|
||||
}
|
||||
|
||||
// Move character to next tile
|
||||
character.setPositionX(nextTile.positionX).setPositionY(nextTile.positionY)
|
||||
|
||||
// Broadcast movement
|
||||
this.broadcastMovement(character, true)
|
||||
|
||||
@ -151,9 +146,10 @@ export default class CharacterMove extends BaseEvent {
|
||||
await new Promise((resolve) => setTimeout(resolve, this.STEP_DELAY))
|
||||
}
|
||||
|
||||
// Update last known position and move time
|
||||
this.lastKnownPosition = {
|
||||
x: character.getPositionX(),
|
||||
y: character.getPositionY()
|
||||
x: nextTile.positionX,
|
||||
y: nextTile.positionY
|
||||
}
|
||||
lastMoveTime = Date.now()
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ export default defineConfig({
|
||||
user: serverConfig.DB_USER,
|
||||
password: serverConfig.DB_PASS,
|
||||
dbName: serverConfig.DB_NAME,
|
||||
debug: false,
|
||||
debug: true,
|
||||
driverOptions: {
|
||||
allowPublicKeyRetrieval: true
|
||||
},
|
||||
|
@ -12,16 +12,62 @@ class PriorityQueue<T> {
|
||||
|
||||
enqueue(item: T): void {
|
||||
this.items.push(item)
|
||||
this.items.sort(this.compare)
|
||||
this.siftUp(this.items.length - 1)
|
||||
}
|
||||
|
||||
dequeue(): T | undefined {
|
||||
return this.items.shift()
|
||||
if (this.items.length === 0) return undefined
|
||||
|
||||
const result = this.items[0]
|
||||
const last = this.items.pop()
|
||||
|
||||
if (this.items.length > 0 && last !== undefined) {
|
||||
this.items[0] = last
|
||||
this.siftDown(0)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
get length(): number {
|
||||
return this.items.length
|
||||
}
|
||||
|
||||
private siftUp(index: number): void {
|
||||
while (index > 0) {
|
||||
const parentIndex = Math.floor((index - 1) / 2)
|
||||
if (this.compare(this.items[index]!, this.items[parentIndex]!) < 0) {
|
||||
;[this.items[index], this.items[parentIndex]] = [this.items[parentIndex]!, this.items[index]!]
|
||||
index = parentIndex
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private siftDown(index: number): void {
|
||||
const length = this.items.length
|
||||
while (true) {
|
||||
let minIndex = index
|
||||
const leftChild = 2 * index + 1
|
||||
const rightChild = 2 * index + 2
|
||||
|
||||
if (leftChild < length && this.compare(this.items[leftChild]!, this.items[minIndex]!) < 0) {
|
||||
minIndex = leftChild
|
||||
}
|
||||
if (rightChild < length && this.compare(this.items[rightChild]!, this.items[minIndex]!) < 0) {
|
||||
minIndex = rightChild
|
||||
}
|
||||
|
||||
if (minIndex === index) break
|
||||
;[this.items[index], this.items[minIndex]] = [this.items[minIndex]!, this.items[index]!]
|
||||
index = minIndex
|
||||
}
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.items = []
|
||||
}
|
||||
}
|
||||
|
||||
class CharacterMoveService extends BaseService {
|
||||
@ -144,14 +190,14 @@ class CharacterMoveService extends BaseService {
|
||||
const h = this.getDistance(neighbor, end)
|
||||
const f = g + h
|
||||
|
||||
const node: Node = {...neighbor, g, h, f, parent: current}
|
||||
const node: Node = { ...neighbor, g, h, f, parent: current }
|
||||
openList.enqueue(node)
|
||||
}
|
||||
}
|
||||
|
||||
return [] // No path found
|
||||
} finally {
|
||||
while(openList.length > 0) openList.dequeue()
|
||||
while (openList.length > 0) openList.dequeue()
|
||||
closedSet.clear()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user