Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cbec696c59 | ||
|
|
0139527147 | ||
|
|
35c7c31f2e | ||
|
|
026eebae3d |
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"pg": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "*",
|
||||
"@types/pg": "*",
|
||||
"typescript": "*"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import pg from "pg";
|
||||
|
||||
const client = new pg.Client({
|
||||
host: process.env.PROVISION_HOST,
|
||||
port: parseInt(process.env.PROVISION_PORT ?? "5432"),
|
||||
user: process.env.PROVISION_USER,
|
||||
password: process.env.PROVISION_PASSWORD,
|
||||
database: process.env.PROVISION_DATABASE,
|
||||
});
|
||||
|
||||
await client.connect();
|
||||
|
||||
try {
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS players (
|
||||
id TEXT PRIMARY KEY,
|
||||
games_played INT NOT NULL DEFAULT 0,
|
||||
games_won INT NOT NULL DEFAULT 0,
|
||||
games_lost INT NOT NULL DEFAULT 0,
|
||||
best_attempts INT,
|
||||
current_streak INT NOT NULL DEFAULT 0,
|
||||
best_streak INT NOT NULL DEFAULT 0,
|
||||
last_played_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
)
|
||||
`);
|
||||
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS games (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
player_id TEXT NOT NULL REFERENCES players(id) ON DELETE CASCADE,
|
||||
target INT NOT NULL,
|
||||
attempts INT NOT NULL,
|
||||
won BOOLEAN NOT NULL,
|
||||
finished_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
)
|
||||
`);
|
||||
|
||||
await client.query(`
|
||||
CREATE INDEX IF NOT EXISTS idx_players_scoreboard
|
||||
ON players (best_attempts NULLS LAST, last_played_at)
|
||||
`);
|
||||
|
||||
await client.query(`
|
||||
CREATE INDEX IF NOT EXISTS idx_games_player_history
|
||||
ON games (player_id, finished_at DESC)
|
||||
`);
|
||||
|
||||
console.log("[txt-game migration-000] score schema created");
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
+16
@@ -11,7 +11,23 @@ docker:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
|
||||
requires:
|
||||
- provider: postgres
|
||||
type: database
|
||||
name: txt_game_scores
|
||||
env_map:
|
||||
TXT_GAME_DB_HOST: host
|
||||
TXT_GAME_DB_PORT: port
|
||||
TXT_GAME_DB_USER: user
|
||||
TXT_GAME_DB_PASSWORD: password
|
||||
TXT_GAME_DB_NAME: database
|
||||
|
||||
env:
|
||||
DOMAIN:
|
||||
from: node.domain
|
||||
default: localhost
|
||||
TXT_GAME_DB_HOST:
|
||||
TXT_GAME_DB_PORT:
|
||||
TXT_GAME_DB_USER:
|
||||
TXT_GAME_DB_PASSWORD:
|
||||
TXT_GAME_DB_NAME:
|
||||
|
||||
+2
-2
@@ -193,10 +193,10 @@ const server = createServer(async (req, res) => {
|
||||
const { id, session } = getSession(req.headers["cookie"]);
|
||||
const url = req.url.split("?")[0];
|
||||
|
||||
if (req.method === "GET" && url === "/") {
|
||||
if ((req.method === "GET" || req.method === "HEAD") && url === "/") {
|
||||
setCookie(res, id);
|
||||
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
||||
res.end(renderPage(session, "", null));
|
||||
res.end(req.method === "HEAD" ? "" : renderPage(session, "", null));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user