From e0d3c291dacb269183ef6a31c4e144413210d61a Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Tue, 14 Nov 2023 13:23:40 +0100 Subject: [PATCH] serveur pour le jeu presque fini --- cryptide_project/src/Components/ChoiceBar.tsx | 26 +++++++++---------- .../src/Components/GraphContainer.tsx | 18 +++++++------ cryptide_project/src/Contexts/GameContext.tsx | 9 ++++++- cryptide_project/src/Pages/InGame.tsx | 4 ++- cryptide_project/src/Pages/Lobby.tsx | 5 ++-- 5 files changed, 36 insertions(+), 26 deletions(-) diff --git a/cryptide_project/src/Components/ChoiceBar.tsx b/cryptide_project/src/Components/ChoiceBar.tsx index 96f40f4..74e22bd 100644 --- a/cryptide_project/src/Components/ChoiceBar.tsx +++ b/cryptide_project/src/Components/ChoiceBar.tsx @@ -13,7 +13,6 @@ const ChoiceBar = () => { function askPlayer(playerId: string){ if (nodeId !== null){ - console.log("CLICK") socket.emit("ask player", nodeId, playerId, players.find((p) => p.id === socket.id)) } } @@ -23,29 +22,28 @@ const ChoiceBar = () => { const person = personNetwork?.getPersons().find((p) => p.getId() == nodeId) if (person != undefined){ const indiceTester = IndiceTesterFactory.Create(indices[actualPlayerIndex]) + let nextPlayerIndex = actualPlayerIndex + 1 + if (nextPlayerIndex == players.length){ + nextPlayerIndex = 0 + } + let playerIndex = actualPlayerIndex + 1 if (indiceTester.Works(person)){ - socket.emit("node checked", nodeId, true, positionToColor(actualPlayerIndex), room, actualPlayerIndex + 1) + socket.emit("node checked", nodeId, true, positionToColor(actualPlayerIndex), room, nextPlayerIndex) while(playerIndex != actualPlayerIndex){ + if (playerIndex == players.length){ + playerIndex = 0 + } const tester = IndiceTesterFactory.Create(indices[playerIndex]) const works = tester.Works(person) - socket.emit("node checked", nodeId, works, positionToColor(playerIndex), room, actualPlayerIndex + 1) - if (!works){ - break + socket.emit("node checked", nodeId, works, positionToColor(playerIndex), room, nextPlayerIndex) + if(!works){ + return } playerIndex ++ - if (playerIndex == players.length){ - playerIndex = 0 - } } } - else{ - console.log("Ne marche pas") - console.log(person) - } } - console.log("CLICK") - //socket.emit("ask player", nodeId, playerId, players.find((p) => p.id === socket.id)) } } diff --git a/cryptide_project/src/Components/GraphContainer.tsx b/cryptide_project/src/Components/GraphContainer.tsx index 96bafbd..6985a93 100644 --- a/cryptide_project/src/Components/GraphContainer.tsx +++ b/cryptide_project/src/Components/GraphContainer.tsx @@ -33,9 +33,9 @@ let first = true const MyGraphComponent: React.FC = ({onNodeClick, handleShowTurnBar}) => { - const { indices, indice, person, personNetwork, setNodeIdData, players, askedPersons, setActualPlayerIndexData, room, actualPlayerIndex } = useGame(); + const { indices, indice, person, personNetwork, setNodeIdData, players, askedPersons, setActualPlayerIndexData, room, actualPlayerIndex, turnPlayerIndex } = useGame(); - let playerIndex: number = actualPlayerIndex + let playerIndex: number = turnPlayerIndex let index = 0 for (let i=0; i = ({onNodeClick, handleS } } let thisPlayerIndex = index - if (playerIndex == thisPlayerIndex){ - handleShowTurnBar(true) - } + if (first){ first = false - console.log(indice) - + setActualPlayerIndexData(index) + if (playerIndex == thisPlayerIndex){ + handleShowTurnBar(true) + } indices.forEach(i => { console.log(i.ToString("en")) }); @@ -104,11 +104,13 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS onNodeClick(false) playerIndex = newPlayerIndex networkData.nodes.update({id: id, label: node.label + colorToEmoji(color, works)}) - if (playerIndex == thisPlayerIndex){ + console.log(playerIndex + " => " + thisPlayerIndex) + if (playerIndex === thisPlayerIndex){ handleShowTurnBar(true) } else{ handleShowTurnBar(false) + console.log("je passe bien ici ?????") } } lastAskingPlayer = 0 diff --git a/cryptide_project/src/Contexts/GameContext.tsx b/cryptide_project/src/Contexts/GameContext.tsx index 93e499b..a725498 100644 --- a/cryptide_project/src/Contexts/GameContext.tsx +++ b/cryptide_project/src/Contexts/GameContext.tsx @@ -13,6 +13,7 @@ interface GameContextProps { nodeId: number | null askedPersons: Person[]; actualPlayerIndex: number; + turnPlayerIndex: number; room: string; setIndicesData: (newIndices: Indice[]) => void; setIndiceData: (newIndice: Indice) => void; @@ -22,6 +23,7 @@ interface GameContextProps { setNodeIdData: (newId: number) => void; setAskedPersonsData: (newAskedPersons: Person[]) => void; setActualPlayerIndexData: (newActualPlayerIndex: number) => void; + setTurnPlayerIndexData: (newTurnPlayerIndex: number) => void; setRoomData: (newRoom: string) => void; } @@ -40,6 +42,7 @@ export const GameProvider: React.FC = ({ children }) => { const [nodeId, setNodeId] = useState(null); const [askedPersons, setAskedPersons] = useState([]) const [actualPlayerIndex, setActualPlayerIndex] = useState(-1) + const [turnPlayerIndex, setTurnPlayerIndex] = useState(-1) const [room, setRoom] = useState("") @@ -76,12 +79,16 @@ export const GameProvider: React.FC = ({ children }) => { setActualPlayerIndex(newActualPlayerIndex) } + const setTurnPlayerIndexData = (newTurnPlayerIndex: number) =>{ + setTurnPlayerIndex(newTurnPlayerIndex) + } + const setRoomData = (newRoom: string) =>{ setRoom(newRoom) } return ( - + {children} ); diff --git a/cryptide_project/src/Pages/InGame.tsx b/cryptide_project/src/Pages/InGame.tsx index 180eab3..a346071 100644 --- a/cryptide_project/src/Pages/InGame.tsx +++ b/cryptide_project/src/Pages/InGame.tsx @@ -50,8 +50,10 @@ const InGame = ({locale, changeLocale}) => { const handleShowTurnBar = (shouldShowTurnBar: boolean) => { + console.log("je passe bien ici avec : " + shouldShowTurnBar) setShowTurnBar(shouldShowTurnBar); }; + /* offcanvas */ //? faire une fonction pour close et show en fonction de l'etat du canva ? //? comment faire pour eviter la recopie de tout le code a chaque canvas boostrap ? @@ -107,7 +109,7 @@ const InGame = ({locale, changeLocale}) => { return (
- {showTurnBar && } + {showTurnBar && }
diff --git a/cryptide_project/src/Pages/Lobby.tsx b/cryptide_project/src/Pages/Lobby.tsx index 6941be6..b1f8811 100644 --- a/cryptide_project/src/Pages/Lobby.tsx +++ b/cryptide_project/src/Pages/Lobby.tsx @@ -31,7 +31,7 @@ function Lobby() { const navigate = useNavigate(); - const { indices, setIndicesData, indice, setIndiceData, person, setPersonData, personNetwork, setPersonNetworkData, players, setPlayersData, setActualPlayerIndexData, setRoomData } = useGame(); + const { indices, setIndicesData, indice, setIndiceData, person, setPersonData, personNetwork, setPersonNetworkData, players, setPlayersData, setActualPlayerIndexData, setTurnPlayerIndexData, setRoomData } = useGame(); let first = true @@ -65,7 +65,8 @@ function Lobby() { if (room != null){ setRoomData(room) } - setActualPlayerIndexData(playerIndex) + setTurnPlayerIndexData(playerIndex) + setActualPlayerIndexData(index) setIndiceData(choosenIndices[index]) setPersonData(choosenOne) setPersonNetworkData(network)