-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathglobal.segmentScanner.js
59 lines (48 loc) · 1.73 KB
/
global.segmentScanner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const segmentImports = [
require("segmentimport.trading")
];
module.exports = class SegmentScanner {
static addPartner(name, segment) {
Memory.segmentScanner.partners.push({ name: name, segment: segment });
}
static removePartner(name) {
Memory.segmentScanner.partners = _.filter(Memory.segmentScanner.partners, (p) => p.name !== name);
}
static listPartners() {
for(let partner of Memory.segmentScanner.partners) {
console.log(`Scanning ${partner.name} at segment ${partner.segment}`);
}
}
constructor() {
if(!Memory.segmentScanner) {
Memory.segmentScanner = {
lastScan: 0,
partners: []
}
}
this.memory = Memory.segmentScanner;
if(!this.memory.partners) this.memory.partners = [];
}
run() {
if(this.memory.partners.length === 0) return;
let segment = RawMemory.foreignSegment;
if(segment) {
let data = JSON.parse(segment.data);
for(let importer of segmentImports) {
new importer(data, segment.username).run();
}
} else {
let partner = this.memory.partners[this.memory.lastScan];
console.log(`SegmentScanner: Unexpected empty segment for partner ${partner.name}`);
}
this.scanNext();
}
scanNext() {
let nextScan = (this.memory.lastScan + 1) % this.memory.partners.length;
let partner = this.memory.partners[nextScan];
RawMemory.setActiveForeignSegment(partner.name, partner.segment);
this.memory.lastScan = nextScan;
}
}
const profiler = require("screeps-profiler");
profiler.registerClass(module.exports, 'SegmentScanner');