tokens fr
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
import { prisma } from "./db/db";
|
||||
import { Elysia, t } from "elysia";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { simpleApi } from "./games/simple";
|
||||
import { human } from "./human";
|
||||
import dayjs from "dayjs";
|
||||
import db from "./db";
|
||||
|
||||
const api = new Elysia({ prefix: "/api" })
|
||||
.post("/whoami", async ({ cookie: { token } }) => {
|
||||
if (token.value == null) {
|
||||
const newHuman = await prisma.human.create({
|
||||
const newHuman = await db.human.create({
|
||||
data: {},
|
||||
});
|
||||
token.value = newHuman.key;
|
||||
token.set({
|
||||
value: newHuman.token,
|
||||
expires: dayjs().add(1, "year").toDate(),
|
||||
httpOnly: true,
|
||||
});
|
||||
}
|
||||
return token.value;
|
||||
})
|
||||
@@ -18,7 +22,7 @@ const api = new Elysia({ prefix: "/api" })
|
||||
.post(
|
||||
"/setName",
|
||||
({ body: { name }, humanKey }) =>
|
||||
prisma.human.update({
|
||||
db.human.update({
|
||||
where: {
|
||||
key: humanKey,
|
||||
},
|
||||
@@ -33,16 +37,21 @@ const api = new Elysia({ prefix: "/api" })
|
||||
}
|
||||
)
|
||||
.get("/profile", ({ humanKey }) =>
|
||||
prisma.human.findFirst({ where: { key: humanKey } })
|
||||
db.human.findFirst({ where: { key: humanKey } })
|
||||
)
|
||||
.get("/games", () => prisma.game.findMany())
|
||||
.get("/games", () => [{ key: "simple", name: "simple" }])
|
||||
|
||||
.get("/instances", ({ query: { game } }) =>
|
||||
prisma.instance.findMany({
|
||||
.get("/instances", ({ query: { game }, humanKey }) =>
|
||||
db.instance.findMany({
|
||||
where: {
|
||||
game: {
|
||||
name: game,
|
||||
},
|
||||
players: {
|
||||
some: {
|
||||
key: humanKey,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
export const prisma = new PrismaClient();
|
||||
export default new PrismaClient();
|
||||
@@ -1,4 +0,0 @@
|
||||
"use server";
|
||||
import { prisma } from "./db";
|
||||
|
||||
export const queryAll = async () => await prisma.game.findMany();
|
||||
@@ -1,25 +0,0 @@
|
||||
"use server";
|
||||
|
||||
import { GameState, newDeck, shuffle } from "../types/cards";
|
||||
import { prisma } from "./db";
|
||||
|
||||
export const queryInstances = async (gameName: string) =>
|
||||
prisma.instance.findMany({ where: { game: { name: gameName } } });
|
||||
|
||||
export const createInstance = (gameName: string) =>
|
||||
prisma.instance.create({
|
||||
data: {
|
||||
gameState: { deck: shuffle(newDeck()), hand: [] } as GameState,
|
||||
game: { connect: { name: gameName } },
|
||||
},
|
||||
});
|
||||
|
||||
export const getGameState = (instanceId: number) =>
|
||||
prisma.instance
|
||||
.findUnique({ where: { id: instanceId } })
|
||||
.then((i) => i?.gameState as GameState | undefined);
|
||||
|
||||
export const updateGameState = async (
|
||||
instanceId: number,
|
||||
gameState: GameState
|
||||
) => prisma.instance.update({ where: { id: instanceId }, data: { gameState } });
|
||||
@@ -1,14 +1,7 @@
|
||||
import {
|
||||
Card,
|
||||
Hand,
|
||||
newDeck,
|
||||
Pile,
|
||||
shuffle,
|
||||
vCard,
|
||||
} from "../../../shared/cards";
|
||||
import { Card, Hand, newDeck, Pile, shuffle, vCard } from "@games/shared/cards";
|
||||
import { heq } from "@games/shared/utils";
|
||||
import { Elysia, t } from "elysia";
|
||||
import { prisma } from "../db/db";
|
||||
import db from "../db";
|
||||
import { human } from "../human";
|
||||
|
||||
// omniscient game state
|
||||
@@ -114,18 +107,22 @@ export const resolveAction = (
|
||||
export const simpleApi = new Elysia({ prefix: "/simple" })
|
||||
.use(human)
|
||||
.post("/newGame", ({ humanKey }) => {
|
||||
return prisma.instance.create({
|
||||
console.log("KEY", humanKey);
|
||||
return db.instance.create({
|
||||
data: {
|
||||
gameState: newGame([humanKey]),
|
||||
gameKey: "simple",
|
||||
createdByKey: humanKey,
|
||||
players: {
|
||||
connect: [{ key: humanKey }],
|
||||
},
|
||||
},
|
||||
});
|
||||
})
|
||||
.group("/:instanceId", (app) =>
|
||||
app
|
||||
.get("/", ({ params: { instanceId }, humanKey }) =>
|
||||
prisma.instance
|
||||
db.instance
|
||||
.findUnique({
|
||||
where: {
|
||||
id: instanceId,
|
||||
@@ -144,7 +141,7 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
|
||||
.post(
|
||||
"/",
|
||||
({ params: { instanceId }, body: { action }, humanKey }) =>
|
||||
prisma.instance
|
||||
db.instance
|
||||
.findUniqueOrThrow({
|
||||
where: {
|
||||
id: instanceId,
|
||||
@@ -156,7 +153,7 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
|
||||
humanKey,
|
||||
action
|
||||
);
|
||||
await prisma.instance.update({
|
||||
await db.instance.update({
|
||||
data: { gameState: newState },
|
||||
where: {
|
||||
id: instanceId,
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import Elysia from "elysia";
|
||||
import db from "./db";
|
||||
|
||||
export const human = new Elysia({ name: "human" })
|
||||
.derive(async ({ cookie: { token }, status }) => {
|
||||
const humanKey = token.value;
|
||||
const humanKey = await db.human
|
||||
.findUnique({
|
||||
where: { token: token.value },
|
||||
})
|
||||
.then((human) => human?.key);
|
||||
return humanKey != null ? { humanKey } : status(401);
|
||||
})
|
||||
.as("scoped");
|
||||
|
||||
Reference in New Issue
Block a user