|
| 1 | +import { sql } from "drizzle-orm"; |
| 2 | +import { Form } from "react-router"; |
| 3 | +import { getAuth } from "~/lib/auth"; |
| 4 | +import { getDB } from "~/lib/db"; |
| 5 | +import { hai, haiyama } from "~/lib/db/schema"; |
| 6 | +import { dbHaiToHai, sortTehai } from "~/lib/hai"; |
| 7 | +import { type GameState, getRedisClient, init } from "~/lib/redis"; |
| 8 | +import type { Route } from "./+types/play"; |
| 9 | + |
| 10 | +export async function loader({ |
| 11 | + context, |
| 12 | + request, |
| 13 | +}: Route.LoaderArgs): Promise<GameState> { |
| 14 | + const { env } = context.cloudflare; |
| 15 | + const db = getDB(env); |
| 16 | + const auth = getAuth(env); |
| 17 | + const session = await auth.api.getSession({ headers: request.headers }); |
| 18 | + |
| 19 | + if (!session?.user?.id) { |
| 20 | + throw new Response("Unauthorized", { status: 401 }); |
| 21 | + } |
| 22 | + const userId = session.user.id; |
| 23 | + |
| 24 | + // Check if game state already exists in Redis |
| 25 | + const redisClient = getRedisClient(env); |
| 26 | + await redisClient.connect(); |
| 27 | + |
| 28 | + try { |
| 29 | + const existingGameState = await redisClient.get(`user:${userId}:game`); |
| 30 | + |
| 31 | + if (existingGameState) { |
| 32 | + // Return existing game state from Redis |
| 33 | + await redisClient.quit(); |
| 34 | + return JSON.parse(existingGameState); |
| 35 | + } |
| 36 | + |
| 37 | + // No existing game state, so initialize from PostgreSQL |
| 38 | + const randomHaiyama = await db |
| 39 | + .select() |
| 40 | + .from(haiyama) |
| 41 | + .orderBy(sql`RANDOM()`) |
| 42 | + .limit(1); |
| 43 | + |
| 44 | + if (randomHaiyama.length === 0) { |
| 45 | + await redisClient.quit(); |
| 46 | + throw new Response("No haiyama found", { status: 404 }); |
| 47 | + } |
| 48 | + |
| 49 | + const selectedHaiyama = randomHaiyama[0]; |
| 50 | + const rawHaiData = await db |
| 51 | + .select() |
| 52 | + .from(hai) |
| 53 | + .where(sql`${hai.haiyamaId} = ${selectedHaiyama.id}`) |
| 54 | + .orderBy(hai.order); |
| 55 | + |
| 56 | + const haiData = rawHaiData.map((hai) => dbHaiToHai(hai)); |
| 57 | + |
| 58 | + // Initialize game state in Redis |
| 59 | + await init(redisClient, userId, haiData); |
| 60 | + |
| 61 | + // Get the initialized game state to return |
| 62 | + const gameStateJSON = await redisClient.get(`user:${userId}:game`); |
| 63 | + const gameState = gameStateJSON ? JSON.parse(gameStateJSON) : null; |
| 64 | + |
| 65 | + await redisClient.quit(); |
| 66 | + return gameState; |
| 67 | + } catch (error) { |
| 68 | + await redisClient.quit(); |
| 69 | + throw error instanceof Error ? error : new Error(String(error)); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export default function Page({ loaderData }: Route.ComponentProps) { |
| 74 | + let { haiyama, sutehai, tsumohai, junme, kyoku, tehai } = loaderData; |
| 75 | + tehai = sortTehai(tehai); |
| 76 | + const indexedSutehai = sutehai.map((hai, index) => ({ |
| 77 | + ...hai, |
| 78 | + index: index, |
| 79 | + })); |
| 80 | + const indexedTehai = tehai.map((hai, index) => ({ |
| 81 | + ...hai, |
| 82 | + index: index, |
| 83 | + })); |
| 84 | + |
| 85 | + return ( |
| 86 | + <div className="p-4"> |
| 87 | + <p className="text-xl mb-4"> |
| 88 | + Play Page - 局 {kyoku} 巡目 {junme} |
| 89 | + </p> |
| 90 | + |
| 91 | + {/* Sutehai grid (3x6) */} |
| 92 | + <div className="mb-6"> |
| 93 | + <h3 className="text-lg mb-2">捨て牌</h3> |
| 94 | + <div className="grid grid-cols-6 gap-0 w-max min-h-48"> |
| 95 | + {indexedSutehai.map((hai) => ( |
| 96 | + <img |
| 97 | + key={hai.index} |
| 98 | + src={`/hai/${hai.kind}_${hai.value}.png`} |
| 99 | + alt={`${hai.kind} ${hai.value}`} |
| 100 | + className="w-12 h-16" |
| 101 | + /> |
| 102 | + ))} |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + |
| 106 | + {/* Tehai and Tsumohai */} |
| 107 | + <div className="flex items-center gap-4"> |
| 108 | + <div> |
| 109 | + <h3 className="text-lg mb-2">手牌</h3> |
| 110 | + <div className="flex gap-0"> |
| 111 | + {indexedTehai.map((hai) => ( |
| 112 | + <Form key={hai.index} method="post" action="/play/tedashi"> |
| 113 | + <input type="hidden" name="index" value={hai.index} /> |
| 114 | + <button type="submit"> |
| 115 | + <img |
| 116 | + src={`/hai/${hai.kind}_${hai.value}.png`} |
| 117 | + alt={`${hai.kind} ${hai.value}`} |
| 118 | + className="w-12 h-16 cursor-pointer hover:scale-105 transition-transform" |
| 119 | + /> |
| 120 | + </button> |
| 121 | + </Form> |
| 122 | + ))} |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + |
| 126 | + {tsumohai && ( |
| 127 | + <div> |
| 128 | + <h3 className="text-lg mb-2">ツモ牌</h3> |
| 129 | + <Form method="post" action="/play/tsumogiri"> |
| 130 | + <button type="submit"> |
| 131 | + <img |
| 132 | + src={`/hai/${tsumohai.kind}_${tsumohai.value}.png`} |
| 133 | + alt={`${tsumohai.kind} ${tsumohai.value}`} |
| 134 | + className="w-12 h-16 object-contain cursor-pointer hover:scale-105 transition-transform" |
| 135 | + /> |
| 136 | + </button> |
| 137 | + </Form> |
| 138 | + </div> |
| 139 | + )} |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + ); |
| 143 | +} |
0 commit comments