94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
const packetTable = require("../Data/packets");
|
|
const { players } = require("../Game/Game");
|
|
|
|
const actions = [];
|
|
global.actions = actions;
|
|
|
|
const { MongoClient } = require("mongodb");
|
|
let db = 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');
|
|
};
|
|
|
|
(async function() {
|
|
db = await connect();
|
|
global.mongodb = db;
|
|
|
|
return;
|
|
setInterval(async () => {
|
|
const t = [];
|
|
for (let i = 0; i < 32; i++) {
|
|
t.push({
|
|
playerID: 1,
|
|
timestamp: new Date(),
|
|
opcode: Math.floor(Math.random() * 120)
|
|
});
|
|
};
|
|
|
|
const bulkOps = t.map(doc => ({
|
|
insertOne: doc
|
|
}));
|
|
|
|
const n = performance.now();
|
|
await db.collection("Actions").insertMany(t, {
|
|
ordered: false,
|
|
writeConcern: { w: 0 },
|
|
bypassDocumentValidation: true
|
|
});
|
|
|
|
/* await db.collection('Actions').bulkWrite(bulkOps, {
|
|
ordered: false,
|
|
writeConcern: { w: 0 },
|
|
bypassDocumentValidation: true
|
|
});*/
|
|
}, 500);
|
|
})();
|
|
|
|
module.exports = async function (ws, message, isBinary) {
|
|
const player = players[ws.accountID];
|
|
if (!player) return;
|
|
|
|
const isLimitReached = ws.limiter.update();
|
|
if (isLimitReached) return ws.close();
|
|
|
|
if (player.isInactive) return; // this shouldn't happen, though
|
|
|
|
const view = new DataView(message);
|
|
const packetID = view.getUint8(0);
|
|
const packet = packetTable[packetID];
|
|
|
|
if (packetID !== 1 && !player.isAvailable) return;
|
|
if (!packet) return;
|
|
if (packet.expectedSize && packet.expectedSize !== view.byteLength) return console.info("expectedSize", view.byteLength, packet.expectedSize);
|
|
if (!packet.isAvailable) return;
|
|
|
|
player.lastInteractionTimestamp = performance.now();
|
|
|
|
const metadata = packet.handler(ws, view, player) ?? [];
|
|
|
|
if (packetID > 1) {
|
|
const action = {
|
|
ts: new Date(),
|
|
pid: player.id,
|
|
// opcode: packetID,
|
|
// metadata
|
|
};
|
|
|
|
const relevantContext = global.actionContext.getElement(player.id).save(2000);
|
|
relevantContext.push(action);
|
|
console.info("asd", relevantContext.length)
|
|
};
|
|
}; |