mirror of
https://github.com/reiyua/github-profile-trophy-reiyua-mirror.git
synced 2026-11-17 06:02:09 +11:00
Refactor code of card.ts
This commit is contained in:
parent
3f7feb8208
commit
b0fe96d3fe
3 changed files with 116 additions and 71 deletions
116
src/card.ts
116
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 { 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";
|
import { Theme } from "./theme.ts";
|
||||||
|
|
||||||
export class Card {
|
export class Card {
|
||||||
|
|
@ -31,75 +17,30 @@ export class Card {
|
||||||
private noBackground: boolean,
|
private noBackground: boolean,
|
||||||
private noFrame: 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(
|
render(
|
||||||
userInfo: UserInfo,
|
userInfo: UserInfo,
|
||||||
theme: Theme,
|
theme: Theme,
|
||||||
): string {
|
): string {
|
||||||
// Base trophies
|
const trophyList = new TrophyList(userInfo);
|
||||||
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),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Filter by hidden
|
trophyList.filterByHideen();
|
||||||
trophyList = trophyList.filter((trophy) =>
|
|
||||||
!trophy.hidden || trophy.rank !== RANK.UNKNOWN
|
|
||||||
);
|
|
||||||
|
|
||||||
// Filter by titles
|
|
||||||
if (this.titles.length != 0) {
|
if (this.titles.length != 0) {
|
||||||
trophyList = trophyList.filter((trophy) => {
|
trophyList.filterByTitles(this.titles);
|
||||||
return trophy.filterTitles.some((title) => this.titles.includes(title));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter by ranks
|
|
||||||
if (this.ranks.length != 0) {
|
if (this.ranks.length != 0) {
|
||||||
trophyList = trophyList.filter((trophy) =>
|
trophyList.filterByRanks(this.ranks);
|
||||||
this.ranks.includes(trophy.rank)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort by rank
|
trophyList.sortByRank();
|
||||||
trophyList = trophyList.sort((a: Trophy, b: Trophy) =>
|
|
||||||
RANK_ORDER.indexOf(a.rank) - RANK_ORDER.indexOf(b.rank)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Calculate the height of card from turns
|
const row = this.getRow(trophyList);
|
||||||
let row = Math.floor((trophyList.length - 1) / this.maxColumn) + 1;
|
this.height = this.getHeight(row);
|
||||||
if (row > this.maxRow) {
|
|
||||||
row = this.maxRow;
|
|
||||||
}
|
|
||||||
this.height = this.panelSize * row + this.marginHeight * (row - 1);
|
|
||||||
|
|
||||||
// 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 `
|
return `
|
||||||
<svg
|
<svg
|
||||||
width="${this.width}"
|
width="${this.width}"
|
||||||
|
|
@ -108,7 +49,40 @@ export class Card {
|
||||||
fill="none"
|
fill="none"
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
>
|
>
|
||||||
${renderedTrophy}
|
${this.renderTrophy(trophyList, theme)}
|
||||||
</svg>`;
|
</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
71
src/trophy_list.ts
Normal 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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Reference in a new issue