mirror of
https://github.com/reiyua/github-profile-trophy-reiyua-mirror.git
synced 2026-11-17 06:02:09 +11:00
Support to next rank progress bar
This commit is contained in:
parent
ca52072528
commit
8876ff22fa
2 changed files with 126 additions and 63 deletions
62
src/icons.ts
62
src/icons.ts
|
|
@ -30,27 +30,66 @@ Created by potrace 1.15, written by Peter Selinger 2001-2017
|
||||||
</g>
|
</g>
|
||||||
</svg>`;
|
</svg>`;
|
||||||
|
|
||||||
function getSmallTrophyIcon(icon: string, color: string, count: number): string {
|
export const getNextRankBar = (title: string, color: string, percentage: number): string => {
|
||||||
|
const maxWidht = 80;
|
||||||
|
return `
|
||||||
|
<style>
|
||||||
|
@keyframes ${title}RankAnimation {
|
||||||
|
from {
|
||||||
|
width: 0px;
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
width: ${maxWidht * percentage}px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#${title}-rank-progress{
|
||||||
|
animation: ${title}RankAnimation 1s forwards ease-in-out;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<rect
|
||||||
|
x="15"
|
||||||
|
y="101"
|
||||||
|
rx="1"
|
||||||
|
width="${maxWidht}"
|
||||||
|
height="3.2"
|
||||||
|
opacity="0.3"
|
||||||
|
fill="${color}"
|
||||||
|
/>
|
||||||
|
<rect
|
||||||
|
id="${title}-rank-progress"
|
||||||
|
x="15"
|
||||||
|
y="101"
|
||||||
|
rx="1"
|
||||||
|
opacity="0.7"
|
||||||
|
height="3.2"
|
||||||
|
fill="${color}"
|
||||||
|
/>
|
||||||
|
`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getSmallTrophyIcon = (
|
||||||
|
icon: string,
|
||||||
|
color: string,
|
||||||
|
count: number,
|
||||||
|
): string => {
|
||||||
const leftXPosition = 7;
|
const leftXPosition = 7;
|
||||||
const rightXPosition = 68;
|
const rightXPosition = 68;
|
||||||
const getIcon = (x: number) => {
|
const getIcon = (x: number) => {
|
||||||
return `<svg x="${x}" y="35" width="65" height="65" viewBox="0 0 30 30" class="bi bi-trophy" fill="${color}" xmlns="http://www.w3.org/2000/svg">
|
return `<svg x="${x}" y="35" width="65" height="65" viewBox="0 0 30 30" class="bi bi-trophy" fill="${color}" xmlns="http://www.w3.org/2000/svg">
|
||||||
${icon}
|
${icon}
|
||||||
</svg>`
|
</svg>`;
|
||||||
};
|
};
|
||||||
if (count == 1) {
|
if (count == 1) {
|
||||||
// Double Rank
|
// Double Rank
|
||||||
return getIcon(rightXPosition);
|
return getIcon(rightXPosition);
|
||||||
} else if (count == 2){
|
} else if (count == 2) {
|
||||||
// Triple Rank
|
// Triple Rank
|
||||||
return `${getIcon(leftXPosition)}${getIcon(rightXPosition)}`
|
return `${getIcon(leftXPosition)}${getIcon(rightXPosition)}`;
|
||||||
}
|
}
|
||||||
// Single Rank
|
// Single Rank
|
||||||
return ''
|
return "";
|
||||||
|
};
|
||||||
}
|
export const getTrophyIcon = (rank = RANK.UNKNOWN) => {
|
||||||
export function getTrophyIcon(rank = RANK.UNKNOWN) {
|
|
||||||
|
|
||||||
let color = "currentColor";
|
let color = "currentColor";
|
||||||
let rankColor = "#333";
|
let rankColor = "#333";
|
||||||
let backgroundIcon = "";
|
let backgroundIcon = "";
|
||||||
|
|
@ -80,5 +119,6 @@ export function getTrophyIcon(rank = RANK.UNKNOWN) {
|
||||||
${optionRankIcon}
|
${optionRankIcon}
|
||||||
<svg x="28" y="20" width="100" height="100" viewBox="0 0 30 30" class="bi bi-trophy" fill="${color}" xmlns="http://www.w3.org/2000/svg">
|
<svg x="28" y="20" width="100" height="100" viewBox="0 0 30 30" class="bi bi-trophy" fill="${color}" xmlns="http://www.w3.org/2000/svg">
|
||||||
${icon}
|
${icon}
|
||||||
</svg>`;
|
</svg>
|
||||||
}
|
`;
|
||||||
|
};
|
||||||
|
|
|
||||||
127
src/trophies.ts
127
src/trophies.ts
|
|
@ -1,15 +1,16 @@
|
||||||
import { getTrophyIcon } from "./icons.ts";
|
import { getTrophyIcon, getNextRankBar } from "./icons.ts";
|
||||||
import { CONSTANTS, RANK, abridgeScore, RANK_ORDER } from "./utils.ts";
|
import { CONSTANTS, RANK, abridgeScore, RANK_ORDER } from "./utils.ts";
|
||||||
|
|
||||||
class RankCondition {
|
class RankCondition {
|
||||||
constructor(
|
constructor(
|
||||||
readonly rank: RANK,
|
readonly rank: RANK,
|
||||||
readonly message: string,
|
readonly message: string,
|
||||||
readonly condition: (score: number) => boolean,
|
readonly requiredScore: number,
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Trophy {
|
export class Trophy {
|
||||||
|
rankCondition: RankCondition | null = null;
|
||||||
rank: RANK = RANK.UNKNOWN;
|
rank: RANK = RANK.UNKNOWN;
|
||||||
topMessage = "Unknown";
|
topMessage = "Unknown";
|
||||||
bottomMessage = "0";
|
bottomMessage = "0";
|
||||||
|
|
@ -27,14 +28,35 @@ export class Trophy {
|
||||||
);
|
);
|
||||||
// Set the rank that hit the first condition
|
// Set the rank that hit the first condition
|
||||||
const rankCondition = sortedRankConditions.find((r) =>
|
const rankCondition = sortedRankConditions.find((r) =>
|
||||||
r.condition(this.score)
|
this.score >= r.requiredScore
|
||||||
);
|
);
|
||||||
if (rankCondition != null) {
|
if (rankCondition != null) {
|
||||||
this.rank = rankCondition.rank;
|
this.rank = rankCondition.rank;
|
||||||
|
this.rankCondition = rankCondition;
|
||||||
this.topMessage = rankCondition.message;
|
this.topMessage = rankCondition.message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private calculateNextRankPercentage() {
|
||||||
|
const nextRankIndex = RANK_ORDER.indexOf(this.rank) - 1;
|
||||||
|
if (nextRankIndex < 0) {
|
||||||
|
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 {
|
render(x = 0, y = 0, panelSize = CONSTANTS.DEFAULT_PANEL_SIZE): string {
|
||||||
|
const nextRankBar = getNextRankBar(
|
||||||
|
this.title,
|
||||||
|
"#0336A6",
|
||||||
|
this.calculateNextRankPercentage(),
|
||||||
|
);
|
||||||
return `
|
return `
|
||||||
<svg
|
<svg
|
||||||
x="${x}"
|
x="${x}"
|
||||||
|
|
@ -58,7 +80,8 @@ export class Trophy {
|
||||||
${getTrophyIcon(this.rank)}
|
${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="12" fill="#000">${this.title}</text>
|
<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="12" 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="9.5" fill="#666">${this.topMessage}</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="9.5" fill="#666">${this.topMessage}</text>
|
||||||
<text x="50%" y="99" text-anchor="middle" font-family="Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;" font-weight="bold" font-size="9" fill="#666">${this.bottomMessage}</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="9" fill="#666">${this.bottomMessage}</text>
|
||||||
|
${nextRankBar}
|
||||||
</svg>
|
</svg>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
@ -70,42 +93,42 @@ export class TotalStarTrophy extends Trophy {
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SSS,
|
RANK.SSS,
|
||||||
"Super Stargazer",
|
"Super Stargazer",
|
||||||
(s) => s >= 2000,
|
2000,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SS,
|
RANK.SS,
|
||||||
"High Stargazer",
|
"High Stargazer",
|
||||||
(s) => s >= 700,
|
700,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.S,
|
RANK.S,
|
||||||
"Stargazer",
|
"Stargazer",
|
||||||
(s) => s >= 200,
|
200,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AAA,
|
RANK.AAA,
|
||||||
"Super Star",
|
"Super Star",
|
||||||
(s) => s >= 100,
|
100,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AA,
|
RANK.AA,
|
||||||
"High Star",
|
"High Star",
|
||||||
(s) => s >= 50,
|
50,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.A,
|
RANK.A,
|
||||||
"You are Star",
|
"You are Star",
|
||||||
(s) => s >= 30,
|
30,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.B,
|
RANK.B,
|
||||||
"Middle Star",
|
"Middle Star",
|
||||||
(s) => s >= 10,
|
10,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.C,
|
RANK.C,
|
||||||
"First Star",
|
"First Star",
|
||||||
(s) => s >= 1,
|
1,
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
super(score, rankConditions);
|
super(score, rankConditions);
|
||||||
|
|
@ -119,42 +142,42 @@ export class TotalCommitTrophy extends Trophy {
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SSS,
|
RANK.SSS,
|
||||||
"God Committer",
|
"God Committer",
|
||||||
(s) => s >= 4000,
|
4000,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SS,
|
RANK.SS,
|
||||||
"Deep Committer",
|
"Deep Committer",
|
||||||
(s) => s >= 2000,
|
2000,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.S,
|
RANK.S,
|
||||||
"Super Committer",
|
"Super Committer",
|
||||||
(s) => s >= 1000,
|
1000,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AAA,
|
RANK.AAA,
|
||||||
"Ultra Committer",
|
"Ultra Committer",
|
||||||
(s) => s >= 500,
|
500,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AA,
|
RANK.AA,
|
||||||
"Hyper Commiter",
|
"Hyper Commiter",
|
||||||
(s) => s >= 200,
|
200,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.A,
|
RANK.A,
|
||||||
"High Committer",
|
"High Committer",
|
||||||
(s) => s >= 100,
|
100,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.B,
|
RANK.B,
|
||||||
"Middle Committer",
|
"Middle Committer",
|
||||||
(s) => s >= 10,
|
10,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.C,
|
RANK.C,
|
||||||
"First Commit",
|
"First Commit",
|
||||||
(s) => s >= 1,
|
1,
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
super(score, rankConditions);
|
super(score, rankConditions);
|
||||||
|
|
@ -168,42 +191,42 @@ export class TotalFollowerTrophy extends Trophy {
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SSS,
|
RANK.SSS,
|
||||||
"Super Celebrity",
|
"Super Celebrity",
|
||||||
(s) => s >= 1000,
|
1000,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SS,
|
RANK.SS,
|
||||||
"Ultra Celebrity",
|
"Ultra Celebrity",
|
||||||
(s) => s >= 400,
|
400,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.S,
|
RANK.S,
|
||||||
"Hyper Celebrity",
|
"Hyper Celebrity",
|
||||||
(s) => s >= 200,
|
200,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AAA,
|
RANK.AAA,
|
||||||
"Famous User",
|
"Famous User",
|
||||||
(s) => s >= 100,
|
100,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AA,
|
RANK.AA,
|
||||||
"Active User",
|
"Active User",
|
||||||
(s) => s >= 50,
|
50,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.A,
|
RANK.A,
|
||||||
"Dynamic User",
|
"Dynamic User",
|
||||||
(s) => s >= 20,
|
20,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.B,
|
RANK.B,
|
||||||
"Many Frieds",
|
"Many Frieds",
|
||||||
(s) => s >= 10,
|
10,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.C,
|
RANK.C,
|
||||||
"First Friend",
|
"First Friend",
|
||||||
(s) => s >= 1,
|
1,
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
super(score, rankConditions);
|
super(score, rankConditions);
|
||||||
|
|
@ -217,42 +240,42 @@ export class TotalIssueTrophy extends Trophy {
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SSS,
|
RANK.SSS,
|
||||||
"God Issuer",
|
"God Issuer",
|
||||||
(s) => s >= 1000,
|
1000,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SS,
|
RANK.SS,
|
||||||
"Deep Issuer",
|
"Deep Issuer",
|
||||||
(s) => s >= 500,
|
500,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.S,
|
RANK.S,
|
||||||
"Super Issuer",
|
"Super Issuer",
|
||||||
(s) => s >= 200,
|
200,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AAA,
|
RANK.AAA,
|
||||||
"Ultra Isuuer",
|
"Ultra Isuuer",
|
||||||
(s) => s >= 100,
|
100,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AA,
|
RANK.AA,
|
||||||
"Hyper Issuer",
|
"Hyper Issuer",
|
||||||
(s) => s >= 50,
|
50,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.A,
|
RANK.A,
|
||||||
"High Issuer",
|
"High Issuer",
|
||||||
(s) => s >= 20,
|
20,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.B,
|
RANK.B,
|
||||||
"Middle Issuer",
|
"Middle Issuer",
|
||||||
(s) => s >= 10,
|
10,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.C,
|
RANK.C,
|
||||||
"First Issue",
|
"First Issue",
|
||||||
(s) => s >= 1,
|
1,
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
super(score, rankConditions);
|
super(score, rankConditions);
|
||||||
|
|
@ -266,42 +289,42 @@ export class TotalPullRequestTrophy extends Trophy {
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SSS,
|
RANK.SSS,
|
||||||
"God PR User",
|
"God PR User",
|
||||||
(s) => s >= 1000,
|
1000,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SS,
|
RANK.SS,
|
||||||
"Deep PR User",
|
"Deep PR User",
|
||||||
(s) => s >= 500,
|
500,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.S,
|
RANK.S,
|
||||||
"Super PR User",
|
"Super PR User",
|
||||||
(s) => s >= 200,
|
200,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AAA,
|
RANK.AAA,
|
||||||
"Ultra PR User",
|
"Ultra PR User",
|
||||||
(s) => s >= 100,
|
100,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AA,
|
RANK.AA,
|
||||||
"Hyper PR User",
|
"Hyper PR User",
|
||||||
(s) => s >= 50,
|
50,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.A,
|
RANK.A,
|
||||||
"High PR User",
|
"High PR User",
|
||||||
(s) => s >= 20,
|
20,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.B,
|
RANK.B,
|
||||||
"Middle PR User",
|
"Middle PR User",
|
||||||
(s) => s >= 10,
|
10,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.C,
|
RANK.C,
|
||||||
"First PR",
|
"First PR",
|
||||||
(s) => s >= 1,
|
1,
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
super(score, rankConditions);
|
super(score, rankConditions);
|
||||||
|
|
@ -315,42 +338,42 @@ export class TotalRepositoryTrophy extends Trophy {
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SSS,
|
RANK.SSS,
|
||||||
"God Repo Creator",
|
"God Repo Creator",
|
||||||
(s) => s >= 200,
|
200,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.SS,
|
RANK.SS,
|
||||||
"Deep Repo Creator",
|
"Deep Repo Creator",
|
||||||
(s) => s >= 100,
|
100,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.S,
|
RANK.S,
|
||||||
"Super Repo Creator",
|
"Super Repo Creator",
|
||||||
(s) => s >= 80,
|
80,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AAA,
|
RANK.AAA,
|
||||||
"Ultra Repo Creator",
|
"Ultra Repo Creator",
|
||||||
(s) => s >= 50,
|
50,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.AA,
|
RANK.AA,
|
||||||
"Hyper Repo Creator",
|
"Hyper Repo Creator",
|
||||||
(s) => s >= 30,
|
30,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.A,
|
RANK.A,
|
||||||
"High Repo Creator",
|
"High Repo Creator",
|
||||||
(s) => s >= 20,
|
20,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.B,
|
RANK.B,
|
||||||
"Middle Repo Creator",
|
"Middle Repo Creator",
|
||||||
(s) => s >= 10,
|
10,
|
||||||
),
|
),
|
||||||
new RankCondition(
|
new RankCondition(
|
||||||
RANK.C,
|
RANK.C,
|
||||||
"First Repository",
|
"First Repository",
|
||||||
(s) => s >= 1,
|
1,
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
super(score, rankConditions);
|
super(score, rankConditions);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue