Skip to content

Commit dfb7c0e

Browse files
committed
fix: formatting of points on the canvas pages
1 parent 6071117 commit dfb7c0e

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

src/handlers/filters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Express } from 'express';
22
import nunjucks from 'nunjucks';
3-
import { timeAgo, timeFromNow } from '../utils';
3+
import { formatThousands, timeAgo, timeFromNow } from '../utils';
44

55
export const setupNunjucksFilters = function(app: Express): void {
66
const nunjucksEnv = nunjucks.configure('templates', {
@@ -15,7 +15,7 @@ export const setupNunjucksFilters = function(app: Express): void {
1515

1616
// Add formatting for numbers to thousands separator
1717
nunjucksEnv.addFilter('thousands', (num: number | string) => {
18-
return num.toString().replace(/\./, ',').replace(/\B(?=(\d{3})+(?!\d))/g, '.');
18+
return formatThousands(num);
1919
});
2020

2121
// Add formatting filter for seconds to hh:mm format

src/routes/canvas.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IntraCoalition, PrismaClient } from '@prisma/client';
22
import { Express } from 'express';
33
import { CanvasRenderingContext2D, createCanvas, loadImage, registerFont } from 'canvas';
4-
import { bonusPointsAwardingStarted, CoalitionScore, getBlocAtDate, getCoalitionScore, getCoalitionTopContributors, getRanking, SingleRanking, SMALL_CONTRIBUTION_TYPES, timeAgo } from '../utils';
4+
import { bonusPointsAwardingStarted, CoalitionScore, formatThousands, getBlocAtDate, getCoalitionScore, getCoalitionTopContributors, getRanking, SingleRanking, SMALL_CONTRIBUTION_TYPES, timeAgo } from '../utils';
55

66
const CANVAS_WIDTH = 1920;
77
const CANVAS_HEIGHT = 1080;
@@ -302,7 +302,7 @@ const drawInitialCanvas = async function(prisma: PrismaClient, ctx: CanvasRender
302302
ctx.fillText(`${coalition.intra_coalition.name} `, textX, textY - entryHeight * 0.15);
303303
const nameWidth = ctx.measureText(`${coalition.intra_coalition.name} `).width;
304304
ctx.font = `bold ${Math.floor(entryHeight * 0.18)}px "Museo Sans"`;
305-
ctx.fillText(`${score.score} pts.`, textX + nameWidth, textY - entryHeight * 0.15);
305+
ctx.fillText(`${formatThousands(score.score)} pts.`, textX + nameWidth, textY - entryHeight * 0.15);
306306

307307
// Draw coalition logo
308308
// TODO: make this work! The logos are SVG, which is not supported properly by canvas loadImage
@@ -323,7 +323,7 @@ const drawInitialCanvas = async function(prisma: PrismaClient, ctx: CanvasRender
323323
ctx.fillText('TOP CONTRIBUTOR', profilePicX + profilePicSize + PADDING * 0.5, profilePicY + profilePicSize * 0.47);
324324

325325
// Draw login and score next to profile picture
326-
ctx.font = `${Math.floor(entryHeight * 0.13)}px "Museo Sans"`;
326+
ctx.font = `${Math.floor(entryHeight * 0.11)}px "Museo Sans"`;
327327
ctx.textBaseline = 'middle';
328328
ctx.fillText(`${(topContributor.user.login)}`, profilePicX + profilePicSize + PADDING * 0.5, profilePicY + profilePicSize * 0.62);
329329
}
@@ -417,7 +417,7 @@ export const setupCanvasRoutes = function(app: Express, prisma: PrismaClient): v
417417
// Draw user profile picture in the vertical center of the entry
418418
await drawUserProfilePicture(ctx, rightX + PADDING + entryPadding, currentY + entryPadding, profilePicSize, topRanking.user.image || '');
419419
// Draw login and score next to profile picture
420-
ctx.fillText(`${topRanking.user.login} / ${topRanking.score} pts.`, entryTextX, entryTextY, entryTextMaxWidth);
420+
ctx.fillText(`${topRanking.user.login} / ${formatThousands(topRanking.score)} pts.`, entryTextX, entryTextY, entryTextMaxWidth);
421421
} else {
422422
ctx.fillText('No data available', entryTextX, entryTextY, entryTextMaxWidth);
423423
}
@@ -533,7 +533,7 @@ export const setupCanvasRoutes = function(app: Express, prisma: PrismaClient): v
533533
ctx.fillStyle = '#FFFFFF';
534534
ctx.textBaseline = 'bottom';
535535
ctx.font = `bold ${Math.floor(entryInnerHeight * 0.21)}px "Museo Sans"`;
536-
ctx.fillText(`${score.user.intra_user.login} / ${score.amount} pts. / ${timeAgo(score.created_at)}`, entryTextX, entryTextY, entryTextMaxWidth);
536+
ctx.fillText(`${score.user.intra_user.login} / ${formatThousands(score.amount)} pts. / ${timeAgo(score.created_at)}`, entryTextX, entryTextY, entryTextMaxWidth);
537537

538538
// Draw user profile picture in the vertical center of the entry
539539
await drawUserProfilePicture(ctx, rightX + PADDING + entryPadding, currentY + entryPadding, profilePicSize, score.user.intra_user.image || '');

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ export const timeFromNow = function(date: Date | null): string {
271271
return `within a minute`; // don't specify: otherwise it's weird when the amount of seconds does not go down
272272
};
273273

274+
export const formatThousands = function(num: number | string): string {
275+
return num.toString().replace(/\./, ',').replace(/\B(?=(\d{3})+(?!\d))/g, '.');
276+
};
277+
274278
const blocCache = new NodeCache({
275279
stdTTL: 60 * 60, // 1 hour
276280
checkperiod: 60 * 5, // 5 minutes

0 commit comments

Comments
 (0)