Add params that are column row and rakns

This commit is contained in:
ryo-ma 2020-08-27 00:44:15 +09:00
parent 95a0b56fa4
commit ba1d95cb4f
4 changed files with 35 additions and 11 deletions

View file

@ -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<string> = 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",

View file

@ -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<string>,
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)

View file

@ -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 `
<svg
x="${x}"

View file

@ -9,10 +9,10 @@ export function parseParams(req: ServerRequest): URLSearchParams {
}
export function abridgeScore(score: number): string {
if (score < 1) {
if (Math.abs(score) < 1) {
return "unknown";
}
if (score < 10) {
if (Math.abs(score) < 10) {
return score.toString() + "+"
}
if (Math.abs(score) > 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 {