Skip to content

Commit

Permalink
attempt to fix NBQ leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
tombertrand committed Mar 21, 2024
1 parent 38060cd commit ac60fb7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
36 changes: 16 additions & 20 deletions server/cron/nanobrowserquestStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,31 @@ const getNanoBrowserQuestPlayers = async () => {

const getNanoBrowserQuestLeaderboard = async () => {
await redisClient.select(NBQ_REDIS_DB_INDEX);
async function findKeys(pattern) {
let cursor = "0";

async function scanAllKeys(pattern = "*") {
let cursor = 0; // Initialize cursor as a number
let keys = [];
let reply;

redisClient.select(NBQ_REDIS_DB_INDEX);
do {
reply = await redisClient.scan(
reply ? reply.cursor : cursor,
"MATCH",
pattern,
"COUNT",
"100",
);
// rawCursor = reply.cursor;

console.log("~~~reply.cursor", reply.cursor);
keys.push(...reply.keys);
} while (reply.cursor);
// Await the SCAN operation with the current cursor
const result = await redisClient.scan(cursor, {
MATCH: pattern,
COUNT: 100,
});

cursor = result.cursor;
keys = keys.concat(result.keys.filter(key => key.startsWith("u:")));
} while (cursor !== 0);
return keys;
}

let playersData = [];
const PER_PAGES = 2;
const PER_PAGES = 500;
// Usage
const players = (await findKeys("u:*")).filter(key => key.startsWith("u:"));
const playersChunks = chunk(players, PER_PAGES);
// const players = (await findKeys("u:*")).filter(key => key.startsWith("u:"));

console.log("~~~~~~~playersChunks", playersChunks);
const players = await scanAllKeys(); //.filter(key => key.startsWith("u:"));
const playersChunks = chunk(players, PER_PAGES);

for (let i = 0; i < playersChunks.length; i++) {
const rawPlayerData = await Promise.all(
Expand Down
2 changes: 0 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ app.get("/api/nanobrowserquest/players", async (req, res, next) => {
});

app.get("/api/nanobrowserquest/leaderboard", async (req, res, next) => {

try {
res.send(nodeCache.get(NANOBROWSERQUEST_LEADERBOARD));
} catch (err) {
Expand Down Expand Up @@ -347,4 +346,3 @@ process.on("SIGTERM", exitHandler(0, "SIGTERM"));
process.on("SIGINT", exitHandler(0, "SIGINT"));

console.log(`Server started on http://localhost:${process.env.SERVER_PORT}`);

2 changes: 1 addition & 1 deletion server/ws/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ws.on("error", err => {
ws.onmessage = msg => {
if (Buffer.isBuffer(msg)) {
const buffer = Buffer.from(msg, "hex");
const jsonString = buffer.toString("utf-8");
const jsonString = buffer?.toString("utf-8");

msg = {
data: jsonString,
Expand Down

0 comments on commit ac60fb7

Please sign in to comment.