Skip to content

Commit 1c3b932

Browse files
committed
switch to redis
1 parent e5dfb26 commit 1c3b932

File tree

3 files changed

+118
-72
lines changed

3 files changed

+118
-72
lines changed

app/lib/redis.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,125 @@
11
import { createClient } from "redis";
2+
import type { Hai } from "./hai";
3+
import { sortTehai } from "./hai";
24

35
export function getRedisClient(env: Env) {
46
const client = createClient({
57
url: env.REDIS_URL,
68
});
79
return client;
810
}
11+
12+
interface GameState {
13+
kyoku: number;
14+
junme: number;
15+
haiyama: Hai[];
16+
sutehai: Hai[];
17+
tehai: Hai[];
18+
tsumohai: Hai | null;
19+
}
20+
21+
const getGameState = async (
22+
client: ReturnType<typeof createClient>,
23+
userId: string,
24+
): Promise<GameState | null> => {
25+
const gameStateJSON = await client.get(`user:${userId}:game`);
26+
if (!gameStateJSON) {
27+
return null;
28+
}
29+
return JSON.parse(gameStateJSON);
30+
};
31+
32+
const setGameState = async (
33+
client: ReturnType<typeof createClient>,
34+
userId: string,
35+
gameState: GameState,
36+
) => {
37+
await client.set(`user:${userId}:game`, JSON.stringify(gameState));
38+
};
39+
40+
export const init = async (
41+
client: ReturnType<typeof createClient>,
42+
userId: string,
43+
initHaiyama: Hai[],
44+
) => {
45+
const tehai = initHaiyama.slice(0, 13);
46+
const tsumohai = initHaiyama[13];
47+
const haiyama = initHaiyama.slice(14);
48+
const initialGameState: GameState = {
49+
kyoku: 1,
50+
junme: 1,
51+
haiyama,
52+
sutehai: [],
53+
tehai,
54+
tsumohai,
55+
};
56+
await setGameState(client, userId, initialGameState);
57+
};
58+
59+
export const tedashi = async (
60+
client: ReturnType<typeof createClient>,
61+
userId: string,
62+
index: number,
63+
) => {
64+
const state = await getGameState(client, userId);
65+
if (!state) {
66+
throw new Error("game not found");
67+
}
68+
if (!state.tsumohai) {
69+
throw new Error("syohai");
70+
}
71+
const tsumohai = state.tsumohai;
72+
73+
if (index < 0 || 12 < index) {
74+
throw new Error("index out of tehai length");
75+
}
76+
const deletedTehai = state.tehai.filter((_, i) => i !== index);
77+
const discardedHai = state.tehai[index];
78+
79+
const newGameState: GameState = {
80+
...state,
81+
junme: state.junme + 1,
82+
haiyama: state.haiyama.slice(1),
83+
sutehai: [...state.sutehai, discardedHai],
84+
tehai: sortTehai([...deletedTehai, tsumohai]),
85+
tsumohai: state.haiyama[0],
86+
};
87+
await setGameState(client, userId, newGameState);
88+
};
89+
90+
export const tsumogiri = async (
91+
client: ReturnType<typeof createClient>,
92+
userId: string,
93+
) => {
94+
const state = await getGameState(client, userId);
95+
if (!state) {
96+
throw new Error("game not found");
97+
}
98+
if (!state.tsumohai) {
99+
throw new Error("syohai");
100+
}
101+
const tsumohai = state.tsumohai;
102+
const newGameState: GameState = {
103+
...state,
104+
junme: state.junme + 1,
105+
haiyama: state.haiyama.slice(1),
106+
sutehai: [...state.sutehai, tsumohai],
107+
tsumohai: state.haiyama[0],
108+
};
109+
await setGameState(client, userId, newGameState);
110+
};
111+
112+
export const jikyoku = async (
113+
client: ReturnType<typeof createClient>,
114+
userId: string,
115+
) => {
116+
const state = await getGameState(client, userId);
117+
if (!state) {
118+
throw new Error("game not found");
119+
}
120+
const newGameState: GameState = {
121+
...state,
122+
kyoku: state.kyoku + 1,
123+
};
124+
await setGameState(client, userId, newGameState);
125+
};

app/lib/store.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
"react": "^19.1.1",
2323
"react-dom": "^19.1.1",
2424
"react-router": "^7.9.2",
25-
"redis": "^5.9.0",
26-
"zustand": "^5.0.8"
25+
"redis": "^5.9.0"
2726
},
2827
"devDependencies": {
2928
"@cloudflare/vite-plugin": "^1.13.5",

0 commit comments

Comments
 (0)