incorporating action types into app state

This commit is contained in:
Mikayla Dobson
2022-07-23 12:41:18 -05:00
parent a5c5b83b27
commit 3cfad260bf
5 changed files with 87 additions and 34 deletions

View File

@@ -2,12 +2,12 @@ import { BrowserRouter, Routes, Route } from 'react-router-dom'
import { useState } from 'react' import { useState } from 'react'
import Gameboard from './components/Gameboard/Gameboard' import Gameboard from './components/Gameboard/Gameboard'
import GameConstructor from './util/GameConstructor'; import GameConstructor from './util/GameConstructor';
import { PlayerData, NobleData, CardData } from './util/types'; import { PlayerData, NobleData, CardData, AppState } from './util/types';
import CardDeck from './data/cards.json'; import CardDeck from './data/cards.json';
import './App.css' import './App.css'
function App() { function App() {
const [state, setState] = useState({ const [state, setState] = useState<AppState>({
gameboard: { gameboard: {
nobles: new Array<NobleData>, nobles: new Array<NobleData>,
cardRows: { cardRows: {
@@ -24,9 +24,20 @@ function App() {
gold: 5 gold: 5
}, },
deck: CardDeck, deck: CardDeck,
}, },
round: 1, round: 1,
players: new Array<PlayerData>, players: new Array<PlayerData>,
actions: {
getChips: {
active: false
},
buyCard: {
active: false
},
reserveCard: {
active: false
}
}
}) })
return ( return (

View File

@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { CardData, FullDeck, NobleData, StateProps } from '../../util/types'; import { FullDeck, NobleData, StateProps } from '../../util/types';
import AllPlayers from '../Player/AllPlayers'; import AllPlayers from '../Player/AllPlayers';
import AvailableChips from '../Resources/AvailableChips'; import AvailableChips from '../Resources/AvailableChips';
import CardRow from './CardRow'; import CardRow from './CardRow';

View File

@@ -1,7 +1,7 @@
import { AppState, PlayerData, ResourceCost, StateProps } from "../../util/types" import { AppState, ActionPrompts, GameActions, PlayerData, ResourceCost, StateProps } from "../../util/types";
import { v4 } from "uuid"; import { Dispatch, SetStateAction, useEffect, useState } from "react";
import React, { Dispatch, SetStateAction, useEffect, useState } from "react";
import { TurnOrderUtil } from "../../util/TurnOrderUtil"; import { TurnOrderUtil } from "../../util/TurnOrderUtil";
import { v4 } from "uuid";
interface PlayerProps extends StateProps { interface PlayerProps extends StateProps {
player: PlayerData player: PlayerData
@@ -11,17 +11,9 @@ interface PlayerProps extends StateProps {
} }
} }
enum ActionPrompts {
"Choose your action type below:",
"Make a selection of three different available resources, or two of the same.",
"Choose a card to purchase above.",
"Select any card above to reserve. You will also automatically take a gold chip.",
"Select any card above to reserve. You have the maximum allowed number of chips, so you cannnot take a gold chip.",
"It is not your turn."
}
export default function Player({ player, state, setState, chipSelection }: PlayerProps) { export default function Player({ player, state, setState, chipSelection }: PlayerProps) {
const [actionPrompt, setActionPrompt] = useState(ActionPrompts[0]); const [actionPrompt, setActionPrompt] = useState(ActionPrompts[0]);
const [actionType, setActionType] = useState<GameActions>();
const [dynamic, setDynamic] = useState<PlayerData>(); const [dynamic, setDynamic] = useState<PlayerData>();
const { selection, setSelection } = chipSelection; const { selection, setSelection } = chipSelection;
@@ -30,18 +22,30 @@ export default function Player({ player, state, setState, chipSelection }: Playe
}, [state]); }, [state]);
useEffect(() => { useEffect(() => {
console.log(selection) return;
}, [selection, setSelection]) }, [selection, setSelection])
useEffect(() => {
switch (actionType) {
case GameActions.GETCHIPS:
console.log('get chips');
getChips();
setSelection([]);
setActionType(GameActions.AWAIT);
break;
case GameActions.AWAIT:
console.log('waiting for next action');
break;
default:
break;
}
}, [actionType]);
const getChips = () => { const getChips = () => {
if (!dynamic?.turnActive) return; if (!dynamic?.turnActive) return;
setActionPrompt(ActionPrompts[1]); setActionPrompt(ActionPrompts[1]);
console.log(selection);
if (selection.length < 3) return; if (selection.length < 3) return;
console.log('conditions met!');
setState((prev: AppState) => { setState((prev: AppState) => {
const { newPlayers, roundIncrement } = TurnOrderUtil(prev, dynamic); const { newPlayers, roundIncrement } = TurnOrderUtil(prev, dynamic);
@@ -66,8 +70,6 @@ export default function Player({ player, state, setState, chipSelection }: Playe
}, },
} }
}) })
setSelection([]);
} }
return ( return (
@@ -80,7 +82,7 @@ export default function Player({ player, state, setState, chipSelection }: Playe
{/* Dynamic data from state */} {/* Dynamic data from state */}
<p>{dynamic?.turnActive ? actionPrompt : "..."}</p> <p>{dynamic?.turnActive ? actionPrompt : "..."}</p>
<button onClick={getChips}> {selection.length < 3 ? "Get Chips" : "Confirm"} </button> <button onClick={() => setActionType(GameActions.GETCHIPS)}>Get Chips</button>
<button onClick={()=>{}}>Buy a Card</button> <button onClick={()=>{}}>Buy a Card</button>
<button onClick={()=>{}}>Reserve a Card</button> <button onClick={()=>{}}>Reserve a Card</button>

View File

@@ -1,7 +1,7 @@
import { ResourceCost, StateProps } from "../../util/types"; import { GameActions, ResourceCost, StateProps } from "../../util/types";
import { v4 } from "uuid"; import { v4 } from "uuid";
import "./AvailableChips.css" import "./AvailableChips.css"
import { Dispatch, SetStateAction, useEffect } from "react"; import { Dispatch, SetStateAction, useEffect, useState } from "react";
interface AvailableChipsProps extends StateProps { interface AvailableChipsProps extends StateProps {
chipSelection: { chipSelection: {
@@ -20,7 +20,6 @@ export default function AvailableChips({ state, chipSelection }: AvailableChipsP
newValue.push(key); newValue.push(key);
setSelection(newValue); setSelection(newValue);
console.log(selection);
} }
useEffect(() => { useEffect(() => {
@@ -28,11 +27,20 @@ export default function AvailableChips({ state, chipSelection }: AvailableChipsP
}, [state]) }, [state])
useEffect(() => { useEffect(() => {
console.log(selection); switch (state.actions) {
}, [selection, setSelection]) case (GameActions.GETCHIPS):
console.log('get chips');
}
})
return ( return (
<div className="available-chips"> <div className="available-chips">
<div className="current-selection">
<strong>Selection:</strong>
{ selection.map((each) => <p key={v4()}>{each}</p>) }
</div>
{ {
Object.keys(state.gameboard.tradingResources).map((key: string) => { Object.keys(state.gameboard.tradingResources).map((key: string) => {
return ( return (
@@ -46,6 +54,7 @@ export default function AvailableChips({ state, chipSelection }: AvailableChipsP
) )
}) })
} }
</div> </div>
) )
} }

View File

@@ -1,5 +1,4 @@
import { Dispatch, SetStateAction } from "react" import { Dispatch, SetStateAction } from "react"
import { AppState } from "../context/types"
export interface AppState { export interface AppState {
gameboard: { gameboard: {
@@ -13,7 +12,23 @@ export interface AppState {
deck: FullDeck, deck: FullDeck,
}, },
round: number, round: number,
players: Array<PlayerData> players: Array<PlayerData>,
actions?: {
getChips?: {
active: boolean
chips?: Array<keyof ResourceCost>
valid?: boolean
}
buyCard?: {
active: boolean
card?: CardData | null
}
reserveCard?: {
active: boolean
card?: CardData | null
includeGold?: boolean
}
}
} }
export interface StateProps { export interface StateProps {
@@ -21,6 +36,22 @@ export interface StateProps {
setState: Dispatch<SetStateAction<AppState>> setState: Dispatch<SetStateAction<AppState>>
} }
export enum GameActions {
GETCHIPS,
BUYCARD,
RESERVECARD,
AWAIT
}
export enum ActionPrompts {
"Choose your action type below:",
"Make a selection of three different available resources, or two of the same.",
"Choose a card to purchase above.",
"Select any card above to reserve. You will also automatically take a gold chip.",
"Select any card above to reserve. You have the maximum allowed number of chips, so you cannnot take a gold chip.",
"It is not your turn."
}
export interface GameInformation { export interface GameInformation {
players: PlayerData[], players: PlayerData[],
nobles: NobleData[], nobles: NobleData[],