Skip to content

Commit 777732d

Browse files
committed
feat: display amount of awarded bonus points on home page
1 parent 614344e commit 777732d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/routes/home.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,47 @@ export const setupHomeRoutes = function(app: Express, prisma: PrismaClient): voi
9898
rankings[rankingType.type] = await getRanking(prisma, rankingType.type);
9999
}
100100

101+
// Calculate when the bonus points awarding will start (7 days prior to end of the bloc)
102+
const bonusPointsAwardingStartTime = currentBlocDeadline ? new Date(currentBlocDeadline.end_at.getTime() - 7 * 24 * 60 * 60 * 1000) : null;
103+
const bonusPointsAwardingStarted = bonusPointsAwardingStartTime ? (now >= bonusPointsAwardingStartTime) : false;
104+
const rankingBonusPoints: { [key: string]: {
105+
total: number;
106+
awarded: number;
107+
remaining: number;
108+
} } = {};
109+
if (bonusPointsAwardingStartTime && bonusPointsAwardingStarted) {
110+
// Calculate how many bonus points are left to award per ranking type
111+
for (const rankingType of rankingTypes) {
112+
const totalBonusPoints = rankingType.bonus_points;
113+
if (!totalBonusPoints || totalBonusPoints <= 0) {
114+
continue;
115+
}
116+
const bonusPointsPerHour = totalBonusPoints / (7 * 24);
117+
// Bonus points are awarded every hour during the last 7 days
118+
const awardedBonusPoints = Math.floor(((now.getTime() - bonusPointsAwardingStartTime.getTime()) / (60 * 60 * 1000)) * bonusPointsPerHour);
119+
const remainingBonusPoints = totalBonusPoints - awardedBonusPoints;
120+
rankingBonusPoints[rankingType.type] = {
121+
total: totalBonusPoints,
122+
awarded: Math.min(awardedBonusPoints, totalBonusPoints),
123+
remaining: Math.max(remainingBonusPoints, 0),
124+
};
125+
}
126+
}
127+
else {
128+
// No bonus points have been awarded yet
129+
for (const rankingType of rankingTypes) {
130+
const totalBonusPoints = rankingType.bonus_points;
131+
if (!totalBonusPoints || totalBonusPoints <= 0) {
132+
continue;
133+
}
134+
rankingBonusPoints[rankingType.type] = {
135+
total: totalBonusPoints,
136+
awarded: 0,
137+
remaining: totalBonusPoints,
138+
};
139+
}
140+
}
141+
101142
// Check if quiz is currently available
102143
const quiz_available = await isQuizAvailable(user, prisma);
103144

@@ -112,6 +153,8 @@ export const setupHomeRoutes = function(app: Express, prisma: PrismaClient): voi
112153
sortedCoalitionScores,
113154
rankingTypes,
114155
rankings,
156+
bonusPointsAwardingStarted,
157+
rankingBonusPoints,
115158
});
116159
});
117160

templates/home.njk

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@
6060
</div>
6161
<div class="card-body p-0">
6262
<p class="p-4 pb-0 mb-0"><b>{{ rankingType.description | striptags(true) | escape | nl2br }}</b></p>
63-
<p class="p-4 pt-0 mb-0"><small>{{ (rankingType.bonus_points / 168) | round(0, 'floor') }} bonus points will be awarded to #1 <i>every hour</i> during the <i>final week of the season</i>!</small></p>
63+
{% if bonusPointsAwardingStarted and rankingBonusPoints[rankingType.type] %}
64+
<!-- bonus points awarded progress bar -->
65+
<p>&nbsp;</p>
66+
<div class="progress mb-2" role="progressbar" aria-label="Bonus points awarded for {{ rankingType.name }}" aria-valuenow="{{ rankingBonusPoints[rankingType.type].awarded }}" aria-valuemin="0" aria-valuemax="{{ rankingBonusPoints[rankingType.type].total }}">
67+
<div class="progress-bar" style="width: {{ (rankingBonusPoints[rankingType.type].awarded / rankingBonusPoints[rankingType.type].total) * 100 }}%; background-color: var(--bs-yellow);"></div>
68+
</div>
69+
<p style="text-align: center;"><b>Bonus points awarding is ongoing!</b> {{ rankingBonusPoints[rankingType.type].awarded | thousands }} out of {{ rankingBonusPoints[rankingType.type].total | thousands }} bonus points awarded, with {{ rankingBonusPoints[rankingType.type].remaining | thousands }} remaining. Bonus points are awarded <i>every hour</i> until the end of the season.</p>
70+
{% else %}
71+
<p class="p-4 pt-0 mb-0"><small>{{ (rankingType.bonus_points / 168) | round(0, 'floor') }} bonus points will be awarded to #1 <i>every hour</i> during the <i>final week of the season</i>!</small></p>
72+
{% endif %}
6473
<table class="table coalition-colored mb-0">
6574
<tbody>
6675
{% for ranking in rankings[rankingType.type] %}

0 commit comments

Comments
 (0)