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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6184c11
Tweak tsoa imports per todo comment
SpaceManiac Apr 26, 2024
12fa90f
Add AchievementsController
SpaceManiac Apr 26, 2024
8f4f478
Include time in getValidity
SpaceManiac Apr 26, 2024
b5bde8c
Log API errors to make debugging actually possible
SpaceManiac Apr 29, 2024
f95430f
Move AchievementService to utils-backend so it can be imported
SpaceManiac Apr 29, 2024
fdea349
Support games that export 'AchievementService' classes
SpaceManiac Apr 29, 2024
334a103
Fix eslint errors
SpaceManiac Apr 29, 2024
ba52254
Interfaces too
SpaceManiac Apr 29, 2024
f22c8ed
Run pgtyped against a temporary Postgres instance
SpaceManiac Apr 30, 2024
a3f4301
Add achievement tables and queries
SpaceManiac Apr 30, 2024
20cb2bc
Use new SQL queries in AchievementsController, remove AchievementService
SpaceManiac Apr 30, 2024
369619a
Update for PRC-1 changes
SpaceManiac Apr 30, 2024
4609d3e
Add table defs for new tables, clarify TODOs, fix issues
SpaceManiac May 1, 2024
ff0c267
Update paima-db README to describe Docker
SpaceManiac May 1, 2024
7692a6c
Use getMainAddress in /wallet/X
SpaceManiac May 1, 2024
1a0cf85
Add achievement_language table to back Accept-Language
SpaceManiac May 1, 2024
6c3221d
TODO -> Future in AchievementsController
SpaceManiac May 2, 2024
68103ad
Improve import.ts documentation
SpaceManiac May 2, 2024
5fc34b0
Move checkedForPackedGameCode, rename importTsoa -> importEndpoints
SpaceManiac May 2, 2024
7d001f4
Use import instead of DB for constant achievement definitions
SpaceManiac May 2, 2024
03dfff7
Fix bugs and linter errors
SpaceManiac May 2, 2024
4513c71
Fix table definition mismatch
SpaceManiac May 3, 2024
0fa78af
Merge branch 'master' into patch/standard-achievements-api
SpaceManiac May 3, 2024
21afec7
Add setAchievementProgress query
SpaceManiac May 3, 2024
b546a1c
Use wallet ID instead of address to handle delegation
SpaceManiac May 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ import {
type Player,
getNftOwner,
} from '@paima/utils-backend';
import { getAchievementTypes, getAchievementProgress } from '@paima/db';
import { getAchievementTypes, getAchievementProgress, getMainAddress } from '@paima/db';

// ----------------------------------------------------------------------------
// Controller and routes per PRC-1

@Route('achievements')
export class AchievementsController extends Controller {
private async game(): Promise<Game> {
return { id: 'TODO' };
return {
id: 'TODO',
// TODO: name, version
};
}

private async validity(): Promise<Validity> {
return {
caip2: ENV.CHAIN_ID,
caip2: `eip155:${ENV.CHAIN_ID}`,
block: await EngineService.INSTANCE.getSM().latestProcessedBlockHeight(),
time: new Date().toISOString(),
};
Expand Down Expand Up @@ -57,10 +60,16 @@ export class AchievementsController extends Controller {
/** Comma-separated list. */
@Query() name?: string
): Promise<PlayerAchievements> {
const names = name ? name.split(',') : [];
const player: Player = { wallet };

const db = EngineService.INSTANCE.getSM().getReadonlyDbConn();
const { address, id } = await getMainAddress(wallet, db);

const player: Player = {
wallet: address,
userId: String(id),
// TODO: walletType, userName
};

const names = name ? name.split(',') : ['*'];
const rows = await getAchievementProgress.run({ wallet, names }, db);

this.setHeader('Content-Language', 'en');
Expand Down Expand Up @@ -97,14 +106,15 @@ export class AchievementsController extends Controller {
const wallet = await getNftOwner(db, cde, BigInt(token_id));
if (wallet) {
return await this.wallet(wallet, name);
} else {
// TODO: throw a different error if no CDE with that name exists
this.setStatus(404);
throw new Error('No owner for that NFT');
}
break;
case 'erc6551':
// TODO
break;
// Future expansion: erc6551
default:
this.setStatus(404);
throw new Error(`No support for /erc/${erc}`);
}

this.setStatus(404);
throw new Error('Not found');
}
}
4 changes: 2 additions & 2 deletions packages/engine/paima-rest/src/tsoa/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const models: TsoaRoute.Models = {
"name": {"dataType":"string"},
"version": {"dataType":"string"},
"block": {"dataType":"double","required":true},
"caip2": {"dataType":"double","required":true},
"caip2": {"dataType":"string","required":true},
"time": {"dataType":"string"},
"achievements": {"dataType":"array","array":{"dataType":"refObject","ref":"Achievement"},"required":true},
},
Expand All @@ -139,7 +139,7 @@ const models: TsoaRoute.Models = {
"dataType": "refObject",
"properties": {
"block": {"dataType":"double","required":true},
"caip2": {"dataType":"double","required":true},
"caip2": {"dataType":"string","required":true},
"time": {"dataType":"string"},
"wallet": {"dataType":"string","required":true},
"walletType": {"dataType":"string"},
Expand Down
6 changes: 2 additions & 4 deletions packages/engine/paima-rest/src/tsoa/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@
"description": "Data block height (0 always valid)"
},
"caip2": {
"type": "number",
"format": "double",
"type": "string",
"description": "CAIP-2 blockchain identifier"
},
"time": {
Expand Down Expand Up @@ -299,8 +298,7 @@
"description": "Data block height (0 always valid)"
},
"caip2": {
"type": "number",
"format": "double",
"type": "string",
"description": "CAIP-2 blockchain identifier"
},
"time": {
Expand Down
4 changes: 1 addition & 3 deletions packages/node-sdk/paima-db/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ Specific game databases do not need to contain these tables, as they will be cre

## Development

To re-generate the queries, you will need a database running with the same connection info as [the config](./pgtypedconfig.json) and then run `npm run compile`.

Before running `npm run compile`, you will need to initialize the DB using `sudo -u postgres psql -d postgres -a -f migrations/up.sql`
To re-generate the queries, run `npm run compile`, which will use Docker to run a temporary Postgres database initialized with `migrations/up.sql` and then watch for query changes. Ctrl+C and restart to reflect changes to `up.sql`.
2 changes: 1 addition & 1 deletion packages/node-sdk/paima-db/migrations/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,4 @@ CREATE TABLE achievement_progress(
progress INTEGER,
total INTEGER,
PRIMARY KEY (wallet, name)
);--
);
3 changes: 2 additions & 1 deletion packages/node-sdk/paima-db/src/delegate-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getMainAddressFromAddress,
} from './sql/wallet-delegation.queries.js';
import type { PoolClient, Notification, Client } from 'pg';
import type { IDatabaseConnection } from '@pgtyped/runtime/lib/tag';

export type WalletDelegate = { address: string; id: number };
export const NO_USER_ID = -1;
Expand All @@ -26,7 +27,7 @@ export const addressCache = new Map<string, WalletDelegate>();
// If wallet does not exist, It will NOT be created in address table.
export async function getMainAddress(
_address: string,
DBConn: PoolClient
DBConn: IDatabaseConnection
): Promise<WalletDelegate> {
const address = _address.toLocaleLowerCase();
let addressMapping: WalletDelegate | undefined = addressCache.get(address);
Expand Down
2 changes: 1 addition & 1 deletion packages/node-sdk/paima-db/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DataMigrations } from './data-migrations.js';

export * from './delegate-wallet.js';

// resolve ambiguity because pgtyped generates multiple Json types
// https://github.com/adelsz/pgtyped/issues/565
export type { Json } from './sql/cde-generic.queries.js';

export * from './sql/achievements.queries.js';
Expand Down
51 changes: 51 additions & 0 deletions packages/node-sdk/paima-db/src/paima-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,55 @@ const TABLE_DATA_DELEGATIONS: TableData = {
creationQuery: QUERY_CREATE_TABLE_DELEGATIONS,
};

const QUERY_CREATE_TABLE_ACHIEVEMENT_TYPE = `
CREATE TABLE achievement_type(
name TEXT NOT NULL PRIMARY KEY,
is_active BOOLEAN NOT NULL DEFAULT true,
display_name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
metadata JSONB NOT NULL DEFAULT '{}'
);
`;

const TABLE_DATA_ACHIEVEMENT_TYPE: TableData = {
tableName: 'achievement_type',
primaryKeyColumns: ['name'],
columnData: packTuples([
['name', 'text', 'NO', ''],
['is_active', 'boolean', 'NO', 'true'],
['display_name', 'text', 'NO', ''],
['description', 'text', 'NO', "''"],
['metadata', 'jsonb', 'NO', "'{}'"],
]),
serialColumns: [],
creationQuery: QUERY_CREATE_TABLE_ACHIEVEMENT_TYPE,
};

const QUERY_CREATE_TABLE_ACHIEVEMENT_PROGRESS = `
CREATE TABLE achievement_progress(
wallet TEXT NOT NULL,
name TEXT NOT NULL,
completed_date TIMESTAMP,
progress INTEGER,
total INTEGER,
PRIMARY KEY (wallet, name)
);
`;

const TABLE_DATA_ACHIEVEMENT_PROGRESS: TableData = {
tableName: 'achievement_progress',
primaryKeyColumns: ['wallet', 'name'],
columnData: packTuples([
['wallet', 'text', 'NO', ''],
['name', 'text', 'NO', ''],
['completed_date', 'timestamp', 'YES', ''],
['progress', 'integer', 'YES', ''],
['total', 'integer', 'YES', ''],
]),
serialColumns: [],
creationQuery: QUERY_CREATE_TABLE_ACHIEVEMENT_PROGRESS,
};

const FUNCTION_NOTIFY_WALLET_CONNECT: string = `
create or replace function public.notify_wallet_connect()
returns trigger
Expand Down Expand Up @@ -663,4 +712,6 @@ export const TABLES: TableData[] = [
TABLE_DATA_CDE_TRACKING_CARDANO_PAGINATION,
TABLE_DATA_CDE_CARDANO_TRANSFER,
TABLE_DATA_CDE_CARDANO_MINT_BURN,
TABLE_DATA_ACHIEVEMENT_TYPE,
TABLE_DATA_ACHIEVEMENT_PROGRESS,
];
2 changes: 1 addition & 1 deletion packages/node-sdk/paima-utils-backend/src/achievements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface Validity {
/** Data block height (0 always valid) */
block: number;
/** CAIP-2 blockchain identifier */
caip2: number;
caip2: string;
/** Optional date. ISO8601, like YYYY-MM-DDTHH:mm:ss.sssZ */
time?: string;
}
Expand Down