-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
39a008d
6bed6eb
abe51db
9cebf44
234b86b
1f3e781
0b9b195
b9efafe
4a2e98e
cd3caee
e995b1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import { date, z } from "zod"; | ||
import { z } from "zod"; | ||
|
||
export const results = z.object({ | ||
export const result = z.object({ | ||
game_date: z.coerce.date(), | ||
result: z.enum(["WIN", "LOSE", "DRAW"]), | ||
}); | ||
|
||
export type Result = z.infer<typeof results>; | ||
|
||
export const resultArray = z.array(results); | ||
export type Result = z.infer<typeof result>; | ||
export const resultArray = z.array(result); | ||
export type ResultArray = z.infer<typeof resultArray>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,38 @@ | ||
import { match, P } from "ts-pattern"; | ||
import { read, Move } from "../model/move"; | ||
import { lastGame, allGames } from "../sql/db"; | ||
import { Result, resultArray, results } from "../model/result"; | ||
import { logRes } from "../sql/db"; | ||
import { ResultArray, resultArray, result } from "../model/result"; | ||
import { logRes, allGames, lastGame } from "../sql/db"; | ||
|
||
export function generateComputerMove(): Move { | ||
return read(String(Math.round(Math.random() * 2))); | ||
} | ||
export function playLogic(userMove: Move, computerMove: Move): string { | ||
return match([userMove, computerMove]) | ||
export async function playLogic( | ||
userMove: Move, | ||
computerMove: Move | ||
): Promise<string> { | ||
const outcome: string[] = match([userMove, computerMove]) | ||
.with( | ||
["Rock", "Scissors"], | ||
["Scissors", "Paper"], | ||
["Paper", "Rock"], | ||
() => { | ||
logRes("WIN"); | ||
return "You Win!!!"; | ||
} | ||
() => ["WIN", "You Win!!!"] | ||
) | ||
.with( | ||
P.when(() => userMove === computerMove), | ||
() => { | ||
logRes("DRAW"); | ||
return "It's a Draw!"; | ||
} | ||
() => ["DRAW", "It's a Draw!"] | ||
) | ||
.otherwise(() => { | ||
logRes("LOSE"); | ||
return "You lose :< "; | ||
}); | ||
.otherwise(() => ["LOSE", "You lose :< "]); | ||
const isDbWritten = await logRes(outcome[0]); | ||
if (isDbWritten === null) { | ||
return outcome[1]; | ||
} else { | ||
return `${outcome[1]} ... but I couldn't save this result because something went wrong`; | ||
} | ||
} | ||
|
||
export async function welcome(): Promise<string[]> { | ||
const res = ["Wanna play? Your move (0: Rock, 1: Paper, 2: Scissors)"]; | ||
const lastGameParsed = results.safeParse(await lastGame()); | ||
const lastGameParsed = result.safeParse(await lastGame()); | ||
if (lastGameParsed.success) { | ||
res.push( | ||
"Our last game timestamp is : " + lastGameParsed.data.game_date + "\n" | ||
|
@@ -50,11 +49,11 @@ export async function welcome(): Promise<string[]> { | |
} | ||
} | ||
|
||
export async function allGamesParsed(): Promise<Result[]> { | ||
export async function allGamesParsed(): Promise<Error | ResultArray> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tendenzialmente in TS è più idiomatico fare e poi fai un nulla vieta di restituire invece un'unione, e noi in progetti passati usavamo per esempio fp-ts con There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ho scelto di restituire There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tendenzialmente farei così:
Il problema è che In pratica però l'errore sarà lo stesso che hai nel campo (In generale mi torna che sia meglio usare |
||
const all = resultArray.safeParse(await allGames()); | ||
if (all.success) { | ||
return all.data; | ||
} else { | ||
return []; | ||
return all.error; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggerirei di fare destructuring tipo
per evitare di usare
outcome[0]
eoutcome[1]
nel seguito, che mi sembrano meno leggibiliThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Che gioia, non avevo proprio considerato che si potesse usare destructuring in TS