Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pre-commit:
commands:
check:
glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}"
run: bunx biome format --write {staged_files}
run: bunx @biomejs/biome check --write {staged_files} && git add {staged_files}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "bun vite build",
"lint": "eslint .",
"preview": "bun vite preview",
"format": "bunx biome check . --write"
"format": "bunx @biomejs/biome check . --write"
},
"dependencies": {},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion rr/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NODE_ENV=development
DATABASE_URL=postgres://user:postgres@localhost:5432/
BETTER_AUTH_SECRET=
BETTER_AUTH_URL=http://localhost:3000
BETTER_AUTH_URL=http://localhost:5173
5 changes: 4 additions & 1 deletion rr/app/lib/auth-client.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import { anonymousClient } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient({});
export const authClient = createAuthClient({
plugins: [anonymousClient()],
});
14 changes: 14 additions & 0 deletions rr/app/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { anonymous } from "better-auth/plugins";
import { eq } from "drizzle-orm";
import { getDB } from "./db";

export function getAuth(env?: Env) {
Expand All @@ -10,6 +12,18 @@ export function getAuth(env?: Env) {
emailAndPassword: {
enabled: true,
},
plugins: [
anonymous({
onLinkAccount: async ({ anonymousUser, newUser }) => {
const db = getDB(env);
// migrate like this
// await db
// .update(playHistory)
// .set({ userId: newUser.user.id })
// .where(eq(playHistory.userId, anonymousUser.user.id));
},
}),
],
});
return auth;
}
Expand Down
58 changes: 52 additions & 6 deletions rr/app/lib/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,64 @@
import { sql } from "drizzle-orm";
import {
boolean,
check,
index,
integer,
pgEnum,
pgTable,
primaryKey,
serial,
text,
timestamp,
varchar,
} from "drizzle-orm/pg-core";

export const usersTable = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: varchar({ length: 255 }).notNull(),
age: integer().notNull(),
email: varchar({ length: 255 }).notNull().unique(),
export const haiyama = pgTable("haiyama", {
id: text("id").primaryKey(),
});

export const haiKindEnum = pgEnum("hai_kind", [
"manzu",
"pinzu",
"souzu",
"jihai",
]);

export const hai = pgTable("hai", {
id: serial("id").primaryKey(),
haiyamaId: text("haiyama_id")
.notNull()
.references(() => haiyama.id, { onDelete: "cascade" }),
kind: haiKindEnum("kind").notNull(), // "manzu" | "pinzu" | "souzu" | "jihai"
value: text("value").notNull(), // 1~9 or "ton" ~ "tyun"
order: integer("order").notNull(), // 0~17
index: integer("index").notNull(), // haiToIndex
});

// relation between user and haiyama
// TODO: index
export const kyoku = pgTable(
"kyoku",
{
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
haiyamaId: text("haiyama_id")
.notNull()
.references(() => haiyama.id, { onDelete: "cascade" }),
didAgari: boolean("did_agari").notNull(),
agariJunme: integer("agari_junme"),
},
(table) => [
primaryKey({ columns: [table.userId, table.haiyamaId] }),
index("kyoku_user_id_idx").on(table.userId),
index("kyoku_haiyama_id_idx").on(table.haiyamaId),
check(
"agari_consistency",
sql`(${table.didAgari} = false) OR (${table.didAgari} = true AND ${table.agariJunme} IS NOT NULL)`,
),
],
);

// better-auth
export const user = pgTable("user", {
id: text("id").primaryKey(),
Expand All @@ -26,6 +71,7 @@ export const user = pgTable("user", {
.defaultNow()
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
isAnonymous: boolean("is_anonymous"),
});

export const session = pgTable("session", {
Expand Down
19 changes: 19 additions & 0 deletions apps/shared/hai.ts → rr/app/lib/hai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,22 @@ export function constructHai(kind: HaiKind, value: number | JihaiValue): Hai {
export function sortTehai(haiArray: Hai[]): Hai[] {
return haiArray.sort((a, b) => haiToIndex(a) - haiToIndex(b));
}

// To store hai in DB
export type DBHai = {
haiyamaId: string;
kind: HaiKind;
value: string;
order: number;
index: number;
};

export function haiToDBHai(hai: Hai, haiyamaId: string, order: number): DBHai {
return {
haiyamaId,
kind: hai.kind,
value: String(hai.value),
order,
index: haiToIndex(hai),
};
}