import React, { useState, useCallback } from "react"; import "./Trade.css"; import { getPlayerName } from './Common.js'; import PlayerColor from './PlayerColor.js'; import Paper from '@material-ui/core/Paper'; import Button from '@material-ui/core/Button'; import Resource from './Resource.js'; const ResourceCounter = ({type, onCount, max}) => { const [count, setCount] = useState(0); const plusClicked = (event) => { if (max === undefined || max > count) { if (onCount) { onCount(type, count+1); } setCount(count+1); } }; const minusClicked = (event) => { if (count > 0) { if (onCount) { onCount(type, count-1); } setCount(count-1); } }; return (
{count}
); }; const Trade = ({table}) => { const [giveLine, setGiveLine] = useState('nothing'); const [getLine, setGetLine] = useState('nothing'); const [gives, setGives] = useState([]); const [gets, setGets] = useState([]); const giveCount = useCallback((type, count) => { gives[type] = count; if (!count) { delete gives[type]; } setGives(gives); const items = []; for (let key in gives) { items.push(`${gives[key]} ${key}`); } if (items.length === 0) { setGiveLine('nothing'); } else { setGiveLine(items.join(', ')); } }, [setGiveLine, setGives]); const getCount = useCallback((type, count) => { gets[type] = count; if (!count) { delete gets[type]; } setGets(gets); const items = []; for (let key in gets) { items.push(`${gets[key]} ${key}`); } if (items.length === 0) { setGetLine('nothing'); } else { setGetLine(items.join(', ')); } }, [setGetLine, setGets]); if (!table.game) { return (<>); } const isTurn = (table.game.turn && table.game.turn.color === table.game.color) ? true : false; const offerClicked = (event) => { const trade = { gives: [], gets: [] }; for (let key in gives) { trade.gives.push({type: key, count: gives[key]}); } for (let key in gets) { trade.gets.push({type: key, count: gets[key]}); } table.offerTrade(trade); } const acceptClicked = (offer) => { table.acceptTrade(offer); }; const cancelClicked = (event) => { table.cancelTrading(); } let players = []; for (let color in table.game.players) { const item = table.game.players[color], name = getPlayerName(table.game.sessions, color); if (name && table.game.name !== name) { players.push({ name: name, color: color, valid: false, gets: item.gets ? item.gets : [], gives: item.gives ? item.gives : [] }); } } players.sort((A, B) => { return A.name.localeCompare(B.name); }); if (isTurn && table.game.player && table.game.player.banks) { table.game.player.banks.forEach(bank => { const count = (bank === 'bank') ? 3 : 2; players.push({ name: `The bank`, color: undefined, gives: [ { count: 1, type: '*' } ], gets: [ { count: count, type: bank } ], valid: false }); }); players.push({ name: `The bank`, color: undefined, gives: [ { count: 1, type: '*' } ], gets: [ { count: 4, type: 'bank' } ], valid: false }); } if (table.game.turn.offer) { players.forEach(trade => { let valid = trade.gets.length && trade.gives.length; trade.gets.forEach(resource => { if (!valid) { return; } if (resource.type !== 'bank') { const offer = table.game.turn.offer.gives.find(item => item.type === resource.type); valid = offer && (offer.count >= resource.count); } else { valid = false; /* Doesn't matter what the resource type is so long as there * are enough of the one kind */ table.game.turn.offer.gives.forEach(offer => { if (offer.count >= resource.count) { valid = true; } }); } }); trade.valid = valid; }); } players = players.map((item, index) => { const gets = item.gets.map(get => `${get.count} ${(get.type === 'bank') ? 'of any one resource' : get.type}`) .join(', '), gives = item.gives.map(give => `${give.count} ${(give.type === '*') ? 'of any resource' : give.type}`) .join(', '); return (
{item.name}
{ gets !== '' && gives !== '' &&
wants {gets} and will give {gives}.
} { (gets === '' || gives === '') &&
has not submitted a trade offer.
} { isTurn && }
); }); const player = (table.game && table.game.player) ? table.game.player : undefined; if (!player) { return <>; } return (
Trading negotiations {isTurn ? '' : `with ${table.game.turn.name}`}
{ players }
You want to receive {getLine}.
You are willing to give {giveLine}.
{ player.brick > 0 && } { player.wood > 0 && } { player.wheat > 0 && } { player.sheep > 0 && } { player.stone > 0 && }
{ isTurn && }
); }; export default Trade;