diff --git a/client/src/App.css b/client/src/App.css index edfbec5..3824d5f 100755 --- a/client/src/App.css +++ b/client/src/App.css @@ -106,9 +106,35 @@ body { flex-grow: 1; } +.Table .Dialogs { + position: absolute; + display: flex; + top: 0; + bottom: 0; + right: 0; + left: 0; + justify-content: space-around; + align-items: center; + z-index: 20000; + pointer-events: none; +} + +.Table .Dialogs > * { + pointer-events: all; +} + .Table .ViewCard { display: flex; - positin: relative; + position: relative; + top: 0; + left: 0; + right: 0; + bottom: 0; +} + +.Table .ChooseCard { + display: flex; + position: relative; top: 0; left: 0; right: 0; diff --git a/client/src/App.js b/client/src/App.js index ea6e118..0555ffb 100755 --- a/client/src/App.js +++ b/client/src/App.js @@ -22,6 +22,7 @@ import { Activities } from "./Activities.js"; import { SelectPlayer } from "./SelectPlayer.js"; import { PlayersStatus } from "./PlayersStatus.js"; import { ViewCard } from "./ViewCard.js"; +import { ChooseCard } from "./ChooseCard.js"; import { Hand } from "./Hand.js"; import history from "./history.js"; @@ -290,25 +291,24 @@ const Table = () => {
- { error &&
- -
{ error }
- -
-
} - { warning &&
- { warning } -
} - { state === 'normal' && } - { color && state === 'game-order' && } - { /* state === 'normal' && */ } - { /* state === 'winner' && */ } - { } - - { /*isTurn && turn && turn.actions && game.turn.actions.indexOf('select-resources') !== -1 && - - */ } - +
+ { error &&
+ +
{ error }
+ +
+
} + { warning &&
+ { warning } +
} + { state === 'normal' && } + { color && state === 'game-order' && } + { /* state === 'normal' && */ } + { /* state === 'winner' && */ } + + +
+ diff --git a/client/src/ChooseCard.css b/client/src/ChooseCard.css index a4fb70e..1a927b7 100644 --- a/client/src/ChooseCard.css +++ b/client/src/ChooseCard.css @@ -1,10 +1,5 @@ .ChooseCard { display: flex; - position: absolute; - left: 0; - right: 30rem; - bottom: 0; - top: 0; justify-content: center; align-items: center; background: rgba(0,0,0,0.5); diff --git a/client/src/ChooseCard.js b/client/src/ChooseCard.js index 29a4867..96f6792 100644 --- a/client/src/ChooseCard.js +++ b/client/src/ChooseCard.js @@ -1,13 +1,63 @@ -import React, { useState, useCallback } from "react"; -import "./ChooseCard.css"; +import React, { useState, useCallback, useEffect, useMemo, useRef, + useContext } from "react"; +import equal from "fast-deep-equal"; + import Paper from '@material-ui/core/Paper'; import Button from '@material-ui/core/Button'; -import Resource from './Resource.js'; +import "./ChooseCard.css"; +import {Resource} from './Resource.js'; +import {GlobalContext} from './GlobalContext.js'; -const ChooseCard = ({table, type}) => { +const ChooseCard = () => { + const { ws } = useContext(GlobalContext); + const [turn, setTurn] = useState(undefined); + const [color, setColor] = useState(undefined); const [cards, setCards] = useState([]); - const count = (type === 'monopoly') ? 1 : 2; + const fields = useMemo(() => [ + 'turn', 'color' + ], []); + + const onWsMessage = (event) => { + const data = JSON.parse(event.data); + switch (data.type) { + case 'game-update': + console.log(`choose-card - game-update: `, data.update); + if ('turn' in data.update && !equal(turn, data.update.turn)) { + setTurn(data.update.turn); + } + if ('color' in data.update && data.update.color !== color) { + setColor(data.update.color); + } + break; + default: + break; + } + }; + const refWsMessage = useRef(onWsMessage); + useEffect(() => { refWsMessage.current = onWsMessage; }); + useEffect(() => { + if (!ws) { return; } + const cbMessage = e => refWsMessage.current(e); + ws.addEventListener('message', cbMessage); + return () => { ws.removeEventListener('message', cbMessage); } + }, [ws, refWsMessage]); + useEffect(() => { + if (!ws) { return; } + ws.send(JSON.stringify({ + type: 'get', + fields + })); + }, [ws, fields]); + const selectResources = useCallback((event) => { + ws.send(JSON.stringify({ + type: 'select-resources', + cards + })); + }, [ws]); + + const count = (turn && turn.active === 'monopoly') ? 1 : 2; + const selectCard = useCallback((event) => { event.target.classList.toggle('Selected'); @@ -27,16 +77,14 @@ const ChooseCard = ({table, type}) => { setCards(tmp); }, [ setCards, count ]); - if (!table.game) { + if (!turn + || turn.color !== color + || !turn.actions + || turn.actions.indexOf('select-resources') === -1 + || !turn.active) { return <>; } - - console.log(cards.length, count); - - const submitCards = () => { - table.selectResources(cards); - } - + const resources = [ 'wheat', 'brick', 'stone', 'sheep', 'wood' ].map(type => { @@ -48,7 +96,7 @@ const ChooseCard = ({table, type}) => { }); let title; - switch (type) { + switch (turn.active) { case 'monopoly': title = <>Monopoly! Tap the resource type you want everyone to give you!; break; @@ -56,7 +104,7 @@ const ChooseCard = ({table, type}) => { title = <>Year of Plenty! Tap the two resources you want to receive from the bank!; break; default: - title = <>Unknown card type {type}.; + title = <>Unknown card type {turn.active}.; break; } @@ -67,10 +115,10 @@ const ChooseCard = ({table, type}) => {
{ resources }
-
+
); }; -export default ChooseCard; \ No newline at end of file +export {ChooseCard}; \ No newline at end of file diff --git a/server/routes/games.js b/server/routes/games.js index f668d9a..943283c 100755 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -3436,7 +3436,7 @@ router.ws("/ws/:id", async (ws, req) => { } break; case 'select-resources': - console.log(`${short}: <- select-resources:${getName(session)}`); + console.log(`${short}: <- select-resources:${getName(session)} - `, data.cards); warning = selectResources(game, session, data.cards); if (warning) { sendWarning(session, warning);