From 4609d3e6e947fe65c3f14bbebe71b301112d7298 Mon Sep 17 00:00:00 2001 From: Tad Hardesty Date: Tue, 30 Apr 2024 17:36:31 -0700 Subject: [PATCH] Add table defs for new tables, clarify TODOs, fix issues --- .../src/controllers/AchievementsController.ts | 11 ++-- packages/engine/paima-rest/src/tsoa/routes.ts | 4 +- .../engine/paima-rest/src/tsoa/swagger.json | 6 +-- packages/node-sdk/paima-db/migrations/up.sql | 2 +- packages/node-sdk/paima-db/src/index.ts | 2 +- .../node-sdk/paima-db/src/paima-tables.ts | 51 +++++++++++++++++++ .../paima-utils-backend/src/achievements.ts | 2 +- 7 files changed, 65 insertions(+), 13 deletions(-) diff --git a/packages/engine/paima-rest/src/controllers/AchievementsController.ts b/packages/engine/paima-rest/src/controllers/AchievementsController.ts index e4fec143..2161224d 100644 --- a/packages/engine/paima-rest/src/controllers/AchievementsController.ts +++ b/packages/engine/paima-rest/src/controllers/AchievementsController.ts @@ -17,12 +17,15 @@ import { getAchievementTypes, getAchievementProgress } from '@paima/db'; @Route('achievements') export class AchievementsController extends Controller { private async game(): Promise { - return { id: 'TODO' }; + return { + id: 'TODO', + // TODO: name, version + }; } private async validity(): Promise { return { - caip2: ENV.CHAIN_ID, + caip2: `eip155:${ENV.CHAIN_ID}`, block: await EngineService.INSTANCE.getSM().latestProcessedBlockHeight(), time: new Date().toISOString(), }; @@ -57,8 +60,8 @@ export class AchievementsController extends Controller { /** Comma-separated list. */ @Query() name?: string ): Promise { - const names = name ? name.split(',') : []; - const player: Player = { wallet }; + const names = name ? name.split(',') : ['*']; + const player: Player = { wallet }; // TODO: walletType, userId, userName const db = EngineService.INSTANCE.getSM().getReadonlyDbConn(); const rows = await getAchievementProgress.run({ wallet, names }, db); diff --git a/packages/engine/paima-rest/src/tsoa/routes.ts b/packages/engine/paima-rest/src/tsoa/routes.ts index 29d11799..b842f410 100644 --- a/packages/engine/paima-rest/src/tsoa/routes.ts +++ b/packages/engine/paima-rest/src/tsoa/routes.ts @@ -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}, }, @@ -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"}, diff --git a/packages/engine/paima-rest/src/tsoa/swagger.json b/packages/engine/paima-rest/src/tsoa/swagger.json index 511a7508..6e4bdf80 100644 --- a/packages/engine/paima-rest/src/tsoa/swagger.json +++ b/packages/engine/paima-rest/src/tsoa/swagger.json @@ -223,8 +223,7 @@ "description": "Data block height (0 always valid)" }, "caip2": { - "type": "number", - "format": "double", + "type": "string", "description": "CAIP-2 blockchain identifier" }, "time": { @@ -299,8 +298,7 @@ "description": "Data block height (0 always valid)" }, "caip2": { - "type": "number", - "format": "double", + "type": "string", "description": "CAIP-2 blockchain identifier" }, "time": { diff --git a/packages/node-sdk/paima-db/migrations/up.sql b/packages/node-sdk/paima-db/migrations/up.sql index bb75b1a5..5bd83389 100644 --- a/packages/node-sdk/paima-db/migrations/up.sql +++ b/packages/node-sdk/paima-db/migrations/up.sql @@ -257,4 +257,4 @@ CREATE TABLE achievement_progress( progress INTEGER, total INTEGER, PRIMARY KEY (wallet, name) -);-- +); diff --git a/packages/node-sdk/paima-db/src/index.ts b/packages/node-sdk/paima-db/src/index.ts index f5a06d31..563af261 100644 --- a/packages/node-sdk/paima-db/src/index.ts +++ b/packages/node-sdk/paima-db/src/index.ts @@ -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'; diff --git a/packages/node-sdk/paima-db/src/paima-tables.ts b/packages/node-sdk/paima-db/src/paima-tables.ts index 6566161b..b4a2b2e4 100644 --- a/packages/node-sdk/paima-db/src/paima-tables.ts +++ b/packages/node-sdk/paima-db/src/paima-tables.ts @@ -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 @@ -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, ]; diff --git a/packages/node-sdk/paima-utils-backend/src/achievements.ts b/packages/node-sdk/paima-utils-backend/src/achievements.ts index a748c990..5f633370 100644 --- a/packages/node-sdk/paima-utils-backend/src/achievements.ts +++ b/packages/node-sdk/paima-utils-backend/src/achievements.ts @@ -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; }