Skip to content

Commit aa55340

Browse files
authored
Zustand (#104)
* install zustand * format * fix lefthook * fix yaml * format? * not store state in localstorage * define global state
1 parent 0d721e0 commit aa55340

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

lefthook.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pre-commit:
22
commands:
3-
check:
4-
glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}"
5-
run: bunx @biomejs/biome check --write {staged_files} && git add {staged_files}
3+
format:
4+
glob: "*.{ts,tsx,json,jsonc}"
5+
run: bunx @biomejs/biome check {staged_files} --write && git add {staged_files}

rr/app/lib/db/schema.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export const hai = pgTable("hai", {
3535
});
3636

3737
// relation between user and haiyama
38-
// TODO: index
3938
export const kyoku = pgTable(
4039
"kyoku",
4140
{

rr/app/lib/store.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { create } from "zustand";
2+
import type { Hai } from "./hai";
3+
import { sortTehai } from "./hai";
4+
5+
interface GameState {
6+
kyoku: number;
7+
junme: number;
8+
haiyama: Hai[];
9+
sutehai: Hai[];
10+
// Hai * 13
11+
tehai: Hai[];
12+
// null as an initial value
13+
tsumohai: Hai | null;
14+
}
15+
16+
interface PlayerAction {
17+
// tsumo just after tedashi or tsumogiri
18+
tedashi: (index: number) => void;
19+
tsumogiri: () => void;
20+
}
21+
22+
const useGameStore = create<GameState & PlayerAction>()((set, get) => ({
23+
kyoku: 1,
24+
junme: 1,
25+
haiyama: [],
26+
sutehai: [],
27+
tehai: [],
28+
tsumohai: null,
29+
30+
tedashi: (index) => {
31+
const state = get();
32+
if (!state.tsumohai) {
33+
throw new Error("syohai");
34+
}
35+
const tsumohai = state.tsumohai;
36+
37+
if (index < 0 || 12 < index) {
38+
throw new Error("index out of tehai length");
39+
}
40+
const deletedTehai = state.tehai.filter((_, i) => i !== index);
41+
const discardedHai = state.tehai[index];
42+
set(() => ({
43+
kyoku: state.kyoku + 1,
44+
junme: state.junme + 1,
45+
haiyama: state.haiyama.slice(1),
46+
sutehai: [...state.sutehai, discardedHai],
47+
tehai: sortTehai([...deletedTehai, tsumohai]),
48+
tsumohai: state.haiyama[0],
49+
}));
50+
},
51+
tsumogiri: () => {
52+
const state = get();
53+
if (!state.tsumohai) {
54+
throw new Error("syohai");
55+
}
56+
const tsumohai = state.tsumohai;
57+
set(() => ({
58+
kyoku: state.kyoku + 1,
59+
junme: state.junme + 1,
60+
haiyama: state.haiyama.slice(1),
61+
sutehai: [...state.sutehai, tsumohai],
62+
tsumohai: state.haiyama[0],
63+
}));
64+
},
65+
}));

rr/bun.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"react": "^19.1.1",
1414
"react-dom": "^19.1.1",
1515
"react-router": "^7.9.2",
16+
"zustand": "^5.0.8",
1617
},
1718
"devDependencies": {
1819
"@cloudflare/vite-plugin": "^1.13.5",
@@ -753,6 +754,8 @@
753754

754755
"zod": ["[email protected]", "", {}, "sha512-JInaHOamG8pt5+Ey8kGmdcAcg3OL9reK8ltczgHTAwNhMys/6ThXHityHxVV2p3fkw/c+MAvBHFVYHFZDmjMCQ=="],
755756

757+
"zustand": ["[email protected]", "", { "peerDependencies": { "@types/react": ">=18.0.0", "immer": ">=9.0.6", "react": ">=18.0.0", "use-sync-external-store": ">=1.2.0" }, "optionalPeers": ["@types/react", "immer", "react", "use-sync-external-store"] }, "sha512-gyPKpIaxY9XcO2vSMrLbiER7QMAMGOQZVRdJ6Zi782jkbzZygq5GI9nG8g+sMgitRtndwaBSl7uiqC49o1SSiw=="],
758+
756759
"@babel/core/semver": ["[email protected]", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA=="],
757760

758761
"@babel/helper-compilation-targets/semver": ["[email protected]", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA=="],

rr/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"pg": "^8.16.3",
2121
"react": "^19.1.1",
2222
"react-dom": "^19.1.1",
23-
"react-router": "^7.9.2"
23+
"react-router": "^7.9.2",
24+
"zustand": "^5.0.8"
2425
},
2526
"devDependencies": {
2627
"@cloudflare/vite-plugin": "^1.13.5",

0 commit comments

Comments
 (0)