-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Public Achivements UI #3530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aotumuri
wants to merge
8
commits into
main
Choose a base branch
from
public-achivements-ui
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a5c2d4f
test
Aotumuri 3c4d1f9
fix
Aotumuri a7a5dac
Merge upstream/main into public-achivements-ui
Aotumuri a65de08
test
Aotumuri 5c30831
test2
Aotumuri bbe4a74
fix for test
Aotumuri f6bc47b
Add achievements UI to player profile
Aotumuri ebccc7c
Merge branch 'main' into public-achivements-ui
Aotumuri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "win_no_nukes": { | ||
| "difficulty": "Hard" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
246 changes: 246 additions & 0 deletions
246
src/client/components/baseComponents/stats/PlayerAchievements.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| import { LitElement, html } from "lit"; | ||
| import { customElement, property } from "lit/decorators.js"; | ||
| import playerAchievementMetadataJson from "../../../../../resources/playerAchievementMetadata.json" with { type: "json" }; | ||
| import type { | ||
| AchievementsResponse, | ||
| PlayerAchievementJson, | ||
| } from "../../../../core/ApiSchemas"; | ||
| import { assetUrl } from "../../../../core/AssetUrls"; | ||
| import type { Difficulty } from "../../../../core/game/Game"; | ||
| import { translateText } from "../../../Utils"; | ||
|
|
||
| type PlayerAchievementMetadata = { | ||
| difficulty: Difficulty; | ||
| }; | ||
|
|
||
| type PlayerAchievementCard = { | ||
| achievement: string; | ||
| achievedAt: string | null; | ||
| isUnlocked: boolean; | ||
| }; | ||
|
|
||
| const playerAchievementMetadata = playerAchievementMetadataJson as Record< | ||
| string, | ||
| PlayerAchievementMetadata | ||
| >; | ||
|
|
||
| @customElement("player-achievements") | ||
| export class PlayerAchievements extends LitElement { | ||
| createRenderRoot() { | ||
| return this; | ||
| } | ||
|
|
||
| @property({ attribute: false }) achievementGroups: AchievementsResponse = []; | ||
|
|
||
| private get unlockedAchievements(): PlayerAchievementJson[] { | ||
| return this.achievementGroups | ||
| .flatMap((group) => (group.type === "player" ? group.data : [])) | ||
| .slice() | ||
| .sort( | ||
| (a, b) => | ||
| new Date(b.achievedAt).getTime() - new Date(a.achievedAt).getTime(), | ||
| ); | ||
| } | ||
|
|
||
| private get achievements(): PlayerAchievementCard[] { | ||
| const unlockedByKey = new Map( | ||
| this.unlockedAchievements.map((achievement) => [ | ||
| achievement.achievement, | ||
| achievement, | ||
| ]), | ||
| ); | ||
| const knownKeys = Object.keys(playerAchievementMetadata); | ||
| const achievementKeys = [ | ||
| ...knownKeys, | ||
| ...this.unlockedAchievements | ||
| .map((achievement) => achievement.achievement) | ||
| .filter((achievement) => !knownKeys.includes(achievement)), | ||
| ]; | ||
| const originalOrder = new Map( | ||
| achievementKeys.map((achievement, index) => [achievement, index]), | ||
| ); | ||
|
|
||
| return achievementKeys | ||
| .map((achievement) => { | ||
| const unlockedAchievement = unlockedByKey.get(achievement); | ||
| return { | ||
| achievement, | ||
| achievedAt: unlockedAchievement?.achievedAt ?? null, | ||
| isUnlocked: unlockedAchievement !== undefined, | ||
| }; | ||
| }) | ||
| .sort((a, b) => { | ||
| if (a.isUnlocked !== b.isUnlocked) { | ||
| return Number(b.isUnlocked) - Number(a.isUnlocked); | ||
| } | ||
| if (a.achievedAt && b.achievedAt) { | ||
| return ( | ||
| new Date(b.achievedAt).getTime() - new Date(a.achievedAt).getTime() | ||
| ); | ||
| } | ||
| return ( | ||
| (originalOrder.get(a.achievement) ?? 0) - | ||
| (originalOrder.get(b.achievement) ?? 0) | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| private formatDate(achievedAt: string): string { | ||
| const date = new Date(achievedAt); | ||
| if (Number.isNaN(date.getTime())) { | ||
| return achievedAt; | ||
| } | ||
| return new Intl.DateTimeFormat(undefined, { | ||
| dateStyle: "medium", | ||
| }).format(date); | ||
| } | ||
|
|
||
| private resolveTitle(achievementKey: string): string { | ||
| const translationKey = `achievements.${achievementKey}`; | ||
| const translated = translateText(translationKey); | ||
| return translated === translationKey ? achievementKey : translated; | ||
| } | ||
|
|
||
| private resolveDescription(achievementKey: string): string | null { | ||
| const translationKey = `achievements.${achievementKey}_desc`; | ||
| const translated = translateText(translationKey); | ||
| return translated === translationKey ? null : translated; | ||
| } | ||
|
|
||
| private resolveDifficulty(achievementKey: string): Difficulty | null { | ||
| return playerAchievementMetadata[achievementKey]?.difficulty ?? null; | ||
| } | ||
|
|
||
| private difficultyClasses(difficulty: Difficulty): string { | ||
| switch (difficulty) { | ||
| case "Easy": | ||
| return "bg-emerald-500/15 text-emerald-300 border-emerald-400/25"; | ||
| case "Medium": | ||
| return "bg-amber-500/15 text-amber-200 border-amber-400/25"; | ||
| case "Hard": | ||
| return "bg-rose-500/15 text-rose-200 border-rose-400/25"; | ||
| case "Impossible": | ||
| return "bg-violet-500/15 text-violet-200 border-violet-400/25"; | ||
| default: | ||
| return "bg-white/5 text-white/60 border-white/10"; | ||
| } | ||
| } | ||
|
|
||
| private renderAchievementIcon(isUnlocked: boolean) { | ||
| const mask = `url('${assetUrl("images/MedalIconWhite.svg")}') no-repeat center / contain`; | ||
| const iconClasses = isUnlocked | ||
| ? "bg-yellow-300 opacity-100" | ||
| : "bg-white/40 opacity-35"; | ||
|
|
||
| return html` | ||
| <div | ||
| class="mt-0.5 h-7 w-7 shrink-0 ${iconClasses}" | ||
| style="mask: ${mask}; -webkit-mask: ${mask};" | ||
| ></div> | ||
| `; | ||
| } | ||
|
|
||
| private renderDifficultyBadge(difficulty: Difficulty | null) { | ||
| if (!difficulty) { | ||
| return html` | ||
| <span | ||
| class="inline-flex items-center rounded-full border border-white/10 bg-white/5 px-3 py-1 text-xs font-semibold uppercase tracking-wider text-white/50" | ||
| > | ||
| ${translateText("account_modal.unknown_difficulty")} | ||
| </span> | ||
| `; | ||
| } | ||
|
|
||
| const translationKey = `difficulty.${difficulty.toLowerCase()}`; | ||
| const translated = translateText(translationKey); | ||
| const label = translated === translationKey ? difficulty : translated; | ||
|
|
||
| return html` | ||
| <span | ||
| class="inline-flex items-center rounded-full border px-3 py-1 text-xs font-semibold uppercase tracking-wider ${this.difficultyClasses( | ||
| difficulty, | ||
| )}" | ||
| > | ||
| ${label} | ||
| </span> | ||
| `; | ||
| } | ||
|
|
||
| private renderAchievementCard(achievement: PlayerAchievementCard) { | ||
| const difficulty = this.resolveDifficulty(achievement.achievement); | ||
| const description = this.resolveDescription(achievement.achievement); | ||
| const cardClasses = achievement.isUnlocked | ||
| ? "border-yellow-400/25 bg-gradient-to-br from-yellow-500/10 via-slate-900/55 to-black/20 shadow-yellow-950/20" | ||
| : "border-white/6 bg-gradient-to-br from-slate-900/40 via-slate-900/20 to-black/10 opacity-80"; | ||
|
|
||
| return html` | ||
| <article | ||
| class="flex h-full flex-col rounded-2xl border p-5 shadow-lg shadow-black/20 ${cardClasses}" | ||
| > | ||
| <div class="flex items-start justify-between gap-4"> | ||
| <div class="flex min-w-0 items-start gap-3"> | ||
| ${this.renderAchievementIcon(achievement.isUnlocked)} | ||
| <h4 class="text-lg font-semibold text-white"> | ||
| ${this.resolveTitle(achievement.achievement)} | ||
| </h4> | ||
| </div> | ||
| ${this.renderDifficultyBadge(difficulty)} | ||
| </div> | ||
|
|
||
| ${description | ||
| ? html` | ||
| <p class="mt-2 text-sm leading-6 text-white/60">${description}</p> | ||
| ` | ||
| : null} | ||
|
|
||
| <div class="mt-auto pt-5"> | ||
| <div class="rounded-xl border border-white/10 bg-black/20 p-4"> | ||
| <div | ||
| class="text-[11px] font-bold uppercase tracking-[0.24em] text-white/35" | ||
| > | ||
| ${achievement.isUnlocked | ||
| ? translateText("account_modal.achieved_on") | ||
| : translateText("account_modal.status")} | ||
| </div> | ||
| ${achievement.isUnlocked && achievement.achievedAt | ||
| ? html` | ||
| <time | ||
| class="mt-2 block text-sm font-medium text-white/80" | ||
| datetime=${achievement.achievedAt} | ||
| > | ||
| ${this.formatDate(achievement.achievedAt)} | ||
| </time> | ||
| ` | ||
| : html` | ||
| <div class="mt-2 text-sm font-medium text-white/50"> | ||
| ${translateText("account_modal.not_unlocked_yet")} | ||
| </div> | ||
| `} | ||
| </div> | ||
| </div> | ||
| </article> | ||
| `; | ||
| } | ||
|
|
||
| render() { | ||
| if (this.achievements.length === 0) { | ||
| return html` | ||
| <div | ||
| class="rounded-2xl border border-dashed border-white/10 bg-black/10 px-5 py-6 text-sm text-white/45" | ||
| > | ||
| ${translateText("account_modal.no_achievements")} | ||
| </div> | ||
| `; | ||
| } | ||
|
|
||
| return html` | ||
| <div class="max-h-[36rem] overflow-y-auto pr-1 custom-scrollbar"> | ||
| <div class="grid grid-cols-1 gap-4 xl:grid-cols-2"> | ||
| ${this.achievements.map((achievement) => | ||
| this.renderAchievementCard(achievement), | ||
| )} | ||
| </div> | ||
| </div> | ||
| `; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.