-
Notifications
You must be signed in to change notification settings - Fork 20
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
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6184c11
Tweak tsoa imports per todo comment
SpaceManiac 12fa90f
Add AchievementsController
SpaceManiac 8f4f478
Include time in getValidity
SpaceManiac b5bde8c
Log API errors to make debugging actually possible
SpaceManiac f95430f
Move AchievementService to utils-backend so it can be imported
SpaceManiac fdea349
Support games that export 'AchievementService' classes
SpaceManiac 334a103
Fix eslint errors
SpaceManiac ba52254
Interfaces too
SpaceManiac f22c8ed
Run pgtyped against a temporary Postgres instance
SpaceManiac a3f4301
Add achievement tables and queries
SpaceManiac 20cb2bc
Use new SQL queries in AchievementsController, remove AchievementService
SpaceManiac 369619a
Update for PRC-1 changes
SpaceManiac 4609d3e
Add table defs for new tables, clarify TODOs, fix issues
SpaceManiac ff0c267
Update paima-db README to describe Docker
SpaceManiac 7692a6c
Use getMainAddress in /wallet/X
SpaceManiac 1a0cf85
Add achievement_language table to back Accept-Language
SpaceManiac 6c3221d
TODO -> Future in AchievementsController
SpaceManiac 68103ad
Improve import.ts documentation
SpaceManiac 5fc34b0
Move checkedForPackedGameCode, rename importTsoa -> importEndpoints
SpaceManiac 7d001f4
Use import instead of DB for constant achievement definitions
SpaceManiac 03dfff7
Fix bugs and linter errors
SpaceManiac 4513c71
Fix table definition mismatch
SpaceManiac 0fa78af
Merge branch 'master' into patch/standard-achievements-api
SpaceManiac 21afec7
Add setAchievementProgress query
SpaceManiac b546a1c
Use wallet ID instead of address to handle delegation
SpaceManiac 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 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 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 |
---|---|---|
@@ -1,18 +1,30 @@ | ||
import type { GameStateMachine } from '@paima/sm'; | ||
import type { AchievementMetadata } from '@paima/utils-backend'; | ||
|
||
export class EngineService { | ||
public static INSTANCE = new EngineService(); | ||
// Useful stuff | ||
readonly stateMachine: GameStateMachine; | ||
readonly achievements: Promise<AchievementMetadata> | null; | ||
|
||
private runtime: GameStateMachine | undefined = undefined; | ||
constructor(alike: { | ||
readonly stateMachine: GameStateMachine; | ||
readonly achievements: Promise<AchievementMetadata> | null; | ||
}) { | ||
this.stateMachine = alike.stateMachine; | ||
this.achievements = alike.achievements; | ||
} | ||
|
||
getSM = (): GameStateMachine => { | ||
if (this.runtime == null) { | ||
throw new Error('EngineService: SM not initialized'); | ||
} | ||
return this.runtime; | ||
}; | ||
getSM = (): GameStateMachine => this.stateMachine; | ||
|
||
updateSM = (runtime: GameStateMachine): void => { | ||
this.runtime = runtime; | ||
}; | ||
// Singleton | ||
private static _instance?: EngineService; | ||
static get INSTANCE(): EngineService { | ||
if (!this._instance) { | ||
throw new Error('EngineService not initialized'); | ||
} | ||
return this._instance; | ||
} | ||
static set INSTANCE(value: EngineService) { | ||
this._instance = value; | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
packages/engine/paima-rest/src/controllers/AchievementsController.ts
This file contains 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,143 @@ | ||
import { getAchievementProgress, getMainAddress } from '@paima/db'; | ||
import { ENV } from '@paima/utils'; | ||
import { | ||
getNftOwner, | ||
type Achievement, | ||
type AchievementMetadata, | ||
type AchievementPublicList, | ||
type Player, | ||
type PlayerAchievements, | ||
type Validity, | ||
} from '@paima/utils-backend'; | ||
import { Controller, Get, Header, Path, Query, Route } from 'tsoa'; | ||
import { EngineService } from '../EngineService.js'; | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Controller and routes per PRC-1 | ||
|
||
function applyLanguage( | ||
achievement: Achievement, | ||
languages: AchievementMetadata['languages'], | ||
accept: string[] | ||
): Achievement { | ||
for (const acceptLang of accept) { | ||
const override = languages?.[acceptLang]?.[achievement.name]; | ||
if (override) { | ||
return { | ||
...achievement, | ||
displayName: override.displayName || achievement.displayName, | ||
description: override.description || achievement.description, | ||
}; | ||
} | ||
} | ||
return achievement; | ||
} | ||
|
||
@Route('achievements') | ||
export class AchievementsController extends Controller { | ||
private async meta(): Promise<AchievementMetadata> { | ||
const meta = EngineService.INSTANCE.achievements; | ||
if (!meta) { | ||
this.setStatus(501); | ||
throw new Error('Achievements are not supported by this game'); | ||
} | ||
return await meta; | ||
} | ||
|
||
private async validity(): Promise<Validity> { | ||
return { | ||
// Note: will need updating when we support non-EVM data availability layers. | ||
caip2: `eip155:${ENV.CHAIN_ID}`, | ||
SpaceManiac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
block: await EngineService.INSTANCE.getSM().latestProcessedBlockHeight(), | ||
time: new Date().toISOString(), | ||
}; | ||
} | ||
|
||
@Get('public/list') | ||
public async public_list( | ||
@Query() category?: string, | ||
@Query() isActive?: boolean, | ||
@Header('Accept-Language') acceptLanguage?: string | ||
): Promise<AchievementPublicList> { | ||
// Future expansion: import a real Accept-Language parser so user can | ||
// ask for more than one, handle 'pt-BR' also implying 'pt', etc. | ||
const acceptLanguages = acceptLanguage ? [acceptLanguage] : []; | ||
|
||
const meta = await this.meta(); | ||
const filtered = meta.list | ||
.filter(ach => category === undefined || category === ach.category) | ||
.filter(ach => isActive === undefined || isActive === ach.isActive); | ||
|
||
this.setHeader('Content-Language', acceptLanguages[0]); | ||
return { | ||
...(await this.validity()), | ||
...meta.game, | ||
achievements: meta.languages | ||
? filtered.map(ach => applyLanguage(ach, meta.languages, acceptLanguages)) | ||
: filtered, | ||
}; | ||
} | ||
|
||
@Get('wallet/{wallet}') | ||
public async wallet( | ||
@Path() wallet: string, | ||
/** Comma-separated list. */ | ||
@Query() name?: string | ||
): Promise<PlayerAchievements> { | ||
const db = EngineService.INSTANCE.getSM().getReadonlyDbConn(); | ||
const { address, id } = await getMainAddress(wallet, db); | ||
|
||
const player: Player = { | ||
wallet: address, | ||
userId: String(id), | ||
// walletType and userName aren't fulfilled here because Paima Engine's | ||
// own DB tables don't include them at the moment. | ||
}; | ||
|
||
const names = name ? name.split(',') : ['*']; | ||
const rows = await getAchievementProgress.run({ wallet: id, names }, db); | ||
|
||
return { | ||
...(await this.validity()), | ||
...player, | ||
completed: rows.reduce((n, row) => n + (row.completed_date ? 1 : 0), 0), | ||
achievements: rows.map(row => ({ | ||
name: row.name, | ||
completed: Boolean(row.completed_date), | ||
completedDate: row.completed_date ?? undefined, | ||
completedRate: row.total | ||
? { | ||
progress: row.progress ?? 0, | ||
total: row.total, | ||
} | ||
: undefined, | ||
})), | ||
}; | ||
} | ||
|
||
@Get('erc/{erc}/{cde}/{token_id}') | ||
public async nft( | ||
@Path() erc: string, | ||
@Path() cde: string, | ||
@Path() token_id: string, | ||
@Query() name?: string | ||
): Promise<PlayerAchievements> { | ||
const db = EngineService.INSTANCE.getSM().getReadonlyDbConn(); | ||
|
||
switch (erc) { | ||
case 'erc721': | ||
const wallet = await getNftOwner(db, cde, BigInt(token_id)); | ||
if (wallet) { | ||
return await this.wallet(wallet, name); | ||
} else { | ||
// Future improvement: throw a different error if no CDE with that name exists | ||
this.setStatus(404); | ||
throw new Error('No owner for that NFT'); | ||
} | ||
// Future improvement: erc6551 | ||
default: | ||
this.setStatus(404); | ||
throw new Error(`No support for /erc/${erc}`); | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
import { RegisterRoutes } from './tsoa/routes.js'; | ||
import * as basicControllerJson from './tsoa/swagger.json'; | ||
// replace the above import with the one below once this is merged and released: https://github.com/prettier/prettier/issues/15699 | ||
// import { default as basicControllerJson } from './tsoa/swagger.json' with { type: "json" }; | ||
import { default as basicControllerJson } from './tsoa/swagger.json' with { type: 'json' }; | ||
export default RegisterRoutes; | ||
export { basicControllerJson }; | ||
export { EngineService } from './EngineService.js'; |
This file contains 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
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.
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
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.
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?
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.
NX should be able to figure out dependencies based on the
references
inside the tsconfig IIRCThere 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.
Seems to not work. It doesn't infer that rest:prebuild depends on utils-backend:build