diff --git a/index.ts b/index.ts index 9244ad9..f1e17cd 100644 --- a/index.ts +++ b/index.ts @@ -1,11 +1,10 @@ import { ServerRequest } from "./deps.ts"; import { GithubAPIClient } from "./src/github_api_client.ts"; import { Card } from "./src/card.ts"; -import { parseParams } from "./src/utils.ts"; +import { parseParams, CONSTANTS } from "./src/utils.ts"; import "https://deno.land/x/dotenv@v0.5.0/load.ts"; const client = new GithubAPIClient(); -const cacheMaxAge = 7200; export default async (req: ServerRequest) => { const username = parseParams(req).get("username"); @@ -18,7 +17,7 @@ export default async (req: ServerRequest) => { headers: new Headers( { "Content-Type": "image/svg+xml", - "Cache-Control": `public, max-age: ${cacheMaxAge}`, + "Cache-Control": `public, max-age: ${CONSTANTS.CACHE_MAX_AGE}`, }, ), }, diff --git a/src/card.ts b/src/card.ts index 53b3e4f..eeb2f6b 100644 --- a/src/card.ts +++ b/src/card.ts @@ -8,16 +8,17 @@ import { TotalRepositoryTrophy, } from "./trophies.ts"; import { UserInfo } from "./github_api_client.ts"; +import { CONSTANTS } from "./utils.ts"; export class Card { private width = 0; private height = 0; constructor( - private panelSize = 110, - private maxPanelWidthCount = 6, - private maxPanelHeightCount = 3, + private panelSize = CONSTANTS.DEFALT_PANEL_SIZE, + private maxColumn = CONSTANTS.DEFALT_MAX_COLUMN, + private maxRow = CONSTANTS.DEFALT_MAX_ROW, ) { - this.width = panelSize * this.maxPanelWidthCount; + this.width = panelSize * this.maxColumn; } render(userInfo: UserInfo): string { const trophyList = new Array( @@ -29,17 +30,16 @@ export class Card { new TotalRepositoryTrophy(userInfo.totalRepositories), ); - let panelHeightCount = - Math.floor((trophyList.length - 1) / this.maxPanelWidthCount) + 1; - if (panelHeightCount > this.maxPanelHeightCount) { - panelHeightCount = this.maxPanelHeightCount; + let row = Math.floor((trophyList.length - 1) / this.maxColumn) + 1; + if (row > this.maxRow) { + row = this.maxRow; } - this.height = this.panelSize * panelHeightCount; + this.height = this.panelSize * row; const renderedTrophy = trophyList.reduce( (sum: string, trophy: Trophy, i: number) => { - const x = this.panelSize * (i % this.maxPanelWidthCount); - const y = this.panelSize * Math.floor(i / this.maxPanelWidthCount); + const x = this.panelSize * (i % this.maxColumn); + const y = this.panelSize * Math.floor(i / this.maxColumn); return sum + trophy.render(x, y, this.panelSize); }, "", diff --git a/src/icons.ts b/src/icons.ts index 00d9232..d5d587a 100644 --- a/src/icons.ts +++ b/src/icons.ts @@ -1,4 +1,4 @@ -export const icons = "icons"; +import { RANK } from "./utils.ts"; const goldColor = "#FFD700"; const silverColor = "#C0C0C0"; @@ -25,16 +25,16 @@ Created by potrace 1.15, written by Peter Selinger 2001-2017 ` -export function getTropyIcon(rank = "?") { +export function getTropyIcon(rank = RANK.UNKNOWN) { let color = "currentColor" let backgroundIcon = ""; - if (rank == "S") { + if (rank == RANK.S) { color = goldColor; backgroundIcon = leafIcon; - } else if (rank == "A") { + } else if (rank == RANK.A) { color = silverColor; backgroundIcon = leafIcon; - } else if (rank == "B") { + } else if (rank == RANK.B) { color = bronzeColor; } return ` diff --git a/src/trophies.ts b/src/trophies.ts index d60915e..d8677f3 100644 --- a/src/trophies.ts +++ b/src/trophies.ts @@ -1,20 +1,20 @@ import { getTropyIcon } from "./icons.ts"; - +import { CONSTANTS, RANK } from "./utils.ts"; export abstract class Trophy { - rank: string = "?"; + rank: RANK = RANK.UNKNOWN; topMessage = "Unknown"; bottomMessage = "+0"; title = ""; abstract setRank(): void; - render(x = 0, y = 0, cardSize = 110): string { + render(x = 0, y = 0, panelSize = CONSTANTS.DEFALT_PANEL_SIZE): string { return ` @@ -22,8 +22,8 @@ export abstract class Trophy { x="0.5" y="0.5" rx="4.5" - width="${cardSize - 1}" - height="${cardSize - 1}" + width="${panelSize - 1}" + height="${panelSize - 1}" stroke="#e1e4e8" fill="#fff" stroke-opacity="1" @@ -45,19 +45,19 @@ export class TotalStarTrophy extends Trophy { } setRank() { if (this.totalStartgazer >= 200) { - this.rank = "S"; + this.rank = RANK.S; this.topMessage = "Super Star"; this.bottomMessage = "+200"; } else if (this.totalStartgazer >= 100) { - this.rank = "A"; + this.rank = RANK.A; this.topMessage = "High Star"; this.bottomMessage = "+100"; } else if (this.totalStartgazer >= 10) { - this.rank = "B"; + this.rank = RANK.B; this.topMessage = "Middle Star"; this.bottomMessage = "+10"; } else if (this.totalStartgazer >= 1) { - this.rank = "C"; + this.rank = RANK.C; this.topMessage = "First Star"; this.bottomMessage = "+1"; } @@ -72,19 +72,19 @@ export class TotalCommitTrophy extends Trophy { } setRank() { if (this.totalStartgazer >= 1000) { - this.rank = "S"; + this.rank = RANK.S; this.topMessage = "Super Commiter"; this.bottomMessage = "+1000"; } else if (this.totalStartgazer >= 500) { - this.rank = "A"; + this.rank = RANK.A; this.topMessage = "High Commiter"; this.bottomMessage = "+500"; } else if (this.totalStartgazer >= 100) { - this.rank = "B"; + this.rank = RANK.B; this.topMessage = "Middle Commiter"; this.bottomMessage = "+100"; } else if (this.totalStartgazer >= 1) { - this.rank = "C"; + this.rank = RANK.C; this.topMessage = "First Commit"; this.bottomMessage = "+1"; } @@ -99,19 +99,19 @@ export class TotalFollowerTrophy extends Trophy { } setRank() { if (this.totalStartgazer >= 100) { - this.rank = "S"; + this.rank = RANK.S; this.topMessage = "Celebrity"; this.bottomMessage = "+100"; } else if (this.totalStartgazer >= 20) { - this.rank = "A"; + this.rank = RANK.A; this.topMessage = "Famous User"; this.bottomMessage = "+20"; } else if (this.totalStartgazer >= 10) { - this.rank = "B"; + this.rank = RANK.B; this.topMessage = "Many Friends"; this.bottomMessage = "+10"; } else if (this.totalStartgazer >= 1) { - this.rank = "C"; + this.rank = RANK.C; this.topMessage = "First Friend"; this.bottomMessage = "+1"; } @@ -125,19 +125,19 @@ export class TotalIssueTrophy extends Trophy { } setRank() { if (this.totalStartgazer >= 100) { - this.rank = "S"; + this.rank = RANK.S; this.topMessage = "Super Issuer"; this.bottomMessage = "+100"; } else if (this.totalStartgazer >= 20) { - this.rank = "A"; + this.rank = RANK.A; this.topMessage = "High Issuer"; this.bottomMessage = "+20"; } else if (this.totalStartgazer >= 10) { - this.rank = "B"; + this.rank = RANK.B; this.topMessage = "Middle Issuer"; this.bottomMessage = "+10"; } else if (this.totalStartgazer >= 1) { - this.rank = "C"; + this.rank = RANK.C; this.topMessage = "First Issue"; this.bottomMessage = "+1"; } @@ -152,19 +152,19 @@ export class TotalPullRequestTrophy extends Trophy { } setRank() { if (this.totalStartgazer >= 100) { - this.rank = "S"; + this.rank = RANK.S; this.topMessage = "God PR User"; this.bottomMessage = "+100"; } else if (this.totalStartgazer >= 20) { - this.rank = "A"; + this.rank = RANK.A; this.topMessage = "High PR User"; this.bottomMessage = "+20"; } else if (this.totalStartgazer >= 10) { - this.rank = "B"; + this.rank = RANK.B; this.topMessage = "Many PRs"; this.bottomMessage = "+10"; } else if (this.totalStartgazer >= 1) { - this.rank = "C"; + this.rank = RANK.C; this.topMessage = "First PR"; this.bottomMessage = "+1"; } @@ -179,21 +179,21 @@ export class TotalRepositoryTrophy extends Trophy { } setRank() { if (this.totalStartgazer >= 100) { - this.rank = "S"; + this.rank = RANK.S; this.topMessage = "God Repo Creator"; this.bottomMessage = "+100"; } else if (this.totalStartgazer >= 20) { - this.rank = "A"; + this.rank = RANK.A; this.topMessage = "High Repo Creator"; this.bottomMessage = "+20"; } else if (this.totalStartgazer >= 10) { - this.rank = "B"; + this.rank = RANK.B; this.topMessage = "Many Repo"; this.bottomMessage = "+10"; } else if (this.totalStartgazer >= 1) { - this.rank = "C"; + this.rank = RANK.C; this.topMessage = "First Repository"; this.bottomMessage = "+1"; } } -} \ No newline at end of file +} diff --git a/src/utils.ts b/src/utils.ts index 9e09ba9..767cb11 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,4 +6,20 @@ export function parseParams(req: ServerRequest): URLSearchParams { return new URLSearchParams(); } return new URLSearchParams(splitedURL[1]); +} + +export const CONSTANTS = { + CACHE_MAX_AGE: 7200, + DEFALT_PANEL_SIZE: 110, + DEFALT_MAX_COLUMN: 6, + DEFALT_MAX_ROW: 3, +}; + +export enum RANK { + S = "S", + A = "A", + B = "B", + C = "C", + UNKNOWN = "?", + } \ No newline at end of file