Skip to content

Commit f029aab

Browse files
tknkaaCopilot
andcommitted
Add manual haiyama seed script
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0bf7fa5 commit f029aab

3 files changed

Lines changed: 62 additions & 24 deletions

File tree

app/lib/game-service.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,7 @@ export async function getRandomHaiyamaOrCreate(
128128
return randomHaiyama[0];
129129
}
130130

131-
// No unused haiyama available, create a new one
132-
const generatedTiles = createShuffledHaiyama();
133-
const insertedHaiyama = await db
134-
.insert(haiyama)
135-
.values({ tiles: generatedTiles })
136-
.returning();
137-
138-
if (insertedHaiyama.length === 0) {
139-
throw new Error("Failed to create haiyama");
140-
}
141-
142-
return insertedHaiyama[0];
131+
throw new Error("No haiyama available; seed the database first");
143132
}
144133

145134
export async function seedHaiyama(db: DrizzleD1Database, count: number) {

app/routes/play.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { sql } from "drizzle-orm";
21
import { useEffect, useState } from "react";
32
import { type ShouldRevalidateFunctionArgs, useFetcher } from "react-router";
43
import { getAuth } from "~/lib/auth";
54
import { getDB } from "~/lib/db";
6-
import { haiyama } from "~/lib/db/schema";
75
import {
86
getGameState,
97
getRandomHaiyamaOrCreate,
108
initGame,
11-
seedHaiyama,
129
toGameState,
1310
} from "~/lib/game-service";
1411
import judgeAgari from "~/lib/hai/agari";
@@ -175,15 +172,6 @@ export async function loader({
175172
const userId = session.user.id;
176173

177174
try {
178-
// Auto-seed haiyama if empty
179-
const haiCount = await db
180-
.select({ count: sql<number>`count(*)` })
181-
.from(haiyama)
182-
.get();
183-
if (!haiCount || haiCount.count === 0) {
184-
await seedHaiyama(db, 40);
185-
}
186-
187175
// Check if game state already exists in D1
188176
const existingState = await getGameState(db, userId);
189177

@@ -209,6 +197,14 @@ export async function loader({
209197
if (error instanceof Response) {
210198
throw error;
211199
}
200+
if (
201+
error instanceof Error &&
202+
error.message === "No haiyama available; seed the database first"
203+
) {
204+
throw new Response("No haiyama available; seed the database first", {
205+
status: 503,
206+
});
207+
}
212208
throw error instanceof Error ? error : new Error(String(error));
213209
}
214210
}

drizzle/seed_haiyama.sql

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
BEGIN;
2+
3+
WITH RECURSIVE
4+
nums(n) AS (
5+
VALUES (1)
6+
UNION ALL
7+
SELECT n + 1 FROM nums WHERE n < 40
8+
),
9+
digits(value) AS (
10+
VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9)
11+
),
12+
copies(copy_no) AS (
13+
VALUES (1), (2), (3), (4)
14+
),
15+
tiles(kind, value) AS (
16+
SELECT 'manzu', value FROM digits CROSS JOIN copies
17+
UNION ALL
18+
SELECT 'pinzu', value FROM digits CROSS JOIN copies
19+
UNION ALL
20+
SELECT 'souzu', value FROM digits CROSS JOIN copies
21+
),
22+
shuffled AS (
23+
SELECT
24+
n,
25+
kind,
26+
value,
27+
ROW_NUMBER() OVER (PARTITION BY n ORDER BY RANDOM()) AS rn
28+
FROM nums
29+
CROSS JOIN tiles
30+
),
31+
picked AS (
32+
SELECT
33+
n,
34+
printf(
35+
'[%s]',
36+
group_concat(
37+
'{"kind":"' || kind || '","value":' || value || '}',
38+
','
39+
)
40+
) AS tiles_json
41+
FROM (
42+
SELECT n, kind, value, rn
43+
FROM shuffled
44+
WHERE rn <= 32
45+
ORDER BY n, rn
46+
)
47+
GROUP BY n
48+
)
49+
INSERT INTO haiyama (id, tiles)
50+
SELECT lower(hex(randomblob(16))), tiles_json
51+
FROM picked;
52+
53+
COMMIT;

0 commit comments

Comments
 (0)