From a281c539cc2b1b58b0a2cd9f550a492d28a4c3db Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Sat, 12 Mar 2022 19:53:03 -0800 Subject: [PATCH] Started hooking in players-status Signed-off-by: James Ketrenos --- client/src/App.css | 5 ++ client/src/App.js | 4 +- client/src/BoardPieces.js | 9 +-- client/src/MediaControl.js | 9 ++- client/src/Placard.js | 2 +- client/src/PlayersStatus.js | 129 ++++++++++++++++++++++++------------ 6 files changed, 102 insertions(+), 56 deletions(-) diff --git a/client/src/App.css b/client/src/App.css index 585af2e..cae1a0d 100755 --- a/client/src/App.css +++ b/client/src/App.css @@ -59,6 +59,11 @@ body { padding: 1rem; } + +.Table .PlayersStatus { + z-index: 5000; +} + .Table .Game { position: relative; display: flex; diff --git a/client/src/App.js b/client/src/App.js index 3ffb29d..061d292 100755 --- a/client/src/App.js +++ b/client/src/App.js @@ -20,6 +20,7 @@ import { base, gamesPath } from './Common.js'; import { GameOrder } from "./GameOrder.js"; import { Activities } from "./Activities.js"; import { SelectPlayer } from "./SelectPlayer.js"; +import { PlayersStatus } from "./PlayersStatus.js"; import history from "./history.js"; import "./App.css"; @@ -296,7 +297,8 @@ const Table = () => { { color && state === 'game-order' && } - + + { /* state === 'winner' && } diff --git a/client/src/BoardPieces.js b/client/src/BoardPieces.js index ddffb24..ac10406 100644 --- a/client/src/BoardPieces.js +++ b/client/src/BoardPieces.js @@ -19,12 +19,7 @@ const City = ({ color, onClick }) => { return
onClick('city')}>
; } -const BoardPieces = ({ table, color, onClick }) => { - if (!table.game || !table.game.player) { - return <>; - } - - const player = table.game.players[color]; +const BoardPieces = ({ player, onClick, color }) => { if (!player) { return <>; } @@ -51,4 +46,4 @@ const BoardPieces = ({ table, color, onClick }) => { ); }; -export default BoardPieces; \ No newline at end of file +export {BoardPieces}; \ No newline at end of file diff --git a/client/src/MediaControl.js b/client/src/MediaControl.js index d54893a..63c7f0b 100644 --- a/client/src/MediaControl.js +++ b/client/src/MediaControl.js @@ -72,11 +72,14 @@ const MediaAgent = () => { return; } const connection = new RTCPeerConnection({ - /*configuration: { + configuration: { offerToReceiveAudio: true, offerToReceiveVideo: false - },*/ - iceServers: [ { urls: "stun:stun.l.google.com:19302" } ] + }, + iceServers: [ + { urls: "stun:stun.l.google.com:19302" }, + { urls: "stun:stun.stunprotocol.org:3478" } + ] }, { /* this will no longer be needed by chrome * eventually (supposedly), but is necessary diff --git a/client/src/Placard.js b/client/src/Placard.js index 36011bb..9baddd9 100644 --- a/client/src/Placard.js +++ b/client/src/Placard.js @@ -76,4 +76,4 @@ const Placard = ({table, type, active, disabled, count}) => { } }; -export default Placard; +export {Placard}; diff --git a/client/src/PlayersStatus.js b/client/src/PlayersStatus.js index ca0edb8..651ee19 100644 --- a/client/src/PlayersStatus.js +++ b/client/src/PlayersStatus.js @@ -1,18 +1,19 @@ -import React from "react"; -import Resource from './Resource.js'; -import "./PlayersStatus.css"; -import BoardPieces from './BoardPieces.js'; -import { getPlayerName } from './Common.js'; -import PlayerColor from './PlayerColor.js'; -import Placard from './Placard.js'; +import React, { useContext, useState, useMemo, useRef, useEffect } from "react"; +import equal from "fast-deep-equal"; -const Player = ({ table, color, onClick, reverse }) => { - if (!table.game) { +import "./PlayersStatus.css"; +import { BoardPieces } from './BoardPieces.js'; +import { Resource } from './Resource.js'; +import { PlayerColor } from './PlayerColor.js'; +import { Placard } from './Placard.js'; +import { GlobalContext } from './GlobalContext.js'; + +const Player = ({ player, onClick, reverse, color, + largestArmy, largestArmySize, isSelf, longestRoad, longestRoadLength }) => { + if (!player) { return <>; } - const game = table.game; - const player = game.players[color]; - + const developmentCards = player.unplayed ? @@ -29,28 +30,28 @@ const Player = ({ table, color, onClick, reverse }) => { disabled/>{player.points}; } - const longestRoad = game.longestRoad && game.longestRoad === color ? + const longestRoadPlacard = longestRoad && longestRoad === color ? : undefined; - const largestArmy = game.largestArmy && game.largestArmy === color ? + const largestArmyPlacard = largestArmy && largestArmy === color ? : undefined; return
- {getPlayerName(game.sessions, color)} + {player.name}
- { game.color === color && + { isSelf &&
Longest road: {player.longestRoad ? player.longestRoad : 0}
@@ -59,58 +60,98 @@ const Player = ({ table, color, onClick, reverse }) => { { (largestArmy || longestRoad || armyCards || developmentCards) && <>
{ !reverse && <> - { largestArmy } - { longestRoad } - { !largestArmy && armyCards } + { largestArmyPlacard } + { longestRoadPlacard } + { !largestArmyPlacard && armyCards } { developmentCards } } { reverse && <> { developmentCards } - { !largestArmy && armyCards } - { longestRoad } - { largestArmy } + { !largestArmyPlacard && armyCards } + { longestRoadPlacard } + { largestArmyPlacard } }
}
- +
}; -const PlayersStatus = ({ table, onClick, color, active }) => { - if (!table.game) { +const PlayersStatus = ({ active }) => { + const { ws } = useContext(GlobalContext); + const [players, setPlayers] = useState(undefined); + const [color, setColor] = useState(undefined); + const fields = useMemo(() => [ + 'players', 'color' + ], []); + const onWsMessage = (event) => { + const data = JSON.parse(event.data); + switch (data.type) { + case 'game-update': + console.log(`players-status - game-update: `, data.update); + if ('players' in data.update && !equal(players, data.update.players)) { + setPlayers(data.update.players); + } + 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]); + + if (!players) { return <>; } - const game = table.game; - let players; - if (color) { - players = { + console.log(`player-status - build-item`); + } + + let elements; + if (active) { + elements = ; } else { - players = Object.getOwnPropertyNames(game.players) - .filter(color => game.players[color].status === 'Active' - && game.color !== color) - .map(color => { + elements = Object.getOwnPropertyNames(players) + .filter(key => players[key].status === 'Active' + && color !== key) + .map(key => { return ; + player={players[key]} + key={`PlayerStatus-${key}}`} + color={key}/>; }); } return (
- { players } + { elements }
); } -export default PlayersStatus; \ No newline at end of file +export { PlayersStatus }; \ No newline at end of file