39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
class PlayerBehaviour {
|
|
constructor(info) {
|
|
this.id = info.id;
|
|
this.size = info.size ?? 32;
|
|
this.entries = Array.from({ length: this.size }, () => ({ isProcessed: false, timestamp: Infinity, entry: {}, id: 0 }));
|
|
this.accumulator = 0;
|
|
this.buffer = info.buffer;
|
|
};
|
|
|
|
add(entry) {
|
|
const index = this.accumulator++ % this.size;
|
|
this.entries[index] = {
|
|
timestamp: Date.now(),
|
|
entry,
|
|
id: this.id,
|
|
isProcessed: false
|
|
};
|
|
}
|
|
|
|
save(threshold) {
|
|
const now = Date.now();
|
|
|
|
for (let i = 0; i < this.size; i++) {
|
|
const entry = this.entries[i];
|
|
if (entry.id === 0) continue;
|
|
if (entry.isProcessed) continue;
|
|
if (entry.timestamp + threshold < now) continue;
|
|
|
|
entry.isProcessed = true;
|
|
// require("../Miscellaneous/winston").error(entry)
|
|
|
|
this.buffer.push(entry.entry); // entry.entry
|
|
};
|
|
|
|
return this.buffer;
|
|
}
|
|
};
|
|
|
|
module.exports = PlayerBehaviour; |