Skip to content

Commit

Permalink
Add achievement_language table to back Accept-Language
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceManiac committed May 1, 2024
1 parent 7692a6c commit 1a0cf85
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Path, Query, Route } from 'tsoa';
import { Controller, Get, Header, Path, Query, Route } from 'tsoa';
import { EngineService } from '../EngineService.js';
import { ENV } from '@paima/utils';
import {
Expand Down Expand Up @@ -34,12 +34,15 @@ export class AchievementsController extends Controller {
@Get('public/list')
public async public_list(
@Query() category?: string,
@Query() isActive?: boolean
@Query() isActive?: boolean,
@Header('Accept-Language') acceptLanguage?: string
): Promise<AchievementPublicList> {
const db = EngineService.INSTANCE.getSM().getReadonlyDbConn();
const rows = await getAchievementTypes.run({ category, is_active: isActive }, db);
// Future expansion: import a real Accept-Language parser so user can provide more than one, handle 'pt-BR' also implying 'pt', etc.
const languages = acceptLanguage ? [acceptLanguage] : [];
const rows = await getAchievementTypes.run({ category, is_active: isActive, languages }, db);

this.setHeader('Content-Language', 'en');
this.setHeader('Content-Language', languages[0]);
return {
...(await this.validity()),
...(await this.game()),
Expand All @@ -48,8 +51,8 @@ export class AchievementsController extends Controller {
// Splat metadata first so that it can't override these:
name: row.name,
isActive: row.is_active,
displayName: row.display_name,
description: row.description,
displayName: row.display_name ?? '',
description: row.description ?? '',
})),
};
}
Expand All @@ -72,7 +75,6 @@ export class AchievementsController extends Controller {
const names = name ? name.split(',') : ['*'];
const rows = await getAchievementProgress.run({ wallet, names }, db);

this.setHeader('Content-Language', 'en');
return {
...(await this.validity()),
...player,
Expand All @@ -99,7 +101,6 @@ export class AchievementsController extends Controller {
@Query() name?: string
): Promise<PlayerAchievements> {
const db = EngineService.INSTANCE.getSM().getReadonlyDbConn();
this.setHeader('Content-Language', 'en');

switch (erc) {
case 'erc721':
Expand Down
1 change: 1 addition & 0 deletions packages/engine/paima-rest/src/tsoa/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ export function RegisterRoutes(app: Router) {
const args: Record<string, TsoaRoute.ParameterSchema> = {
category: {"in":"query","name":"category","dataType":"string"},
isActive: {"in":"query","name":"isActive","dataType":"boolean"},
acceptLanguage: {"in":"header","name":"Accept-Language","dataType":"string"},
};

// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
Expand Down
8 changes: 8 additions & 0 deletions packages/engine/paima-rest/src/tsoa/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,14 @@
"schema": {
"type": "boolean"
}
},
{
"in": "header",
"name": "Accept-Language",
"required": false,
"schema": {
"type": "string"
}
}
]
}
Expand Down
8 changes: 8 additions & 0 deletions packages/node-sdk/paima-db/migrations/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ CREATE TABLE achievement_type(
metadata JSONB NOT NULL DEFAULT '{}'
);

CREATE TABLE achievement_language(
name TEXT NOT NULL,
language TEXT NOT NULL,
display_name TEXT,
description TEXT,
PRIMARY KEY (name, language)
);

CREATE TABLE achievement_progress(
wallet TEXT NOT NULL,
name TEXT NOT NULL,
Expand Down
24 changes: 24 additions & 0 deletions packages/node-sdk/paima-db/src/paima-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,29 @@ const TABLE_DATA_ACHIEVEMENT_TYPE: TableData = {
creationQuery: QUERY_CREATE_TABLE_ACHIEVEMENT_TYPE,
};

const QUERY_CREATE_TABLE_ACHIEVEMENT_LANGUAGE = `
CREATE TABLE achievement_language(
name TEXT NOT NULL,
language TEXT NOT NULL,
display_name TEXT,
description TEXT,
PRIMARY KEY (name, language)
);
`;

const TABLE_DATA_ACHIEVEMENT_LANGUAGE: TableData = {
tableName: 'achievement_language',
primaryKeyColumns: ['name', 'language'],
columnData: packTuples([
['name', 'text', 'NO', ''],
['language', 'text', 'NO', ''],
['display_name', 'text', 'YES', ''],
['description', 'text', 'YES', ''],
]),
serialColumns: [],
creationQuery: QUERY_CREATE_TABLE_ACHIEVEMENT_LANGUAGE,
};

const QUERY_CREATE_TABLE_ACHIEVEMENT_PROGRESS = `
CREATE TABLE achievement_progress(
wallet TEXT NOT NULL,
Expand Down Expand Up @@ -713,5 +736,6 @@ export const TABLES: TableData[] = [
TABLE_DATA_CDE_CARDANO_TRANSFER,
TABLE_DATA_CDE_CARDANO_MINT_BURN,
TABLE_DATA_ACHIEVEMENT_TYPE,
TABLE_DATA_ACHIEVEMENT_LANGUAGE,
TABLE_DATA_ACHIEVEMENT_PROGRESS,
];
25 changes: 20 additions & 5 deletions packages/node-sdk/paima-db/src/sql/achievements.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ import { PreparedQuery } from '@pgtyped/runtime';

export type Json = null | boolean | number | string | Json[] | { [key: string]: Json };

export type stringArray = (string)[];

/** 'GetAchievementTypes' parameters type */
export interface IGetAchievementTypesParams {
category?: string | null | void;
is_active?: boolean | null | void;
languages?: stringArray | null | void;
}

/** 'GetAchievementTypes' return type */
export interface IGetAchievementTypesResult {
description: string;
display_name: string;
description: string | null;
display_name: string | null;
is_active: boolean;
metadata: Json;
name: string;
Expand All @@ -24,12 +27,24 @@ export interface IGetAchievementTypesQuery {
result: IGetAchievementTypesResult;
}

const getAchievementTypesIR: any = {"usedParamSet":{"is_active":true,"category":true},"params":[{"name":"is_active","required":false,"transform":{"type":"scalar"},"locs":[{"a":38,"b":47},{"a":69,"b":78}]},{"name":"category","required":false,"transform":{"type":"scalar"},"locs":[{"a":98,"b":106},{"a":125,"b":133}]}],"statement":"SELECT * FROM achievement_type\nWHERE (:is_active::BOOLEAN IS NULL OR :is_active = is_active)\nAND (:category::TEXT IS NULL OR :category = metadata ->> 'category')"};
const getAchievementTypesIR: any = {"usedParamSet":{"languages":true,"is_active":true,"category":true},"params":[{"name":"languages","required":false,"transform":{"type":"scalar"},"locs":[{"a":355,"b":364},{"a":429,"b":438}]},{"name":"is_active","required":false,"transform":{"type":"scalar"},"locs":[{"a":508,"b":517},{"a":539,"b":548}]},{"name":"category","required":false,"transform":{"type":"scalar"},"locs":[{"a":568,"b":576},{"a":595,"b":603}]}],"statement":"SELECT\n achievement_type.name,\n achievement_type.is_active,\n coalesce(sub.display_name, achievement_type.display_name) AS display_name,\n coalesce(sub.description, achievement_type.description) AS description,\n achievement_type.metadata\nFROM achievement_type\nLEFT JOIN (\n SELECT DISTINCT ON(name) *\n FROM achievement_language\n WHERE array_position(:languages::text[], language) IS NOT NULL\n ORDER BY name, array_position(:languages::text[], language)\n) sub ON achievement_type.name = sub.name\nWHERE (:is_active::BOOLEAN IS NULL OR :is_active = is_active)\nAND (:category::TEXT IS NULL OR :category = metadata ->> 'category')"};

/**
* Query generated from SQL:
* ```
* SELECT * FROM achievement_type
* ```sql
* SELECT
* achievement_type.name,
* achievement_type.is_active,
* coalesce(sub.display_name, achievement_type.display_name) AS display_name,
* coalesce(sub.description, achievement_type.description) AS description,
* achievement_type.metadata
* FROM achievement_type
* LEFT JOIN (
* SELECT DISTINCT ON(name) *
* FROM achievement_language
* WHERE array_position(:languages::text[], language) IS NOT NULL
* ORDER BY name, array_position(:languages::text[], language)
* ) sub ON achievement_type.name = sub.name
* WHERE (:is_active::BOOLEAN IS NULL OR :is_active = is_active)
* AND (:category::TEXT IS NULL OR :category = metadata ->> 'category')
* ```
Expand Down
14 changes: 13 additions & 1 deletion packages/node-sdk/paima-db/src/sql/achievements.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
/* @name getAchievementTypes */
SELECT * FROM achievement_type
SELECT
achievement_type.name,
achievement_type.is_active,
coalesce(sub.display_name, achievement_type.display_name) AS display_name,
coalesce(sub.description, achievement_type.description) AS description,
achievement_type.metadata
FROM achievement_type
LEFT JOIN (
SELECT DISTINCT ON(name) *
FROM achievement_language
WHERE array_position(:languages::text[], language) IS NOT NULL
ORDER BY name, array_position(:languages::text[], language)
) sub ON achievement_type.name = sub.name
WHERE (:is_active::BOOLEAN IS NULL OR :is_active = is_active)
AND (:category::TEXT IS NULL OR :category = metadata ->> 'category')
;
Expand Down

0 comments on commit 1a0cf85

Please sign in to comment.