|
1 | 1 | import { createClient } from "redis"; |
| 2 | +import type { Hai } from "./hai"; |
| 3 | +import { sortTehai } from "./hai"; |
2 | 4 |
|
3 | 5 | export function getRedisClient(env: Env) { |
4 | 6 | const client = createClient({ |
5 | 7 | url: env.REDIS_URL, |
6 | 8 | }); |
7 | 9 | return client; |
8 | 10 | } |
| 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 | +}; |
0 commit comments