@@ -3,20 +3,7 @@ import type { DrizzleD1Database } from "drizzle-orm/d1";
33import { gameState , haiyama , kyoku } from "~/lib/db/schema" ;
44import type { Hai } from "~/lib/hai/types" ;
55import { sortTehai } from "~/lib/hai/types" ;
6- import type { GameState } from "~/lib/types" ;
7-
8- export interface GameStateRecord {
9- userId : string ;
10- kyoku : number ;
11- junme : number ;
12- remainTsumo : number ;
13- score : number ;
14- haiyama : Hai [ ] ;
15- sutehai : Hai [ ] ;
16- tehai : Hai [ ] ;
17- tsumohai : Hai [ ] ;
18- haiyamaId : string | null ;
19- }
6+ import type { GameState , GameStateRecord } from "~/lib/types" ;
207
218export async function getGameState (
229 db : DrizzleD1Database ,
@@ -40,6 +27,7 @@ export async function initGame(
4027 const tehai = tiles . slice ( 0 , 13 ) ;
4128 const tsumohai = tiles [ 13 ] ? [ tiles [ 13 ] ] : [ ] ;
4229 const remainingHai = tiles . slice ( 14 ) ;
30+ await db . delete ( kyoku ) . where ( eq ( kyoku . userId , userId ) ) ;
4331
4432 await db
4533 . insert ( gameState )
@@ -71,72 +59,23 @@ export async function initGame(
7159 } ) ;
7260}
7361
74- /**
75- * Create a shuffled haiyama with only suited tiles (no jihai)
76- * Each haiyama has exactly `tileCount` tiles (default 32)
77- */
78- export function createShuffledHaiyama ( tileCount = 32 ) : Hai [ ] {
79- const tiles : Hai [ ] = [ ] ;
80-
81- for ( const kind of [ "manzu" , "pinzu" , "souzu" ] as const ) {
82- for ( let value = 1 ; value <= 9 ; value += 1 ) {
83- for ( let i = 0 ; i < 4 ; i += 1 ) {
84- tiles . push ( { kind, value } ) ;
85- }
86- }
87- }
88-
89- // Shuffle
90- for ( let i = tiles . length - 1 ; i > 0 ; i -= 1 ) {
91- const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
92- [ tiles [ i ] , tiles [ j ] ] = [ tiles [ j ] , tiles [ i ] ] ;
93- }
94-
95- return tiles . slice ( 0 , tileCount ) ;
96- }
97-
98- export async function getRandomHaiyamaOrCreate (
99- db : DrizzleD1Database ,
100- userId : string ,
101- ) {
102- // Get haiyama IDs the user has already played
62+ export async function getRandomHaiyama ( db : DrizzleD1Database , userId : string ) {
10363 const playedHaiyama = await db
10464 . select ( { haiyamaId : kyoku . haiyamaId } )
10565 . from ( kyoku )
10666 . where ( eq ( kyoku . userId , userId ) ) ;
10767
10868 const playedIds = playedHaiyama . map ( ( r ) => r . haiyamaId ) ;
10969
110- // Select a random haiyama that the user hasn't played yet
111- let randomHaiyama : ( typeof haiyama . $inferSelect ) [ ] ;
11270 if ( playedIds . length > 0 ) {
113- randomHaiyama = await db
71+ return await db
11472 . select ( )
11573 . from ( haiyama )
11674 . where ( notInArray ( haiyama . id , playedIds ) )
11775 . orderBy ( sql `RANDOM()` )
11876 . limit ( 1 ) ;
119- } else {
120- randomHaiyama = await db
121- . select ( )
122- . from ( haiyama )
123- . orderBy ( sql `RANDOM()` )
124- . limit ( 1 ) ;
12577 }
126-
127- if ( randomHaiyama . length > 0 ) {
128- return randomHaiyama [ 0 ] ;
129- }
130-
131- throw new Error ( "No haiyama available; seed the database first" ) ;
132- }
133-
134- export async function seedHaiyama ( db : DrizzleD1Database , count : number ) {
135- const values = Array . from ( { length : count } , ( ) => ( {
136- tiles : createShuffledHaiyama ( 32 ) ,
137- } ) ) ;
138- await db . insert ( haiyama ) . values ( values ) ;
139- return count ;
78+ return await db . select ( ) . from ( haiyama ) . orderBy ( sql `RANDOM()` ) . limit ( 1 ) ;
14079}
14180
14281export async function tedashi (
@@ -238,10 +177,10 @@ export async function restartGame(
238177 return { newKyoku, isGameOver : true } ;
239178 }
240179
241- const randomHaiyama = await getRandomHaiyamaOrCreate ( db , userId ) ;
180+ const randomHaiyama = await getRandomHaiyama ( db , userId ) ;
242181
243- const newHaiyama = randomHaiyama . tiles ;
244- const newHaiyamaId = randomHaiyama . id ;
182+ const newHaiyama = randomHaiyama [ 0 ] . tiles ;
183+ const newHaiyamaId = randomHaiyama [ 0 ] . id ;
245184
246185 const tehai = newHaiyama . slice ( 0 , 13 ) ;
247186 const tsumohai = newHaiyama [ 13 ] ? [ newHaiyama [ 13 ] ] : [ ] ;
0 commit comments