A realtime multiplayer version of the boardgame Ricochet Robots
at master 44 lines 1.2 kB view raw
1import { eq } from "drizzle-orm"; 2import { z } from "zod"; 3import { generateToken } from "~/lib/generateToken"; 4 5import { 6 createTRPCRouter, 7 protectedProcedure, 8 publicProcedure, 9} from "~/server/api/trpc"; 10import { db } from "~/server/db"; 11import { games, gameUsers } from "~/server/db/schema"; 12 13export const gameRouter = createTRPCRouter({ 14 create: protectedProcedure 15 .input( 16 z.object({ 17 name: z.string(), 18 password: z.string().optional(), 19 }), 20 ) 21 .mutation(async ({ ctx, input }) => { 22 const insertedGame = await db.insert(games).values({ 23 name: input.name, 24 password: input.password, 25 state: "lobby", 26 code: generateToken(64), 27 ownerId: ctx.user.id, 28 }); 29 const game = ( 30 await db 31 .select() 32 .from(games) 33 .where(eq(games.id, insertedGame[0].insertId)) 34 )[0]; 35 await db.insert(gameUsers).values({ 36 gameId: game!.id, 37 userId: ctx.user.id, 38 }); 39 return game!.code; 40 }), 41 getGamesInLobby: publicProcedure.query(async ({ ctx }) => { 42 return await db.select().from(games).where(eq(games.state, "lobby")); 43 }), 44});