Skip to content

Commit

Permalink
Separate DB tables for team and ffa leaderboards
Browse files Browse the repository at this point in the history
  • Loading branch information
geneotech committed Jan 16, 2024
1 parent bf2769d commit d5cb083
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 39 deletions.
14 changes: 13 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ if (!fs.existsSync(dbPath)) {
// Create the players table if it doesn't exist
db.serialize(() => {
db.run(`
CREATE TABLE IF NOT EXISTS players (
CREATE TABLE IF NOT EXISTS mmr_team (
account_id TEXT UNIQUE,
nickname TEXT,
mmr FLOAT DEFAULT 0,
mu FLOAT DEFAULT 0,
sigma FLOAT DEFAULT 0,
matches_won INTEGER DEFAULT 0,
matches_lost INTEGER DEFAULT 0
)
`);

db.run(`
CREATE TABLE IF NOT EXISTS mmr_ffa (
account_id TEXT UNIQUE,
nickname TEXT,
mmr FLOAT DEFAULT 0,
Expand Down
20 changes: 12 additions & 8 deletions src/leaderboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,34 @@ router.get('/', (req, res) => {
const db = new Database(dbPath);

// Prepare and execute the query to fetch all players
const stmt = db.prepare('SELECT account_id, mmr, mu, sigma, matches_won, matches_lost, nickname FROM players');
const rows = stmt.all();
const rows_team = db.prepare('SELECT account_id, mmr, mu, sigma, matches_won, matches_lost, nickname FROM mmr_team').all();
const rows_ffa = db.prepare('SELECT account_id, mmr, mu, sigma, matches_won, matches_lost, nickname FROM mmr_ffa').all();

// Create a JSON response with players and match counts
const leaderboards = rows.map((row) => ({
const row_reader = (row) => ({
account_id: row.account_id,
nickname: row.nickname,
mmr: row.mmr,
mu: row.mu,
sigma: row.sigma,
matches_won: row.matches_won,
matches_lost: row.matches_lost
}));
});

leaderboards.sort((a, b) => b.mmr - a.mmr);
const leaderboards_team = rows_team.map(row_reader);
const leaderboards_ffa = rows_ffa.map(row_reader);

leaderboards_team.sort((a, b) => b.mmr - a.mmr);
leaderboards_ffa.sort( (a, b) => b.mmr - a.mmr);

if (req.query.format !== undefined && req.query.format == 'json') {
return res.status(200).json({ leaderboards });
return res.status(200).json({ leaderboards_team, leaderboards_ffa });
}
else {
res.render('leaderboards', {
page: 'Leaderboards',
user: req.user,
leaderboards: leaderboards
leaderboards_team: leaderboards_team,
leaderboards_ffa: leaderboards_ffa
});
}
} catch (error) {
Expand Down
18 changes: 15 additions & 3 deletions src/report_match.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,21 @@ router.post('/', apiKeyAuth, (req, res) => {
const allPlayers = win_players.concat(lose_players);
const playerRatings = {};
const default_rating = rating();
const stmt_insert_player = db.prepare('INSERT OR IGNORE INTO players (account_id, mu, sigma) VALUES (?, ?, ?)');
const stmt_get_player = db.prepare('SELECT mu, sigma FROM players WHERE account_id = ?');
const stmt_update_player = db.prepare('UPDATE players SET mu = ?, sigma = ?, mmr = ?, matches_won = matches_won + ?, matches_lost = matches_lost + ?, nickname = ? WHERE account_id = ?');
const table_name = (() => {
if (game_mode === 'bomb_defusal') {
return 'mmr_team';
}

if (game_mode === 'gun_game') {
return 'mmr_ffa';
}

return 'mmr_team';
})();

const stmt_insert_player = db.prepare(`INSERT OR IGNORE INTO ${table_name} (account_id, mu, sigma) VALUES (?, ?, ?)`);
const stmt_get_player = db.prepare(`SELECT mu, sigma FROM ${table_name} WHERE account_id = ?`);
const stmt_update_player = db.prepare(`UPDATE ${table_name} SET mu = ?, sigma = ?, mmr = ?, matches_won = matches_won + ?, matches_lost = matches_lost + ?, nickname = ? WHERE account_id = ?`);

// Insert player entries if they do not exist
allPlayers.forEach(playerId => {
Expand Down
107 changes: 82 additions & 25 deletions views/leaderboards.ejs
Original file line number Diff line number Diff line change
@@ -1,31 +1,88 @@
<%- include('overall_header'); %>

<h1>Leaderboards</h1>
<div class="table-responsive">
<table class="sortable">
<thead>
<tr>
<th>Nickname</th>
<th>MMR</th>
<th>Mu</th>
<th>Sigma</th>
<th>Matches Won</th>
<th>Matches Lost</th>
</tr>
</thead>
<tbody>
<% leaderboards.forEach(i => { %>
<tr>
<td><%= i.nickname %></td>
<td><%= i.mmr %></td>
<td><%= i.mu %></td>
<td><%= i.sigma %></td>
<td><%= i.matches_won %></td>
<td><%= i.matches_lost %></td>
</tr>
<% }); %>
</tbody>
</table>
<div style="display: flex; justify-content: space-around;">

<!-- TEAM Leaderboard Section -->
<div>
<h2 style="text-align: center;">BOMB DEFUSAL</h2>
<div class="table-responsive">
<table class="sortable">
<thead>
<tr>
<th>Rank</th>
<th>Nickname</th>
<th>OpenSkill</th>
<!--
<th>Mu</th>
<th>Sigma</th>
-->
<th>Won #</th>
<th>Lost #</th>
</tr>
</thead>
<tbody>
<% leaderboards_team.forEach((i, index) => { %>
<tr>
<td><b><%= index + 1 %></b></td>
<td>
<%= index === 0 ? 'πŸ†' : index === 1 ? 'πŸ₯ˆ' : index === 2 ? 'πŸ₯‰' : '' %>
<%= i.nickname %>
</td>
<td><b><%= i.mmr.toFixed(3) %></b></td>
<!--
<td><%= i.mu.toFixed(3) %></td>
<td><%= i.sigma.toFixed(3) %></td>
-->
<td><%= i.matches_won %></td>
<td><%= i.matches_lost %></td>
</tr>
<% }); %>
</tbody>
</table>
</div>
</div>

<!-- FFA Leaderboard Section -->
<div>
<h2 style="text-align: center;">FFA</h2>
<div class="table-responsive">
<table class="sortable">
<thead>
<tr>
<th>Rank</th>
<th>Nickname</th>
<th>OpenSkill</th>
<!--
<th>Mu</th>
<th>Sigma</th>
-->
<th>Won #</th>
<th>Lost #</th>
</tr>
</thead>
<tbody>
<% leaderboards_ffa.forEach((i, index) => { %>
<tr>
<td><b><%= index + 1 %></b></td>
<td>
<%= index === 0 ? 'πŸ†' : index === 1 ? 'πŸ₯ˆ' : index === 2 ? 'πŸ₯‰' : '' %>
<%= i.nickname %>
</td>
<td><b><%= i.mmr.toFixed(3) %></b></td>
<!--
<td><%= i.mu.toFixed(3) %></td>
<td><%= i.sigma.toFixed(3) %></td>
-->
<td><%= i.matches_won %></td>
<td><%= i.matches_lost %></td>
</tr>
<% }); %>
</tbody>
</table>
</div>
</div>

</div>

<%- include('overall_footer'); %>
4 changes: 2 additions & 2 deletions views/matches.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<td><%= i.match_id %></td>
<td>
<% i.winners.forEach(player => { %>
<span style="color: <%= player.mmr_delta >= 0 ? 'chartreuse' : 'red' %>; font-family: monospace;">
<span style="color: <%= player.mmr_delta >= 0 ? 'chartreuse' : '#f85e73' %>; font-family: monospace;">
<%= player.mmr_delta >= 0 ? '↑' : '↓' %><%= Math.abs(player.mmr_delta).toFixed(2) %>
</span>
<a href="/user/<%= player.id %>">
Expand All @@ -31,7 +31,7 @@
</td>
<td>
<% i.losers.forEach(player => { %>
<span style="color: <%= player.mmr_delta >= 0 ? 'chartreuse' : 'red' %>; font-family: monospace;">
<span style="color: <%= player.mmr_delta >= 0 ? 'chartreuse' : '#f85e73' %>; font-family: monospace;">
<%= player.mmr_delta >= 0 ? '↑' : '↓' %><%= Math.abs(player.mmr_delta).toFixed(2) %>
</span>
<a href="/user/<%= player.id %>">
Expand Down

0 comments on commit d5cb083

Please sign in to comment.