From ba1d95cb4f450885d898cedcaccd3574cb676098 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Thu, 27 Aug 2020 00:44:15 +0900 Subject: [PATCH] Add params that are column row and rakns --- index.ts | 22 ++++++++++++++++++++-- src/card.ts | 12 +++++++++--- src/trophies.ts | 2 +- src/utils.ts | 10 +++++----- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/index.ts b/index.ts index f1e17cd..35c161c 100644 --- a/index.ts +++ b/index.ts @@ -7,13 +7,31 @@ import "https://deno.land/x/dotenv@v0.5.0/load.ts"; const client = new GithubAPIClient(); export default async (req: ServerRequest) => { - const username = parseParams(req).get("username"); + const params = parseParams(req); + const username = params.get("username"); + let row = CONSTANTS.DEFAULT_MAX_ROW; + let column = CONSTANTS.DEFAULT_MAX_COLUMN; + let ranks: Array = params.getAll("rank"); + if (params.has("row")) { + const param = params.get("row"); + if (param != null) { + row = parseInt(param); + } + } + + if (params.has("column")) { + const param = params.get("column"); + if (param != null) { + column = parseInt(param); + } + } + if (username != null) { const userInfo = await client.requestUserInfo(username); req.respond( { - body: new Card().render(userInfo), + body: new Card(ranks, column, row).render(userInfo), headers: new Headers( { "Content-Type": "image/svg+xml", diff --git a/src/card.ts b/src/card.ts index 4a9896a..0a64a62 100644 --- a/src/card.ts +++ b/src/card.ts @@ -14,9 +14,10 @@ export class Card { private width = 0; private height = 0; constructor( - private panelSize = CONSTANTS.DEFALT_PANEL_SIZE, - private maxColumn = CONSTANTS.DEFALT_MAX_COLUMN, - private maxRow = CONSTANTS.DEFALT_MAX_ROW, + private ranks: Array, + private maxColumn = CONSTANTS.DEFAULT_MAX_COLUMN, + private maxRow = CONSTANTS.DEFAULT_MAX_ROW, + private panelSize = CONSTANTS.DEFAULT_PANEL_SIZE, ) { this.width = panelSize * this.maxColumn; } @@ -30,6 +31,11 @@ export class Card { new TotalRepositoryTrophy(userInfo.totalRepositories), ); + // Filter by ranks + if (this.ranks.length != 0) { + trophyList = trophyList.filter((trophy) => this.ranks.includes(trophy.rank)) + } + // Sort by rank trophyList = trophyList.sort((a: Trophy, b: Trophy) => RANK_ORDER.indexOf(a.rank) - RANK_ORDER.indexOf(b.rank) diff --git a/src/trophies.ts b/src/trophies.ts index 794b2d1..d48b472 100644 --- a/src/trophies.ts +++ b/src/trophies.ts @@ -34,7 +34,7 @@ export class Trophy { this.topMessage = rankCondition.message; } } - render(x = 0, y = 0, panelSize = CONSTANTS.DEFALT_PANEL_SIZE): string { + render(x = 0, y = 0, panelSize = CONSTANTS.DEFAULT_PANEL_SIZE): string { return ` 999){ @@ -25,9 +25,9 @@ export function abridgeScore(score: number): string { export const CONSTANTS = { CACHE_MAX_AGE: 7200, - DEFALT_PANEL_SIZE: 110, - DEFALT_MAX_COLUMN: 6, - DEFALT_MAX_ROW: 3, + DEFAULT_PANEL_SIZE: 110, + DEFAULT_MAX_COLUMN: 6, + DEFAULT_MAX_ROW: 3, }; export enum RANK {