Initial commit
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
const { parentPort } = require("worker_threads");
|
||||
|
||||
const grids = require("../Game/Grids");
|
||||
|
||||
parentPort.on("message", data => {
|
||||
const { position, mapID, id, preSelectedDestinationNode, predefinedArea } = data;
|
||||
const grid = grids[mapID - 1];
|
||||
const obj = grid.getCell(position.x, position.y);
|
||||
const open = [];
|
||||
const close = {};
|
||||
|
||||
let desinationNode = preSelectedDestinationNode ? preSelectedDestinationNode : grid.randomNode(10);
|
||||
|
||||
while (desinationNode.x === position.x && desinationNode.y === position.y)
|
||||
desinationNode = predefinedArea ? grid.randomNodeInArea(predefinedArea) : grid.randomNode(10);
|
||||
|
||||
const destination = grid.getCell(desinationNode.x, desinationNode.y);
|
||||
|
||||
open.push({
|
||||
xw: obj.xw,
|
||||
xh: obj.xh,
|
||||
gCost: 0,
|
||||
hCost: 0,
|
||||
fCost: 0
|
||||
});
|
||||
|
||||
while (open.length > 0) {
|
||||
let current = open.reduce((prev, curr) => prev.hCost < curr.hCost ? prev : curr); // uhhhh
|
||||
open.splice(open.findIndex(s => s === current), 1); // uhh
|
||||
|
||||
close[`${current.xw}-${current.xh}`] = true;
|
||||
|
||||
if (current.xw === destination.xw && current.xh === destination.xh) {
|
||||
const path = backtrace(current, grid);
|
||||
path.shift();
|
||||
|
||||
return parentPort.postMessage({
|
||||
path,
|
||||
id,
|
||||
mapID,
|
||||
messageId: 1
|
||||
});
|
||||
};
|
||||
|
||||
const neighbours = grid.getNodeNeighbours(current);
|
||||
for (let i = 0; i < neighbours.length; i++) {
|
||||
const n = neighbours[i];
|
||||
if (!grid.isValidNode(n)) continue;
|
||||
|
||||
let closed = close[`${n.xw}-${n.xh}`];
|
||||
if (closed) continue;
|
||||
|
||||
let alreadyIn = open.find(c => c.xw === n.xw && c.xh === n.xh);
|
||||
if (alreadyIn) continue;
|
||||
|
||||
n.gCost = current.gCost + 1.4;
|
||||
n.hCost = Math.abs(n.xw - destination.xw) + Math.abs(n.xh - destination.xh);
|
||||
n.fCost = n.gCost + n.hCost;
|
||||
n.parent = current;
|
||||
|
||||
open.push(n);
|
||||
};
|
||||
};
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
function backtrace(node, map) {
|
||||
let coords = map.getCoords(node.xw, node.xh);
|
||||
let path = [[coords.x, coords.y]];
|
||||
|
||||
while (node.parent) {
|
||||
node = node.parent;
|
||||
coords = map.getCoords(node.xw, node.xh);
|
||||
path.push([coords.x, coords.y]);
|
||||
};
|
||||
|
||||
return path.reverse();
|
||||
};
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
const { parentPort } = require("worker_threads");
|
||||
|
||||
const PriorityQueue = require("../Utility/PriorityQueue");
|
||||
|
||||
const grids = require("../Game/Grids");
|
||||
|
||||
const COUNT_NEIGHBOURS = 4;
|
||||
|
||||
parentPort.on("message", d => {
|
||||
const { destination, position, id, mapId, wasMoving, movementRequestTracker } = d;
|
||||
const grid = grids[mapId - 1];
|
||||
|
||||
const start = grid.getCell(position.x, position.y);
|
||||
const open = new PriorityQueue();
|
||||
const openRead = {};
|
||||
const close = {};
|
||||
|
||||
open.enqueue({
|
||||
xw: start.xw,
|
||||
xh: start.xh,
|
||||
gCost: 0,
|
||||
hCost: 0,
|
||||
fCost: 0
|
||||
}, 0);
|
||||
|
||||
while (open.items.length > 0) {
|
||||
const current = open.dequeue();
|
||||
if (current.xw === destination.xw && current.xh === destination.xh) {
|
||||
const path = backtrace(current, grid);
|
||||
const startingPosition = path.shift();
|
||||
|
||||
return parentPort.postMessage({
|
||||
startingPosition,
|
||||
path,
|
||||
id,
|
||||
mapId,
|
||||
wasMoving,
|
||||
movementRequestTracker
|
||||
});
|
||||
};
|
||||
|
||||
close[`${current.xw}-${current.xh}`] = current;
|
||||
|
||||
const neighbours = grid.getNodeNeighbours(current);
|
||||
for (let i = 0; i < COUNT_NEIGHBOURS; i++) {
|
||||
const n = neighbours[i];
|
||||
if (!grid.isValidNode(n)) continue;
|
||||
|
||||
let closed = close[`${n.xw}-${n.xh}`];
|
||||
if (closed) continue;
|
||||
|
||||
let alreadyIn = openRead[`${n.xw}-${n.xh}`];
|
||||
if (alreadyIn) continue;
|
||||
|
||||
n.hCost = Math.abs(n.xw - destination.xw) + Math.abs(n.xh - destination.xh);
|
||||
n.gCost = current.gCost + 1;
|
||||
n.fCost = n.gCost + n.hCost;
|
||||
n.parent = current;
|
||||
|
||||
open.enqueue(n, n.hCost);
|
||||
openRead[`${n.xw}-${n.xh}`] = n;
|
||||
};
|
||||
};
|
||||
});
|
||||
|
||||
function backtrace(node, map) {
|
||||
let coords = map.getCoords(node.xw, node.xh);
|
||||
let path = [[coords.x, coords.y]];
|
||||
|
||||
while (node.parent) {
|
||||
node = node.parent;
|
||||
coords = map.getCoords(node.xw, node.xh);
|
||||
path.push([coords.x, coords.y]);
|
||||
};
|
||||
|
||||
return path.reverse();
|
||||
};
|
||||
@@ -0,0 +1,89 @@
|
||||
const { parentPort } = require("worker_threads");
|
||||
const { MongoClient } = require("mongodb");
|
||||
|
||||
let mongoDB = null;
|
||||
let collectionRewards = null;
|
||||
let collectionFeatures = null
|
||||
|
||||
async function connect() {
|
||||
const client = new MongoClient('mongodb://localhost:27017');
|
||||
await client.connect({
|
||||
maxPoolSize: 100,
|
||||
minPoolSize: 20,
|
||||
maxConnecting: 20,
|
||||
waitQueueTimeoutMS: 0,
|
||||
socketTimeoutMS: 30000,
|
||||
connectTimeoutMS: 10000,
|
||||
retryWrites: false,
|
||||
directConnection: true
|
||||
});
|
||||
|
||||
return client.db("test");
|
||||
};
|
||||
function delay(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
};
|
||||
(async function() {
|
||||
mongoDB = await connect();
|
||||
collectionRewards = mongoDB.collection("Rewards");
|
||||
collectionFeatures = mongoDB.collection("Features");
|
||||
})();
|
||||
|
||||
const rewardsBatch = [];
|
||||
const featuresBatch = [];
|
||||
|
||||
const fns = {
|
||||
1: async function (data) {
|
||||
rewardsBatch.push(data);
|
||||
},
|
||||
2: function (data) {
|
||||
featuresBatch.push(data);
|
||||
},
|
||||
3: async function (data) {
|
||||
const n = performance.now();
|
||||
|
||||
await collectionFeatures.insertMany(data, {
|
||||
ordered: false,
|
||||
writeConcern: { w: 0 },
|
||||
bypassDocumentValidation: true
|
||||
});
|
||||
|
||||
console.info("Written!", performance.now() - n);
|
||||
}
|
||||
};
|
||||
|
||||
const queue = [];
|
||||
|
||||
parentPort.on("message", data => {
|
||||
const fn = fns[data.id];
|
||||
if (!fn) return;
|
||||
|
||||
queue.push(data);
|
||||
});
|
||||
|
||||
setInterval(async () => {
|
||||
const n = performance.now();
|
||||
for (let i = 0; i < queue.length; i++) {
|
||||
const item = queue[i];
|
||||
fns[item.id](item.data);
|
||||
};
|
||||
|
||||
queue.length = 0;
|
||||
|
||||
if (rewardsBatch.length)
|
||||
await collectionRewards.insertMany(rewardsBatch, {
|
||||
ordered: false,
|
||||
writeConcern: { w: 0 },
|
||||
bypassDocumentValidation: true
|
||||
});
|
||||
|
||||
if (featuresBatch.length)
|
||||
await collectionFeatures.insertMany(featuresBatch, {
|
||||
ordered: false,
|
||||
writeConcern: { w: 0 },
|
||||
bypassDocumentValidation: true
|
||||
});
|
||||
|
||||
rewardsBatch.length = 0;
|
||||
featuresBatch.length = 0;
|
||||
}, 60000);
|
||||
Reference in New Issue
Block a user