Skip to content

Commit

Permalink
Pull leaderboard more often
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterbrandsen committed Oct 3, 2024
1 parent 39bc9ce commit 561802d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
5 changes: 5 additions & 0 deletions files/defaultActions.unknown.json
Original file line number Diff line number Diff line change
Expand Up @@ -863,5 +863,10 @@
"path": "intents.offensive.rangedHeal.effect",
"value": 0,
"type": 0
},
{
"path": "resourcesStored.score",
"value": 0,
"type": 1
}
]
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"https-proxy-agent": "^7.0.2",
"jest": "^29.7.0",
"postgres": "^3.4.3",
"screeps-advanced-api": "^1.5.2",
"screeps-advanced-api": "^1.5.3",
"screeps-api": "^1.16.0",
"winston": "^3.11.0",
"winston-daily-rotate-file": "^4.7.1"
Expand Down
36 changes: 18 additions & 18 deletions src/setup/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const userRoomsCache = cleanSource(baseCache)
export default class Cache {
static shouldUpdateCache(type) {
let cache;
const shouldUpdateInterval = 1000 * 60 * 10;
let shouldUpdateInterval = 1000 * 60 * 10;
switch (type) {
case 'rooms':
cache = roomsCache;
Expand Down Expand Up @@ -124,23 +124,6 @@ export default class Cache {
roomsCache.data = shards;
}

static async updateUserLeaderboardCache() {
const forcedUsers = process.env.USERNAMES && process.env.USERNAMES.length > 0
? ("Unknown," + process.env.USERNAMES).split(",") : [];
let users = await advancedScreepsApi.getAllUsers()
users = users.filter(forcedUsers.length > 0 ? (user) => forcedUsers.includes(user.username) : () => true);
users.sort((a, b) => GetRoomTotal(b.shards, 'type') - GetRoomTotal(a.shards, 'type'));
const userValues = Object.values(users);

for (let u = 0; u < userValues.length; u += 1) {
const user = userValues[u];
ProcessDataBroker.usernamesById[user.id] = user.username;
}
fs.writeFileSync("./files/users.json", JSON.stringify(users, null, 2));
UpdateLocalUsersCache(users);
usersCache.data = users;
}

static async updateUsersCache() {
const forcedUsers = process.env.USERNAMES && process.env.USERNAMES.length > 0
? ("Unknown," + process.env.USERNAMES).split(",") : [];
Expand Down Expand Up @@ -168,4 +151,21 @@ export default class Cache {

userRoomsCache.data = userRooms;
}

static async updateUserLeaderboardCache() {
const users = await Cache.getUsersCache();
const leaderboards = await advancedScreepsApi.getAllLeaderboard();
const leaderboardKeys = Object.keys(leaderboards);

for (let u = 0; u < users.length; u += 1) {
const user = users[u];
const username = user.username

for (let l = 0; l < leaderboardKeys.length; l += 1) {
const leaderboardName = leaderboardKeys[l];
const leaderboard = leaderboards[leaderboardName];
user[leaderboardName] = leaderboard[username];
}
}
}
}
11 changes: 10 additions & 1 deletion src/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ const initialRoomUpdaterJob = new CronJob(
false,
"Europe/Amsterdam"
);
initialRoomUpdaterJob.start();
initialRoomUpdaterJob.start();

const userLeaderboardSyncer = new CronJob(
"* * * * *",
Cache.updateUserLeaderboardCache,
null,
false,
"Europe/Amsterdam"
);
userLeaderboardSyncer.start();

0 comments on commit 561802d

Please sign in to comment.