Skip to content

Commit 94c5a1c

Browse files
committed
load haiyama
1 parent d55686e commit 94c5a1c

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

app/lib/auth.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { betterAuth } from "better-auth";
22
import { drizzleAdapter } from "better-auth/adapters/drizzle";
33
import { anonymous } from "better-auth/plugins";
4-
import { eq } from "drizzle-orm";
54
import * as schema from "../lib/db/schema";
65
import { getDB } from "./db";
76

@@ -29,5 +28,5 @@ export function getAuth(env?: Env) {
2928
});
3029
return auth;
3130
}
32-
33-
export const auth = getAuth();
31+
// This is for @better-auth/cli
32+
// export const auth = getAuth();

app/lib/hai.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,17 @@ export function haiToDBHai(hai: Hai, haiyamaId: string, order: number): DBHai {
9797
index: haiToIndex(hai),
9898
};
9999
}
100+
101+
export function dbHaiToHai(dbHai: DBHai): Hai {
102+
if (dbHai.kind === "jihai") {
103+
return {
104+
kind: dbHai.kind,
105+
value: dbHai.value as JihaiValue,
106+
};
107+
} else {
108+
return {
109+
kind: dbHai.kind as SuhaiKind,
110+
value: Number(dbHai.value),
111+
};
112+
}
113+
}

app/routes/_index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export async function loader({ context }: Route.LoaderArgs) {
2323
export default function Page() {
2424
const navigate = useNavigate();
2525
const anonymousLoginAndStart = async () => {
26-
const _user = await authClient.signIn.anonymous();
26+
const user = await authClient.getSession();
27+
if (!user) {
28+
const _user = await authClient.signIn.anonymous();
29+
}
2730
navigate("/play");
2831
};
2932
return (

app/routes/play.tsx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
1-
export default function Page() {
1+
import { sql } from "drizzle-orm";
2+
import { getAuth } from "~/lib/auth";
3+
import { getDB } from "~/lib/db";
4+
import { hai, haiyama } from "~/lib/db/schema";
5+
import { dbHaiToHai } from "~/lib/hai";
6+
import type { Route } from "./+types/play";
7+
8+
export async function loader({ context, request }: Route.LoaderArgs) {
9+
const { env } = context.cloudflare;
10+
const db = getDB(env);
11+
const auth = getAuth(env);
12+
const session = await auth.api.getSession({ headers: request.headers });
13+
if (!session?.user?.id) {
14+
throw new Response("Unauthorized", { status: 401 });
15+
}
16+
const userId = session.user.id;
17+
18+
const randomHaiyama = await db
19+
.select()
20+
.from(haiyama)
21+
.orderBy(sql`RANDOM()`)
22+
.limit(1);
23+
24+
if (randomHaiyama.length === 0) {
25+
throw new Response("No haiyama found", { status: 404 });
26+
}
27+
const selectedHaiyama = randomHaiyama[0];
28+
const rawHaiData = await db
29+
.select()
30+
.from(hai)
31+
.where(sql`${hai.haiyamaId} = ${selectedHaiyama.id}`)
32+
.orderBy(hai.order);
33+
const haiData = rawHaiData.map((hai) => dbHaiToHai(hai));
34+
//TODO: store data in redis
35+
return haiData;
36+
}
37+
38+
export default function Page({ loaderData }: Route.ComponentProps) {
39+
console.log(loaderData);
240
return (
341
<>
442
<p>Play Page</p>

0 commit comments

Comments
 (0)