-
Notifications
You must be signed in to change notification settings - Fork 7
/
global.shardTravel.js
71 lines (55 loc) · 2.29 KB
/
global.shardTravel.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
60
61
62
63
64
65
66
67
68
69
70
71
const sourceShardRegex = /-([A-Za-z0-9]+)$/;
// used to test whether we are on the public MMO server
// or (e.g.) a private server
const publicShardRegex = /^shard[0-9]+$/;
// TODO: proper caching:
// * only load remote shard memory once per Tick
// * only load local shard memory once per Tick
function loadLocalTravelMemory() {
let segment = JSON.parse(InterShardMemory.getLocal() || "{}");
return segment.travels || { departures: {} };
}
function loadRemoteTravelMemory(shardId) {
let segment = JSON.parse(InterShardMemory.getRemote(shardId) || "{}");
return segment.travels || { departures: {} };
}
function storeLocalTravelMemory(memory) {
let segment = JSON.parse(InterShardMemory.getLocal() || "{}");
segment.travels = memory;
InterShardMemory.setLocal(JSON.stringify(segment));
}
function interShardAvailable() {
return publicShardRegex.exec(Game.shard.name);
}
module.exports = class ShardTravel {
static loadArrivals() {
if(!interShardAvailable()) return;
let arrivals = _.filter(Game.creeps, (c) => !c.memory.role);
for(let creep of arrivals) {
let sourceShard = (sourceShardRegex.exec(creep.name) || [])[1];
if(!sourceShard) continue;
if(sourceShard === Game.shard.name) continue;
let departures = loadRemoteTravelMemory(sourceShard).departures;
if(!departures[creep.name]) continue;
creep.memory = departures[creep.name].memory;
}
}
static announceDepartures() {
if(!interShardAvailable()) return;
let travels = loadLocalTravelMemory();
let departingCreeps = _.filter(Game.creeps, (c) => c.memory.destinationShard && c.memory.destinationShard !== Game.shard.name);
for(let creep of departingCreeps) {
travels.departures[creep.name] = {
memory: creep.memory,
eol: Game.time + creep.ticksToLive
};
}
let endOfLifeCreeps = _.compact(_.map(travels.departures, (info, name) => info.eol < Game.time && name));
for(let creepName of endOfLifeCreeps) {
delete travels.departures[creepName];
}
storeLocalTravelMemory(travels);
}
}
const profiler = require("screeps-profiler");
profiler.registerClass(module.exports, 'ShardTravel');