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 { } else {
req.respond( req.respond(
{ {
body: "Can not find username", body: "Can not find a query parameter: username",
status: 404, status: 404,
headers: new Headers({ "Content-Type": "text" }), headers: new Headers({ "Content-Type": "text" }),
}, },

View file

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