Refactor code of card.ts

This commit is contained in:
ryo-ma 2021-05-08 18:38:06 +09:00
parent 3f7feb8208
commit b0fe96d3fe
3 changed files with 116 additions and 71 deletions

View file

@ -1,20 +1,6 @@
import {
Trophy,
TotalStarTrophy,
TotalCommitTrophy,
TotalFollowerTrophy,
TotalIssueTrophy,
TotalPullRequestTrophy,
TotalRepositoryTrophy,
MultipleLangTrophy,
LongTimeAccountTrophy,
AncientAccountTrophy,
Joined2020Trophy,
AllSuperRankTrophy,
MultipleOrganizationsTrophy,
} from "./trophies.ts";
import { UserInfo } from "./user_info.ts";
import { RANK_ORDER, RANK } from "./utils.ts";
import { TrophyList } from "./trophy_list.ts";
import { Trophy } from "./trophy.ts";
import { Theme } from "./theme.ts";
export class Card {
@ -31,75 +17,30 @@ export class Card {
private noBackground: boolean,
private noFrame: boolean,
) {
this.width = panelSize * this.maxColumn + this.marginWidth * (this.maxColumn - 1);
this.width = panelSize * this.maxColumn +
this.marginWidth * (this.maxColumn - 1);
}
render(
userInfo: UserInfo,
theme: Theme,
): string {
// Base trophies
let trophyList = new Array<Trophy>(
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 isAllSRank =
trophyList.every((trophy) => trophy.rank.slice(0, 1) == RANK.S) ? 1 : 0;
// Secret trophies
trophyList.push(
new MultipleLangTrophy(userInfo.languageCount),
new LongTimeAccountTrophy(userInfo.durationYear),
new AncientAccountTrophy(userInfo.ancientAccount),
new Joined2020Trophy(userInfo.joined2020),
new AllSuperRankTrophy(isAllSRank),
new MultipleOrganizationsTrophy(userInfo.totalOrganizations),
);
const trophyList = new TrophyList(userInfo);
// Filter by hidden
trophyList = trophyList.filter((trophy) =>
!trophy.hidden || trophy.rank !== RANK.UNKNOWN
);
trophyList.filterByHideen();
// Filter by titles
if (this.titles.length != 0) {
trophyList = trophyList.filter((trophy) => {
return trophy.filterTitles.some((title) => this.titles.includes(title));
});
trophyList.filterByTitles(this.titles);
}
// Filter by ranks
if (this.ranks.length != 0) {
trophyList = trophyList.filter((trophy) =>
this.ranks.includes(trophy.rank)
);
trophyList.filterByRanks(this.ranks);
}
// Sort by rank
trophyList = trophyList.sort((a: Trophy, b: Trophy) =>
RANK_ORDER.indexOf(a.rank) - RANK_ORDER.indexOf(b.rank)
);
trophyList.sortByRank();
// Calculate the height of card from turns
let row = Math.floor((trophyList.length - 1) / this.maxColumn) + 1;
if (row > this.maxRow) {
row = this.maxRow;
}
this.height = this.panelSize * row + this.marginHeight * (row - 1);
const row = this.getRow(trophyList);
this.height = this.getHeight(row);
// Join all trophy
const renderedTrophy = trophyList.reduce(
(sum: string, trophy: Trophy, i: number) => {
const currentColumn = i % this.maxColumn;
const currentRow = Math.floor(i / this.maxColumn);
const x = this.panelSize * currentColumn + this.marginWidth * currentColumn;
const y = this.panelSize * currentRow + this.marginHeight * currentRow;
return sum + trophy.render(theme, x, y, this.panelSize, this.noBackground, this.noFrame);
},
"",
);
return `
<svg
width="${this.width}"
@ -108,7 +49,40 @@ export class Card {
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
${renderedTrophy}
${this.renderTrophy(trophyList, theme)}
</svg>`;
}
private getRow(trophyList: TrophyList) {
let row = Math.floor((trophyList.length - 1) / this.maxColumn) + 1;
if (row > this.maxRow) {
row = this.maxRow;
}
return row;
}
private getHeight(row: number) {
// Calculate the height of card from turns
return this.panelSize * row + this.marginHeight * (row - 1);
}
private renderTrophy(trophyList: TrophyList, theme: Theme) {
return trophyList.getArray.reduce(
(sum: string, trophy: Trophy, i: number) => {
const currentColumn = i % this.maxColumn;
const currentRow = Math.floor(i / this.maxColumn);
const x = this.panelSize * currentColumn +
this.marginWidth * currentColumn;
const y = this.panelSize * currentRow + this.marginHeight * currentRow;
return sum +
trophy.render(
theme,
x,
y,
this.panelSize,
this.noBackground,
this.noFrame,
);
},
"",
);
}
}

71
src/trophy_list.ts Normal file
View file

@ -0,0 +1,71 @@
import {
Trophy,
TotalStarTrophy,
TotalCommitTrophy,
TotalFollowerTrophy,
TotalIssueTrophy,
TotalPullRequestTrophy,
TotalRepositoryTrophy,
MultipleLangTrophy,
LongTimeAccountTrophy,
AncientAccountTrophy,
Joined2020Trophy,
AllSuperRankTrophy,
MultipleOrganizationsTrophy,
} from "./trophy.ts";
import { UserInfo } from "./user_info.ts";
import { RANK_ORDER, RANK } from "./utils.ts";
export class TrophyList {
private trophies = new Array<Trophy>();
constructor(userInfo: UserInfo) {
// Base trophies
this.trophies.push(
new TotalStarTrophy(userInfo.totalStargazers),
new TotalCommitTrophy(userInfo.totalCommits),
new TotalFollowerTrophy(userInfo.totalFollowers),
new TotalIssueTrophy(userInfo.totalIssues),
new TotalPullRequestTrophy(userInfo.totalPullRequests),
new TotalRepositoryTrophy(userInfo.totalRepositories),
);
// Secret trophies
this.trophies.push(
new AllSuperRankTrophy(this.isAllSRank),
new MultipleLangTrophy(userInfo.languageCount),
new LongTimeAccountTrophy(userInfo.durationYear),
new AncientAccountTrophy(userInfo.ancientAccount),
new Joined2020Trophy(userInfo.joined2020),
new MultipleOrganizationsTrophy(userInfo.totalOrganizations),
);
}
get length() {
return this.trophies.length;
}
get getArray() {
return this.trophies;
}
private get isAllSRank() {
return this.trophies.every((trophy) => trophy.rank.slice(0, 1) == RANK.S) ? 1 : 0;
}
filterByHideen() {
this.trophies = this.trophies.filter((trophy) =>
!trophy.hidden || trophy.rank !== RANK.UNKNOWN
);
}
filterByTitles(titles: Array<string>) {
this.trophies = this.trophies.filter((trophy) => {
return trophy.filterTitles.some((title) => titles.includes(title));
});
}
filterByRanks(ranks: Array<string>) {
this.trophies = this.trophies.filter((trophy) =>
ranks.includes(trophy.rank)
);
}
sortByRank() {
this.trophies = this.trophies.sort((a: Trophy, b: Trophy) =>
RANK_ORDER.indexOf(a.rank) - RANK_ORDER.indexOf(b.rank)
);
}
}