cleanup
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { useState } from 'react'
|
||||
import { BrowserRouter, Routes, Route } from 'react-router-dom'
|
||||
import { initialState } from './util/stateSetters';
|
||||
import { useState } from 'react'
|
||||
|
||||
import Gameboard from './components/Gameboard/Gameboard'
|
||||
import GameConstructor from './components/GameConstructor';
|
||||
import { initialState } from './util/stateSetters';
|
||||
import './App.css'
|
||||
|
||||
function App() {
|
||||
@@ -13,7 +14,7 @@ function App() {
|
||||
<h1>SPLENDOR</h1>
|
||||
<BrowserRouter>
|
||||
<Routes>
|
||||
{/* @ts-ignore */}
|
||||
{/* @ts-ignore */}a
|
||||
<Route path="/" element={<GameConstructor state={state} setState={setState} />} />
|
||||
{/* @ts-ignore */}
|
||||
<Route path="/game" element={<Gameboard state={state} setState={setState} />} />
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { v4 } from 'uuid';
|
||||
import { CardData, StateProps } from '../../util/types';
|
||||
import { tooExpensive, buyCard } from '../Player/ActionMethods';
|
||||
import { buyCardActions } from '../Player/ActionMethods';
|
||||
const { buyCard, tooExpensive } = buyCardActions;
|
||||
|
||||
interface CardProps extends StateProps {
|
||||
data: CardData
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
import { AppState, PlayerData, ResourceCost, SetActionType, StateProps } from '../../util/types';
|
||||
import { setStateBuyCard, setStateGetChips, setStateReserveCard } from '../../util/stateSetters';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import Nobles from './Nobles';
|
||||
import { getChipsActions } from '../Player/ActionMethods';
|
||||
const { validateChips } = getChipsActions;
|
||||
|
||||
// components
|
||||
import Nobles from './Nobles';
|
||||
import initializeBoard from '../../util/initializeBoard';
|
||||
import AvailableChips from '../Resources/AvailableChips';
|
||||
import AllPlayers from '../Player/AllPlayers';
|
||||
import CardRow from '../Card/CardRow';
|
||||
import { validateChips } from '../Player/ActionMethods';
|
||||
import SelectionView from '../Resources/SelectionView';
|
||||
|
||||
export default function Gameboard({ state, setState }: StateProps) {
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
import { AppState, CardData, PlayerData, setStateType } from "../../util/types";
|
||||
import { turnOrderUtil } from "../../util/turnOrderUtil";
|
||||
import { initialActions } from "../../util/stateSetters";
|
||||
import { useCurrentPlayer } from "../../util/useCurrentPlayer";
|
||||
|
||||
// GET CHIPS ACTION HANDLERS
|
||||
export const validateChips = (state: AppState): boolean => {
|
||||
if (!state.actions.getChips.active || !state.actions.getChips.selection) return false;
|
||||
|
||||
const selection = state.actions.getChips.selection;
|
||||
|
||||
if (selection.length === 0 || selection.length > 3) return false;
|
||||
const unique = new Set(selection);
|
||||
|
||||
if (selection.length === 3 && selection.length > unique.size) return false;
|
||||
|
||||
let globalResourceCopy = { ...state.gameboard.tradingResources }
|
||||
|
||||
for (let item of selection) {
|
||||
for (let key of Object.keys(globalResourceCopy)) {
|
||||
if (item === key) {
|
||||
globalResourceCopy[key] -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let value of Object.values(globalResourceCopy)) {
|
||||
if (value < 0) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export const getChips = (state: AppState, setState: setStateType) => {
|
||||
let targetPlayer = useCurrentPlayer(state);
|
||||
|
||||
setState((prev) => {
|
||||
if (!targetPlayer) return prev;
|
||||
|
||||
const { newPlayers, roundIncrement } = turnOrderUtil(state, targetPlayer);
|
||||
const idx = newPlayers.indexOf(targetPlayer);
|
||||
const resources = state.actions.getChips.selection;
|
||||
let newResources = prev.gameboard.tradingResources;
|
||||
|
||||
if (resources) {
|
||||
for (let value of resources) {
|
||||
|
||||
// update player inventory
|
||||
for (let each in newPlayers[idx].inventory) {
|
||||
if (value === each) {
|
||||
newPlayers[idx].inventory[value] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// update globally available resources
|
||||
for (let each in newResources) {
|
||||
if (value === each) {
|
||||
newResources[value] -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...prev,
|
||||
round: (roundIncrement ? prev.round + 1 : prev.round),
|
||||
gameboard: {
|
||||
...prev.gameboard,
|
||||
tradingResources: newResources
|
||||
},
|
||||
players: newPlayers,
|
||||
actions: initialActions
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// BUY CARDS ACTION HANDLERS
|
||||
export const tooExpensive = (card: CardData, state: AppState): boolean => {
|
||||
const currentPlayer = useCurrentPlayer(state);
|
||||
if (!currentPlayer) return true;
|
||||
|
||||
for (let [gemType, cost] of Object.entries(card.resourceCost)) {
|
||||
for (let [heldResource, quantity] of Object.entries(currentPlayer.inventory)) {
|
||||
if (gemType === heldResource && quantity < cost) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export const buyCard = (card: CardData, state: AppState, setState: setStateType) => {
|
||||
/**
|
||||
* functionality: adds target card's data to current player's collection of cards,
|
||||
* removes the card from the active state of the gameboard, replaces it with
|
||||
* a new card in the correct tier, and runs turn order utility
|
||||
*
|
||||
* @param card -> the target card, @param state -> current app state
|
||||
*/
|
||||
|
||||
const currentPlayer = useCurrentPlayer(state);
|
||||
if (!currentPlayer) return;
|
||||
|
||||
setState((prev: AppState) => {
|
||||
let updatedPlayer = {
|
||||
...currentPlayer,
|
||||
cards: [
|
||||
...currentPlayer.cards,
|
||||
card
|
||||
]
|
||||
}
|
||||
|
||||
return {
|
||||
...prev
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const reserveCard = () => {
|
||||
return;
|
||||
}
|
||||
44
src/components/Player/ActionMethods/buyCardActions.ts
Normal file
44
src/components/Player/ActionMethods/buyCardActions.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { AppState, CardData, setStateType } from "../../../util/types";
|
||||
import { useCurrentPlayer } from "../../../util/useCurrentPlayer";
|
||||
|
||||
export const tooExpensive = (card: CardData, state: AppState): boolean => {
|
||||
const currentPlayer = useCurrentPlayer(state);
|
||||
if (!currentPlayer) return true;
|
||||
|
||||
for (let [gemType, cost] of Object.entries(card.resourceCost)) {
|
||||
for (let [heldResource, quantity] of Object.entries(currentPlayer.inventory)) {
|
||||
if (gemType === heldResource && quantity < cost) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export const buyCard = (card: CardData, state: AppState, setState: setStateType) => {
|
||||
/**
|
||||
* functionality: adds target card's data to current player's collection of cards,
|
||||
* removes the card from the active state of the gameboard, replaces it with
|
||||
* a new card in the correct tier, and runs turn order utility
|
||||
*
|
||||
* @param card -> the target card, @param state -> current app state
|
||||
*/
|
||||
|
||||
const currentPlayer = useCurrentPlayer(state);
|
||||
if (!currentPlayer) return;
|
||||
|
||||
setState((prev: AppState) => {
|
||||
let updatedPlayer = {
|
||||
...currentPlayer,
|
||||
cards: [
|
||||
...currentPlayer.cards,
|
||||
card
|
||||
]
|
||||
}
|
||||
|
||||
return {
|
||||
...prev
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { AppState, setStateType } from '../../../util/types';
|
||||
import { useCurrentPlayer } from '../../../util/useCurrentPlayer';
|
||||
// @ts-ignore
|
||||
import { turnOrderUtil } from '../../../util/turnOrderUtil';
|
||||
import { initialActions } from "../../../util/stateSetters";
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export * as getChipsActions from './getChipsActions';
|
||||
export * as buyCardActions from './buyCardActions';
|
||||
export * as reserveCardActions from './reserveCardActions';
|
||||
@@ -0,0 +1,3 @@
|
||||
export const reserveCard = () => {
|
||||
return;
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { useEffect } from "react";
|
||||
import { v4 } from "uuid";
|
||||
import "./AvailableChips.css"
|
||||
import { setStateGetChips } from "../../util/stateSetters";
|
||||
import { validateChips } from "../Player/ActionMethods";
|
||||
// import { validateChips } from "../Player/ActionMethods";
|
||||
|
||||
interface ResourceProps extends StateProps {
|
||||
liftSelection: (value: keyof ResourceCost) => void
|
||||
|
||||
@@ -30,9 +30,5 @@ export default function SelectionView({ state, setState }: StateProps) {
|
||||
})
|
||||
}, [state])
|
||||
|
||||
return (
|
||||
<div className="selection-view">
|
||||
{ view }
|
||||
</div>
|
||||
)
|
||||
return view
|
||||
}
|
||||
@@ -2,7 +2,8 @@ import { v4 } from "uuid";
|
||||
import { useEffect, useState } from "react";
|
||||
import { setStateGetChips } from "../../util/stateSetters";
|
||||
import { ResourceCost, StateProps } from "../../util/types";
|
||||
import { getChips } from "../Player/ActionMethods";
|
||||
import { getChipsActions } from "../Player/ActionMethods";
|
||||
const { getChips } = getChipsActions;
|
||||
|
||||
export const GetChipsHTML = ({ state, setState }: StateProps) => {
|
||||
const [prompt, setPrompt] = useState("");
|
||||
|
||||
@@ -5,7 +5,6 @@ export const turnOrderUtil = (prev: AppState, dynamic: PlayerData) => {
|
||||
const newPlayers = prev.players;
|
||||
|
||||
for (let each of newPlayers) {
|
||||
console.log(each);
|
||||
if (each.id === dynamic.id) {
|
||||
each.turnActive = false;
|
||||
} else if (each.id === dynamic.id + 1) {
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import initializeBoard from './initializeBoard';
|
||||
import * as stateSetters from './stateSetters';
|
||||
import { turnOrderUtil } from './turnOrderUtil';
|
||||
import * as APPTYPES from './types';
|
||||
import { useCurrentPlayer } from './useCurrentPlayer';
|
||||
|
||||
export default class UTIL {
|
||||
initializeBoard = initializeBoard
|
||||
turnOrderUtil = turnOrderUtil
|
||||
useCurrentPlayer = useCurrentPlayer
|
||||
stateSetters = stateSetters
|
||||
APPTYPES = APPTYPES
|
||||
}
|
||||
Reference in New Issue
Block a user