-
Notifications
You must be signed in to change notification settings - Fork 38
/
statistics.js
111 lines (100 loc) · 5.66 KB
/
statistics.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var mod = {
storedStatisticsTime: (Memory.statistics && Memory.statistics.time ? Memory.statistics.time : 0 ),
loop: function(){
if( _.isUndefined(Memory.statistics.reports) )
Memory.statistics.reports = []
if( this.storedStatisticsTime > 0 ) {
var message = '<h3><b>Status report</b></h3> '
+ '<h4>at ' + DateTimeString(LocalDate()) + ',<br/>'
+ 'comparison to state before: ' + this.toTimeSpanString(new Date(), new Date(this.storedStatisticsTime)) + ' (' + (Game.time - Memory.statistics.tick) + ' loops)</h4>';
if( Game.cpu.bucket ){
bucketDif = Game.cpu.bucket - Memory.statistics.bucket;
message += 'CPU Bucket: ' + Game.cpu.bucket + ' (' + (bucketDif >= 0 ? '+' : '' ) + bucketDif + ')';
}
Memory.statistics.reports.push(message);
}
var invaderReport = invader => {
let snip = '<li>' + invader.owner + ': ' + invader.body.replace(/"/g,'');
if( invader.leave === undefined ) snip += " since " + TimeString(LocalDate(new Date(invader.time))) + '</li>';
else snip += " for " + (invader.leave - invader.enter) + ' loops at ' + TimeString(LocalDate(new Date(invader.time))) + '</li>';
if( message.length + snip.length > 1000 ){
Memory.statistics.reports.push(message);
message = snip;
} else message += snip;
};
var processRoom = room => {
if( this.storedStatisticsTime > 0 ) { // send Report
if( room.controller && room.controller.my ){
// controller
message = '<ul><li><b>Room ' + room.name + '</b><br/><u>Controller</u><ul>';
var isUpgraded = room.controller.progress < room.memory.statistics.controllerProgress;
var cdif = isUpgraded ? (room.memory.statistics.controllerProgressTotal - room.memory.statistics.controllerProgress) + room.controller.progress : (room.controller.progress - room.memory.statistics.controllerProgress);
message += '<li>Level ' + room.controller.level + ', ' + (100*room.controller.progress/room.controller.progressTotal).toFixed(0).toString() + '% of ' + room.controller.progressTotal + ' (+' + cdif + ')' + (isUpgraded ? ' <b><i>Upgraded!</i></b></li></ul>' : '</li></ul>');
// storage
if( room.storage && room.memory.statistics.store ){
var memoryStoreRecord = room.memory.statistics.store;
var currentRecord = room.storage.store;
message += '<u>Storage</u><ul>';
for( var type in memoryStoreRecord ){ // changed & depleted
var dif = (currentRecord[type] ? currentRecord[type] - memoryStoreRecord[type] : memoryStoreRecord[type] * -1);
message += '<li>' + type + ': ' + (currentRecord[type] || 0) + ' (' + (dif > -1 ? '+' : '' ) + dif + ')</li>';
}
// new
for( var type in currentRecord ){
if(!memoryStoreRecord[type])
message += '<li>' + type + ': ' + currentRecord[type] + ' (+' + currentRecord[type] + ')</li>';
}
message += '</ul>';
}
// invaders
if( room.memory.statistics.invaders && room.memory.statistics.invaders.length > 0 ){
message += '<u>Invaders</u><ul>';
_.forEach(room.memory.statistics.invaders, invaderReport);
message += '</ul>';
}
message += '</li></ul>';
Memory.statistics.reports.push(message);
}
else if( room.controller && !room.controller.my && room.controller.reservation ){
// controller
message = '<ul><li><b>Room ' + room.name + '</b><br/><u>Controller</u><ul><li>Reservation: ' +
room.controller.reservation.ticksToEnd + ' for ' +
room.controller.reservation.username + '</li></ul></li></ul>';
Memory.statistics.reports.push(message);
}
}
// set statistics
var present = invader => invader.leave === undefined;
var invaders = _.filter(room.memory.statistics.invader, present);
room.memory.statistics = {
tick: Game.time,
time: Date.now(),
store: room.storage ? room.storage.store : null,
controllerProgress: room.controller.progress,
controllerProgressTotal: room.controller.progressTotal,
invaders: invaders
};
}
_.forEach(Game.rooms, processRoom);
Memory.statistics = {
tick: Game.time,
time: Date.now(),
bucket: Game.cpu.bucket
}
},
toTimeSpanString: function(dateA, dateB){
var spanTicks = dateA.getTime() - dateB.getTime();
if( spanTicks < 0 ) spanTicks *= -1;
var span = new Date(spanTicks);
var h = Math.floor(spanTicks / 3600000);
var m = span.getMinutes();
var s = span.getSeconds();
var text;
if( h > 0 ) text = h + 'h ' + m + 'm ' + s + 's';
else if ( m > 0 ) text = m + 'm ' + s + 's';
else if( s > 0 ) text = s + 's';
else text = "0";
return text;
}
};
module.exports = mod;