Initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
const cache = {};
|
||||
|
||||
const opcodesInterestedIn = {
|
||||
// 200: true, // ammunition change
|
||||
164: true, // item usage
|
||||
163: true, // gets attacked
|
||||
162: true, // authorless hp change
|
||||
160: true, // design change
|
||||
151: true, // map change state
|
||||
148: true, // equipment change
|
||||
131: true, // rewards
|
||||
133: true, // entity disappears
|
||||
134: true // entity appears
|
||||
};
|
||||
|
||||
function packet(packetsToAdd, batched, id, metadataToAdd) {
|
||||
if (!(packetsToAdd instanceof Array)) packetsToAdd = [packetsToAdd];
|
||||
|
||||
if (!cache[id]) cache[id] = new ArrayBuffer(1024, { maxByteLength: 65536 });
|
||||
|
||||
const playerBehaviourContext = global.actionContext.getElement(id);
|
||||
|
||||
const targetBuffer = cache[id];
|
||||
const targetArray = new Uint8Array(targetBuffer);
|
||||
let offset = batched.offset;
|
||||
|
||||
packetsToAdd.forEach((packet, i) => {
|
||||
const newSize = offset + packet.byteLength;
|
||||
if (newSize > targetBuffer.byteLength) {
|
||||
const newBufferSize = Math.pow(2, Math.ceil(Math.log(newSize) / Math.log(2)));
|
||||
targetBuffer.resize(newBufferSize);
|
||||
};
|
||||
|
||||
const newArray = new Uint8Array(packet);
|
||||
if (opcodesInterestedIn[newArray[0]]) {
|
||||
const metadata = {
|
||||
ts: new Date(),
|
||||
pid: id,
|
||||
metadata: metadataToAdd && metadataToAdd[i] ? metadataToAdd[i] : {}
|
||||
};
|
||||
|
||||
playerBehaviourContext.add(metadata);
|
||||
};
|
||||
|
||||
targetArray.set(newArray, offset);
|
||||
offset += packet.byteLength;
|
||||
});
|
||||
|
||||
return {
|
||||
packet: targetBuffer,
|
||||
offset
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = packet;
|
||||
Reference in New Issue
Block a user