From 5ea6956f401f4375e31a9dfc20a0e69f410fc271 Mon Sep 17 00:00:00 2001 From: Gautam Anand Date: Sat, 16 Nov 2024 17:56:59 -0600 Subject: [PATCH] Bug fixes --- api/eloRank.js | 7 ++++++- components/leagueModal.js | 4 ++-- cron.js | 23 ++++++----------------- models/User.js | 5 +---- public/locales/en/common.json | 3 ++- ws/classes/Player.js | 1 + ws/ws.js | 9 +++++++++ 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/api/eloRank.js b/api/eloRank.js index 90ff306..351d059 100644 --- a/api/eloRank.js +++ b/api/eloRank.js @@ -26,9 +26,14 @@ export default async function handler(req, res) { return res.status(404).json({ message: 'User not found' }); } + console.time('get user rank', user.username); + const rank = (await User.countDocuments({ elo: { $gt: user.elo }, + banned: false + }).cache(2000)) + 1; + console.timeEnd('get user rank', user.username); // Return the user's elo and rank - return res.status(200).json({ elo: user.elo, rank: user.rank, league: getLeague(user.elo), + return res.status(200).json({ elo: user.elo, rank, league: getLeague(user.elo), duels_wins: user.duels_wins, duels_losses: user.duels_losses, duels_tied: user.duels_tied, win_rate: user.duels_wins / (user.duels_wins + user.duels_losses + user.duels_tied) diff --git a/components/leagueModal.js b/components/leagueModal.js index 88e4b1e..eef1224 100644 --- a/components/leagueModal.js +++ b/components/leagueModal.js @@ -67,13 +67,13 @@ export default function LeagueModal({ shown, onClose, session, eloData }) { {text("yourElo")}: {eloData.elo}

- {/*

{text("yourGlobalRank")}: #{eloData.rank} -

*/} +

24 * 60 * 60 * 1000)) { bulkOps.push({ updateOne: { filter: { _id: user._id }, update: { - $set: { rank: currentRank }, - ...(Date.now() - user.lastEloHistoryUpdate > 24 * 60 * 60 * 1000 && { $push: { elo_history: { elo: user.elo, time: Date.now() } }, $set: { lastEloHistoryUpdate: Date.now() }, $set: { elo_today: 0 }, - }) }, }, }); } + } // Execute bulk operations if (bulkOps.length > 0) { @@ -80,13 +68,14 @@ async function calculateRanks() { } console.timeEnd('Updated ranks'); + console.log('bulkOps', bulkOps.length); } -// Calculate ranks every 3 hours +// Calculate ranks every 12 hours // setInterval(calculateRanks, 60 * 60 * 1000 * 3); function recursiveCalculateRanks() { calculateRanks().then(() => { - setTimeout(recursiveCalculateRanks, 3 * 60 * 60 * 1000); + setTimeout(recursiveCalculateRanks, 12 * 60 * 60 * 1000); }); } recursiveCalculateRanks(); diff --git a/models/User.js b/models/User.js index 29d577d..91d3f0f 100644 --- a/models/User.js +++ b/models/User.js @@ -114,10 +114,7 @@ const userSchema = new mongoose.Schema({ duels_tied: { type: Number, default: 0 - }, - rank: { - type: Number, - }, + } }); const User = mongoose.models.User || mongoose.model('User', userSchema); diff --git a/public/locales/en/common.json b/public/locales/en/common.json index ddda062..54a0b6f 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -228,5 +228,6 @@ "elo": "ELO", "xp": "XP", "leagueModalDesc": "Play duels to gain ELO and climb the leaderboard.", - "leagueModalDesc2": "Stats may be delayed up to 1 hour" + "leagueModalDesc2": "Stats may be delayed up to 1 hour", + "unableToJoinDuel": "Unable to find duel at this time. Please email support@worldguessr.com" } diff --git a/ws/classes/Player.js b/ws/classes/Player.js index 35da5fa..cf6922a 100644 --- a/ws/classes/Player.js +++ b/ws/classes/Player.js @@ -81,6 +81,7 @@ export default class Player { this.username = valid.username; this.accountId = valid._id.toString(); this.elo = valid.elo; + this.banned = valid.banned; this.league = getLeague(this.elo).name; this.send({ type: 'verify' diff --git a/ws/ws.js b/ws/ws.js index 37828a5..fb99c3e 100644 --- a/ws/ws.js +++ b/ws/ws.js @@ -373,6 +373,15 @@ app.ws('/wg', { if ((json.type === 'publicDuel') && !player.gameId) { console.log('public duel requested by', player.username, player.ip); + if(player.banned) { + player.send({ + type: 'toast', + key: 'unableToJoinDuel', + toastType: 'error' + }); + return; + + } // get range of league player.inQueue = true;