Skip to content

Commit 7d4100d

Browse files
committed
牌山を生成 (#108)
1 parent 3f7a5f8 commit 7d4100d

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

app/lib/db/seed.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { drizzle } from "drizzle-orm/node-postgres";
2+
import { Client } from "pg";
3+
import { v4 as uuidv4 } from "uuid";
4+
import type { Hai, SuhaiKind } from "../hai";
5+
import { constructHai, haiToDBHai } from "../hai";
6+
import { hai, haiyama } from "./schema";
7+
8+
function shuffleArray<T>(array: T[]): T[] {
9+
for (let i = array.length - 1; i > 0; i--) {
10+
const j = Math.floor(Math.random() * (i + 1));
11+
const temp = array[i];
12+
if (temp !== undefined && array[j] !== undefined) {
13+
array[i] = array[j];
14+
array[j] = temp;
15+
}
16+
}
17+
return array;
18+
}
19+
20+
function createHaiyama(): Hai[] {
21+
const kinds: SuhaiKind[] = ["manzu", "pinzu", "souzu"];
22+
let sortedHaiyama: Hai[] = [];
23+
24+
for (const kind of kinds) {
25+
for (let value = 1; value < 10; value++) {
26+
sortedHaiyama = sortedHaiyama.concat(
27+
new Array<Hai>(4).fill(constructHai(kind, value)),
28+
);
29+
}
30+
}
31+
return shuffleArray(sortedHaiyama);
32+
}
33+
34+
async function main() {
35+
const client = new Client({
36+
connectionString: process.env.DATABASE_URL,
37+
});
38+
await client.connect();
39+
const db = drizzle(client);
40+
41+
const newHaiyamaId = uuidv4();
42+
const haiyamaData = createHaiyama();
43+
44+
await db.insert(haiyama).values({ id: newHaiyamaId });
45+
46+
const haiData = haiyamaData.map((h, i) => haiToDBHai(h, newHaiyamaId, i));
47+
48+
await db.insert(hai).values(haiData);
49+
50+
console.log("Seeding complete");
51+
await client.end();
52+
}
53+
54+
main().catch((err) => {
55+
console.error(err);
56+
process.exit(1);
57+
});

bun.lock

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"react-dom": "^19.1.1",
1515
"react-router": "^7.9.2",
1616
"redis": "^5.9.0",
17-
"zustand": "^5.0.8",
1817
},
1918
"devDependencies": {
2019
"@cloudflare/vite-plugin": "^1.13.5",
@@ -25,11 +24,13 @@
2524
"@types/react": "^19.1.13",
2625
"@types/react-dom": "^19.1.9",
2726
"@types/redis": "^4.0.11",
27+
"@types/uuid": "^11.0.0",
2828
"daisyui": "^5.3.10",
2929
"drizzle-kit": "^0.31.5",
3030
"lefthook": "^2.0.2",
3131
"tailwindcss": "^4.1.13",
3232
"typescript": "^5.9.2",
33+
"uuid": "^13.0.0",
3334
"vite": "^7.1.7",
3435
"vite-tsconfig-paths": "^5.1.4",
3536
"wrangler": "^4.45.0",
@@ -391,6 +392,8 @@
391392

392393
"@types/redis": ["@types/[email protected]", "", { "dependencies": { "redis": "*" } }, "sha512-bI+gth8La8Wg/QCR1+V1fhrL9+LZUSWfcqpOj2Kc80ZQ4ffbdL173vQd5wovmoV9i071FU9oP2g6etLuEwb6Rg=="],
393394

395+
"@types/uuid": ["@types/[email protected]", "", { "dependencies": { "uuid": "*" } }, "sha512-HVyk8nj2m+jcFRNazzqyVKiZezyhDKrGUA3jlEcg/nZ6Ms+qHwocba1Y/AaVaznJTAM9xpdFSh+ptbNrhOGvZA=="],
396+
394397
"acorn": ["[email protected]", "", { "bin": { "acorn": "bin/acorn" } }, "sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA=="],
395398

396399
"acorn-walk": ["[email protected]", "", {}, "sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A=="],
@@ -761,6 +764,8 @@
761764

762765
"update-browserslist-db": ["[email protected]", "", { "dependencies": { "escalade": "^3.2.0", "picocolors": "^1.1.1" }, "peerDependencies": { "browserslist": ">= 4.21.0" }, "bin": { "update-browserslist-db": "cli.js" } }, "sha512-q0SPT4xyU84saUX+tomz1WLkxUbuaJnR1xWt17M7fJtEJigJeWUNGUqrauFXsHnqev9y9JTRGwk13tFBuKby4A=="],
763766

767+
"uuid": ["[email protected]", "", { "bin": { "uuid": "dist-node/bin/uuid" } }, "sha512-XQegIaBTVUjSHliKqcnFqYypAd4S+WCYt5NIeRs6w/UAry7z8Y9j5ZwRRL4kzq9U3sD6v+85er9FvkEaBpji2w=="],
768+
764769
"valibot": ["[email protected]", "", { "peerDependencies": { "typescript": ">=5" }, "optionalPeers": ["typescript"] }, "sha512-Nk8lX30Qhu+9txPYTwM0cFlWLdPFsFr6LblzqIySfbZph9+BFsAHsNvHOymEviUepeIW6KFHzpX8TKhbptBXXw=="],
765770

766771
"validate-npm-package-license": ["[email protected]", "", { "dependencies": { "spdx-correct": "^3.0.0", "spdx-expression-parse": "^3.0.0" } }, "sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew=="],
@@ -795,8 +800,6 @@
795800

796801
"zod": ["[email protected]", "", {}, "sha512-JInaHOamG8pt5+Ey8kGmdcAcg3OL9reK8ltczgHTAwNhMys/6ThXHityHxVV2p3fkw/c+MAvBHFVYHFZDmjMCQ=="],
797802

798-
"zustand": ["[email protected]", "", { "peerDependencies": { "@types/react": ">=18.0.0", "immer": ">=9.0.6", "react": ">=18.0.0", "use-sync-external-store": ">=1.2.0" }, "optionalPeers": ["@types/react", "immer", "react", "use-sync-external-store"] }, "sha512-gyPKpIaxY9XcO2vSMrLbiER7QMAMGOQZVRdJ6Zi782jkbzZygq5GI9nG8g+sMgitRtndwaBSl7uiqC49o1SSiw=="],
799-
800803
"@babel/core/semver": ["[email protected]", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA=="],
801804

802805
"@babel/helper-compilation-targets/semver": ["[email protected]", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA=="],

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"build": "react-router build",
77
"cf-typegen": "wrangler types",
88
"deploy": "bun run build && wrangler deploy",
9+
"db:seed": "bun run app/lib/db/seed.ts",
910
"dev": "react-router dev",
1011
"postinstall": "npm run cf-typegen",
1112
"preview": "bun run build && vite preview",
@@ -33,11 +34,13 @@
3334
"@types/react": "^19.1.13",
3435
"@types/react-dom": "^19.1.9",
3536
"@types/redis": "^4.0.11",
37+
"@types/uuid": "^11.0.0",
3638
"daisyui": "^5.3.10",
3739
"drizzle-kit": "^0.31.5",
3840
"lefthook": "^2.0.2",
3941
"tailwindcss": "^4.1.13",
4042
"typescript": "^5.9.2",
43+
"uuid": "^13.0.0",
4144
"vite": "^7.1.7",
4245
"vite-tsconfig-paths": "^5.1.4",
4346
"wrangler": "^4.45.0"

0 commit comments

Comments
 (0)