From 6e281991b42fc48c65b631b1270f7e81ff3e2de8 Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Mon, 14 Mar 2022 00:09:58 -0700 Subject: [PATCH] Added missing files Signed-off-by: James Ketrenos --- client/src/Hand.css | 73 +++++++++++++++++++++++ client/src/Hand.js | 141 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 client/src/Hand.css create mode 100644 client/src/Hand.js diff --git a/client/src/Hand.css b/client/src/Hand.css new file mode 100644 index 0000000..3a657a5 --- /dev/null +++ b/client/src/Hand.css @@ -0,0 +1,73 @@ +.Hand { + display: flex; + bottom: 0; + left: 0; + right: 0; + justify-content: space-between; + align-items: flex-end; + height: 10.5rem; + max-height: 10.5; + overflow: visible; + flex-shrink: 1; +} + +.Hand .CardGroup { + display: flex; + min-height: calc(7.2em + 0.5em); + justify-items: space-between; + justify-content: space-between; +} + +.Hand .Cards { + display: inline-block; + position: absolute; + text-align: right; + vertical-align: bottom; + padding: 0.5em; + box-sizing: border-box; + max-height: 100%; + max-width: 100%; +} + +.Hand .Stack { + position: relative; + display: flex; +} + +.Hand .Stack:not(:first-child) { + margin-left: -3.5em; + transition: margin-left 1s ease-in-out 0.25s; +} + +.Hand .Stack > * { + transition: margin-left 1s ease-in-out 0.25s, margin-right 1s ease-in-out 0.25s; +} + +.Hand .Stack > *:not(:first-child) { + margin-left: -4em; +} + +.Hand .Placard:hover { + filter: brightness(105%); +} + +.Hand .Development:hover { + filter: brightness(150%); +} + +.Hand .Development.Selected { + filter: brightness(150%); + top: -1em; +} + +.Hand .Development { + position: relative; + display: inline-block; + width: 4.9em; + height: 7.2em; + background-position: center; + background-repeat: no-repeat; + background-size: cover; + margin: 0.25em; + cursor: pointer; +} diff --git a/client/src/Hand.js b/client/src/Hand.js new file mode 100644 index 0000000..7b49d42 --- /dev/null +++ b/client/src/Hand.js @@ -0,0 +1,141 @@ +import React, { useContext, useState, useMemo, useRef, useEffect } from "react"; +import equal from "fast-deep-equal"; + +import { Resource } from './Resource.js'; +import { Placard } from './Placard.js'; +import { GlobalContext } from './GlobalContext.js'; +import { assetsPath } from "./Common.js"; +import "./Hand.css"; + +const Development = ({type, card, onClick}) => { + return ( +
+ ); +}; + +const Hand = ({buildActive, setBuildActive, setCardActive}) => { + const { ws } = useContext(GlobalContext); + const [priv, setPriv] = useState(undefined); + const [color, setColor] = useState(undefined); + const [turn, setTurn] = useState(undefined); + const [longestRoad, setLongestRoad] = useState(undefined); + const [largestArmy, setLargestArmy] = useState(undefined); + const [development, setDevelopment] = useState([]); + + const fields = useMemo(() => [ + 'private', 'turn', 'color', 'longestRoad', 'largestArmy' + ], []); + const onWsMessage = (event) => { + const data = JSON.parse(event.data); + switch (data.type) { + case 'game-update': + console.log(`hand - game-update: `, data.update); + if ('private' in data.update && !equal(priv, data.update.private)) { + setPriv(data.update.private); + } + if ('turn' in data.update && !equal(turn, data.update.turn)) { + setTurn(data.update.turn); + } + if ('color' in data.update && color !== data.update.color) { + setColor(data.update.color); + } + if ('longestRoad' in data.update && longestRoad !== data.update.longestRoad) { + setLongestRoad(data.update.longestRoad); + } + if ('largestArmy' in data.update && largestArmy !== data.update.largestArmy) { + setLargestArmy(data.update.largestArmy); + } + 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]); + + useEffect(() => { + if (!priv) { + return; + } + + const cardClicked = (card) => { + setCardActive(card); + } + + const stacks = {}; + priv.development.forEach(card => + (card.type in stacks) + ? stacks[card.type].push(card) + : stacks[card.type] = [card]); + + const development = []; + for (let type in stacks) { + const cards = stacks[type] + .sort((A, B) => { + if (A.played) { + return -1; + } + if (B.played) { + return +1; + } + return 0; + }).map(card => cardClicked(card)} + card={card} + key={`${type}-${card.card}`} + type={`${type}-${card.card}`}/>); + development.push(
{ cards }
); + } + setDevelopment(development); + }, [priv, setDevelopment, setCardActive]); + + if (!priv) { + return <>; + } + + return
+
+ + + + + +
+
+ { development } +
+ { longestRoad && longestRoad === color && + + } + { largestArmy && largestArmy === color && + + } + +
; +} + +export { Hand }; \ No newline at end of file