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
4 changes: 3 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"scripts": {
"dev": "wrangler dev",
"deploy": "wrangler deploy --minify",
"cf-typegen": "wrangler types --env-interface CloudflareBindings"
"cf-typegen": "wrangler types --env-interface CloudflareBindings",
"db:generate": "drizzle-kit generate",
"db:push": "drizzle-kit push"
},
"dependencies": {
"drizzle-orm": "^0.44.5",
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const sessions = pgTable("Session", {
export const rooms = pgTable("Room", {
id: text("id").primaryKey(),
name: text("name").notNull(),
gameTitle: text("gameType").notNull(),
createdAt: timestamp("createdAt", { withTimezone: true }).defaultNow(),
users: text("users").array().notNull(),
hostId: text("hostId")
Expand Down
47 changes: 37 additions & 10 deletions apps/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import {
type Rule,
} from "./magic";

import { Memory } from "./memory";

type Bindings = {
MAGIC: DurableObjectNamespace;
MEMORY: DurableObjectNamespace;
DATABASE_URL: string;
ENV: string;
};
Expand All @@ -30,6 +33,8 @@ type Variables = {
user: User;
};

type GameTitles = "magic-square" | "memory-optimization";

// TODO: 環境変数にする
const secret = "hoge";

Expand Down Expand Up @@ -151,9 +156,14 @@ const apiApp = new Hono<{
const db = c.get("db");
const user = c.get("user");

const { name } = await c.req.json<{ name: string }>();
if (!name) {
throw new HTTPException(400, { message: "Room name is required" });
const { name, gameTitle } = await c.req.json<{
name: string;
gameTitle: GameTitles;
}>();
if (!name || !gameTitle) {
throw new HTTPException(400, {
message: "Room name and game title are required",
});
}

const roomSecret = Math.floor(100000 + Math.random() * 900000).toString();
Expand All @@ -164,6 +174,7 @@ const apiApp = new Hono<{
.values({
id: roomId,
name,
gameTitle: gameTitle,
hostId: user.id,
users: [user.id],
})
Expand Down Expand Up @@ -300,18 +311,34 @@ const apiApp = new Hono<{
throw new HTTPException(403, { message: "Unauthorized" });
}

const id = c.env.MAGIC.idFromName(roomId);
const stub = c.env.MAGIC.get(id);
switch (room.gameTitle) {
case "magic-square": {
const id = c.env.MAGIC.idFromName(roomId);
const stub = c.env.MAGIC.get(id);

const url = new URL(c.req.url);
url.searchParams.set("playerId", user.id);
const url = new URL(c.req.url);
url.searchParams.set("playerId", user.id);

const request = new Request(url.toString(), c.req.raw);
return stub.fetch(request);
const request = new Request(url.toString(), c.req.raw);
return stub.fetch(request);
}
case "memory-optimization": {
const id = c.env.MEMORY.idFromName(roomId);
const stub = c.env.MEMORY.get(id);

const url = new URL(c.req.url);
url.searchParams.set("playerId", user.id);

const request = new Request(url.toString(), c.req.raw);
return stub.fetch(request);
}
default:
throw new HTTPException(400, { message: "Invalid game title" });
}
});

export type AppType = typeof apiApp;
export default apiApp;

export type { GameState, MoveAction, MessageType, Rule, Operation };
export { Magic };
export { Magic, Memory };
Loading