Add CONSTANTS and RANK enum

This commit is contained in:
ryo-ma 2020-08-24 22:07:55 +09:00
parent 6a64dbe1f1
commit bc9fe8f59f
5 changed files with 67 additions and 52 deletions

View file

@ -1,11 +1,10 @@
import { ServerRequest } from "./deps.ts"; import { ServerRequest } from "./deps.ts";
import { GithubAPIClient } from "./src/github_api_client.ts"; import { GithubAPIClient } from "./src/github_api_client.ts";
import { Card } from "./src/card.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"; import "https://deno.land/x/dotenv@v0.5.0/load.ts";
const client = new GithubAPIClient(); const client = new GithubAPIClient();
const cacheMaxAge = 7200;
export default async (req: ServerRequest) => { export default async (req: ServerRequest) => {
const username = parseParams(req).get("username"); const username = parseParams(req).get("username");
@ -18,7 +17,7 @@ export default async (req: ServerRequest) => {
headers: new Headers( headers: new Headers(
{ {
"Content-Type": "image/svg+xml", "Content-Type": "image/svg+xml",
"Cache-Control": `public, max-age: ${cacheMaxAge}`, "Cache-Control": `public, max-age: ${CONSTANTS.CACHE_MAX_AGE}`,
}, },
), ),
}, },

View file

@ -8,16 +8,17 @@ import {
TotalRepositoryTrophy, TotalRepositoryTrophy,
} from "./trophies.ts"; } from "./trophies.ts";
import { UserInfo } from "./github_api_client.ts"; import { UserInfo } from "./github_api_client.ts";
import { CONSTANTS } from "./utils.ts";
export class Card { export class Card {
private width = 0; private width = 0;
private height = 0; private height = 0;
constructor( constructor(
private panelSize = 110, private panelSize = CONSTANTS.DEFALT_PANEL_SIZE,
private maxPanelWidthCount = 6, private maxColumn = CONSTANTS.DEFALT_MAX_COLUMN,
private maxPanelHeightCount = 3, private maxRow = CONSTANTS.DEFALT_MAX_ROW,
) { ) {
this.width = panelSize * this.maxPanelWidthCount; this.width = panelSize * this.maxColumn;
} }
render(userInfo: UserInfo): string { render(userInfo: UserInfo): string {
const trophyList = new Array<Trophy>( const trophyList = new Array<Trophy>(
@ -29,17 +30,16 @@ export class Card {
new TotalRepositoryTrophy(userInfo.totalRepositories), new TotalRepositoryTrophy(userInfo.totalRepositories),
); );
let panelHeightCount = let row = Math.floor((trophyList.length - 1) / this.maxColumn) + 1;
Math.floor((trophyList.length - 1) / this.maxPanelWidthCount) + 1; if (row > this.maxRow) {
if (panelHeightCount > this.maxPanelHeightCount) { row = this.maxRow;
panelHeightCount = this.maxPanelHeightCount;
} }
this.height = this.panelSize * panelHeightCount; this.height = this.panelSize * row;
const renderedTrophy = trophyList.reduce( const renderedTrophy = trophyList.reduce(
(sum: string, trophy: Trophy, i: number) => { (sum: string, trophy: Trophy, i: number) => {
const x = this.panelSize * (i % this.maxPanelWidthCount); const x = this.panelSize * (i % this.maxColumn);
const y = this.panelSize * Math.floor(i / this.maxPanelWidthCount); const y = this.panelSize * Math.floor(i / this.maxColumn);
return sum + trophy.render(x, y, this.panelSize); return sum + trophy.render(x, y, this.panelSize);
}, },
"", "",

View file

@ -1,4 +1,4 @@
export const icons = "icons"; import { RANK } from "./utils.ts";
const goldColor = "#FFD700"; const goldColor = "#FFD700";
const silverColor = "#C0C0C0"; const silverColor = "#C0C0C0";
@ -25,16 +25,16 @@ Created by potrace 1.15, written by Peter Selinger 2001-2017
<path d="M7249 1054 c-42 -7 -109 -25 -150 -40 -76 -27 -226 -101 -267 -133 l-24 -18 41 -66 c22 -37 36 -67 31 -67 -18 0 -11 -80 11 -121 11 -22 53 -73 92 -113 279 -282 682 -361 1251 -245 113 23 172 49 188 84 14 30 3 72 -20 77 -9 3 18 15 61 29 l78 24 -93 81 c-362 313 -622 460 -902 509 -106 18 -194 18 -297 -1z"/> <path d="M7249 1054 c-42 -7 -109 -25 -150 -40 -76 -27 -226 -101 -267 -133 l-24 -18 41 -66 c22 -37 36 -67 31 -67 -18 0 -11 -80 11 -121 11 -22 53 -73 92 -113 279 -282 682 -361 1251 -245 113 23 172 49 188 84 14 30 3 72 -20 77 -9 3 18 15 61 29 l78 24 -93 81 c-362 313 -622 460 -902 509 -106 18 -194 18 -297 -1z"/>
</g> </g>
</svg>` </svg>`
export function getTropyIcon(rank = "?") { export function getTropyIcon(rank = RANK.UNKNOWN) {
let color = "currentColor" let color = "currentColor"
let backgroundIcon = ""; let backgroundIcon = "";
if (rank == "S") { if (rank == RANK.S) {
color = goldColor; color = goldColor;
backgroundIcon = leafIcon; backgroundIcon = leafIcon;
} else if (rank == "A") { } else if (rank == RANK.A) {
color = silverColor; color = silverColor;
backgroundIcon = leafIcon; backgroundIcon = leafIcon;
} else if (rank == "B") { } else if (rank == RANK.B) {
color = bronzeColor; color = bronzeColor;
} }
return ` return `

View file

@ -1,20 +1,20 @@
import { getTropyIcon } from "./icons.ts"; import { getTropyIcon } from "./icons.ts";
import { CONSTANTS, RANK } from "./utils.ts";
export abstract class Trophy { export abstract class Trophy {
rank: string = "?"; rank: RANK = RANK.UNKNOWN;
topMessage = "Unknown"; topMessage = "Unknown";
bottomMessage = "+0"; bottomMessage = "+0";
title = ""; title = "";
abstract setRank(): void; abstract setRank(): void;
render(x = 0, y = 0, cardSize = 110): string { render(x = 0, y = 0, panelSize = CONSTANTS.DEFALT_PANEL_SIZE): string {
return ` return `
<svg <svg
x="${x}" x="${x}"
y="${y}" y="${y}"
width="${cardSize}" width="${panelSize}"
height="${cardSize}" height="${panelSize}"
viewBox="0 0 ${cardSize} ${cardSize}" viewBox="0 0 ${panelSize} ${panelSize}"
fill="none" fill="none"
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
> >
@ -22,8 +22,8 @@ export abstract class Trophy {
x="0.5" x="0.5"
y="0.5" y="0.5"
rx="4.5" rx="4.5"
width="${cardSize - 1}" width="${panelSize - 1}"
height="${cardSize - 1}" height="${panelSize - 1}"
stroke="#e1e4e8" stroke="#e1e4e8"
fill="#fff" fill="#fff"
stroke-opacity="1" stroke-opacity="1"
@ -45,19 +45,19 @@ export class TotalStarTrophy extends Trophy {
} }
setRank() { setRank() {
if (this.totalStartgazer >= 200) { if (this.totalStartgazer >= 200) {
this.rank = "S"; this.rank = RANK.S;
this.topMessage = "Super Star"; this.topMessage = "Super Star";
this.bottomMessage = "+200"; this.bottomMessage = "+200";
} else if (this.totalStartgazer >= 100) { } else if (this.totalStartgazer >= 100) {
this.rank = "A"; this.rank = RANK.A;
this.topMessage = "High Star"; this.topMessage = "High Star";
this.bottomMessage = "+100"; this.bottomMessage = "+100";
} else if (this.totalStartgazer >= 10) { } else if (this.totalStartgazer >= 10) {
this.rank = "B"; this.rank = RANK.B;
this.topMessage = "Middle Star"; this.topMessage = "Middle Star";
this.bottomMessage = "+10"; this.bottomMessage = "+10";
} else if (this.totalStartgazer >= 1) { } else if (this.totalStartgazer >= 1) {
this.rank = "C"; this.rank = RANK.C;
this.topMessage = "First Star"; this.topMessage = "First Star";
this.bottomMessage = "+1"; this.bottomMessage = "+1";
} }
@ -72,19 +72,19 @@ export class TotalCommitTrophy extends Trophy {
} }
setRank() { setRank() {
if (this.totalStartgazer >= 1000) { if (this.totalStartgazer >= 1000) {
this.rank = "S"; this.rank = RANK.S;
this.topMessage = "Super Commiter"; this.topMessage = "Super Commiter";
this.bottomMessage = "+1000"; this.bottomMessage = "+1000";
} else if (this.totalStartgazer >= 500) { } else if (this.totalStartgazer >= 500) {
this.rank = "A"; this.rank = RANK.A;
this.topMessage = "High Commiter"; this.topMessage = "High Commiter";
this.bottomMessage = "+500"; this.bottomMessage = "+500";
} else if (this.totalStartgazer >= 100) { } else if (this.totalStartgazer >= 100) {
this.rank = "B"; this.rank = RANK.B;
this.topMessage = "Middle Commiter"; this.topMessage = "Middle Commiter";
this.bottomMessage = "+100"; this.bottomMessage = "+100";
} else if (this.totalStartgazer >= 1) { } else if (this.totalStartgazer >= 1) {
this.rank = "C"; this.rank = RANK.C;
this.topMessage = "First Commit"; this.topMessage = "First Commit";
this.bottomMessage = "+1"; this.bottomMessage = "+1";
} }
@ -99,19 +99,19 @@ export class TotalFollowerTrophy extends Trophy {
} }
setRank() { setRank() {
if (this.totalStartgazer >= 100) { if (this.totalStartgazer >= 100) {
this.rank = "S"; this.rank = RANK.S;
this.topMessage = "Celebrity"; this.topMessage = "Celebrity";
this.bottomMessage = "+100"; this.bottomMessage = "+100";
} else if (this.totalStartgazer >= 20) { } else if (this.totalStartgazer >= 20) {
this.rank = "A"; this.rank = RANK.A;
this.topMessage = "Famous User"; this.topMessage = "Famous User";
this.bottomMessage = "+20"; this.bottomMessage = "+20";
} else if (this.totalStartgazer >= 10) { } else if (this.totalStartgazer >= 10) {
this.rank = "B"; this.rank = RANK.B;
this.topMessage = "Many Friends"; this.topMessage = "Many Friends";
this.bottomMessage = "+10"; this.bottomMessage = "+10";
} else if (this.totalStartgazer >= 1) { } else if (this.totalStartgazer >= 1) {
this.rank = "C"; this.rank = RANK.C;
this.topMessage = "First Friend"; this.topMessage = "First Friend";
this.bottomMessage = "+1"; this.bottomMessage = "+1";
} }
@ -125,19 +125,19 @@ export class TotalIssueTrophy extends Trophy {
} }
setRank() { setRank() {
if (this.totalStartgazer >= 100) { if (this.totalStartgazer >= 100) {
this.rank = "S"; this.rank = RANK.S;
this.topMessage = "Super Issuer"; this.topMessage = "Super Issuer";
this.bottomMessage = "+100"; this.bottomMessage = "+100";
} else if (this.totalStartgazer >= 20) { } else if (this.totalStartgazer >= 20) {
this.rank = "A"; this.rank = RANK.A;
this.topMessage = "High Issuer"; this.topMessage = "High Issuer";
this.bottomMessage = "+20"; this.bottomMessage = "+20";
} else if (this.totalStartgazer >= 10) { } else if (this.totalStartgazer >= 10) {
this.rank = "B"; this.rank = RANK.B;
this.topMessage = "Middle Issuer"; this.topMessage = "Middle Issuer";
this.bottomMessage = "+10"; this.bottomMessage = "+10";
} else if (this.totalStartgazer >= 1) { } else if (this.totalStartgazer >= 1) {
this.rank = "C"; this.rank = RANK.C;
this.topMessage = "First Issue"; this.topMessage = "First Issue";
this.bottomMessage = "+1"; this.bottomMessage = "+1";
} }
@ -152,19 +152,19 @@ export class TotalPullRequestTrophy extends Trophy {
} }
setRank() { setRank() {
if (this.totalStartgazer >= 100) { if (this.totalStartgazer >= 100) {
this.rank = "S"; this.rank = RANK.S;
this.topMessage = "God PR User"; this.topMessage = "God PR User";
this.bottomMessage = "+100"; this.bottomMessage = "+100";
} else if (this.totalStartgazer >= 20) { } else if (this.totalStartgazer >= 20) {
this.rank = "A"; this.rank = RANK.A;
this.topMessage = "High PR User"; this.topMessage = "High PR User";
this.bottomMessage = "+20"; this.bottomMessage = "+20";
} else if (this.totalStartgazer >= 10) { } else if (this.totalStartgazer >= 10) {
this.rank = "B"; this.rank = RANK.B;
this.topMessage = "Many PRs"; this.topMessage = "Many PRs";
this.bottomMessage = "+10"; this.bottomMessage = "+10";
} else if (this.totalStartgazer >= 1) { } else if (this.totalStartgazer >= 1) {
this.rank = "C"; this.rank = RANK.C;
this.topMessage = "First PR"; this.topMessage = "First PR";
this.bottomMessage = "+1"; this.bottomMessage = "+1";
} }
@ -179,21 +179,21 @@ export class TotalRepositoryTrophy extends Trophy {
} }
setRank() { setRank() {
if (this.totalStartgazer >= 100) { if (this.totalStartgazer >= 100) {
this.rank = "S"; this.rank = RANK.S;
this.topMessage = "God Repo Creator"; this.topMessage = "God Repo Creator";
this.bottomMessage = "+100"; this.bottomMessage = "+100";
} else if (this.totalStartgazer >= 20) { } else if (this.totalStartgazer >= 20) {
this.rank = "A"; this.rank = RANK.A;
this.topMessage = "High Repo Creator"; this.topMessage = "High Repo Creator";
this.bottomMessage = "+20"; this.bottomMessage = "+20";
} else if (this.totalStartgazer >= 10) { } else if (this.totalStartgazer >= 10) {
this.rank = "B"; this.rank = RANK.B;
this.topMessage = "Many Repo"; this.topMessage = "Many Repo";
this.bottomMessage = "+10"; this.bottomMessage = "+10";
} else if (this.totalStartgazer >= 1) { } else if (this.totalStartgazer >= 1) {
this.rank = "C"; this.rank = RANK.C;
this.topMessage = "First Repository"; this.topMessage = "First Repository";
this.bottomMessage = "+1"; this.bottomMessage = "+1";
} }
} }
} }

View file

@ -6,4 +6,20 @@ export function parseParams(req: ServerRequest): URLSearchParams {
return new URLSearchParams(); return new URLSearchParams();
} }
return new URLSearchParams(splitedURL[1]); 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 = "?",
} }