Skip to content
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

✨ 🐛 Minor fixes and New parser for V1 #1000

Merged
merged 8 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/profile/src/components/trophies/achievement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ export function Achievement({
</div>
</div>
<Description description={description} />
<div className="flex flex-col gap-y-2">
{(!hidden || completed) &&
tasks.map((task) => (
{(!hidden || completed) && (
<div className="flex flex-col gap-y-2">
{tasks.map((task) => (
<Task key={task.id} task={task} completed={completed} />
))}
</div>
</div>
)}
</div>
{completed &&
!softview &&
Expand Down Expand Up @@ -255,7 +256,7 @@ function Progress({
</div>
) : total > 1 ? (
<div className="flex items-center gap-1">
<div className="fa-solid fa-check text-xs text-quaternary-foreground" />
<div className="text-xs text-quaternary-foreground" />
<p className="text-xs text-quaternary-foreground">
{`${count.toLocaleString()} of ${total.toLocaleString()}`}
</p>
Expand Down
6 changes: 3 additions & 3 deletions packages/profile/src/hooks/achievements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ export function useAchievements(accountAddress?: string) {
completed = completed && completion;
});
// Compute percentage of players who completed the achievement
const percentage = ((100 * stats[trophy.id]) / players.length).toFixed(
0,
);
const percentage = (
players.length ? (100 * stats[trophy.id]) / players.length : 0
).toFixed(0);
return {
...trophy,
completed,
Expand Down
65 changes: 65 additions & 0 deletions packages/profile/src/models/trophy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class Trophy {
hidden: boolean;
index: number;
earning: number;
start: number;
end: number;
group: string;
icon: string;
title: string;
Expand All @@ -24,6 +26,8 @@ export class Trophy {
hidden: boolean,
index: number,
earning: number,
start: number,
end: number,
group: string,
icon: string,
title: string,
Expand All @@ -35,6 +39,8 @@ export class Trophy {
this.hidden = hidden;
this.index = index;
this.earning = earning;
this.start = start;
this.end = end;
this.group = group;
this.icon = icon;
this.title = title;
Expand All @@ -48,6 +54,63 @@ export class Trophy {
}

static parse(node: EventNode): Trophy {
try {
return Trophy.parseV0(node);
} catch {
return Trophy.parseV1(node);
}
}

static parseV1(node: EventNode): Trophy {
const descriptionIndex = 11;
const descriptionLength = parseInt(node.data[descriptionIndex]);
const taskIndex = descriptionIndex + descriptionLength + 3;
// const tasksLength = parseInt(node.data[taskIndex]);
const taskDescriptionIndex = taskIndex + 3;
const taskDescriptionLength = parseInt(node.data[taskDescriptionIndex]);
return {
id: shortString.decodeShortString(node.data[1]),
hidden: !parseInt(node.data[3]) ? false : true,
index: parseInt(node.data[4]),
earning: parseInt(node.data[5]),
start: parseInt(node.data[6]),
end: parseInt(node.data[7]),
group: shortString.decodeShortString(node.data[8]),
icon: shortString.decodeShortString(node.data[9]),
title: shortString.decodeShortString(node.data[10]),
description: byteArray.stringFromByteArray({
data: descriptionLength
? node.data.slice(
descriptionIndex + 1,
descriptionIndex + 1 + descriptionLength,
)
: [],
pending_word: node.data[descriptionIndex + 1 + descriptionLength],
pending_word_len: node.data[descriptionIndex + 2 + descriptionLength],
}),
tasks: [
{
id: shortString.decodeShortString(node.data[taskIndex + 1]),
total: parseInt(node.data[taskIndex + 2]),
description: byteArray.stringFromByteArray({
data: taskDescriptionLength
? node.data.slice(
taskDescriptionIndex + 1,
taskDescriptionIndex + 1 + taskDescriptionLength,
)
: [],
pending_word:
node.data[taskDescriptionIndex + 1 + taskDescriptionLength],
pending_word_len:
node.data[taskDescriptionIndex + 2 + taskDescriptionLength],
}),
},
],
data: "",
};
}

static parseV0(node: EventNode): Trophy {
const descriptionIndex = 9;
const descriptionLength = parseInt(node.data[descriptionIndex]);
const taskIndex = descriptionIndex + descriptionLength + 3;
Expand All @@ -59,6 +122,8 @@ export class Trophy {
hidden: !parseInt(node.data[3]) ? false : true,
index: parseInt(node.data[4]),
earning: parseInt(node.data[5]),
start: 0,
end: 0,
group: shortString.decodeShortString(node.data[6]),
icon: shortString.decodeShortString(node.data[7]),
title: shortString.decodeShortString(node.data[8]),
Expand Down
Loading