This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
71 lines (62 loc) · 1.79 KB
/
index.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
/*
* Author: Clement Delalande (https://github.com/iKlem)
* More informations on the github repo (https://github.com/ScreepsMods/screepsmod-gcltocpu)
* Version: 0.3.1
*/
module.exports = (config) => {
const constants = config.common.constants,
storage = config.common.storage,
engine = config.engine;
let gclMult = 1,
gclLevel = 1,
gclPow = 1,
newCPU = 0,
oldUsers;
/**
* Update the CPU with the change of GCL.
*
* @param {boolean} forceUpdate - Force the update instead of waiting a change.
*/
function doUpdate() {
if (engine) {
oldUsers = engine.driver.getAllUsers;
engine.driver.getAllUsers = function() {
var promise = oldUsers.apply(this, Array.prototype.slice.call(arguments));
return promise.then(result => {
for(let user of result.ivm) {
if(!user.bot) {
gclLevel = Math.floor(Math.pow((user.gcl || 0) / gclMult, 1 / gclPow)) + 1,
newCPU = gclLevel * 10 + 20
if ((user.cpu === 100 && newCPU < user.cpu) || newCPU > user.cpu) {
updateUser(user, newCPU);
}
}
}
return result;
});
}
}
};
/**
* Update the new cpu from the storage db
*
* @param {Object} user - The user to update
* @param {number} cpu - The new value for the CPU
*/
function updateUser(user, cpu) {
if(storage) {
console.log(`Updated maximum cpu to ${cpu} for user ${user.username}`);
let retVal = storage.db.users.update({username: user.username}, {$set: {cpu: cpu}});
if (retVal) {
return true;
}
}
return false;
};
// main (looping by the engine runners)
if (constants) {
gclMult = constants.GCL_MULTIPLY;
gclPow = constants.GCL_POW;
doUpdate();
}
};