props passed through components successfully

This commit is contained in:
Mikayla Dobson
2022-07-23 09:42:07 -05:00
parent ec53f1889d
commit d01f555c1e
41 changed files with 148 additions and 144 deletions

View File

@@ -1,27 +1,45 @@
import { BrowserRouter, Routes, Route } from 'react-router-dom' import { BrowserRouter, Routes, Route } from 'react-router-dom'
import { useState } from 'react'
import Gameboard from './components/Gameboard/Gameboard' import Gameboard from './components/Gameboard/Gameboard'
import './App.css'
import { useCallback, useContext, useEffect, useState } from 'react'
import { appState, Context } from './context/Context'
import AvailableChips from './components/Resources/AvailableChips';
import GameConstructor from './util/GameConstructor'; import GameConstructor from './util/GameConstructor';
import { PlayerData } from './util/types'; import { PlayerData, NobleData, CardData } from './util/types';
import CardDeck from './data/cards.json';
import './App.css'
function App() { function App() {
let AppContext = useContext(Context); const [state, setState] = useState({
let { players } = AppContext; gameboard: {
nobles: new Array<NobleData>,
cardRows: {
tierOne: new Array<CardData>,
tierTwo: new Array<CardData>,
tierThree: new Array<CardData>,
},
tradingResources: {
ruby: 7,
sapphire: 7,
emerald: 7,
diamond: 7,
onyx: 7,
gold: 5
},
deck: CardDeck,
},
round: 0,
players: new Array<PlayerData>,
})
return ( return (
<div className="App"> <div className="App">
<Context.Provider value={appState}> <h1>SPLENDOR</h1>
<h1>SPLENDOR</h1> <BrowserRouter>
<BrowserRouter> <Routes>
<Routes> {/* @ts-ignore */}
<Route path="/" element={<GameConstructor />} /> <Route path="/" element={<GameConstructor state={state} setState={setState} />} />
<Route path="/game" element={<Gameboard />} /> {/* @ts-ignore */}
</Routes> <Route path="/game" element={<Gameboard state={state} setState={setState} />} />
</BrowserRouter> </Routes>
</Context.Provider> </BrowserRouter>
</div> </div>
); );
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 885 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 634 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 605 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 534 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 916 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 959 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1017 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1015 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 195 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 591 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 732 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

@@ -1,15 +1,12 @@
import { useContext, useEffect, useState } from 'react'; import { Dispatch, SetStateAction, useEffect, useState } from 'react';
import { appState, Context } from '../../context/Context'; import { CardData, FullDeck, NobleData, StateProps } from '../../util/types';
import { NobleData } 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';
import Nobles from './Nobles'; import Nobles from './Nobles';
import NobleStore from '../../data/nobles.json'; import NobleStore from '../../data/nobles.json';
export default function Gameboard() { export default function Gameboard({ state, setState }: StateProps) {
let AppContext = useContext(Context);
let { gameboard, players } = AppContext;
const [view, setView] = useState(<p>Loading...</p>) const [view, setView] = useState(<p>Loading...</p>)
useEffect(() => { useEffect(() => {
@@ -17,7 +14,7 @@ export default function Gameboard() {
}, []) }, [])
useEffect(() => { useEffect(() => {
if (!players.length) { if (!state.players.length) {
setView( setView(
<div className="error-page"> <div className="error-page">
<strong>Sorry! It appears we've lost track of your game data.</strong> <strong>Sorry! It appears we've lost track of your game data.</strong>
@@ -27,20 +24,20 @@ export default function Gameboard() {
} else { } else {
setView( setView(
<div className="gameboard-rows"> <div className="gameboard-rows">
<Nobles AppContext={AppContext} /> <Nobles state={state} setState={setState} />
<CardRow tier={3} cards={gameboard.cardRows.tierThree} /> <CardRow tier={3} cards={state.gameboard.cardRows.tierThree} />
<CardRow tier={2} cards={gameboard.cardRows.tierTwo} /> <CardRow tier={2} cards={state.gameboard.cardRows.tierTwo} />
<CardRow tier={1} cards={gameboard.cardRows.tierOne} /> <CardRow tier={1} cards={state.gameboard.cardRows.tierOne} />
<AvailableChips /> <AvailableChips state={state} setState={setState} />
<AllPlayers AppContext={AppContext} /> <AllPlayers state={state} setState={setState} />
</div> </div>
) )
} }
}, [AppContext]); }, [state]);
const shuffleDeck = () => { const shuffleDeck = () => {
if (!gameboard.deck) return; if (!state.gameboard.deck) return;
let newDeck = gameboard.deck; let newDeck: FullDeck = state.gameboard.deck;
for (const [key, value] of Object.entries(newDeck)) { for (const [key, value] of Object.entries(newDeck)) {
for (let i = value.length - 1; i > 0; i--) { for (let i = value.length - 1; i > 0; i--) {
@@ -51,7 +48,7 @@ export default function Gameboard() {
} }
} }
gameboard.deck = newDeck; setState({ ...state, gameboard: { ...state.gameboard, deck: newDeck }})
} }
const setNobles = () => { const setNobles = () => {
@@ -64,26 +61,24 @@ export default function Gameboard() {
shuffledNobles.push(randNoble); shuffledNobles.push(randNoble);
} }
gameboard.nobles = shuffledNobles; setState({ ...state, gameboard: { ...state.gameboard, nobles: shuffledNobles }})
} }
const initializeBoard = () => { const initializeBoard = () => {
shuffleDeck(); shuffleDeck();
setNobles();
let newState = gameboard; let newDeck = state.gameboard.cardRows;
for (const [key, value] of Object.entries(state.gameboard.deck)) {
for (const [key, value] of Object.entries(gameboard.deck)) { while (newDeck[key as keyof FullDeck].length < 4) {
// @ts-ignore
while (newState.cardRows[key].length < 4) {
const nextCard = value.shift();
// @ts-ignore // @ts-ignore
newState.cardRows[key].push(nextCard); const nextCard = value.shift();
newDeck[key as keyof FullDeck].push(nextCard);
} }
} }
gameboard = newState; setState({ ...state, gameboard: { ...state.gameboard, cardRows: newDeck } })
setNobles();
} }
return view; return view
} }

View File

@@ -1,16 +1,28 @@
import { useEffect } from "react";
import { v4 } from "uuid"; import { v4 } from "uuid";
import { NobleData, ResourceCost } from "../../util/types"; import { NobleData, ResourceCost, StateProps } from "../../util/types";
import "./Nobles.css" import "./Nobles.css"
export default function Nobles({ AppContext }: any) { export default function Nobles({ state, setState }: StateProps) {
const { gameboard } = AppContext; const removeNoble = (noble: NobleData) => {
console.log(noble);
setState((prev) => {
return {
...prev,
gameboard: {
...prev.gameboard,
nobles: prev.gameboard.nobles.filter((each) => each.nobleid !== noble.nobleid)
}
}
})
}
return ( return (
<div className="nobles-panel"> <div className="nobles-panel">
<strong>NOBLES</strong> <strong>NOBLES</strong>
<div className="all-nobles"> <div className="all-nobles">
{ {
gameboard.nobles && gameboard.nobles.map((noble: NobleData) => { state && state.gameboard.nobles.map((noble: NobleData) => {
return ( return (
<div className="noble-card" key={v4()}> <div className="noble-card" key={v4()}>
<p>Points: {noble.points}</p> <p>Points: {noble.points}</p>
@@ -23,7 +35,7 @@ export default function Nobles({ AppContext }: any) {
} }
</div> </div>
) )
}) || <p>Nobles not found. Please wait...</p> })
} }
</div> </div>
</div> </div>

View File

@@ -1,17 +1,13 @@
import { useContext } from "react"
import { Context } from "../../context/Context"
import Player from "./Player"; import Player from "./Player";
import { v4 } from "uuid"; import { v4 } from "uuid";
import "./AllPlayers.css" import "./AllPlayers.css"
import { PlayerData } from "../../util/types"; import { PlayerData, StateProps } from "../../util/types";
export default function AllPlayers({ AppContext }: any) {
const { players } = AppContext;
export default function AllPlayers({ state, setState }: StateProps) {
return ( return (
<div className="all-players"> <div className="all-players">
{ {
players.map((player: PlayerData) => <Player key={v4()} data={player} />) state.players?.map((player: PlayerData) => <Player key={v4()} player={player} state={state} setState={setState} />)
} }
</div> </div>
) )

View File

@@ -1,43 +1,36 @@
import { useContext } from "react"; import { AppState, PlayerData, ResourceCost, StateProps } from "../../util/types"
import { v4 } from "uuid"; import { v4 } from "uuid";
import { Context } from "../../context/Context";
import { GemValue, PlayerData, ResourceCost } from "../../util/types"
interface PlayerProps { interface PlayerProps extends StateProps {
data: PlayerData player: PlayerData
} }
export default function Player({ data }: PlayerProps) { export default function Player({ player, state, setState }: PlayerProps) {
const AppContext = useContext(Context);
let state: any;
let setState: any;
// const [state, setState] = useState({
// name: data.name,
// starter: data.starter,
// points: data.points,
// nobles: data.nobles,
// cards: data.cards,
// inventory: data.inventory
// })
// const buyCard = (cardData: CardData) => {
// let newCards = state.cards;
// newCards.push(cardData);
// setState({ ...state, cards: newCards })
// }
const getChips = (resource: string) => { const getChips = (resource: string) => {
AppContext.gameboard.tradingResources[resource as keyof ResourceCost] -= 1; setState((prev: AppState) => {
console.log(AppContext); return {
...prev,
gameboard: {
...prev.gameboard,
tradingResources: {
...prev.gameboard.tradingResources,
[resource as keyof ResourceCost]: prev.gameboard.tradingResources[resource as keyof ResourceCost] -= 1
}
}
}
})
console.log(state);
} }
return ( return (
<div className="player-ui" key={v4()}> <div className="player-ui" key={v4()}>
<p>Name: {data.name}</p> {/* Static Data */}
<p>Score: {data.points}</p> <p>Name: {player.name}</p>
<p>Is {data.starter || "not"} round starter</p> <p>Score: {player.points}</p>
<p>Is {player.starter || "not"} round starter</p>
{/* Dynamic data from state */}
<button onClick={() => getChips('ruby')}>Get Chips</button> <button onClick={() => getChips('ruby')}>Get Chips</button>
<div className="player-cards"></div> <div className="player-cards"></div>
<div className="player-resources"></div> <div className="player-resources"></div>

View File

@@ -1,19 +1,20 @@
import { useContext, useEffect, useState } from "react" import { ResourceCost, StateProps } from "../../util/types";
import { v4 } from "uuid"; import { v4 } from "uuid";
import { appState, Context } from "../../context/Context";
import { ResourceCost } from "../../util/types";
import "./AvailableChips.css" import "./AvailableChips.css"
import { useEffect } from "react";
export default function AvailableChips() { export default function AvailableChips({ state }: StateProps) {
const AppContext = useContext(Context); useEffect(() => {
return;
}, [state])
return ( return (
<div className="available-chips"> <div className="available-chips">
{ {
Object.keys(AppContext.gameboard.tradingResources).map((key: string) => { Object.keys(state.gameboard.tradingResources).map((key: string) => {
return ( return (
<div key={v4()} className={`chips-${key}`}> <div key={v4()} className={`chips-${key}`}>
<p>{key}: {AppContext.gameboard.tradingResources[key as keyof ResourceCost]}</p> <p>{key}: {state.gameboard.tradingResources[key as keyof ResourceCost]}</p>
</div> </div>
) )
}) })

View File

@@ -1,28 +0,0 @@
import { CardData, NobleData, PlayerData } from '../util/types';
import CardDeck from '../data/cards.json';
import { createContext } from 'react';
import { AppState } from './types';
export const appState: AppState = {
gameboard: {
nobles: new Array<NobleData>,
cardRows: {
tierOne: new Array<CardData>,
tierTwo: new Array<CardData>,
tierThree: new Array<CardData>,
},
tradingResources: {
ruby: 7,
sapphire: 7,
emerald: 7,
diamond: 7,
onyx: 7,
gold: 5
},
deck: CardDeck,
},
round: 0,
players: new Array<PlayerData>,
}
export const Context = createContext(appState);

View File

@@ -1,16 +0,0 @@
import { CardData, FullDeck, NobleData, PlayerData, ResourceCost } from "../util/types"
export interface AppState {
gameboard: {
nobles: Array<NobleData>,
cardRows: {
tierOne: Array<CardData>
tierTwo: Array<CardData>
tierThree: Array<CardData>
},
tradingResources: ResourceCost
deck: FullDeck,
},
round: number,
players: Array<PlayerData>
}

View File

@@ -1,6 +1,7 @@
{ {
"nobles": [ "nobles": [
{ {
"nobleid": 1,
"points": 3, "points": 3,
"resourceCost": { "resourceCost": {
"ruby": 0, "ruby": 0,
@@ -11,6 +12,7 @@
} }
}, },
{ {
"nobleid": 2,
"points": 3, "points": 3,
"resourceCost": { "resourceCost": {
"ruby": 0, "ruby": 0,
@@ -21,6 +23,7 @@
} }
}, },
{ {
"nobleid": 3,
"points": 3, "points": 3,
"resourceCost": { "resourceCost": {
"ruby": 3, "ruby": 3,
@@ -31,6 +34,7 @@
} }
}, },
{ {
"nobleid": 4,
"points": 3, "points": 3,
"resourceCost": { "resourceCost": {
"ruby": 4, "ruby": 4,
@@ -41,6 +45,7 @@
} }
}, },
{ {
"nobleid": 5,
"points": 3, "points": 3,
"resourceCost": { "resourceCost": {
"ruby": 0, "ruby": 0,
@@ -51,6 +56,7 @@
} }
}, },
{ {
"nobleid": 6,
"points": 3, "points": 3,
"resourceCost": { "resourceCost": {
"ruby": 3, "ruby": 3,
@@ -61,6 +67,7 @@
} }
}, },
{ {
"nobleid": 7,
"points": 3, "points": 3,
"resourceCost": { "resourceCost": {
"ruby": 0, "ruby": 0,
@@ -71,6 +78,7 @@
} }
}, },
{ {
"nobleid": 8,
"points": 3, "points": 3,
"resourceCost": { "resourceCost": {
"ruby": 3, "ruby": 3,
@@ -81,6 +89,7 @@
} }
}, },
{ {
"nobleid": 9,
"points": 3, "points": 3,
"resourceCost": { "resourceCost": {
"ruby": 4, "ruby": 4,
@@ -91,6 +100,7 @@
} }
}, },
{ {
"nobleid": 10,
"points": 3, "points": 3,
"resourceCost": { "resourceCost": {
"ruby": 0, "ruby": 0,

View File

@@ -1,7 +1,7 @@
import { useContext, useEffect, useState } from "react" import { useContext, useEffect, useState } from "react"
import { useNavigate } from "react-router-dom" import { useNavigate } from "react-router-dom"
import { Context } from "../context/Context"; import { Context } from "../context/Context";
import { CardData, NobleData, PlayerData } from "./types"; import { CardData, NobleData, PlayerData, StateProps } from "./types";
interface InputState { interface InputState {
playerOne: PlayerInput playerOne: PlayerInput
@@ -15,12 +15,10 @@ interface PlayerInput {
starter: boolean starter: boolean
} }
export default function GameConstructor() { export default function GameConstructor({ state, setState }: StateProps) {
const AppContext = useContext(Context);
const navigate = useNavigate(); const navigate = useNavigate();
const [starter, setStarter] = useState(-1); const [starter, setStarter] = useState(-1);
const [input, setInput] = useState<InputState>({ const [input, setInput] = useState<InputState>({
playerOne: { playerOne: {
name: '', name: '',
@@ -66,7 +64,7 @@ export default function GameConstructor() {
if (!player.name) newPlayers.splice(newPlayers.indexOf(player)); if (!player.name) newPlayers.splice(newPlayers.indexOf(player));
} }
AppContext.players = newPlayers; setState({ ...state, players: newPlayers });
navigate('/game'); navigate('/game');
} }

25
src/util/types.d.ts vendored
View File

@@ -1,3 +1,26 @@
import { Dispatch, SetStateAction } from "react"
import { AppState } from "../context/types"
export interface AppState {
gameboard: {
nobles: Array<NobleData>,
cardRows: {
tierOne: Array<CardData>
tierTwo: Array<CardData>
tierThree: Array<CardData>
},
tradingResources: ResourceCost
deck: FullDeck,
},
round: number,
players: Array<PlayerData>
}
export interface StateProps {
state: AppState,
setState: Dispatch<SetStateAction<AppState>>
}
export interface GameInformation { export interface GameInformation {
players: PlayerData[], players: PlayerData[],
nobles: NobleData[], nobles: NobleData[],
@@ -8,6 +31,7 @@ export interface GameInformation {
export interface PlayerData { export interface PlayerData {
name: string, name: string,
starter: boolean, starter: boolean,
turnActive?: boolean,
points: number, points: number,
nobles: NobleData[], nobles: NobleData[],
cards: CardData[], cards: CardData[],
@@ -39,6 +63,7 @@ export interface ResourceCost {
} }
export interface NobleData { export interface NobleData {
nobleid?: number,
points: number, points: number,
resourceCost: ResourceCost resourceCost: ResourceCost
} }