Skip to content

Commit 78a8fbb

Browse files
authored
Merge pull request #110 from ut-code/judge-agari
アガり判定 & 流局
2 parents 4d60966 + 8886462 commit 78a8fbb

File tree

11 files changed

+128
-27
lines changed

11 files changed

+128
-27
lines changed

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ worker-configuration.d.ts
1616

1717
!.dev.vars.example
1818
!.env.example
19+
20+
.direnv

old-workspaces/web/src/utils/judgeAgari.ts renamed to app/lib/hai/judgeAgari.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Hai, haiToIndex } from "shared/hai.ts";
1+
import { type Hai, haiToIndex } from "./utils";
22

33
function deleteSyuntsu(remainingTehai: TehaiIndex): number {
44
let extractCount = 0;
File renamed without changes.

app/lib/redis.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createClient } from "redis";
2-
import type { Hai } from "./hai";
3-
import { sortTehai } from "./hai";
2+
import type { Hai } from "./hai/utils";
3+
import { sortTehai } from "./hai/utils";
44

55
export function getRedisClient(env: Env) {
66
const client = createClient({
@@ -73,8 +73,9 @@ export const tedashi = async (
7373
if (index < 0 || 12 < index) {
7474
throw new Error("index out of tehai length");
7575
}
76-
const deletedTehai = state.tehai.filter((_, i) => i !== index);
77-
const discardedHai = state.tehai[index];
76+
const sortedTehai = sortTehai(state.tehai);
77+
const deletedTehai = sortedTehai.filter((_, i) => i !== index);
78+
const discardedHai = sortedTehai[index];
7879

7980
const newGameState: GameState = {
8081
...state,

app/routes/_index.tsx

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1-
import { drizzle as drizzleNeon } from "drizzle-orm/neon-http";
2-
import { drizzle as drizzlePg } from "drizzle-orm/node-postgres";
3-
import type { c } from "node_modules/better-auth/dist/shared/better-auth.C9FmyZ5W.cjs";
41
import { Link, useNavigate } from "react-router";
52
import { authClient } from "~/lib/auth-client";
6-
import { getRedisClient } from "~/lib/redis";
7-
import type { Route } from "./+types/_index";
8-
9-
export async function loader({ context }: Route.LoaderArgs) {
10-
const { env } = context.cloudflare;
11-
const db =
12-
env.NODE_ENV === "development"
13-
? drizzlePg(env.DATABASE_URL)
14-
: drizzleNeon(env.DATABASE_URL);
15-
const redisClient = getRedisClient(env);
16-
redisClient.on("error", (err) => console.log("Redis Client Error", err));
17-
await redisClient.connect();
18-
await redisClient.set("key", "value");
19-
const value = await redisClient.get("key");
20-
console.log(value);
21-
}
223

234
export default function Page() {
245
const navigate = useNavigate();
256
const anonymousLoginAndStart = async () => {
267
const user = await authClient.getSession();
27-
if (!user) {
28-
const _user = await authClient.signIn.anonymous();
8+
if (user.data) {
9+
const _user = await authClient.signOut();
2910
}
11+
const _user = await authClient.signIn.anonymous();
3012
navigate("/play");
3113
};
3214
return (

app/routes/play.agari.ts

Whitespace-only changes.

app/routes/play.ryukyoku.ts

Whitespace-only changes.

app/routes/play.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { Form } from "react-router";
33
import { getAuth } from "~/lib/auth";
44
import { getDB } from "~/lib/db";
55
import { hai, haiyama } from "~/lib/db/schema";
6-
import { dbHaiToHai, sortTehai } from "~/lib/hai";
6+
import judgeAgari from "~/lib/hai/judgeAgari";
7+
import { dbHaiToHai, sortTehai } from "~/lib/hai/utils";
78
import { type GameState, getRedisClient, init } from "~/lib/redis";
89
import type { Route } from "./+types/play";
910

@@ -73,6 +74,9 @@ export async function loader({
7374
export default function Page({ loaderData }: Route.ComponentProps) {
7475
let { haiyama, sutehai, tsumohai, junme, kyoku, tehai } = loaderData;
7576
tehai = sortTehai(tehai);
77+
const isAgari =
78+
tehai && tsumohai ? judgeAgari(sortTehai([...tehai, tsumohai])) : false;
79+
const isRyukyoku = junme === 19;
7680
const indexedSutehai = sutehai.map((hai, index) => ({
7781
...hai,
7882
index: index,
@@ -84,6 +88,36 @@ export default function Page({ loaderData }: Route.ComponentProps) {
8488

8589
return (
8690
<div className="p-4">
91+
{isAgari && (
92+
<dialog id="agari_modal" className="modal" open>
93+
<div className="modal-box">
94+
<h3 className="font-bold text-lg">ツモ!</h3>
95+
<div className="modal-action">
96+
<form method="post" action="/play/agari">
97+
<input type="hidden" value={junme} name="junme" />
98+
<button className="btn" type="submit">
99+
確認
100+
</button>
101+
</form>
102+
</div>
103+
</div>
104+
</dialog>
105+
)}
106+
{isRyukyoku && (
107+
<dialog id="ryukyoku_modal" className="modal" open>
108+
<div className="modal-box">
109+
<h3 className="font-bold text-lg">流局</h3>
110+
<div className="modal-action">
111+
<form method="post" action="/play/ryukyoku">
112+
<button className="btn" type="submit">
113+
確認
114+
</button>
115+
</form>
116+
</div>
117+
</div>
118+
</dialog>
119+
)}
120+
87121
<p className="text-xl mb-4">
88122
Play Page - 局 {kyoku} 巡目 {junme}
89123
</p>

flake.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)