Support flexible card size

This commit is contained in:
ryo-ma 2020-08-23 22:40:58 +09:00
parent 964276d1f8
commit 7c88457692
2 changed files with 13 additions and 10 deletions

View file

@ -19,7 +19,7 @@ for await (const req of s) {
} else {
req.respond(
{
body: "Can not find username",
body: "Can not find a query parameter: username",
status: 404,
headers: new Headers({ "Content-Type": "text" }),
},

View file

@ -15,10 +15,9 @@ export class Card {
constructor(
private panelSize = 110,
private maxPanelWidthCount = 6,
private maxPanelHeightCount = 2,
private maxPanelHeightCount = 3,
) {
this.width = panelSize * this.maxPanelWidthCount;
this.height = panelSize * this.maxPanelHeightCount;
}
render(userInfo: UserInfo): string {
const trophyList = new Array<Trophy>(
@ -29,14 +28,18 @@ export class Card {
new TotalPullRequestTrophy(userInfo.totalPullRequests),
new TotalRepositoryTrophy(userInfo.totalRepositories),
);
let panelHeightCount =
Math.floor((trophyList.length - 1) / this.maxPanelWidthCount) + 1;
if (panelHeightCount > this.maxPanelHeightCount) {
panelHeightCount = this.maxPanelHeightCount;
}
this.height = this.panelSize * panelHeightCount;
const renderdTrophy = trophyList.reduce(
(sum: string, trophy: Trophy, i: number) => {
let x = (this.panelSize * i);
let y = 0;
if (i >= this.maxPanelWidthCount) {
x = (this.panelSize * (i - this.maxPanelWidthCount));
y = this.panelSize;
}
const x = this.panelSize * (i % this.maxPanelWidthCount);
const y = this.panelSize * Math.floor(i / this.maxPanelWidthCount);
return sum + trophy.render(x, y, this.panelSize);
},
"",