diff --git a/server/cron/nanobrowserquestStats.js b/server/cron/nanobrowserquestStats.js index 0db4a329..a8830baa 100644 --- a/server/cron/nanobrowserquestStats.js +++ b/server/cron/nanobrowserquestStats.js @@ -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( diff --git a/server/server.js b/server/server.js index 094faff3..05278b61 100644 --- a/server/server.js +++ b/server/server.js @@ -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) { @@ -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}`); - diff --git a/server/ws/index.js b/server/ws/index.js index 98f5ee15..8c624423 100644 --- a/server/ws/index.js +++ b/server/ws/index.js @@ -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,