Browse Source

Add rating history.

master
t123yh 7 years ago
parent
commit
60695a30c5
  1. 2
      libs/rating.js
  2. 6
      models/rating_history.js
  3. 2
      modules/admin.js
  4. 27
      modules/user.js
  5. 30
      views/user.ejs

2
libs/rating.js

@ -73,5 +73,5 @@ function calculateDeltas(allContestants) {
module.exports = function(allContestants) {
const deltas = calculateDeltas(allContestants);
return allContestants.map((contestant, i) => ({ user: contestant.user, currentRating: contestant.currentRating + deltas[i] }));
return allContestants.map((contestant, i) => ({ user: contestant.user, rank: contestant.rank, currentRating: contestant.currentRating + deltas[i] }));
}

6
models/rating_history.js

@ -27,6 +27,7 @@ let model = db.define('rating_history', {
rating_calculation_id: { type: Sequelize.INTEGER, primaryKey: true },
user_id: { type: Sequelize.INTEGER, primaryKey: true },
rating_after: { type: Sequelize.INTEGER },
rank: { type: Sequelize.INTEGER },
}, {
timestamps: false,
tableName: 'rating_history',
@ -42,11 +43,12 @@ let model = db.define('rating_history', {
let Model = require('./common');
class RatingHistory extends Model {
static async create(rating_calculation_id, user_id, rating) {
static async create(rating_calculation_id, user_id, rating, rank) {
return RatingHistory.fromRecord(RatingHistory.model.build({
rating_calculation_id: rating_calculation_id,
user_id: user_id,
rating_after: rating
rating_after: rating,
rank: rank
}));
}

2
modules/admin.js

@ -250,7 +250,7 @@ app.post('/admin/rating/add', async (req, res) => {
const user = newRating[i].user;
user.rating = newRating[i].currentRating;
await user.save();
const newHistory = await RatingHistory.create(newcalc.id, user.id, user.rating);
const newHistory = await RatingHistory.create(newcalc.id, user.id, newRating[i].currentRating, newRating[i].rank);
await newHistory.save();
}

27
modules/user.js

@ -20,6 +20,10 @@
'use strict';
let User = syzoj.model('user');
const RatingCalculation = syzoj.model('rating_calculation');
const RatingHistory = syzoj.model('rating_history');
const Contest = syzoj.model('contest');
const ContestPlayer = syzoj.model('contest_player');
// Ranklist
app.get('/ranklist', async (req, res) => {
@ -103,9 +107,30 @@ app.get('/user/:id', async (req, res) => {
await user.renderInformation();
user.emailVisible = user.public_email || user.allowedEdit;
const ratingHistoryValues = await RatingHistory.query(null, { user_id: user.id }, [['rating_calculation_id', 'asc']]);
const ratingHistories = [{
name: "初始积分",
value: syzoj.config.default.user.rating,
delta: null,
rank: null
}];
ratingHistories.reverse();
for (const history of ratingHistoryValues) {
const contest = await Contest.fromId((await RatingCalculation.fromID(history.rating_calculation_id)).contest_id);
ratingHistories.push({
contestName: contest.title,
value: history.rating_after,
delta: history.rating_after - ratingHistories[ratingHistories.length - 1].value,
rank: history.rank,
participants: await ContestPlayer.count({ contest_id: contest.id })
});
}
res.render('user', {
show_user: user,
statistics: statistics
statistics: statistics,
ratingHistories: ratingHistories
});
} catch (e) {
syzoj.log(e);

30
views/user.ejs

@ -118,6 +118,36 @@
</div>
</div>
</div>
<div class="row">
<div class="column">
<h4 class="ui top attached block header">比赛</h4>
<div class="ui bottom attached segment">
<table class="ui very basic table">
<thead>
<tr>
<th>比赛</th>
<th>名次</th>
<th>Rating</th>
</tr>
</thead>
<tbody>
<% for (const history of ratingHistories) { %>
<tr>
<td><%= history.contestName %></td>
<td><%= history.rank != null ? history.rank + " / " + history.participants : '' %></td>
<td><%= history.value %>
<% if(history.delta != null) { %>
<span class="<%= history.delta >= 0 ? 'red' : 'green' %>">
(<%= (history.delta < 0 ? '' : '+') + history.delta %>)
<% } %>
</td>
</tr>
<% } %>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>

Loading…
Cancel
Save