From 8876ff22faf89155c1520a67d45005a2e332bce6 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Thu, 27 Aug 2020 23:57:48 +0900 Subject: [PATCH] Support to next rank progress bar --- src/icons.ts | 62 ++++++++++++++++++----- src/trophies.ts | 127 ++++++++++++++++++++++++++++-------------------- 2 files changed, 126 insertions(+), 63 deletions(-) diff --git a/src/icons.ts b/src/icons.ts index a225339..1513826 100644 --- a/src/icons.ts +++ b/src/icons.ts @@ -30,27 +30,66 @@ Created by potrace 1.15, written by Peter Selinger 2001-2017 `; -function getSmallTrophyIcon(icon: string, color: string, count: number): string { +export const getNextRankBar = (title: string, color: string, percentage: number): string => { + const maxWidht = 80; + return ` + + + + `; +}; + +const getSmallTrophyIcon = ( + icon: string, + color: string, + count: number, +): string => { const leftXPosition = 7; const rightXPosition = 68; const getIcon = (x: number) => { return ` ${icon} - ` + `; }; if (count == 1) { // Double Rank return getIcon(rightXPosition); - } else if (count == 2){ + } else if (count == 2) { // Triple Rank - return `${getIcon(leftXPosition)}${getIcon(rightXPosition)}` + return `${getIcon(leftXPosition)}${getIcon(rightXPosition)}`; } // Single Rank - return '' - -} -export function getTrophyIcon(rank = RANK.UNKNOWN) { - + return ""; +}; +export const getTrophyIcon = (rank = RANK.UNKNOWN) => { let color = "currentColor"; let rankColor = "#333"; let backgroundIcon = ""; @@ -80,5 +119,6 @@ export function getTrophyIcon(rank = RANK.UNKNOWN) { ${optionRankIcon} ${icon} - `; -} + + `; +}; diff --git a/src/trophies.ts b/src/trophies.ts index d48b472..3a80ed4 100644 --- a/src/trophies.ts +++ b/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"; class RankCondition { constructor( readonly rank: RANK, readonly message: string, - readonly condition: (score: number) => boolean, + readonly requiredScore: number, ) {} } export class Trophy { + rankCondition: RankCondition | null = null; rank: RANK = RANK.UNKNOWN; topMessage = "Unknown"; bottomMessage = "0"; @@ -27,14 +28,35 @@ export class Trophy { ); // Set the rank that hit the first condition const rankCondition = sortedRankConditions.find((r) => - r.condition(this.score) + this.score >= r.requiredScore ); if (rankCondition != null) { this.rank = rankCondition.rank; + this.rankCondition = rankCondition; 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 { + const nextRankBar = getNextRankBar( + this.title, + "#0336A6", + this.calculateNextRankPercentage(), + ); return ` ${this.title} ${this.topMessage} - ${this.bottomMessage} + ${this.bottomMessage} + ${nextRankBar} `; } @@ -70,42 +93,42 @@ export class TotalStarTrophy extends Trophy { new RankCondition( RANK.SSS, "Super Stargazer", - (s) => s >= 2000, + 2000, ), new RankCondition( RANK.SS, "High Stargazer", - (s) => s >= 700, + 700, ), new RankCondition( RANK.S, "Stargazer", - (s) => s >= 200, + 200, ), new RankCondition( RANK.AAA, "Super Star", - (s) => s >= 100, + 100, ), new RankCondition( RANK.AA, "High Star", - (s) => s >= 50, + 50, ), new RankCondition( RANK.A, "You are Star", - (s) => s >= 30, + 30, ), new RankCondition( RANK.B, "Middle Star", - (s) => s >= 10, + 10, ), new RankCondition( RANK.C, "First Star", - (s) => s >= 1, + 1, ), ]; super(score, rankConditions); @@ -119,42 +142,42 @@ export class TotalCommitTrophy extends Trophy { new RankCondition( RANK.SSS, "God Committer", - (s) => s >= 4000, + 4000, ), new RankCondition( RANK.SS, "Deep Committer", - (s) => s >= 2000, + 2000, ), new RankCondition( RANK.S, "Super Committer", - (s) => s >= 1000, + 1000, ), new RankCondition( RANK.AAA, "Ultra Committer", - (s) => s >= 500, + 500, ), new RankCondition( RANK.AA, "Hyper Commiter", - (s) => s >= 200, + 200, ), new RankCondition( RANK.A, "High Committer", - (s) => s >= 100, + 100, ), new RankCondition( RANK.B, "Middle Committer", - (s) => s >= 10, + 10, ), new RankCondition( RANK.C, "First Commit", - (s) => s >= 1, + 1, ), ]; super(score, rankConditions); @@ -168,42 +191,42 @@ export class TotalFollowerTrophy extends Trophy { new RankCondition( RANK.SSS, "Super Celebrity", - (s) => s >= 1000, + 1000, ), new RankCondition( RANK.SS, "Ultra Celebrity", - (s) => s >= 400, + 400, ), new RankCondition( RANK.S, "Hyper Celebrity", - (s) => s >= 200, + 200, ), new RankCondition( RANK.AAA, "Famous User", - (s) => s >= 100, + 100, ), new RankCondition( RANK.AA, "Active User", - (s) => s >= 50, + 50, ), new RankCondition( RANK.A, "Dynamic User", - (s) => s >= 20, + 20, ), new RankCondition( RANK.B, "Many Frieds", - (s) => s >= 10, + 10, ), new RankCondition( RANK.C, "First Friend", - (s) => s >= 1, + 1, ), ]; super(score, rankConditions); @@ -217,42 +240,42 @@ export class TotalIssueTrophy extends Trophy { new RankCondition( RANK.SSS, "God Issuer", - (s) => s >= 1000, + 1000, ), new RankCondition( RANK.SS, "Deep Issuer", - (s) => s >= 500, + 500, ), new RankCondition( RANK.S, "Super Issuer", - (s) => s >= 200, + 200, ), new RankCondition( RANK.AAA, "Ultra Isuuer", - (s) => s >= 100, + 100, ), new RankCondition( RANK.AA, "Hyper Issuer", - (s) => s >= 50, + 50, ), new RankCondition( RANK.A, "High Issuer", - (s) => s >= 20, + 20, ), new RankCondition( RANK.B, "Middle Issuer", - (s) => s >= 10, + 10, ), new RankCondition( RANK.C, "First Issue", - (s) => s >= 1, + 1, ), ]; super(score, rankConditions); @@ -266,42 +289,42 @@ export class TotalPullRequestTrophy extends Trophy { new RankCondition( RANK.SSS, "God PR User", - (s) => s >= 1000, + 1000, ), new RankCondition( RANK.SS, "Deep PR User", - (s) => s >= 500, + 500, ), new RankCondition( RANK.S, "Super PR User", - (s) => s >= 200, + 200, ), new RankCondition( RANK.AAA, "Ultra PR User", - (s) => s >= 100, + 100, ), new RankCondition( RANK.AA, "Hyper PR User", - (s) => s >= 50, + 50, ), new RankCondition( RANK.A, "High PR User", - (s) => s >= 20, + 20, ), new RankCondition( RANK.B, "Middle PR User", - (s) => s >= 10, + 10, ), new RankCondition( RANK.C, "First PR", - (s) => s >= 1, + 1, ), ]; super(score, rankConditions); @@ -315,42 +338,42 @@ export class TotalRepositoryTrophy extends Trophy { new RankCondition( RANK.SSS, "God Repo Creator", - (s) => s >= 200, + 200, ), new RankCondition( RANK.SS, "Deep Repo Creator", - (s) => s >= 100, + 100, ), new RankCondition( RANK.S, "Super Repo Creator", - (s) => s >= 80, + 80, ), new RankCondition( RANK.AAA, "Ultra Repo Creator", - (s) => s >= 50, + 50, ), new RankCondition( RANK.AA, "Hyper Repo Creator", - (s) => s >= 30, + 30, ), new RankCondition( RANK.A, "High Repo Creator", - (s) => s >= 20, + 20, ), new RankCondition( RANK.B, "Middle Repo Creator", - (s) => s >= 10, + 10, ), new RankCondition( RANK.C, "First Repository", - (s) => s >= 1, + 1, ), ]; super(score, rankConditions);