Skip to content

Commit c0b4853

Browse files
authored
Merge pull request #167 from ut-code/refactor
refactor
2 parents 6b8344c + af96f51 commit c0b4853

17 files changed

Lines changed: 856 additions & 759 deletions

app/lib/game-service.ts

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,7 @@ import type { DrizzleD1Database } from "drizzle-orm/d1";
33
import { gameState, haiyama, kyoku } from "~/lib/db/schema";
44
import type { Hai } from "~/lib/hai/types";
55
import { 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

218
export 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

14281
export 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]] : [];

app/lib/types.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Hai } from "./hai/types";
22

3-
export interface GameState {
3+
export type GameState = {
44
kyoku: number;
55
junme: number;
66
remainTsumo: number;
@@ -9,4 +9,17 @@ export interface GameState {
99
sutehai: Hai[];
1010
tehai: Hai[];
1111
tsumohai: Hai | null;
12-
}
12+
};
13+
14+
export type GameStateRecord = {
15+
userId: string;
16+
kyoku: number;
17+
junme: number;
18+
remainTsumo: number;
19+
score: number;
20+
haiyama: Hai[];
21+
sutehai: Hai[];
22+
tehai: Hai[];
23+
tsumohai: Hai[];
24+
haiyamaId: string | null;
25+
};

app/routes/_index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ export default function Page() {
77
const navigate = useNavigate();
88
const anonymousLoginAndStart = async () => {
99
const user = await authClient.getSession();
10-
if (user.data) {
11-
await authClient.signOut();
10+
if (!user.data) {
11+
await authClient.signIn.anonymous();
1212
}
13-
await authClient.signIn.anonymous();
1413
navigate("/play");
1514
};
1615
return (

app/routes/learn.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect } from "react";
22
import { Link, useLocation } from "react-router";
3-
import BasicRule from "~/lib/components/BasicRule";
4-
import LocalRule from "~/lib/components/LocalRule";
3+
import BasicRule from "./learn/BasicRule";
4+
import LocalRule from "./learn/LocalRule";
55

66
export default function Page() {
77
const location = useLocation();

0 commit comments

Comments
 (0)