From b0fe96d3fea97a48364f630c521d83cc860402af Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Sat, 8 May 2021 18:38:06 +0900 Subject: [PATCH] Refactor code of card.ts --- src/card.ts | 116 +++++++++++++-------------------- src/{trophies.ts => trophy.ts} | 0 src/trophy_list.ts | 71 ++++++++++++++++++++ 3 files changed, 116 insertions(+), 71 deletions(-) rename src/{trophies.ts => trophy.ts} (100%) create mode 100644 src/trophy_list.ts diff --git a/src/card.ts b/src/card.ts index c0a67d2..1763a31 100644 --- a/src/card.ts +++ b/src/card.ts @@ -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( - 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 ` - ${renderedTrophy} + ${this.renderTrophy(trophyList, theme)} `; } + 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, + ); + }, + "", + ); + } } diff --git a/src/trophies.ts b/src/trophy.ts similarity index 100% rename from src/trophies.ts rename to src/trophy.ts diff --git a/src/trophy_list.ts b/src/trophy_list.ts new file mode 100644 index 0000000..d317f6c --- /dev/null +++ b/src/trophy_list.ts @@ -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(); + 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) { + this.trophies = this.trophies.filter((trophy) => { + return trophy.filterTitles.some((title) => titles.includes(title)); + }); + } + filterByRanks(ranks: Array) { + 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) + ); + } +} +