mirror of
https://github.com/reiyua/github-profile-trophy-reiyua-mirror.git
synced 2026-11-17 06:02:09 +11:00
432 lines
9.2 KiB
TypeScript
432 lines
9.2 KiB
TypeScript
import { getTrophyIcon, getNextRankBar } from "./icons.ts";
|
|
import { CONSTANTS, RANK, abridgeScore, RANK_ORDER, COLORS } from "./utils.ts";
|
|
|
|
class RankCondition {
|
|
constructor(
|
|
readonly rank: RANK,
|
|
readonly message: string,
|
|
readonly requiredScore: number,
|
|
) {}
|
|
}
|
|
|
|
|
|
export class Trophy {
|
|
rankCondition: RankCondition | null = null;
|
|
rank: RANK = RANK.UNKNOWN;
|
|
topMessage = "Unknown";
|
|
bottomMessage = "0";
|
|
title = "";
|
|
hidden = false;
|
|
constructor(
|
|
private score: number,
|
|
private rankConditions: Array<RankCondition>,
|
|
) {
|
|
this.bottomMessage = abridgeScore(score);
|
|
this.setRank();
|
|
}
|
|
setRank() {
|
|
const sortedRankConditions = this.rankConditions.sort((a, b) =>
|
|
RANK_ORDER.indexOf(a.rank) - RANK_ORDER.indexOf(b.rank)
|
|
);
|
|
// Set the rank that hit the first condition
|
|
const rankCondition = sortedRankConditions.find((r) =>
|
|
this.score >= r.requiredScore
|
|
);
|
|
if (rankCondition != null) {
|
|
this.rank = rankCondition.rank;
|
|
this.rankCondition = rankCondition;
|
|
this.topMessage = rankCondition.message;
|
|
}
|
|
}
|
|
private calculateNextRankPercentage() {
|
|
if (this.rank === RANK.UNKNOWN) {
|
|
return 0;
|
|
}
|
|
const nextRankIndex = RANK_ORDER.indexOf(this.rank) - 1;
|
|
// When got the max rank
|
|
if (nextRankIndex < 0 || this.rank === RANK.SSS) {
|
|
return 1;
|
|
}
|
|
const nextRank = RANK_ORDER[nextRankIndex];
|
|
const nextRankCondition = this.rankConditions.find((r) =>
|
|
r.rank == nextRank
|
|
);
|
|
const distance = nextRankCondition!.requiredScore -
|
|
this.rankCondition!.requiredScore;
|
|
const progress = this.score - this.rankCondition!.requiredScore;
|
|
const result = progress / distance;
|
|
return result;
|
|
}
|
|
render(x = 0, y = 0, panelSize = CONSTANTS.DEFAULT_PANEL_SIZE): string {
|
|
const nextRankBar = getNextRankBar(
|
|
this.title,
|
|
this.calculateNextRankPercentage(),
|
|
);
|
|
return `
|
|
<svg
|
|
x="${x}"
|
|
y="${y}"
|
|
width="${panelSize}"
|
|
height="${panelSize}"
|
|
viewBox="0 0 ${panelSize} ${panelSize}"
|
|
fill="none"
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
>
|
|
<rect
|
|
x="0.5"
|
|
y="0.5"
|
|
rx="4.5"
|
|
width="${panelSize - 1}"
|
|
height="${panelSize - 1}"
|
|
stroke="#e1e4e8"
|
|
fill="#fff"
|
|
stroke-opacity="1"
|
|
/>
|
|
${getTrophyIcon(this.rank)}
|
|
<text x="50%" y="18" text-anchor="middle" font-family="Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji" font-weight="bold" font-size="13" fill="#000">${this.title}</text>
|
|
<text x="50%" y="85" text-anchor="middle" font-family="Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji" font-weight="bold" font-size="10.5" fill="#666">${this.topMessage}</text>
|
|
<text x="50%" y="97" text-anchor="middle" font-family="Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji" font-weight="bold" font-size="10" fill="#666">${this.bottomMessage}</text>
|
|
${nextRankBar}
|
|
</svg>
|
|
`;
|
|
}
|
|
}
|
|
|
|
export class MultipleLangTrophy extends Trophy{
|
|
constructor(score: number){
|
|
const rankConditions = [
|
|
new RankCondition(
|
|
RANK.SECRET,
|
|
"Rainbow Lang User",
|
|
10,
|
|
),
|
|
];
|
|
super(score, rankConditions);
|
|
this.title = "MultipleLang";
|
|
this.hidden = true;
|
|
}
|
|
}
|
|
|
|
export class AncientAccountTrophy extends Trophy{
|
|
constructor(score: number){
|
|
const rankConditions = [
|
|
new RankCondition(
|
|
RANK.SECRET,
|
|
"Ancient User",
|
|
1,
|
|
),
|
|
];
|
|
super(score, rankConditions);
|
|
this.title = "AncientUser";
|
|
this.bottomMessage = "Before 2010"
|
|
this.hidden = true;
|
|
}
|
|
}
|
|
export class LongTimeAccountTrophy extends Trophy{
|
|
constructor(score: number){
|
|
const rankConditions = [
|
|
new RankCondition(
|
|
RANK.SECRET,
|
|
"Village Elder",
|
|
10,
|
|
),
|
|
];
|
|
super(score, rankConditions);
|
|
this.title = "LongTimeUser";
|
|
this.hidden = true;
|
|
}
|
|
}
|
|
|
|
export class TotalStarTrophy extends Trophy {
|
|
constructor(score: number) {
|
|
const rankConditions = [
|
|
new RankCondition(
|
|
RANK.SSS,
|
|
"Super Stargazer",
|
|
2000,
|
|
),
|
|
new RankCondition(
|
|
RANK.SS,
|
|
"High Stargazer",
|
|
700,
|
|
),
|
|
new RankCondition(
|
|
RANK.S,
|
|
"Stargazer",
|
|
200,
|
|
),
|
|
new RankCondition(
|
|
RANK.AAA,
|
|
"Super Star",
|
|
100,
|
|
),
|
|
new RankCondition(
|
|
RANK.AA,
|
|
"High Star",
|
|
50,
|
|
),
|
|
new RankCondition(
|
|
RANK.A,
|
|
"You are Star",
|
|
30,
|
|
),
|
|
new RankCondition(
|
|
RANK.B,
|
|
"Middle Star",
|
|
10,
|
|
),
|
|
new RankCondition(
|
|
RANK.C,
|
|
"First Star",
|
|
1,
|
|
),
|
|
];
|
|
super(score, rankConditions);
|
|
this.title = "Star";
|
|
}
|
|
}
|
|
|
|
export class TotalCommitTrophy extends Trophy {
|
|
constructor(score: number) {
|
|
const rankConditions = [
|
|
new RankCondition(
|
|
RANK.SSS,
|
|
"God Committer",
|
|
4000,
|
|
),
|
|
new RankCondition(
|
|
RANK.SS,
|
|
"Deep Committer",
|
|
2000,
|
|
),
|
|
new RankCondition(
|
|
RANK.S,
|
|
"Super Committer",
|
|
1000,
|
|
),
|
|
new RankCondition(
|
|
RANK.AAA,
|
|
"Ultra Committer",
|
|
500,
|
|
),
|
|
new RankCondition(
|
|
RANK.AA,
|
|
"Hyper Committer",
|
|
200,
|
|
),
|
|
new RankCondition(
|
|
RANK.A,
|
|
"High Committer",
|
|
100,
|
|
),
|
|
new RankCondition(
|
|
RANK.B,
|
|
"Middle Committer",
|
|
10,
|
|
),
|
|
new RankCondition(
|
|
RANK.C,
|
|
"First Commit",
|
|
1,
|
|
),
|
|
];
|
|
super(score, rankConditions);
|
|
this.title = "Commit";
|
|
}
|
|
}
|
|
|
|
export class TotalFollowerTrophy extends Trophy {
|
|
constructor(score: number) {
|
|
const rankConditions = [
|
|
new RankCondition(
|
|
RANK.SSS,
|
|
"Super Celebrity",
|
|
1000,
|
|
),
|
|
new RankCondition(
|
|
RANK.SS,
|
|
"Ultra Celebrity",
|
|
400,
|
|
),
|
|
new RankCondition(
|
|
RANK.S,
|
|
"Hyper Celebrity",
|
|
200,
|
|
),
|
|
new RankCondition(
|
|
RANK.AAA,
|
|
"Famous User",
|
|
100,
|
|
),
|
|
new RankCondition(
|
|
RANK.AA,
|
|
"Active User",
|
|
50,
|
|
),
|
|
new RankCondition(
|
|
RANK.A,
|
|
"Dynamic User",
|
|
20,
|
|
),
|
|
new RankCondition(
|
|
RANK.B,
|
|
"Many Friends",
|
|
10,
|
|
),
|
|
new RankCondition(
|
|
RANK.C,
|
|
"First Friend",
|
|
1,
|
|
),
|
|
];
|
|
super(score, rankConditions);
|
|
this.title = "Follower";
|
|
}
|
|
}
|
|
|
|
export class TotalIssueTrophy extends Trophy {
|
|
constructor(score: number) {
|
|
const rankConditions = [
|
|
new RankCondition(
|
|
RANK.SSS,
|
|
"God Issuer",
|
|
1000,
|
|
),
|
|
new RankCondition(
|
|
RANK.SS,
|
|
"Deep Issuer",
|
|
500,
|
|
),
|
|
new RankCondition(
|
|
RANK.S,
|
|
"Super Issuer",
|
|
200,
|
|
),
|
|
new RankCondition(
|
|
RANK.AAA,
|
|
"Ultra Issuer",
|
|
100,
|
|
),
|
|
new RankCondition(
|
|
RANK.AA,
|
|
"Hyper Issuer",
|
|
50,
|
|
),
|
|
new RankCondition(
|
|
RANK.A,
|
|
"High Issuer",
|
|
20,
|
|
),
|
|
new RankCondition(
|
|
RANK.B,
|
|
"Middle Issuer",
|
|
10,
|
|
),
|
|
new RankCondition(
|
|
RANK.C,
|
|
"First Issue",
|
|
1,
|
|
),
|
|
];
|
|
super(score, rankConditions);
|
|
this.title = "Issue";
|
|
}
|
|
}
|
|
|
|
export class TotalPullRequestTrophy extends Trophy {
|
|
constructor(score: number) {
|
|
const rankConditions = [
|
|
new RankCondition(
|
|
RANK.SSS,
|
|
"God PR User",
|
|
1000,
|
|
),
|
|
new RankCondition(
|
|
RANK.SS,
|
|
"Deep PR User",
|
|
500,
|
|
),
|
|
new RankCondition(
|
|
RANK.S,
|
|
"Super PR User",
|
|
200,
|
|
),
|
|
new RankCondition(
|
|
RANK.AAA,
|
|
"Ultra PR User",
|
|
100,
|
|
),
|
|
new RankCondition(
|
|
RANK.AA,
|
|
"Hyper PR User",
|
|
50,
|
|
),
|
|
new RankCondition(
|
|
RANK.A,
|
|
"High PR User",
|
|
20,
|
|
),
|
|
new RankCondition(
|
|
RANK.B,
|
|
"Middle PR User",
|
|
10,
|
|
),
|
|
new RankCondition(
|
|
RANK.C,
|
|
"First PR",
|
|
1,
|
|
),
|
|
];
|
|
super(score, rankConditions);
|
|
this.title = "PR";
|
|
}
|
|
}
|
|
|
|
export class TotalRepositoryTrophy extends Trophy {
|
|
constructor(score: number) {
|
|
const rankConditions = [
|
|
new RankCondition(
|
|
RANK.SSS,
|
|
"God Repo Creator",
|
|
100,
|
|
),
|
|
new RankCondition(
|
|
RANK.SS,
|
|
"Deep Repo Creator",
|
|
90,
|
|
),
|
|
new RankCondition(
|
|
RANK.S,
|
|
"Super Repo Creator",
|
|
80,
|
|
),
|
|
new RankCondition(
|
|
RANK.AAA,
|
|
"Ultra Repo Creator",
|
|
50,
|
|
),
|
|
new RankCondition(
|
|
RANK.AA,
|
|
"Hyper Repo Creator",
|
|
30,
|
|
),
|
|
new RankCondition(
|
|
RANK.A,
|
|
"High Repo Creator",
|
|
20,
|
|
),
|
|
new RankCondition(
|
|
RANK.B,
|
|
"Middle Repo Creator",
|
|
10,
|
|
),
|
|
new RankCondition(
|
|
RANK.C,
|
|
"First Repository",
|
|
1,
|
|
),
|
|
];
|
|
super(score, rankConditions);
|
|
this.title = "Repo";
|
|
}
|
|
}
|