Skip to content

persistence ok and zod on db entity #9

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
docker*
docker-compose.yml
postgres-data*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
Expand Down
20 changes: 20 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: "3.7"
services:
postgres:
image: postgres:12.1-alpine
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
- POSTGRES_DB=root
logging:
options:
max-size: 10m
max-file: "3"
ports:
- "5438:5432"
volumes:
- local_test_andrea:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql

volumes:
local_test_andrea:
5 changes: 5 additions & 0 deletions docker/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create table Results (
id SERIAL PRIMARY KEY,
result varchar NOT NULL,
log_game timestamp NOT NULL DEFAULT now()
);
12 changes: 6 additions & 6 deletions game.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { playLogic } from "./game";
import { read } from "./model/move";

test("game logic test", () => {
expect(playLogic("Rock", "Paper")).toBe("Lose");
expect(playLogic("Rock", "Scissors")).toBe("Win");
expect(playLogic("Paper", "Rock")).toBe("Win");
expect(playLogic("Paper", "Scissors")).toBe("Lose");
expect(playLogic("Scissors", "Rock")).toBe("Lose");
expect(playLogic("Scissors", "Paper")).toBe("Win");
expect(playLogic("Rock", "Paper")).toBe("You lose :< ");
expect(playLogic("Rock", "Scissors")).toBe("You Win!!!");
expect(playLogic("Paper", "Rock")).toBe("You Win!!!");
expect(playLogic("Paper", "Scissors")).toBe("You lose :< ");
expect(playLogic("Scissors", "Rock")).toBe("You lose :< ");
expect(playLogic("Scissors", "Paper")).toBe("You Win!!!");
});

test("read function test", () => {
Expand Down
22 changes: 18 additions & 4 deletions game.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as readline from "node:readline/promises";
import { match, P } from "ts-pattern";
import { read, Move } from "./model/move";
import { lastGame, logRes, closeDB } from "./sql/db";
import { results } from "./model/result";

export function generateComputerMove() {
return read(String(Math.round(Math.random() * 2)));
Expand All @@ -26,16 +28,28 @@ export async function play(
) {
const rl = readline.createInterface({ input, output });
const computerMove = generateComputerMove();
const lastG = results.safeParse(await lastGame());
if (lastG.success) {
rl.write("Our last game timestamp is : " + lastG.data.log_game + "\n");
rl.write(
"and the game finished with this result : " + lastG.data.result + "\n"
);
} else {
rl.write("Welcome to the best RPS game ever! \n");
}

const userMove = read(
await rl.question(
"Wanna play? Your move (0: Rock, 1: Paper, 2: Scissors) \n"
)
);

output.write(`You chose: ${userMove}\nComputer chosed: ${computerMove}\n`);
output.write(
"The result is... " + playLogic(userMove, computerMove) + " !\n"
);
const res = playLogic(userMove, computerMove);
logRes(res);

rl.write(`You chose: ${userMove}\nComputer chose: ${computerMove}\n`);
rl.write("The result is... " + res + " !\n");

rl.close();
closeDB();
}
14 changes: 6 additions & 8 deletions model/result.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { z } from "zod";
import { date, z } from "zod";

export enum Result {
WIN = "You Win!!!",
LOSE = "You lose :< ",
DRAW = "It's a Draw!",
}
export const results = z.object({
log_game: z.ZodDate.create(),
result: z.enum(["You Win!!!", "You lose :< ", "It's a Draw!"]),
});

const ResultEnum = z.nativeEnum(Result);
export type ResultEnum = z.infer<typeof ResultEnum>;
export type Result = z.infer<typeof results>;
29 changes: 29 additions & 0 deletions sql/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pgPromise, { ParameterizedQuery as PQ } from "pg-promise";
import { Result } from "../model/result";

const cn = {
host: "localhost",
port: 5438,
database: "root",
user: "root",
password: "root",
max: 30, // use up to 30 connections
};

const pgp = pgPromise();
const db = pgp(cn);

export async function lastGame(): Promise<Result> {
return await db.one<Result>(
"SELECT log_game, result FROM results ORDER BY log_game DESC LIMIT 1"
);
}

export async function logRes(res: String) {
const addResult = new PQ("INSERT into results(result) VALUES($1)");
return await db.none(addResult, [res]);
}

export function closeDB() {
db.$pool.end();
}