in progress: cleanup project structure
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { v4 } from 'uuid';
|
||||
import { CardData, StateProps } from '../../util/types';
|
||||
import { tooExpensive } from '../Player/ActionMethods';
|
||||
import { tooExpensive, buyCard } from '../Player/ActionMethods';
|
||||
|
||||
interface CardProps extends StateProps {
|
||||
data: CardData
|
||||
@@ -19,7 +19,14 @@ export default function Card({ data, state, setState }: CardProps) {
|
||||
return (data.resourceCost[key] > 0) && <p key={v4()}>{key}: {data.resourceCost[key]}</p>
|
||||
})
|
||||
}
|
||||
{ state.actions.buyCard.active && <button disabled={tooExpensive(data, state)}>Buy This Card</button> }
|
||||
{ state.actions.buyCard.active &&
|
||||
<button
|
||||
onClick={() => buyCard(data, state, setState)}
|
||||
disabled={tooExpensive(data, state)}
|
||||
>
|
||||
Buy This Card
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { AppState, CardData, PlayerData, setStateType } from "../../util/types";
|
||||
import { turnOrderUtil } from "../../util/TurnOrderUtil";
|
||||
import { turnOrderUtil } from "../../util/turnOrderUtil";
|
||||
import { initialActions } from "../../util/stateSetters";
|
||||
import { useCurrentPlayer } from "../../util/useCurrentPlayer";
|
||||
|
||||
@@ -90,8 +90,31 @@ export const tooExpensive = (card: CardData, state: AppState): boolean => {
|
||||
return false;
|
||||
}
|
||||
|
||||
export const buyCard = () => {
|
||||
return;
|
||||
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 = () => {
|
||||
|
||||
74
src/components/Player/ActionMethods/getChipsActions.ts
Normal file
74
src/components/Player/ActionMethods/getChipsActions.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { AppState, setStateType } from '../../../util/types';
|
||||
import { useCurrentPlayer } from '../../../util/useCurrentPlayer';
|
||||
import { turnOrderUtil } from '../../../util/turnOrderUtil';
|
||||
import { initialActions } from "../../../util/stateSetters";
|
||||
|
||||
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
|
||||
}
|
||||
})
|
||||
}
|
||||
0
src/components/Player/ActionMethods/index.ts
Normal file
0
src/components/Player/ActionMethods/index.ts
Normal file
13
src/util/index.ts
Normal file
13
src/util/index.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
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