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

Add scaffolding for PRC-1 achievements API #358

Merged
merged 25 commits into from
May 14, 2024

Conversation

SpaceManiac
Copy link
Contributor

@SpaceManiac SpaceManiac commented Apr 29, 2024

Approach:

  • paima-rest defines a PRC-1 compliant /achievements controller
  • For achievement definitions (types + languages), it reads the achievements export from endpoints.cjs
  • For achievement progress, it reads the new Paima-defined table achievement_progress
  • Games can upsert into achievement_progress as needed
    • paima-db exports the setAchievementProgress query for this purpose
  • In the worst case, games can fully override this controller by duplicating the relevant routes in their game, which will take priority (with a warning)

Benefits:

  • TypeScript types for PRC-1 are defined in utils-backend for easy import by games in a pinch
  • Exposed API is statically PRC-1 compliant, unless overridden

For future improvement:

  • Currently hardcoding eip155 in the chain ID, which won't suffice when we add data availability layer support
  • Looking up achievements by NFT owner could also support ERC-6551

"build": "tsc --build tsconfig.build.json",
"prebuild": "npm run compile:api",
"compile:api": "npx tsoa spec-and-routes"
"build": "npm run compile:api && tsc --build tsconfig.build.json",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to move this out of the prebuild?

I don't remember exactly why it was here, but I think it may have to be with the fact that putting it in the prebuild ensures that it gets run before the build step of any package in paima-engine (so any package can access the generated types). I don't think this is used in practice though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I ran into is that if this stays in prebuild, tsoa can't import the PRC-1 types from utils-backend on clean builds, or otherwise sees the old definitions, because they're produced on that package's build.

Maybe can somehow be improved by teaching NX to build utils-backend before prebuilding paima-rest?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NX should be able to figure out dependencies based on the references inside the tsconfig IIRC

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to not work. It doesn't infer that rest:prebuild depends on utils-backend:build

~/paima-engine (patch/standard-achievements-api|✚3) $ rm -r packages/node-sdk/paima-utils-backend/build/
~/paima-engine (patch/standard-achievements-api|✚3) $ npm run build

> @paima/[email protected] prebuild
> npx nx run-many --parallel=${NX_PARALLEL:-3} -t prebuild

 
    ✔  nx run @paima/utils:prebuild (2s)

    ✖  nx run @paima/rest:prebuild
       > @paima/[email protected] prebuild
       > npm run compile:api
       
       
       > @paima/[email protected] compile:api
       > npx tsoa spec-and-routes
       
       Generate routes error.
        GenerateMetadataError: No declarations found for referenced type AchievementPublicList.
           at TypeResolver.getModelTypeDeclarations (/home/paima/paima-engine/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:1125:19)
           at TypeResolver.calcRefTypeName (/home/paima/paima-engine/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:685:39)
           at TypeResolver.calcTypeReferenceTypeName (/home/paima/paima-engine/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:876:34)
           at TypeResolver.getReferenceType (/home/paima/paima-engine/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:886:27)
           at TypeResolver.resolve (/home/paima/paima-engine/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:513:36)
           at TypeResolver.resolve (/home/paima/paima-engine/node_modules/@tsoa/cli/dist/metadataGeneration/typeResolver.js:503:118)
           at MethodGenerator.Generate (/home/paima/paima-engine/node_modules/@tsoa/cli/dist/metadataGeneration/methodGenerator.js:62:78)
           at /home/paima/paima-engine/node_modules/@tsoa/cli/dist/metadataGeneration/controllerGenerator.js:46:41
           at Array.map (<anonymous>)
           at ControllerGenerator.buildMethods (/home/paima/paima-engine/node_modules/@tsoa/cli/dist/metadataGeneration/controllerGenerator.js:46:14)
       npm ERR! Lifecycle script `compile:api` failed with error: 
       npm ERR! Error: command failed 
       npm ERR!   in workspace: @paima/[email protected] 
       npm ERR!   at location: /home/paima/paima-engine/packages/engine/paima-rest 
       npm ERR! Lifecycle script `prebuild` failed with error: 
       npm ERR! Error: command failed 
       npm ERR!   in workspace: @paima/[email protected] 
       npm ERR!   at location: /home/paima/paima-engine/packages/engine/paima-rest 
       
       

 ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Ran target prebuild for 2 projects (4s)
 
    ✔    1/2 succeeded [0 read from cache]
 
    ✖    1/2 targets failed, including the following:
         - nx run @paima/rest:prebuild

packages/node-sdk/paima-utils-backend/src/achievements.ts Outdated Show resolved Hide resolved
packages/node-sdk/paima-utils-backend/src/achievements.ts Outdated Show resolved Hide resolved
packages/node-sdk/paima-db/docker-pgtyped.sh Show resolved Hide resolved
Comment on lines 248 to 249
display_name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we want to include these two in the db as-is despite the fact they're language-specific depending on the header field you use when querying the achievement endpoint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a table for language-agnostic info + another one for per-language info works for you, we can do that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an ideal world translations wouldn't be in the database at all, but if it has to be, probably it's best for it to be in a separate table

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible alternatives:

  1. Another export from packaged/endpoints.cjs
  2. Another .json file in packaged/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I find putting "code" (stuff that's defined by the programmer and not by user input) in the database kind of goofy, this is what I'm looking at now:

  • Game ID, achievement types, and languages are an additional packaged/endpoints.cjs export
    • But it's just a big data object, not a class with methods on it
  • Achievement progress is stored in a DB table

@SebastienGllmt SebastienGllmt merged commit 819e32e into master May 14, 2024
@SebastienGllmt SebastienGllmt deleted the patch/standard-achievements-api branch May 14, 2024 17:31
GGAlanSmithee pushed a commit to GGAlanSmithee/paima-engine that referenced this pull request May 16, 2024
* Tweak tsoa imports per todo comment

* Add AchievementsController

* Include time in getValidity

* Log API errors to make debugging actually possible

* Move AchievementService to utils-backend so it can be imported

* Support games that export 'AchievementService' classes

* Fix eslint errors

* Interfaces too

* Run pgtyped against a temporary Postgres instance

* Add achievement tables and queries

* Use new SQL queries in AchievementsController, remove AchievementService

* Update for PRC-1 changes

* Add table defs for new tables, clarify TODOs, fix issues

* Update paima-db README to describe Docker

* Use getMainAddress in /wallet/X

* Add achievement_language table to back Accept-Language

* TODO -> Future in AchievementsController

* Improve import.ts documentation

* Move checkedForPackedGameCode, rename importTsoa -> importEndpoints

* Use import instead of DB for constant achievement definitions

* Fix bugs and linter errors

* Fix table definition mismatch

* Add setAchievementProgress query

* Use wallet ID instead of address to handle delegation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants