-
Notifications
You must be signed in to change notification settings - Fork 952
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
Draft
Aotumuri
wants to merge
4
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.
+347
−3
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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,8 @@ | ||
| { | ||
| "test": { | ||
| "difficulty": "Medium" | ||
| }, | ||
| "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
252 changes: 252 additions & 0 deletions
252
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,252 @@ | ||
| 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 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 | ||
| >; | ||
| const MOCK_UNLOCKED_TEST_ACHIEVEMENT = { | ||
| playerId: "0", | ||
| achievement: "test", | ||
| achievedAt: "2025-01-01T00:00:00.000Z", | ||
| gameId: "0", | ||
| game: "ui-test", | ||
| } satisfies PlayerAchievementJson; | ||
|
|
||
| @customElement("player-achievements") | ||
| export class PlayerAchievements extends LitElement { | ||
| createRenderRoot() { | ||
| return this; | ||
| } | ||
|
|
||
| @property({ attribute: false }) achievementGroups: AchievementsResponse = []; | ||
|
|
||
| private get unlockedAchievements(): PlayerAchievementJson[] { | ||
| const unlockedAchievements = this.achievementGroups | ||
| .flatMap((group) => (group.type === "player" ? group.data : [])) | ||
| .slice(); | ||
|
|
||
| if ( | ||
| !unlockedAchievements.some( | ||
| (achievement) => | ||
| achievement.achievement === | ||
| MOCK_UNLOCKED_TEST_ACHIEVEMENT.achievement, | ||
| ) | ||
| ) { | ||
| unlockedAchievements.push(MOCK_UNLOCKED_TEST_ACHIEVEMENT); | ||
| } | ||
|
|
||
| return unlockedAchievements.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 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-white/10 bg-gradient-to-br from-slate-900/70 via-slate-900/40 to-black/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="rounded-2xl border p-5 shadow-lg shadow-black/20 ${cardClasses}" | ||
| > | ||
| <div class="flex items-start justify-between gap-4"> | ||
| <div class="min-w-0"> | ||
| <div | ||
| class="text-[11px] font-bold uppercase tracking-[0.24em] text-white/35" | ||
| > | ||
| ${translateText("account_modal.achievement_label")} | ||
| </div> | ||
| <h4 class="mt-2 text-lg font-semibold text-white"> | ||
| ${this.resolveTitle(achievement.achievement)} | ||
| </h4> | ||
| ${description | ||
| ? html` | ||
| <p class="mt-2 text-sm leading-6 text-white/60"> | ||
| ${description} | ||
| </p> | ||
| ` | ||
| : null} | ||
| </div> | ||
| ${this.renderDifficultyBadge(difficulty)} | ||
| </div> | ||
|
|
||
| <div class="mt-5 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> | ||
| </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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the typo:
achivementsshould beachievements.This mirrors the typo in
en.json. Both should be fixed together.✏️ Suggested fix
private resolveTitle(achievementKey: string): string { - const translationKey = `achivements.${achievementKey}`; + const translationKey = `achievements.${achievementKey}`; const translated = translateText(translationKey); return translated === translationKey ? achievementKey : translated; } private resolveDescription(achievementKey: string): string | null { - const translationKey = `achivements.${achievementKey}_desc`; + const translationKey = `achievements.${achievementKey}_desc`; const translated = translateText(translationKey); return translated === translationKey ? null : translated; }🤖 Prompt for AI Agents