diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index cd15247..3f0fdbb 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ worker-configuration.d.ts !.dev.vars.example !.env.example + +.direnv diff --git a/old-workspaces/web/src/utils/judgeAgari.ts b/app/lib/hai/judgeAgari.ts similarity index 96% rename from old-workspaces/web/src/utils/judgeAgari.ts rename to app/lib/hai/judgeAgari.ts index 7a0b590..ec31626 100644 --- a/old-workspaces/web/src/utils/judgeAgari.ts +++ b/app/lib/hai/judgeAgari.ts @@ -1,4 +1,4 @@ -import { type Hai, haiToIndex } from "shared/hai.ts"; +import { type Hai, haiToIndex } from "./utils"; function deleteSyuntsu(remainingTehai: TehaiIndex): number { let extractCount = 0; diff --git a/app/lib/hai.ts b/app/lib/hai/utils.ts similarity index 100% rename from app/lib/hai.ts rename to app/lib/hai/utils.ts diff --git a/app/lib/redis.ts b/app/lib/redis.ts index e34464e..0df3ca0 100644 --- a/app/lib/redis.ts +++ b/app/lib/redis.ts @@ -1,6 +1,6 @@ import { createClient } from "redis"; -import type { Hai } from "./hai"; -import { sortTehai } from "./hai"; +import type { Hai } from "./hai/utils"; +import { sortTehai } from "./hai/utils"; export function getRedisClient(env: Env) { const client = createClient({ @@ -73,8 +73,9 @@ export const tedashi = async ( if (index < 0 || 12 < index) { throw new Error("index out of tehai length"); } - const deletedTehai = state.tehai.filter((_, i) => i !== index); - const discardedHai = state.tehai[index]; + const sortedTehai = sortTehai(state.tehai); + const deletedTehai = sortedTehai.filter((_, i) => i !== index); + const discardedHai = sortedTehai[index]; const newGameState: GameState = { ...state, diff --git a/app/routes/_index.tsx b/app/routes/_index.tsx index 6443792..2dfbe27 100644 --- a/app/routes/_index.tsx +++ b/app/routes/_index.tsx @@ -1,32 +1,14 @@ -import { drizzle as drizzleNeon } from "drizzle-orm/neon-http"; -import { drizzle as drizzlePg } from "drizzle-orm/node-postgres"; -import type { c } from "node_modules/better-auth/dist/shared/better-auth.C9FmyZ5W.cjs"; import { Link, useNavigate } from "react-router"; import { authClient } from "~/lib/auth-client"; -import { getRedisClient } from "~/lib/redis"; -import type { Route } from "./+types/_index"; - -export async function loader({ context }: Route.LoaderArgs) { - const { env } = context.cloudflare; - const db = - env.NODE_ENV === "development" - ? drizzlePg(env.DATABASE_URL) - : drizzleNeon(env.DATABASE_URL); - const redisClient = getRedisClient(env); - redisClient.on("error", (err) => console.log("Redis Client Error", err)); - await redisClient.connect(); - await redisClient.set("key", "value"); - const value = await redisClient.get("key"); - console.log(value); -} export default function Page() { const navigate = useNavigate(); const anonymousLoginAndStart = async () => { const user = await authClient.getSession(); - if (!user) { - const _user = await authClient.signIn.anonymous(); + if (user.data) { + const _user = await authClient.signOut(); } + const _user = await authClient.signIn.anonymous(); navigate("/play"); }; return ( diff --git a/app/routes/play.agari.ts b/app/routes/play.agari.ts new file mode 100644 index 0000000..e69de29 diff --git a/app/routes/play.ryukyoku.ts b/app/routes/play.ryukyoku.ts new file mode 100644 index 0000000..e69de29 diff --git a/app/routes/play.tsx b/app/routes/play.tsx index 3794876..bab529f 100644 --- a/app/routes/play.tsx +++ b/app/routes/play.tsx @@ -3,7 +3,8 @@ import { Form } from "react-router"; import { getAuth } from "~/lib/auth"; import { getDB } from "~/lib/db"; import { hai, haiyama } from "~/lib/db/schema"; -import { dbHaiToHai, sortTehai } from "~/lib/hai"; +import judgeAgari from "~/lib/hai/judgeAgari"; +import { dbHaiToHai, sortTehai } from "~/lib/hai/utils"; import { type GameState, getRedisClient, init } from "~/lib/redis"; import type { Route } from "./+types/play"; @@ -73,6 +74,9 @@ export async function loader({ export default function Page({ loaderData }: Route.ComponentProps) { let { haiyama, sutehai, tsumohai, junme, kyoku, tehai } = loaderData; tehai = sortTehai(tehai); + const isAgari = + tehai && tsumohai ? judgeAgari(sortTehai([...tehai, tsumohai])) : false; + const isRyukyoku = junme === 19; const indexedSutehai = sutehai.map((hai, index) => ({ ...hai, index: index, @@ -84,6 +88,36 @@ export default function Page({ loaderData }: Route.ComponentProps) { return (
Play Page - 局 {kyoku} 巡目 {junme}
diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..6967c77 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1763806073, + "narHash": "sha256-FHsEKDvfWpzdADWj99z7vBk4D716Ujdyveo5+A048aI=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "878e468e02bfabeda08c79250f7ad583037f2227", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..b7ebe54 --- /dev/null +++ b/flake.nix @@ -0,0 +1,20 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = + { nixpkgs, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + devShells.default = pkgs.mkShell { + packages = with pkgs; [ redis ]; + }; + } + ); +}