wip but I need to sleep

This commit is contained in:
2025-08-09 01:40:42 -04:00
parent a7e339a8ce
commit 2ff5d781fd
12 changed files with 213 additions and 82 deletions

View File

@@ -1,34 +1,29 @@
import { Accessor, createContext, createResource, Show } from "solid-js";
import { GameState } from "../../../shared/types/cards";
import { GameState, Action } from "../../../server/src/games/simple";
import api from "../api";
import Hand from "./Hand";
import Pile from "./Pile";
export const GameContext = createContext<{
gameState: Accessor<GameState | undefined>;
setGameState: (state: GameState) => Promise<any>;
submitAction: (action: Action) => Promise<any>;
}>();
export default (props: { instanceId: number }) => {
const [gameState, { refetch, mutate }] = createResource(() =>
export default (props: { instanceId: string }) => {
const [gameState, { mutate }] = createResource(() =>
api
.gameState({ gameId: props.instanceId.toString() })
.simple(props)
.get()
.then((res) => res.data as GameState)
);
const setGameState = (state: GameState) => {
mutate(state);
return api
.gameState({ gameId: props.instanceId.toString() })
.put({
gameState: state,
})
.then(refetch);
};
const submitAction = (action: Action) =>
api
.simple(props)
.post({ action })
.then((res) => mutate(res.data as GameState));
return (
<GameContext.Provider value={{ gameState, setGameState }}>
<GameContext.Provider value={{ gameState, submitAction }}>
<Show when={gameState.latest != undefined}>
<div
class="full column center"
@@ -38,16 +33,12 @@ export default (props: { instanceId: number }) => {
<Pile
pile={gameState.latest!.deck}
style={{ cursor: "pointer" }}
onClick={() => {
const [drawn, ...rest] = gameState()!.deck;
setGameState({
deck: rest,
hand: [drawn, ...gameState()!.hand],
});
}}
onClick={() =>
api.simple({ instanceId: props.instanceId })
}
/>
</div>
<Hand hand={gameState.latest!.hand} />
<Hand hand={gameState.latest!.players[0]} />
</div>
</Show>
</GameContext.Provider>