diff --git a/dev_main.ts b/dev_main.ts index 7cc6726..af842e1 100644 --- a/dev_main.ts +++ b/dev_main.ts @@ -1,11 +1,29 @@ import { serve } from "https://deno.land/std@0.66.0/http/server.ts"; +import { GithubAPIClient } from "./src/github_api_client.ts"; import { Card } from "./src/card.ts"; +import "https://deno.land/x/dotenv@v0.5.0/load.ts"; + const s = serve({ port: 8080 }); +const client = new GithubAPIClient(); for await (const req of s) { - req.respond( - { - body: new Card().render(), - headers: new Headers({ "Content-Type": "image/svg+xml" }), - }, - ); + const username = new URLSearchParams(req.url.split("?")[1]).get("username") + + if (username != null) { + const userInfo = await client.requestUserInfo(username); + req.respond( + { + body: new Card().render(userInfo), + headers: new Headers({ "Content-Type": "image/svg+xml" }), + }, + ); + } else { + req.respond( + { + body: "Can not find username", + status: 404, + headers: new Headers({ "Content-Type": "text" }), + }, + ); + + } } diff --git a/index.ts b/index.ts index 26d44bc..440ea63 100644 --- a/index.ts +++ b/index.ts @@ -5,12 +5,23 @@ import "https://deno.land/x/dotenv@v0.5.0/load.ts"; const client = new GithubAPIClient(); export default async (req: ServerRequest) => { - const userInfo = await client.requestUserInfo("ryo-ma") - const cardSize = 110; - req.respond( - { - body: new Card(cardSize).render(), - headers: new Headers({ "Content-Type": "image/svg+xml" }), - }, - ) + const username = new URLSearchParams(req.url.split("?")[1]).get("username"); + + if (username != null) { + const userInfo = await client.requestUserInfo(username); + req.respond( + { + body: new Card().render(userInfo), + headers: new Headers({ "Content-Type": "image/svg+xml" }), + }, + ); + } else { + req.respond( + { + body: "Can not find username", + status: 404, + headers: new Headers({ "Content-Type": "text" }), + }, + ); + } }; diff --git a/src/card.ts b/src/card.ts index 04aedda..6351162 100644 --- a/src/card.ts +++ b/src/card.ts @@ -3,7 +3,11 @@ import { TotalStarTrophy, TotalCommitTrophy, TotalFollowerTrophy, + TotalIssueTrophy, + TotalPullRequestTrophy, + TotalRepositoryTrophy, } from "./trophies.ts"; +import {UserInfo} from "./github_api_client.ts"; export class Card { private width = 0; @@ -16,32 +20,15 @@ export class Card { this.width = panelSize * this.maxPanelWidthCount; this.height = panelSize * this.maxPanelHeightCount; } - render(): string { - const trophy1 = new TotalStarTrophy(0); - const trophy2 = new TotalStarTrophy(100); - const trophy3 = new TotalStarTrophy(500); - const trophy4 = new TotalStarTrophy(1000); - const trophy5 = new TotalCommitTrophy(1); - const trophy6 = new TotalCommitTrophy(200); - const trophy7 = new TotalCommitTrophy(1000); - const trophy8 = new TotalCommitTrophy(5000); - const trophy9 = new TotalFollowerTrophy(1); - const trophy10 = new TotalFollowerTrophy(50); - const trophy11 = new TotalFollowerTrophy(200); - const trophy12 = new TotalFollowerTrophy(500); - const trophyList = new Array(); - trophyList.push(trophy1); - trophyList.push(trophy2); - trophyList.push(trophy3); - trophyList.push(trophy4); - trophyList.push(trophy5); - trophyList.push(trophy6); - trophyList.push(trophy7); - trophyList.push(trophy8); - trophyList.push(trophy9); - trophyList.push(trophy10); - trophyList.push(trophy11); - trophyList.push(trophy12); + render(userInfo: UserInfo): string { + const trophyList = new Array( + new TotalStarTrophy(userInfo.totalStargazers), + new TotalCommitTrophy(userInfo.totalCommits), + new TotalFollowerTrophy(userInfo.totalFollowers), + new TotalIssueTrophy(userInfo.totalIssues), + new TotalPullRequestTrophy(userInfo.totalPullRequests), + new TotalRepositoryTrophy(userInfo.totalRepositories), + ); const renderdTrophy = trophyList.reduce( (sum: string, trophy: Trophy, i: number) => { let x = (this.panelSize * i); diff --git a/src/github_api_client.ts b/src/github_api_client.ts index 58aa998..c10e72b 100644 --- a/src/github_api_client.ts +++ b/src/github_api_client.ts @@ -1,5 +1,5 @@ import {soxa} from "../deps.ts"; -class UserInfo { +export class UserInfo { constructor( public totalCommits: Number, public totalFollowers: Number, diff --git a/src/trophies.ts b/src/trophies.ts index 817afe6..1b6fa10 100644 --- a/src/trophies.ts +++ b/src/trophies.ts @@ -41,22 +41,22 @@ export abstract class Trophy { export class TotalStarTrophy extends Trophy { constructor(private totalStartgazer: Number) { super(); - this.title = "STAR"; + this.title = "Star"; this.setRank(); } setRank() { - if (this.totalStartgazer >= 1000) { + if (this.totalStartgazer >= 200) { this.rank = "S"; this.topMessage = "Super Star"; - this.bottomMessage = "+1000"; - } else if (this.totalStartgazer >= 500) { + this.bottomMessage = "+200"; + } else if (this.totalStartgazer >= 100) { this.rank = "A"; this.topMessage = "High Star"; - this.bottomMessage = "+500"; - } else if (this.totalStartgazer >= 100) { + this.bottomMessage = "+100"; + } else if (this.totalStartgazer >= 10) { this.rank = "B"; this.topMessage = "Middle Star"; - this.bottomMessage = "+100"; + this.bottomMessage = "+10"; } else if (this.totalStartgazer >= 1) { this.rank = "C"; this.topMessage = "First Star"; @@ -68,22 +68,22 @@ export class TotalStarTrophy extends Trophy { export class TotalCommitTrophy extends Trophy { constructor(private totalStartgazer: Number) { super(); - this.title = "COMMIT"; + this.title = "Commit"; this.setRank(); } setRank() { - if (this.totalStartgazer >= 5000) { + if (this.totalStartgazer >= 1000) { this.rank = "S"; this.topMessage = "Super Commiter"; - this.bottomMessage = "+5000"; - } else if (this.totalStartgazer >= 1000) { + this.bottomMessage = "+1000"; + } else if (this.totalStartgazer >= 500) { this.rank = "A"; this.topMessage = "High Commiter"; - this.bottomMessage = "+1000"; - } else if (this.totalStartgazer >= 200) { + this.bottomMessage = "+500"; + } else if (this.totalStartgazer >= 100) { this.rank = "B"; this.topMessage = "Middle Commiter"; - this.bottomMessage = "+200"; + this.bottomMessage = "+100"; } else if (this.totalStartgazer >= 1) { this.rank = "C"; this.topMessage = "First Commit"; @@ -95,22 +95,22 @@ export class TotalCommitTrophy extends Trophy { export class TotalFollowerTrophy extends Trophy { constructor(private totalStartgazer: Number) { super(); - this.title = "FOLLOWER"; + this.title = "Follower"; this.setRank(); } setRank() { - if (this.totalStartgazer >= 500) { + if (this.totalStartgazer >= 100) { this.rank = "S"; this.topMessage = "Celebrity"; - this.bottomMessage = "+500"; - } else if (this.totalStartgazer >= 200) { + this.bottomMessage = "+100"; + } else if (this.totalStartgazer >= 20) { this.rank = "A"; this.topMessage = "Famous User"; - this.bottomMessage = "+200"; - } else if (this.totalStartgazer >= 50) { + this.bottomMessage = "+20"; + } else if (this.totalStartgazer >= 10) { this.rank = "B"; this.topMessage = "Many Friends"; - this.bottomMessage = "+50"; + this.bottomMessage = "+10"; } else if (this.totalStartgazer >= 1) { this.rank = "C"; this.topMessage = "First Friend"; @@ -118,3 +118,83 @@ export class TotalFollowerTrophy extends Trophy { } } } +export class TotalIssueTrophy extends Trophy { + constructor(private totalStartgazer: Number) { + super(); + this.title = "Issue"; + this.setRank(); + } + setRank() { + if (this.totalStartgazer >= 100) { + this.rank = "S"; + this.topMessage = "Super Issuer"; + this.bottomMessage = "+100"; + } else if (this.totalStartgazer >= 20) { + this.rank = "A"; + this.topMessage = "High Issuer"; + this.bottomMessage = "+20"; + } else if (this.totalStartgazer >= 10) { + this.rank = "B"; + this.topMessage = "Middle Issuer"; + this.bottomMessage = "+10"; + } else if (this.totalStartgazer >= 1) { + this.rank = "C"; + this.topMessage = "First Issue"; + this.bottomMessage = "+1"; + } + } +} + +export class TotalPullRequestTrophy extends Trophy { + constructor(private totalStartgazer: Number) { + super(); + this.title = "PR"; + this.setRank(); + } + setRank() { + if (this.totalStartgazer >= 100) { + this.rank = "S"; + this.topMessage = "God PR User"; + this.bottomMessage = "+100"; + } else if (this.totalStartgazer >= 20) { + this.rank = "A"; + this.topMessage = "High PR User"; + this.bottomMessage = "+20"; + } else if (this.totalStartgazer >= 10) { + this.rank = "B"; + this.topMessage = "Many PRs"; + this.bottomMessage = "+10"; + } else if (this.totalStartgazer >= 1) { + this.rank = "C"; + this.topMessage = "First PR"; + this.bottomMessage = "+1"; + } + } +} + +export class TotalRepositoryTrophy extends Trophy { + constructor(private totalStartgazer: Number) { + super(); + this.title = "Repo"; + this.setRank(); + } + setRank() { + if (this.totalStartgazer >= 100) { + this.rank = "S"; + this.topMessage = "God Repo Creator"; + this.bottomMessage = "+100"; + } else if (this.totalStartgazer >= 20) { + this.rank = "A"; + this.topMessage = "High Repo Creator"; + this.bottomMessage = "+20"; + } else if (this.totalStartgazer >= 10) { + this.rank = "B"; + this.topMessage = "Many Repo"; + this.bottomMessage = "+10"; + } else if (this.totalStartgazer >= 1) { + this.rank = "C"; + this.topMessage = "First Repository"; + this.bottomMessage = "+1"; + } + } +} \ No newline at end of file