{}}
- class="full column center"
- style={{ "row-gap": "20px", "font-size": "32px" }}
- >
-
+
);
};
diff --git a/src/components/Hand.tsx b/src/components/Hand.tsx
index 0311eeb..72f83ff 100644
--- a/src/components/Hand.tsx
+++ b/src/components/Hand.tsx
@@ -5,7 +5,7 @@ import { GameContext } from "./Game";
import { produce } from "solid-js/store";
export default ((props) => {
- const { setGameState } = useContext(GameContext)!;
+ const { setGameState, gameState } = useContext(GameContext)!;
return (
{
style={{
cursor: "pointer",
}}
- onClick={() =>
- setGameState(
- produce((state) => {
- const index = state.hand.indexOf(card);
- console.log(index);
- state.deck.push(
- state.hand.splice(
- props.hand.indexOf(card),
- 1
- )[0]!
- );
- })
- )
- }
+ onClick={() => {
+ const index = gameState()!.hand.indexOf(card);
+ setGameState({
+ deck: [card, ...gameState()!.deck],
+ hand: [
+ ...gameState()!.hand.slice(0, index),
+ ...gameState()!.hand.slice(index + 1),
+ ],
+ });
+ }}
/>
)}
diff --git a/src/db/Instances.ts b/src/db/Instances.ts
index 7996310..71380ee 100644
--- a/src/db/Instances.ts
+++ b/src/db/Instances.ts
@@ -1,6 +1,25 @@
"use server";
+import { GameState, newDeck, shuffle } from "../types/cards";
import { prisma } from "./db";
export const queryInstances = async (gameName: string) =>
- await prisma.instance.findMany({ where: { game: { name: gameName } } });
+ 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 } });
diff --git a/src/routes/[game]/[instance].tsx b/src/routes/[game]/[instance].tsx
index 73de752..cb9f6cc 100644
--- a/src/routes/[game]/[instance].tsx
+++ b/src/routes/[game]/[instance].tsx
@@ -1,19 +1,30 @@
-import { useParams } from "@solidjs/router";
+import { A, useParams } from "@solidjs/router";
import { createEffect, createResource, Show, Suspense } from "solid-js";
import Game from "../../components/Game";
-import { aql } from "arangojs";
+import { getGameState } from "../../db/Instances";
export default () => {
- const params = useParams();
-
- const [instance] = createResource(async () => {
- return null;
- });
+ const params = useParams<{ game: string; instance: string }>();
return (
-
-
-
+ <>
+
+
+ Back
+
+ >
);
};
diff --git a/src/routes/[game]/index.tsx b/src/routes/[game]/index.tsx
index 727e98d..de0d87c 100644
--- a/src/routes/[game]/index.tsx
+++ b/src/routes/[game]/index.tsx
@@ -5,26 +5,31 @@ import * as Instance from "../../db/Instances";
export default () => {
const params = useParams<{ game: string }>();
- createEffect(() => {
- console.log(">>", params.game);
- });
- const [instances] = createResource(
+
+ const [instances, { refetch }] = createResource(
() => params.game,
() => Instance.queryInstances(params.game)
);
- createEffect(() => console.log(instances()));
-
return (
{params.game}
+
+ Instance.createInstance(params.game).then(refetch)
+ }
+ >
+ New Game
+
diff --git a/src/routes/index.tsx b/src/routes/index.tsx
index 9d9f3e6..8ca09e1 100644
--- a/src/routes/index.tsx
+++ b/src/routes/index.tsx
@@ -1,6 +1,6 @@
import { A } from "@solidjs/router";
import { createResource, For } from "solid-js";
-import * as Games from "../db/games";
+import * as Games from "../db/Games";
export default () => {
const [games] = createResource(() => Games.queryAll());