Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
codergautam committed Nov 16, 2024
1 parent 8dd045a commit 5ea6956
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
7 changes: 6 additions & 1 deletion api/eloRank.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions components/leagueModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ export default function LeagueModal({ shown, onClose, session, eloData }) {
{text("yourElo")}: <span style={{ color: '#ffd700' }}>{eloData.elo}</span>
</p>

{/* <p style={{
<p style={{
fontSize: '18px',
color: '#b0b0b0',
marginBottom: '5px'
}}>
{text("yourGlobalRank")}: <span style={{ color: '#ffd700' }}>#{eloData.rank}</span>
</p> */}
</p>
<p style={{
fontSize: '18px',
color: '#b0b0b0',
Expand Down
23 changes: 6 additions & 17 deletions cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,56 +37,45 @@ async function calculateRanks() {
console.log('Calculating ranks');
console.time('Updated ranks');

const users = await User.find({ banned: false }).select('elo totalXp lastEloHistoryUpdate').sort({ elo: -1, totalXp: -1 });
const users = await User.find({ banned: false }).select('elo lastEloHistoryUpdate').sort({ elo: -1 });

// Prepare bulk operations
const bulkOps = [];
let currentRank = 1;
let previousElo = null;
let previousTotalXp = null;

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


// If the elo or totalXp changes, update the rank
if (user.elo !== previousElo || user.totalXp !== previousTotalXp) {
currentRank = i + 1; // 1-based rank
previousElo = user.elo;
previousTotalXp = user.totalXp;
}

// Prepare the update operation for the current user
if((Date.now() - user.lastEloHistoryUpdate > 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) {
await User.bulkWrite(bulkOps);
}

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();
Expand Down
5 changes: 1 addition & 4 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
1 change: 1 addition & 0 deletions ws/classes/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 9 additions & 0 deletions ws/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 5ea6956

Please sign in to comment.