-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/matches: Save history of all matches to database.
- Loading branch information
Showing
5 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const express = require('express'); | ||
const Database = require('better-sqlite3'); | ||
|
||
const router = express.Router(); | ||
const dbPath = process.env.DB_PATH; | ||
|
||
router.get('/', (req, res) => { | ||
try { | ||
// Open the database connection | ||
const db = new Database(dbPath); | ||
|
||
// Prepare and execute the query to fetch all matches | ||
const stmt = db.prepare('SELECT * FROM matches'); | ||
const rows = stmt.all(); | ||
|
||
// Create a response array with detailed match information | ||
const matches = rows.map(match => { | ||
// Parse winner and loser IDs from JSON strings | ||
const winners = JSON.parse(match.winners); | ||
const losers = JSON.parse(match.losers); | ||
|
||
// Return the detailed match information | ||
return { | ||
match_id: match.match_id, | ||
server_name: match.server_name, | ||
arena: match.arena, | ||
game_mode: match.game_mode, | ||
winners, | ||
losers, | ||
win_score: match.win_score, | ||
lose_score: match.lose_score, | ||
match_date: match.match_date | ||
}; | ||
}); | ||
|
||
matches.sort((a, b) => b.match_id - a.match_id); | ||
|
||
if (req.query.format !== undefined && req.query.format == 'json') { | ||
return res.status(200).json({ matches }); | ||
} | ||
else { | ||
res.render('matches', { | ||
page: 'Matches', | ||
user: req.user, | ||
matches: matches | ||
}); | ||
} | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
} | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<%- include('overall_header'); %> | ||
|
||
<h1>Matches</h1> | ||
<div class="table-responsive"> | ||
<table class="sortable"> | ||
<thead> | ||
<tr> | ||
<th>#</th> | ||
<th>Winning team</th> | ||
<th>Losing team</th> | ||
<th>Score</th> | ||
<th>Arena</th> | ||
<th>Game mode</th> | ||
<th>Date</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% matches.forEach(i => { %> | ||
<tr> | ||
<td><%= i.match_id %></td> | ||
<td> | ||
<% i.winners.forEach(player => { %> | ||
<a href="/user/<%= player.id %>"> | ||
<%= player.nickname %> | ||
</a> | ||
<span style="color: <%= player.mmr_delta >= 0 ? 'chartreuse' : 'red' %>;"> | ||
<%= player.mmr_delta >= 0 ? '↑' : '↓' %><%= Math.abs(player.mmr_delta.toFixed(2)) %> | ||
</span> | ||
<% if (i.winners.indexOf(player) < i.winners.length - 1) { %>, <% } %> | ||
<% }); %> | ||
</td> | ||
<td> | ||
<% i.losers.forEach(player => { %> | ||
<a href="/user/<%= player.id %>"> | ||
<%= player.nickname %> | ||
</a> | ||
<span style="color: <%= player.mmr_delta >= 0 ? 'chartreuse' : 'red' %>;"> | ||
<%= player.mmr_delta >= 0 ? '↑' : '↓' %><%= Math.abs(player.mmr_delta.toFixed(2)) %> | ||
</span> | ||
<% if (i.losers.indexOf(player) < i.losers.length - 1) { %>, <% } %> | ||
<% }); %> | ||
</td> | ||
<td><%= i.win_score %>:<%= i.lose_score %></td> | ||
<td><%= i.arena %></td> | ||
<td><%= i.game_mode %></td> | ||
<td><%= new Date(i.match_date).toLocaleString() %></td> | ||
</tr> | ||
<% }); %> | ||
</tbody> | ||
</table> | ||
</div> | ||
|
||
<%- include('overall_footer'); %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters