@@ -3,9 +3,10 @@ import { Form } from "react-router";
33import { getAuth } from "~/lib/auth" ;
44import { getDB } from "~/lib/db" ;
55import { haiyama } from "~/lib/db/schema" ;
6+ import type { GameState } from "~/lib/do" ;
7+ import getDOStub from "~/lib/do" ;
68import judgeAgari from "~/lib/hai/judgeAgari" ;
79import { sortTehai } from "~/lib/hai/utils" ;
8- import { type GameState , getRedisClient , init } from "~/lib/redis" ;
910import type { Route } from "./+types/play" ;
1011
1112export async function loader ( {
@@ -23,16 +24,14 @@ export async function loader({
2324 const userId = session . user . id ;
2425
2526 // Check if game state already exists in Redis
26- const redisClient = getRedisClient ( env ) ;
27- await redisClient . connect ( ) ;
27+ const stub = getDOStub ( env , userId ) ;
2828
2929 try {
30- const existingGameState = await redisClient . get ( `user: ${ userId } :game` ) ;
30+ const existingGameState = await stub . getCurrentGameState ( ) ;
3131
3232 if ( existingGameState ) {
3333 // Return existing game state from Redis
34- await redisClient . quit ( ) ;
35- return JSON . parse ( existingGameState ) ;
34+ return existingGameState ;
3635 }
3736
3837 // No existing game state, so initialize from PostgreSQL
@@ -43,22 +42,20 @@ export async function loader({
4342 . limit ( 1 ) ;
4443
4544 if ( randomHaiyama . length === 0 ) {
46- await redisClient . quit ( ) ;
4745 throw new Response ( "No haiyama found" , { status : 404 } ) ;
4846 }
4947
5048 const haiData = randomHaiyama [ 0 ] . tiles ;
5149 // Initialize game state in Redis
52- await init ( redisClient , userId , haiData ) ;
50+ await stub . init ( haiData ) ;
5351
5452 // Get the initialized game state to return
55- const gameStateJSON = await redisClient . get ( `user: ${ userId } :game` ) ;
56- const gameState = gameStateJSON ? JSON . parse ( gameStateJSON ) : null ;
57-
58- await redisClient . quit ( ) ;
53+ const gameState = await stub . getCurrentGameState ( ) ;
54+ if ( ! gameState ) {
55+ throw new Error ( "Failed to get current game state" ) ;
56+ }
5957 return gameState ;
6058 } catch ( error ) {
61- await redisClient . quit ( ) ;
6259 throw error instanceof Error ? error : new Error ( String ( error ) ) ;
6360 }
6461}
0 commit comments