Skip to content

Commit 0465c09

Browse files
committed
define schema
1 parent e470161 commit 0465c09

File tree

3 files changed

+151
-88
lines changed

3 files changed

+151
-88
lines changed

apps/shared/hai.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

rr/app/lib/db/schema.ts

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,65 @@
1+
import { sql } from "drizzle-orm";
12
import {
2-
boolean,
3+
check,
34
integer,
45
pgTable,
6+
serial,
57
text,
68
timestamp,
7-
varchar,
9+
boolean,
10+
primaryKey,
11+
pgEnum,
12+
index,
813
} from "drizzle-orm/pg-core";
914

10-
export const usersTable = pgTable("users", {
11-
id: integer().primaryKey().generatedAlwaysAsIdentity(),
12-
name: varchar({ length: 255 }).notNull(),
13-
age: integer().notNull(),
14-
email: varchar({ length: 255 }).notNull().unique(),
15+
export const haiyama = pgTable("haiyama", {
16+
id: text("id").primaryKey(),
1517
});
1618

17-
// better-auth
19+
export const haiKindEnum = pgEnum("hai_kind", [
20+
"manzu",
21+
"pinzu",
22+
"souzu",
23+
"jihai",
24+
]);
1825

26+
export const hai = pgTable("hai", {
27+
id: serial("id").primaryKey(),
28+
haiyamaId: text("haiyama_id")
29+
.notNull()
30+
.references(() => haiyama.id, { onDelete: "cascade" }),
31+
kind: haiKindEnum("kind").notNull(), // "manzu" | "pinzu" | "souzu" | "jihai"
32+
value: text("value").notNull(), // 1~9 or "ton" ~ "tyun"
33+
order: integer("order").notNull(), // 0~17
34+
index: integer("index").notNull(), // haiToIndex
35+
});
36+
37+
// relation between user and haiyama
38+
// TODO: index
39+
export const kyoku = pgTable(
40+
"kyoku",
41+
{
42+
userId: text("user_id")
43+
.notNull()
44+
.references(() => user.id, { onDelete: "cascade" }),
45+
haiyamaId: text("haiyama_id")
46+
.notNull()
47+
.references(() => haiyama.id, { onDelete: "cascade" }),
48+
didAgari: boolean("did_agari").notNull(),
49+
agariJunme: integer("agari_junme"),
50+
},
51+
(table) => [
52+
primaryKey({ columns: [table.userId, table.haiyamaId] }),
53+
index("kyoku_user_id_idx").on(table.userId),
54+
index("kyoku_haiyama_id_idx").on(table.haiyamaId),
55+
check(
56+
"agari_consistency",
57+
sql`(${table.didAgari} = false) OR (${table.didAgari} = true AND ${table.agariJunme} IS NOT NULL)`,
58+
),
59+
],
60+
);
61+
62+
// better-auth
1963
export const user = pgTable("user", {
2064
id: text("id").primaryKey(),
2165
name: text("name").notNull(),

rr/app/lib/hai.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
export type Hai =
2+
| {
3+
kind: SuhaiKind;
4+
value: number;
5+
}
6+
| {
7+
kind: "jihai";
8+
value: JihaiValue;
9+
};
10+
11+
export type SuhaiKind = "manzu" | "pinzu" | "souzu";
12+
export type HaiKind = SuhaiKind | "jihai";
13+
14+
export type HaiWithID = {
15+
kind: HaiKind;
16+
value: JihaiValue | number;
17+
id: number;
18+
};
19+
20+
const kindOrder: HaiKind[] = ["manzu", "pinzu", "souzu", "jihai"];
21+
22+
const jihaiOrder: JihaiValue[] = [
23+
"ton",
24+
"nan",
25+
"sya",
26+
"pei",
27+
"haku",
28+
"hatsu",
29+
"tyun",
30+
];
31+
32+
export function haiToIndex(hai: Hai): number {
33+
if (hai.kind === "jihai") {
34+
return 27 + 1 + jihaiOrder.indexOf(hai.value);
35+
}
36+
return 9 * kindOrder.indexOf(hai.kind) + hai.value;
37+
}
38+
39+
export function indexToHai(index: number): Hai {
40+
if (index <= 0 || index > 34) {
41+
throw new Error("invalid index");
42+
}
43+
const hai = constructHai(
44+
kindOrder[Math.floor(index / 9)],
45+
Math.floor(index / 9) === 4 ? jihaiOrder[index % 9] : index % 9,
46+
);
47+
return hai;
48+
}
49+
50+
export type JihaiValue =
51+
| "ton"
52+
| "nan"
53+
| "sya"
54+
| "pei"
55+
| "haku"
56+
| "hatsu"
57+
| "tyun";
58+
59+
export function constructHai(kind: HaiKind, value: number | JihaiValue): Hai {
60+
if (kind === "manzu" || kind === "pinzu" || kind === "souzu") {
61+
if (typeof value === "number") {
62+
return {
63+
kind,
64+
value,
65+
};
66+
}
67+
throw new Error("suhai requires number");
68+
}
69+
if (typeof value === "string") {
70+
return {
71+
kind,
72+
value,
73+
};
74+
}
75+
throw new Error("jihai requires JihaiValue");
76+
}
77+
78+
export function sortTehai(haiArray: Hai[]): Hai[] {
79+
return haiArray.sort((a, b) => haiToIndex(a) - haiToIndex(b));
80+
}
81+
82+
// To store hai in DB
83+
export type DBHai = {
84+
haiyamaId: string;
85+
kind: HaiKind;
86+
value: string;
87+
order: number;
88+
index: number;
89+
};
90+
91+
export function haiToDBHai(hai: Hai, haiyamaId: string, order: number): DBHai {
92+
return {
93+
haiyamaId,
94+
kind: hai.kind,
95+
value: String(hai.value),
96+
order,
97+
index: haiToIndex(hai),
98+
};
99+
}

0 commit comments

Comments
 (0)