forked from ScreepsMods/screepsmod-gcltocpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (84 loc) · 2.63 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Author: Clement Delalande (https://github.com/iKlem)
* Version: 0.2.1
*/
module.exports = (config) => {
const constants = config.common.constants,
storage = config.common.storage,
cli = config.cli,
engine = config.engine;
let gclMult = 1,
gclPow = 1,
gclLevel = 1,
newCPU = 0,
oldUsers,
doUpdate,
updateUser;
doUpdate = (forceUpdate = false) => {
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) {
if(!user.bot) {
gclLevel = Math.floor(Math.pow((user.gcl || 0) / gclMult, 1 / gclPow)) + 1,
newCPU = gclLevel * 10 + 20
// TODO: Add check for errors
if (user.cpu == 100) {
if (newCPU < user.cpu) {
updateUser(user, newCPU);
}
}
if (!forceUpdate) {
if (newCPU > user.cpu) {
updateUser(user, newCPU);
}
} else {
updateUser(user, newCPU);
}
}
}
return result;
});
}
}
};
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;
};
if (constants) {
gclMult = constants.GCL_MULTIPLY;
gclPow = constants.GCL_POW;
doUpdate();
if (cli) {
cli.on("cliSandbox", (sandbox) => {
sandbox.updateCPU = (playerName = "") => {
if (storage) {
if (playerName != "") { // If a player name is given, update only for the player
return storage.db.users.findOne({username: playerName}).then((user) => {
if(user) {
gclLevel = Math.floor(Math.pow((user.gcl || 0) / gclMult, 1 / gclPow)) + 1,
newCPU = gclLevel * 10 + 20
sandbox.print(`Updating cpu for ${user.username} to ${newCPU}...`);
return updateUser(user, newCPU) ? "Done" : `Error updating cpu for ${user.username}!`;
}
});
} else { // If no player name, update all players CPU
sandbox.print(`Updating cpu for all users to their GCL...`);
doUpdate(true);
return "Update finished!";
}
}
}
});
}
}
};