-
Notifications
You must be signed in to change notification settings - Fork 12
feat(client): migrate achievements from GraphQL to gRPC #258
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
MartianGreed
wants to merge
1
commit into
main
Choose a base branch
from
feat/achievements-grpc
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
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,116 @@ | ||
| import { Atom } from "@effect-atom/atom-react"; | ||
| import { Effect } from "effect"; | ||
| import { ToriiGrpcClient, toriiRuntime } from "../runtime"; | ||
| import { Progress } from "@/models"; | ||
| import { mapResult } from "../utils/result"; | ||
| import type { Progressions } from "@/lib/achievements"; | ||
| import type { PlayerAchievementsPage } from "@dojoengine/grpc"; | ||
|
|
||
| export type ProgressionProject = { | ||
| project: string; | ||
| namespace: string; | ||
| }; | ||
|
|
||
| export type ProgressionItem = { | ||
| project: string; | ||
| progressions: { [key: string]: Progress }; | ||
| }; | ||
|
|
||
| const fetchProgressionsEffect = (projects: ProgressionProject[]) => | ||
| Effect.gen(function* () { | ||
| if (projects.length === 0) return []; | ||
|
|
||
| const { client } = yield* ToriiGrpcClient; | ||
|
|
||
| const namespaces = projects.map((p) => p.namespace); | ||
|
|
||
| const response: PlayerAchievementsPage = yield* Effect.tryPromise(() => | ||
| client.getPlayerAchievements({ | ||
| world_addresses: [], | ||
| namespaces, | ||
| player_addresses: [], | ||
| pagination: { | ||
| limit: 10000, | ||
| cursor: undefined, | ||
| direction: "Forward", | ||
| order_by: [], | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| const progressionsByProject: { | ||
| [project: string]: { [key: string]: Progress }; | ||
| } = {}; | ||
|
|
||
| for (const project of projects) { | ||
| progressionsByProject[project.project] = {}; | ||
| } | ||
|
|
||
| for (const playerEntry of response.items) { | ||
| const playerAddress = playerEntry.player_address; | ||
|
|
||
| for (const achievementProgress of playerEntry.achievements) { | ||
| const achievement = achievementProgress.achievement; | ||
| if (!achievement) continue; | ||
|
|
||
| const project = projects.find( | ||
| (p) => p.namespace === achievement.namespace, | ||
| ); | ||
| if (!project) continue; | ||
|
|
||
| for (const taskProgress of achievementProgress.task_progress) { | ||
| const progress = Progress.fromGrpc( | ||
| playerAddress, | ||
| achievement, | ||
| taskProgress, | ||
| playerEntry.stats?.last_achievement_at, | ||
| ); | ||
| progressionsByProject[project.project][progress.key] = progress; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const result: ProgressionItem[] = projects.map((p) => ({ | ||
| project: p.project, | ||
| progressions: progressionsByProject[p.project], | ||
| })); | ||
|
|
||
| return result; | ||
| }); | ||
|
|
||
| const progressionsFamily = Atom.family((key: string) => { | ||
| const projects: ProgressionProject[] = JSON.parse(key); | ||
| return toriiRuntime | ||
| .atom(fetchProgressionsEffect(projects)) | ||
| .pipe(Atom.keepAlive); | ||
| }); | ||
|
|
||
| export const progressionsAtom = (projects: ProgressionProject[]) => { | ||
| const sorted = [...projects].sort((a, b) => | ||
| a.project.localeCompare(b.project), | ||
| ); | ||
| return progressionsFamily(JSON.stringify(sorted)); | ||
| }; | ||
|
|
||
| const progressionsDataFamily = Atom.family((key: string) => { | ||
| const baseAtom = progressionsFamily(key); | ||
| return baseAtom.pipe( | ||
| Atom.map((result) => | ||
| mapResult(result, (items) => | ||
| items.reduce((acc, item) => { | ||
| acc[item.project] = item.progressions; | ||
| return acc; | ||
| }, {} as Progressions), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| export const progressionsDataAtom = (projects: ProgressionProject[]) => { | ||
| const sorted = [...projects].sort((a, b) => | ||
| a.project.localeCompare(b.project), | ||
| ); | ||
| return progressionsDataFamily(JSON.stringify(sorted)); | ||
| }; | ||
|
|
||
| export type { Progress }; | ||
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,97 @@ | ||
| import { Atom } from "@effect-atom/atom-react"; | ||
| import { Effect } from "effect"; | ||
| import { ToriiGrpcClient, toriiRuntime } from "../runtime"; | ||
| import { Trophy } from "@/models"; | ||
| import { mapResult } from "../utils/result"; | ||
| import type { Trophies } from "@/lib/achievements"; | ||
| import type { AchievementsPage } from "@dojoengine/grpc"; | ||
|
|
||
| export type TrophyProject = { | ||
| project: string; | ||
| namespace: string; | ||
| }; | ||
|
|
||
| export type TrophyItem = { | ||
| project: string; | ||
| trophies: { [id: string]: Trophy }; | ||
| }; | ||
|
|
||
| const fetchTrophiesEffect = (projects: TrophyProject[]) => | ||
| Effect.gen(function* () { | ||
| if (projects.length === 0) return []; | ||
|
|
||
| const { client } = yield* ToriiGrpcClient; | ||
|
|
||
| const namespaces = projects.map((p) => p.namespace); | ||
|
|
||
| const response: AchievementsPage = yield* Effect.tryPromise(() => | ||
| client.getAchievements({ | ||
| world_addresses: [], | ||
| namespaces, | ||
| hidden: undefined, | ||
| pagination: { | ||
| limit: 1000, | ||
| cursor: undefined, | ||
| direction: "Forward", | ||
| order_by: [], | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| const trophiesByProject: { [project: string]: { [id: string]: Trophy } } = | ||
| {}; | ||
|
|
||
| for (const project of projects) { | ||
| trophiesByProject[project.project] = {}; | ||
| } | ||
|
|
||
| for (const item of response.items) { | ||
| const project = projects.find((p) => p.namespace === item.namespace); | ||
| if (!project) continue; | ||
|
|
||
| const trophy = Trophy.fromGrpc(item); | ||
| trophiesByProject[project.project][trophy.id] = trophy; | ||
| } | ||
|
|
||
| const result: TrophyItem[] = projects.map((p) => ({ | ||
| project: p.project, | ||
| trophies: trophiesByProject[p.project], | ||
| })); | ||
|
|
||
| return result; | ||
| }); | ||
|
|
||
| const trophiesFamily = Atom.family((key: string) => { | ||
| const projects: TrophyProject[] = JSON.parse(key); | ||
| return toriiRuntime.atom(fetchTrophiesEffect(projects)).pipe(Atom.keepAlive); | ||
| }); | ||
|
|
||
| export const trophiesAtom = (projects: TrophyProject[]) => { | ||
| const sorted = [...projects].sort((a, b) => | ||
| a.project.localeCompare(b.project), | ||
| ); | ||
| return trophiesFamily(JSON.stringify(sorted)); | ||
| }; | ||
|
|
||
| const trophiesDataFamily = Atom.family((key: string) => { | ||
| const baseAtom = trophiesFamily(key); | ||
| return baseAtom.pipe( | ||
| Atom.map((result) => | ||
| mapResult(result, (items) => | ||
| items.reduce((acc, item) => { | ||
| acc[item.project] = item.trophies; | ||
| return acc; | ||
| }, {} as Trophies), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| export const trophiesDataAtom = (projects: TrophyProject[]) => { | ||
| const sorted = [...projects].sort((a, b) => | ||
| a.project.localeCompare(b.project), | ||
| ); | ||
| return trophiesDataFamily(JSON.stringify(sorted)); | ||
| }; | ||
|
|
||
| export type { Trophy }; |
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
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
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.
All player achievements share the same timestamp
High Severity
The
Progress.fromGrpccall passesplayerEntry.stats?.last_achievement_atas the timestamp for all task progress entries of all achievements belonging to a player. This single player-level timestamp replaces what was previously per-achievementcompletionTimein the GraphQL implementation. As a result, all achievements for a player will appear to have been completed at the same time, breaking leaderboard calculations that rely on completion order and making achievement event timelines inaccurate. The code likely needs to use a per-achievement completion timestamp (possibly fromachievementProgress) rather than the player's overalllast_achievement_at.Additional Locations (1)
client/src/models/progress.ts#L85-L86