1
1
var ScreepsStats = function ( ) {
2
- if ( ! Memory . ___screeps_stats ) {
2
+ if ( ! Memory . ___screeps_stats ) {
3
3
Memory . ___screeps_stats = { }
4
4
}
5
5
this . username = _ . get (
6
- _ . find ( Game . structures , ( s ) => true ) , 'owner.username' ,
7
- _ . get ( _ . find ( Game . creeps , ( s ) => true ) , 'owner.username' )
6
+ _ . find ( Game . structures , ( s ) => true ) , 'owner.username' ,
7
+ _ . get ( _ . find ( Game . creeps , ( s ) => true ) , 'owner.username' )
8
8
) || false
9
9
this . clean ( )
10
10
}
@@ -58,19 +58,21 @@ ScreepsStats.prototype.runBuiltinStats = function () {
58
58
}
59
59
} )
60
60
61
- _ . forEach ( Game . rooms , ( room ) => {
61
+ _ . forEach ( Game . rooms , ( room ) => {
62
62
63
- if ( ! stats [ room . name ] ) {
63
+ if ( ! stats [ room . name ] ) {
64
64
stats . rooms [ room . name ] = { }
65
65
}
66
66
67
- if ( _ . isEmpty ( room . controller ) ) { return }
67
+ if ( _ . isEmpty ( room . controller ) ) {
68
+ return
69
+ }
68
70
var controller = room . controller
69
71
70
72
// Is hostile room? Continue
71
- if ( ! controller . my ) {
72
- if ( ! ! controller . owner ) { // Owner is set but is not this user.
73
- if ( controller . owner . username != this . username ) {
73
+ if ( ! controller . my ) {
74
+ if ( ! ! controller . owner ) { // Owner is set but is not this user.
75
+ if ( controller . owner . username != this . username ) {
74
76
return
75
77
}
76
78
} else {
@@ -83,20 +85,20 @@ ScreepsStats.prototype.runBuiltinStats = function () {
83
85
level : controller . level ,
84
86
progress : controller . progress ,
85
87
upgradeBlocked : controller . upgradeBlocked ,
86
- reservation : _ . get ( controller , 'reservation.ticksToEnd' ) ,
88
+ reservation : _ . get ( controller , 'reservation.ticksToEnd' ) ,
87
89
ticksToDowngrade : controller . ticksToDowngrade
88
90
} )
89
91
90
- if ( controller . level > 0 ) {
92
+ if ( controller . level > 0 ) {
91
93
92
94
// Room
93
- _ . merge ( stats . rooms [ room . name ] , {
95
+ _ . merge ( stats . rooms [ room . name ] , {
94
96
energyAvailable : room . energyAvailable ,
95
97
energyCapacityAvailable : room . energyCapacityAvailable ,
96
98
} )
97
99
98
100
// Storage
99
- if ( room . storage ) {
101
+ if ( room . storage ) {
100
102
_ . defaults ( stats , {
101
103
storage : {
102
104
subgroups : true
@@ -107,14 +109,14 @@ ScreepsStats.prototype.runBuiltinStats = function () {
107
109
store : _ . sum ( room . storage . store ) ,
108
110
resources : { }
109
111
}
110
- for ( var resourceType in room . storage . store ) {
112
+ for ( var resourceType in room . storage . store ) {
111
113
stats . storage [ room . storage . id ] . resources [ resourceType ] = room . storage . store [ resourceType ]
112
114
stats . storage [ room . storage . id ] [ resourceType ] = room . storage . store [ resourceType ]
113
115
}
114
116
}
115
117
116
118
// Terminals
117
- if ( room . terminal ) {
119
+ if ( room . terminal ) {
118
120
_ . defaults ( stats , {
119
121
terminal : {
120
122
subgroups : true
@@ -125,7 +127,7 @@ ScreepsStats.prototype.runBuiltinStats = function () {
125
127
store : _ . sum ( room . terminal . store ) ,
126
128
resources : { }
127
129
}
128
- for ( var resourceType in room . terminal . store ) {
130
+ for ( var resourceType in room . terminal . store ) {
129
131
stats . terminal [ room . terminal . id ] . resources [ resourceType ] = room . terminal . store [ resourceType ]
130
132
stats . terminal [ room . terminal . id ] [ resourceType ] = room . terminal . store [ resourceType ]
131
133
}
@@ -144,11 +146,11 @@ ScreepsStats.prototype.runBuiltinStats = function () {
144
146
subgroups : true
145
147
}
146
148
} )
147
- _ . forEach ( Game . spawns , function ( spawn ) {
149
+ _ . forEach ( Game . spawns , function ( spawn ) {
148
150
stats . spawns [ spawn . name ] = {
149
151
room : spawn . room . name ,
150
152
busy : ! ! spawn . spawning ,
151
- remainingTime : _ . get ( spawn , 'spawning.remainingTime' , 0 )
153
+ remainingTime : _ . get ( spawn , 'spawning.remainingTime' , 0 )
152
154
}
153
155
} )
154
156
@@ -174,16 +176,16 @@ ScreepsStats.prototype.roomExpensive = function (stats, room) {
174
176
stats . rooms [ room . name ] . sources = { }
175
177
var sources = room . find ( FIND_SOURCES )
176
178
177
- _ . forEach ( sources , ( source ) => {
179
+ _ . forEach ( sources , ( source ) => {
178
180
stats . sources [ source . id ] = {
179
181
room : room . name ,
180
182
energy : source . energy ,
181
183
energyCapacity : source . energyCapacity ,
182
184
ticksToRegeneration : source . ticksToRegeneration
183
185
}
184
- if ( source . energy < source . energyCapacity && source . ticksToRegeneration ) {
186
+ if ( source . energy < source . energyCapacity && source . ticksToRegeneration ) {
185
187
var energyHarvested = source . energyCapacity - source . energy
186
- if ( source . ticksToRegeneration < ENERGY_REGEN_TIME ) {
188
+ if ( source . ticksToRegeneration < ENERGY_REGEN_TIME ) {
187
189
var ticksHarvested = ENERGY_REGEN_TIME - source . ticksToRegeneration
188
190
stats . sources [ source . id ] . averageHarvest = energyHarvested / ticksHarvested
189
191
}
@@ -198,7 +200,7 @@ ScreepsStats.prototype.roomExpensive = function (stats, room) {
198
200
// Mineral Mining
199
201
var minerals = room . find ( FIND_MINERALS )
200
202
stats . rooms [ room . name ] . minerals = { }
201
- _ . forEach ( minerals , ( mineral ) => {
203
+ _ . forEach ( minerals , ( mineral ) => {
202
204
stats . minerals [ mineral . id ] = {
203
205
room : room . name ,
204
206
mineralType : mineral . mineralType ,
@@ -212,8 +214,8 @@ ScreepsStats.prototype.roomExpensive = function (stats, room) {
212
214
// Hostiles in Room
213
215
var hostiles = room . find ( FIND_HOSTILE_CREEPS )
214
216
stats . rooms [ room . name ] . hostiles = { }
215
- _ . forEach ( hostiles , ( hostile ) => {
216
- if ( ! stats . rooms [ room . name ] . hostiles [ hostile . owner . username ] ) {
217
+ _ . forEach ( hostiles , ( hostile ) => {
218
+ if ( ! stats . rooms [ room . name ] . hostiles [ hostile . owner . username ] ) {
217
219
stats . rooms [ room . name ] . hostiles [ hostile . owner . username ] = 1
218
220
} else {
219
221
stats . rooms [ room . name ] . hostiles [ hostile . owner . username ] ++
@@ -226,14 +228,14 @@ ScreepsStats.prototype.roomExpensive = function (stats, room) {
226
228
227
229
ScreepsStats . prototype . removeTick = function ( tick ) {
228
230
229
- if ( Array . isArray ( tick ) ) {
230
- for ( var index in tick ) {
231
+ if ( Array . isArray ( tick ) ) {
232
+ for ( var index in tick ) {
231
233
this . removeTick ( tick [ index ] )
232
234
}
233
235
return 'ScreepStats: Processed ' + tick . length + ' ticks'
234
236
}
235
237
236
- if ( ! ! Memory . ___screeps_stats [ tick ] ) {
238
+ if ( ! ! Memory . ___screeps_stats [ tick ] ) {
237
239
delete Memory . ___screeps_stats [ tick ]
238
240
return 'ScreepStats: Removed tick ' + tick
239
241
} else {
@@ -242,19 +244,24 @@ ScreepsStats.prototype.removeTick = function (tick) {
242
244
}
243
245
244
246
ScreepsStats . prototype . getStats = function ( json ) {
245
- if ( json ) {
247
+ if ( json ) {
246
248
return JSON . stringify ( Memory . ___screeps_stats )
247
249
} else {
248
250
return Memory . __screeps_stats
249
251
}
250
252
}
251
253
252
254
ScreepsStats . prototype . getStatsForTick = function ( tick ) {
253
- if ( ! Memory . __screeps_stats [ tick ] ) {
255
+ if ( ! Memory . __screeps_stats [ tick ] ) {
254
256
return false
255
257
} else {
256
258
return Memory . __screeps_stats [ tick ]
257
259
}
258
260
}
259
261
262
+ if ( ENABLE_PROFILER && ! ! Profiler ) {
263
+ ScreepsStats . prototype = Profiler . registerObject ( ScreepsStats , 'ScreepsStats' )
264
+ }
265
+
266
+
260
267
module . exports = ScreepsStats
0 commit comments