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

Cardgame modifications #83

Open
wants to merge 4 commits into
base: feature/shinkai-game
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions card-game/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CHAIN_CURRENCY_SYMBOL="TEST"
CHAIN_CURRENCY_DECIMALS="18"
# Note: This is in seconds as a float
# Example: "2.0"
BLOCK_TIME="2"
BLOCK_TIME="1"

## CONTRACT DEPLOYMENT
# Example: "0xA02F7744868945A346Ee6994068F54D039683445"
Expand Down Expand Up @@ -67,8 +67,8 @@ BATCHER_PORT="3340"
# Batcher Validation parameters:
GAME_NODE_URI="http://localhost:3333"
DEFAULT_VALIDATION_ACTIVE="false"
GAME_INPUT_VALIDATOR_PERIOD="1000"
BATCHED_TRANSACTION_POSTER_PERIOD="3000"
GAME_INPUT_VALIDATOR_PERIOD="100"
BATCHED_TRANSACTION_POSTER_PERIOD="100"
BATCHED_MESSAGE_SIZE_LIMIT="100000"
MAX_USER_INPUTS_PER_MINUTE="10"
MAX_USER_INPUTS_PER_DAY="500"
Expand Down
4 changes: 2 additions & 2 deletions card-game/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ snapshots
*.local

.env
.env.testnet
.env.mainnet
.env.*
!.env.example

# Frontend
build.zip
Expand Down
1 change: 1 addition & 0 deletions card-game/.npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
engine-strict=true
@paima:registry=http://localhost:4873
15 changes: 15 additions & 0 deletions card-game/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"files.associations": {
".env.*": "properties"
},
"[sql]": {
"editor.formatOnSave": false
},
"[json][jsonc][json5]": {
"editor.formatOnSave": true
}
}
1 change: 1 addition & 0 deletions card-game/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/tsoa
7 changes: 4 additions & 3 deletions card-game/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
"main": "build/index.js",
"type": "module",
"scripts": {
"build": "tsc",
"build": "npm run compile && tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"compile": "npx tsoa spec-and-routes"
},
"author": "",
"license": "ISC",
"dependencies": {
"@game/utils": "1.0.0",
"@game/db": "1.0.0",
"@game/utils": "1.0.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"fp-ts": "^2.16.2",
"http-status-codes": "^2.3.0",
"io-ts": "^2.2.21",
"tsoa": "^6.0.1"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21"
}
}
}
15 changes: 7 additions & 8 deletions card-game/api/src/controllers/game.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { Controller, Get, Query, Route } from 'tsoa';
import {
requirePool,
getUserStats,
getCards,
IGetCardsResult,
} from '@game/db';
import { Controller, Get, Response, Route } from 'tsoa';
import { requirePool, getCards } from '@game/db';
import type { IGetCardsResult } from '@game/db';
import { StatusCodes } from 'http-status-codes';
import type { InternalServerErrorResult, ValidateErrorResult } from '@paima/sdk/utils';

@Route('game')
export class GameController extends Controller {
@Get('/')
@Response<InternalServerErrorResult>(StatusCodes.INTERNAL_SERVER_ERROR)
@Response<ValidateErrorResult>(StatusCodes.UNPROCESSABLE_ENTITY)
public async getGame(): Promise<{ stats: IGetCardsResult[] }> {
const pool = requirePool();
const stats = await getCards.run(undefined, pool);
if (!stats) throw new Error('not found');
return { stats };
}

}
6 changes: 5 additions & 1 deletion card-game/api/src/controllers/userStats.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Body, Controller, Get, Path, Post, Query, Route, SuccessResponse } from 'tsoa';
import { Controller, Get, Query, Route, Response } from 'tsoa';
import { requirePool, getUserStats } from '@game/db';
import type { UserStats } from '@game/utils';
import type { InternalServerErrorResult, ValidateErrorResult } from '@paima/sdk/utils';
import { StatusCodes } from 'http-status-codes';

interface GetUserStatsResponse {
stats: UserStats;
}

@Route('user_stats')
export class UserStatsController extends Controller {
@Response<InternalServerErrorResult>(StatusCodes.INTERNAL_SERVER_ERROR)
@Response<ValidateErrorResult>(StatusCodes.UNPROCESSABLE_ENTITY)
@Get()
public async get(@Query() wallet: string): Promise<GetUserStatsResponse> {
const pool = requirePool();
Expand Down
40 changes: 39 additions & 1 deletion card-game/api/src/tsoa/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,42 @@ const models: TsoaRoute.Models = {
"additionalProperties": false,
},
// 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
"FailedResult": {
"dataType": "refObject",
"properties": {
"success": {"dataType":"enum","enums":[false],"required":true},
"errorMessage": {"dataType":"string","required":true},
"errorCode": {"dataType":"double"},
},
"additionalProperties": false,
},
// 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
"InternalServerErrorResult": {
"dataType": "refAlias",
"type": {"ref":"FailedResult","validators":{}},
},
// 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
"FieldErrors": {
"dataType": "refObject",
"properties": {
},
"additionalProperties": {"dataType":"nestedObjectLiteral","nestedProperties":{"value":{"dataType":"any"},"message":{"dataType":"string","required":true}}},
},
// 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
"ValidateErrorResult": {
"dataType": "refObject",
"properties": {
"message": {"dataType":"enum","enums":["Validation Failed"],"required":true},
"details": {"ref":"FieldErrors"},
},
"additionalProperties": false,
},
// 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
"IGetCardsResult": {
"dataType": "refObject",
"properties": {
"card": {"dataType":"double","required":true},
"upwards": {"dataType":"union","subSchemas":[{"dataType":"boolean"},{"dataType":"enum","enums":[null]}],"required":true},
"upwards": {"dataType":"boolean","required":true},
},
"additionalProperties": false,
},
Expand All @@ -48,11 +79,18 @@ const templateService = new ExpressTemplateService(models, {"noImplicitAdditiona

// 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




export function RegisterRoutes(app: Router) {

// ###########################################################################################################
// NOTE: If you do not see routes for all of your controllers in this file, then you might not have informed tsoa of where to look
// Please look into the "controllerPathGlobs" config option described in the readme: https://github.com/lukeautry/tsoa
// ###########################################################################################################



app.get('/user_stats',
...(fetchMiddlewares<RequestHandler>(UserStatsController)),
...(fetchMiddlewares<RequestHandler>(UserStatsController.prototype.get)),
Expand Down
118 changes: 116 additions & 2 deletions card-game/api/src/tsoa/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,69 @@
"type": "object",
"additionalProperties": false
},
"FailedResult": {
"properties": {
"success": {
"type": "boolean",
"enum": [
false
],
"nullable": false
},
"errorMessage": {
"type": "string"
},
"errorCode": {
"type": "number",
"format": "double"
}
},
"required": [
"success",
"errorMessage"
],
"type": "object",
"additionalProperties": false
},
"InternalServerErrorResult": {
"$ref": "#/components/schemas/FailedResult"
},
"FieldErrors": {
"description": "comes from the `tsoa` package, but we don't want it as a dependency just for this type",
"properties": {},
"type": "object",
"additionalProperties": {
"properties": {
"value": {},
"message": {
"type": "string"
}
},
"required": [
"message"
],
"type": "object"
}
},
"ValidateErrorResult": {
"properties": {
"message": {
"type": "string",
"enum": [
"Validation Failed"
],
"nullable": false
},
"details": {
"$ref": "#/components/schemas/FieldErrors"
}
},
"required": [
"message"
],
"type": "object",
"additionalProperties": false
},
"IGetCardsResult": {
"description": "'GetCards' return type",
"properties": {
Expand All @@ -43,8 +106,7 @@
"format": "double"
},
"upwards": {
"type": "boolean",
"nullable": true
"type": "boolean"
}
},
"required": [
Expand Down Expand Up @@ -80,6 +142,32 @@
}
}
}
},
"422": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidateErrorResult"
},
"examples": {
"Example 1": {}
}
}
}
},
"500": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/InternalServerErrorResult"
},
"examples": {
"Example 1": {}
}
}
}
}
},
"security": [],
Expand Down Expand Up @@ -119,6 +207,32 @@
}
}
}
},
"422": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidateErrorResult"
},
"examples": {
"Example 1": {}
}
}
}
},
"500": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/InternalServerErrorResult"
},
"examples": {
"Example 1": {}
}
}
}
}
},
"security": [],
Expand Down
4 changes: 2 additions & 2 deletions card-game/db/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
ports:
- "5432:5432"
volumes:
- generic-1721453102636-db:/var/lib/postgresql/data
- generic-1723171942228-db:/var/lib/postgresql/data
- ../migrations/init/init.sql:/docker-entrypoint-initdb.d/init.sql
healthcheck:
# Use pg_isready to check postgres is running. Substitute different
Expand All @@ -20,4 +20,4 @@ services:
retries: 5

volumes:
generic-1721453102636-db:
generic-1723171942228-db:
38 changes: 26 additions & 12 deletions card-game/db/migrations/2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@
-- recall: migrations need to be repackaged with `npm run pack` when changed
INSERT INTO global_cards (card) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9);

INSERT INTO scheduled_data (block_height, input_data )
VALUES (
-- get the latest block + 1
coalesce((
SELECT block_height
FROM block_heights
ORDER BY block_height DESC
LIMIT 1
), 0) + 2,
'tick|0'
);

-- todo: replace https://github.com/PaimaStudios/paima-engine/issues/414
WITH
new_tick AS (
INSERT INTO rollup_inputs (from_address, input_data )
VALUES (
'0x9bcf794c089d151e8edf1d8a40d9594d432bd494',
'tick|0'
)
RETURNING id AS new_tick_id
),
future_block AS (
INSERT INTO rollup_input_future_block (id, future_block_height )
VALUES (
(SELECT new_tick_id FROM new_tick),
-- get the latest block + 1
coalesce((
SELECT block_height
FROM paima_blocks
ORDER BY block_height DESC
LIMIT 1
), 0) + 2
)
)
INSERT INTO rollup_input_origin (id, primitive_name, caip2, tx_hash)
SELECT new_tick_id, NULL, NULL, NULL
FROM new_tick
2 changes: 1 addition & 1 deletion card-game/db/migrations/init/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ CREATE TABLE global_user_state (

CREATE TABLE global_cards (
card INTEGER PRIMARY KEY,
upwards BOOLEAN DEFAULT TRUE
upwards BOOLEAN DEFAULT TRUE NOT NULL
);
Loading