From ed69aebcadb85254b159b203cf6c69f463f039e6 Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Wed, 15 Nov 2023 10:48:20 +0100 Subject: [PATCH 1/5] find du jeu de master mind --- cryptide_project/src/Components/ChoiceBar.tsx | 4 +- .../src/Components/GraphContainer.tsx | 44 ++++++++++++++----- cryptide_project/src/Pages/Play.tsx | 2 +- cryptide_project/src/Style/Theme.tsx | 4 +- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/cryptide_project/src/Components/ChoiceBar.tsx b/cryptide_project/src/Components/ChoiceBar.tsx index 96422d2..1329712 100644 --- a/cryptide_project/src/Components/ChoiceBar.tsx +++ b/cryptide_project/src/Components/ChoiceBar.tsx @@ -29,7 +29,7 @@ const ChoiceBar = () => { let playerIndex = actualPlayerIndex + 1 if (indiceTester.Works(person)){ - socket.emit("node checked", nodeId, true, positionToColor(actualPlayerIndex), room, nextPlayerIndex) + socket.emit("node checked", nodeId, true, actualPlayerIndex, room, nextPlayerIndex) while(playerIndex != actualPlayerIndex){ if (playerIndex == players.length){ playerIndex = 0 @@ -38,7 +38,7 @@ const ChoiceBar = () => { const works = tester.Works(person) await delay(500); socket.emit("asked all 1by1", person.getId(), players[playerIndex].id) - socket.emit("node checked", nodeId, works, positionToColor(playerIndex), room, nextPlayerIndex) + socket.emit("node checked", nodeId, works, playerIndex, room, nextPlayerIndex) if(!works){ return } diff --git a/cryptide_project/src/Components/GraphContainer.tsx b/cryptide_project/src/Components/GraphContainer.tsx index 0aa002c..de859f5 100644 --- a/cryptide_project/src/Components/GraphContainer.tsx +++ b/cryptide_project/src/Components/GraphContainer.tsx @@ -16,7 +16,7 @@ import JSONParser from "../JSONParser"; import PersonNetwork from "../model/PersonsNetwork"; import Person from "../model/Person"; import Indice from "../model/Indices/Indice"; -import { useLocation } from "react-router-dom"; +import { Navigate, useLocation, useNavigate } from "react-router-dom"; import { useGame } from "../Contexts/GameContext"; import { socket } from "../SocketConfig" import { colorToEmoji, positionToColor } from "../ColorHelper"; @@ -33,6 +33,7 @@ let lastNodeId = -1 let first = true let askedWrong = false let solo: boolean = true +let mapIndexPersons: Map = new Map() const MyGraphComponent: React.FC = ({onNodeClick, handleShowTurnBar, handleTurnBarTextChange}) => { @@ -41,6 +42,8 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS const params = new URLSearchParams(window.location.search); const solotmp = params.get('solo'); + const navigate = useNavigate(); + let playerIndex: number = turnPlayerIndex @@ -60,6 +63,9 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS solo=false } if (!solo){ + for(let i = 0; i = ({onNodeClick, handleS } }) - socket.on("node checked",(id, works, color, newPlayerIndex) => { + socket.on("node checked",(id, works, askedIndex, newPlayerIndex) => { const node = nodes.get().find((n) => id == n.id) if (node!=undefined){ onNodeClick(false) playerIndex = newPlayerIndex - if (!node.label.includes(colorToEmoji(color, works))){ - networkData.nodes.update({id: id, label: node.label + colorToEmoji(color, works)}) + if (!node.label.includes(colorToEmoji(positionToColor(askedIndex), works))){ + networkData.nodes.update({id: id, label: node.label + colorToEmoji(positionToColor(askedIndex), works)}) } if (playerIndex === thisPlayerIndex){ handleTurnBarTextChange("À vous de jouer") @@ -167,14 +173,14 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS playerIndex = 0 } if (tester.Works(pers)){ - socket.emit("node checked", nodeId, true, positionToColor(thisPlayerIndex), room, playerIndex) + socket.emit("node checked", nodeId, true, thisPlayerIndex, room, playerIndex) } else{ maybe = maybe - 1 if(maybe == 0){ maybe = players.length - 1 } - socket.emit("node checked", nodeId, false, positionToColor(thisPlayerIndex), room, maybe) + socket.emit("node checked", nodeId, false, thisPlayerIndex, room, maybe) socket.emit("asked wrong", askingPlayer, room) } } @@ -213,7 +219,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS } }); - network.on("click", (params) => { + network.on("click", async (params) => { if(params.nodes.length > 0){ setNodeIdData(params.nodes[0]) @@ -227,7 +233,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS if(playerIndex == players.length){ playerIndex = 0 } - socket.emit("node checked", params.nodes[0], false, positionToColor(thisPlayerIndex), room, playerIndex) + socket.emit("node checked", params.nodes[0], false, thisPlayerIndex, room, playerIndex) askedPersons.push(person) askedWrong = false } @@ -243,14 +249,28 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS else{ const person = personNetwork?.getPersons().find((p) => p.getId() == params.nodes[0]) //person sélectionnée if (person != undefined){ - indices.forEach(async (i, index) =>{ + let index =0 + let works = true + let promises: Promise[] = [] + for (const i of indices){ const tester = IndiceTesterFactory.Create(i) const test = tester.Works(person) const node = nodes.get().find((n) => params.nodes[0] == n.id) if (node!=undefined){ - networkData.nodes.update({id: params.nodes[0], label: node.label + colorToEmoji(positionToColor(index), test)}) + if (!node.label.includes(colorToEmoji(positionToColor(index), test))){ + networkData.nodes.update({id: params.nodes[0], label: node.label + colorToEmoji(positionToColor(index), test)}) + await delay(500) + if(!test){ + works = false + } + if (index == indices.length - 1 && works){ + navigate("/endgame") + } + + } } - }) + index++ + } } } } @@ -268,7 +288,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS ); - function delay(ms: number) { + function delay(ms: number): Promise { return new Promise(resolve => setTimeout(resolve, ms)); } } diff --git a/cryptide_project/src/Pages/Play.tsx b/cryptide_project/src/Pages/Play.tsx index 13d3f62..2c4b42a 100644 --- a/cryptide_project/src/Pages/Play.tsx +++ b/cryptide_project/src/Pages/Play.tsx @@ -37,7 +37,7 @@ function Play() { } function launchMastermind(){ - const [networkPerson, choosenPerson, choosenIndices] = GameCreator.CreateGame(5, 30) + const [networkPerson, choosenPerson, choosenIndices] = GameCreator.CreateGame(3, 30) setPersonData(choosenPerson) setPersonNetworkData(networkPerson) setIndicesData(choosenIndices) diff --git a/cryptide_project/src/Style/Theme.tsx b/cryptide_project/src/Style/Theme.tsx index 50df20a..d29d470 100644 --- a/cryptide_project/src/Style/Theme.tsx +++ b/cryptide_project/src/Style/Theme.tsx @@ -1,7 +1,7 @@ // theme.js const theme = { colors: { - primary: '#0064E0', + primary: '#7AA3F4', secondary: '#0052B8', text: '#fff' @@ -10,4 +10,4 @@ const theme = { }, }; -export default theme; +export default theme; \ No newline at end of file From deb3e59cae48a3a465170033ba22b6a1a9a7431b Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Wed, 15 Nov 2023 16:35:15 +0100 Subject: [PATCH 2/5] fin du jeu en multi + grisage des cases impossibles --- cryptide_project/server/server.js | 16 ++ .../src/Components/GraphContainer.tsx | 151 ++++++++++++++++-- .../src/Components/PersonStatus.tsx | 39 ++++- .../src/Components/PlayerList.tsx | 61 ++++++- cryptide_project/src/Contexts/GameContext.tsx | 9 +- cryptide_project/src/Pages/EndGame.tsx | 16 +- cryptide_project/src/Pages/InGame.tsx | 32 ++-- cryptide_project/src/Translations/en.json | 2 +- cryptide_project/src/Translations/fr.json | 2 +- .../src/model/Graph/GraphCreator.ts | 2 +- .../NbEdgesIndiceEdgesCreator.ts | 2 +- .../model/IndiceTester/NbEdgesIndiceTester.ts | 2 +- cryptide_project/src/model/Stub.ts | 17 +- 13 files changed, 286 insertions(+), 65 deletions(-) diff --git a/cryptide_project/server/server.js b/cryptide_project/server/server.js index faee172..9364c59 100644 --- a/cryptide_project/server/server.js +++ b/cryptide_project/server/server.js @@ -84,4 +84,20 @@ io.on('connection', (socket) => { io.to(room).emit("node checked", id, works, color, playerIndex) }) + socket.on("put correct background", (id) =>{ + io.to(id).emit("put correct background") + }) + + socket.on("put grey background", (id, player) =>{ + io.to(id).emit("put grey background", player) + }) + + socket.on("put imossible grey", (id) =>{ + io.to(id).emit("put imossible grey") + }) + + socket.on("end game", (winnerIndex, room) =>{ + console.log("endgame") + io.to(room).emit("end game", winnerIndex) + }) }); diff --git a/cryptide_project/src/Components/GraphContainer.tsx b/cryptide_project/src/Components/GraphContainer.tsx index de859f5..96b88d1 100644 --- a/cryptide_project/src/Components/GraphContainer.tsx +++ b/cryptide_project/src/Components/GraphContainer.tsx @@ -1,4 +1,4 @@ -import React, { useEffect } from "react"; +import React, { useEffect, useState } from "react"; import { DataSet, Network} from "vis-network/standalone/esm/vis-network"; import EdgesCreator from "../model/EdgesCreator"; import GraphCreator from "../model/Graph/GraphCreator"; @@ -20,12 +20,15 @@ import { Navigate, useLocation, useNavigate } from "react-router-dom"; import { useGame } from "../Contexts/GameContext"; import { socket } from "../SocketConfig" import { colorToEmoji, positionToColor } from "../ColorHelper"; +import { ColorToHexa } from "../model/EnumExtender"; interface MyGraphComponentProps { onNodeClick: (shouldShowChoiceBar: boolean) => void; handleShowTurnBar: (shouldShowTurnBar: boolean) => void handleTurnBarTextChange: (newTurnBarText: string) => void + setPlayerTouched: (newPlayerTouch: number) => void; + playerTouched: number } let lastAskingPlayer = 0 @@ -34,17 +37,35 @@ let first = true let askedWrong = false let solo: boolean = true let mapIndexPersons: Map = new Map() +let touchedPlayer = -1 -const MyGraphComponent: React.FC = ({onNodeClick, handleShowTurnBar, handleTurnBarTextChange}) => { - - const { indices, indice, person, personNetwork, setNodeIdData, players, askedPersons, setActualPlayerIndexData, room, actualPlayerIndex, turnPlayerIndex, onlyFalse, setOnlyFalseData } = useGame(); +const MyGraphComponent: React.FC = ({onNodeClick, handleShowTurnBar, handleTurnBarTextChange, playerTouched, setPlayerTouched}) => { + const { indices, indice, person, personNetwork, setNodeIdData, players, askedPersons, setActualPlayerIndexData, room, actualPlayerIndex, turnPlayerIndex, setWinnerData } = useGame(); const params = new URLSearchParams(window.location.search); const solotmp = params.get('solo'); const navigate = useNavigate(); + useEffect(() =>{ + touchedPlayer=playerTouched + if (touchedPlayer == -1){ + if (!askedWrong){ + socket.emit("put correct background", socket.id) + } + } + else if (touchedPlayer < players.length && touchedPlayer>=0){ + if(!askedWrong){ + socket.emit("put correct background", socket.id) + socket.emit("put grey background", socket.id, touchedPlayer) + } + } + else if(touchedPlayer == players.length){ + socket.emit("put imossible grey", socket.id) + } + }, [playerTouched]) + let playerIndex: number = turnPlayerIndex let index = 0 @@ -111,6 +132,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS const networkData = { nodes: nodes, edges: graph.edges }; const network = new Network(container, networkData, initialOptions); + if (!solo){ socket.on("asked all", (id) =>{ const pers = personNetwork.getPersons().find((p) => p.getId() == id) @@ -124,9 +146,18 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS if (node!=undefined){ onNodeClick(false) playerIndex = newPlayerIndex + if (mapIndexPersons.get(askedIndex)?.find((p) => p.getId() == id) == undefined){ + const p = personNetwork.getPersons().find((p)=> p.getId() == id) + const tab = mapIndexPersons.get(askedIndex) + if (p!=undefined && tab != undefined){ + tab.push(p) + } + } + if (!node.label.includes(colorToEmoji(positionToColor(askedIndex), works))){ networkData.nodes.update({id: id, label: node.label + colorToEmoji(positionToColor(askedIndex), works)}) } + if (playerIndex === thisPlayerIndex){ handleTurnBarTextChange("À vous de jouer") handleShowTurnBar(true) @@ -144,15 +175,14 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS }) socket.on("asked wrong", () =>{ - setOnlyFalseData(true) askedWrong = true handleShowTurnBar(true) handleTurnBarTextChange("Mauvais choix, posez un carré !") + socket.emit("put grey background", socket.id, thisPlayerIndex) }) socket.on("asked", (nodeId, askingPlayer, askingPlayerIndex) => { - console.log(askingPlayerIndex) if (askingPlayer.id !== lastAskingPlayer || nodeId !== lastNodeId ){ lastAskingPlayer = askingPlayer.id lastNodeId = nodeId @@ -192,6 +222,61 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS } + socket.on("put correct background", () =>{ + if (personNetwork != null){ + for(const person of personNetwork.getPersons()){ + networkData.nodes.update({id: person.getId(), color: ColorToHexa(person.getColor())}) + } + } + }) + + socket.on("put grey background", (player) =>{ + if (personNetwork != null){ + const tab = mapIndexPersons.get(player) + if (tab != undefined){ + if (player != thisPlayerIndex){ + for(const person of personNetwork.getPersons().filter((p) => tab.includes(p))){ + networkData.nodes.update({id: person.getId(), color: "#808080"}) + } + } + else if(indice != null){ + const tester = IndiceTesterFactory.Create(indice) + for(const person of personNetwork.getPersons().filter((p) => tab.includes(p) || tester.Works(p))){ + networkData.nodes.update({id: person.getId(), color: "#808080"}) + } + } + } + } + }) + + socket.on("put imossible grey", ()=>{ + if (personNetwork != null && indice!=null){ + const tabNodes: any = [] + const tester = IndiceTesterFactory.Create(indice) + for (const pers of personNetwork.getPersons()){ + const node = nodes.get().find((n) => pers.getId() == n.id) + if (node != undefined){ + for(let i=0; i{ + setWinnerData(players[winnerIndex]) + navigate("/endgame") + }) + personNetwork.getPersons().forEach(p => { @@ -234,24 +319,59 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS playerIndex = 0 } socket.emit("node checked", params.nodes[0], false, thisPlayerIndex, room, playerIndex) + socket.emit("put correct background", socket.id) + touchedPlayer=-1 askedPersons.push(person) askedWrong = false } } } - else if (thisPlayerIndex == playerIndex){ - onNodeClick(true) + else if(touchedPlayer != - 1 && playerIndex == actualPlayerIndex && touchedPlayer p.id === socket.id, actualPlayerIndex)) + socket.emit("put correct background", socket.id) + touchedPlayer=-1 } - else{ - onNodeClick(false) + else if(playerIndex == actualPlayerIndex && touchedPlayer==players.length){ + const person = personNetwork?.getPersons().find((p) => p.getId() == params.nodes[0]) + 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", params.nodes[0], true, actualPlayerIndex, room, nextPlayerIndex) + while(playerIndex != actualPlayerIndex){ + if (playerIndex == players.length){ + playerIndex = 0 + } + const tester = IndiceTesterFactory.Create(indices[playerIndex]) + const works = tester.Works(person) + await delay(500); + socket.emit("asked all 1by1", person.getId(), players[playerIndex].id) + socket.emit("node checked", params.nodes[0], works, playerIndex, room, nextPlayerIndex) + if(!works){ + socket.emit("put correct background", socket.id) + touchedPlayer=-1 + return + } + playerIndex ++ + } + touchedPlayer=-1 + socket.emit("put correct background", socket.id) + await delay(1000) + socket.emit("end game", thisPlayerIndex, room) + } + } } - } + } else{ const person = personNetwork?.getPersons().find((p) => p.getId() == params.nodes[0]) //person sélectionnée if (person != undefined){ let index =0 let works = true - let promises: Promise[] = [] for (const i of indices){ const tester = IndiceTesterFactory.Create(i) const test = tester.Works(person) @@ -278,6 +398,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS else{ // Renvoyer un false pour cacher la choice bar onNodeClick(false) + setPlayerTouched(-1) } }); }, []); // Le tableau vide signifie que cela ne s'exécutera qu'une fois après le premier rendu @@ -291,6 +412,12 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS function delay(ms: number): Promise { return new Promise(resolve => setTimeout(resolve, ms)); } + + function putGreyBackgroud(index: number){ + /* + + */ + } } diff --git a/cryptide_project/src/Components/PersonStatus.tsx b/cryptide_project/src/Components/PersonStatus.tsx index 7afcca2..5a3ab8f 100644 --- a/cryptide_project/src/Components/PersonStatus.tsx +++ b/cryptide_project/src/Components/PersonStatus.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; /* Style */ import '../Style/Global.css' @@ -7,17 +7,40 @@ import { useTheme } from '../Style/ThemeContext'; /* Ressources */ import Person from '../res/img/Person.png' import leave from '../res/img/bot.png' +interface PlayerStatusProps { + img: any + state: any + name: string + index: number + setPlayerTouched: (newPlayerTouch: number) => void; + playerTouched: number + } + let touchedPlayer = -1 //@ts-ignore -function PersonStatus({img = Person, state= Person, name = "Dummy"}) { +const PersonStatus: React.FC = ({img = Person, state= Person, name = "Dummy", index, playerTouched, setPlayerTouched}) => { const theme=useTheme(); + + const [touchedPlayer, setTouchedPlayer] = useState(-2) + useEffect(() =>{ + setTouchedPlayer(playerTouched) + console.log(touchedPlayer) + }, [playerTouched]) return ( -
- player -

{name}

-
- state -
+
setPlayerTouched(index)}> + player +
{name}
+ {(touchedPlayer == index) ?( +
+ state +
+ ): + ( +
+ state +
+ ) } +
); } diff --git a/cryptide_project/src/Components/PlayerList.tsx b/cryptide_project/src/Components/PlayerList.tsx index 5dcfb25..6d01a0f 100644 --- a/cryptide_project/src/Components/PlayerList.tsx +++ b/cryptide_project/src/Components/PlayerList.tsx @@ -1,19 +1,66 @@ import React from 'react'; import { colorToEmoji, positionToColor } from '../ColorHelper'; +import Player from '../model/Player'; +import { useTheme } from '../Style/ThemeContext'; import PersonStatus from './PersonStatus'; +import Person from '../res/img/Person.png' +import { socket } from '../SocketConfig'; + //@ts-ignore -function PlayerList({ players }) { + +interface PlayerListProps { + players: Player[]; + playerTouched: number + setPlayerTouched: (newPlayerTouch: number) => void; + } + +const PlayerList: React.FC = ({ players, playerTouched, setPlayerTouched}) => { + const theme = useTheme(); + return ( -
- { - //@ts-ignore - players.map((player, index) => ( - - )) +
+
+ { + //@ts-ignore + players.map((player, index) => ( + player.id!=socket.id && + )) + } +
+
+ {(playerTouched == players.length) + ?( + + ): + ( + + ) } +
+ ); } diff --git a/cryptide_project/src/Contexts/GameContext.tsx b/cryptide_project/src/Contexts/GameContext.tsx index 380d7d3..bfc46ef 100644 --- a/cryptide_project/src/Contexts/GameContext.tsx +++ b/cryptide_project/src/Contexts/GameContext.tsx @@ -16,6 +16,7 @@ interface GameContextProps { turnPlayerIndex: number; room: string; onlyFalse: boolean + winner: Player | null setIndicesData: (newIndices: Indice[]) => void; setIndiceData: (newIndice: Indice) => void; setPersonData: (newPerson: Person) => void; @@ -27,6 +28,7 @@ interface GameContextProps { setTurnPlayerIndexData: (newTurnPlayerIndex: number) => void; setRoomData: (newRoom: string) => void; setOnlyFalseData: (newOnlyFalse: boolean) => void + setWinnerData: (winner: Player) => void } const GameContext = createContext(undefined); @@ -47,6 +49,7 @@ export const GameProvider: React.FC = ({ children }) => { const [turnPlayerIndex, setTurnPlayerIndex] = useState(-1) const [room, setRoom] = useState("") const [onlyFalse, setOnlyFalse] = useState(false) + const [winner, setWinner] = useState(null) const setIndicesData = (newIndices: Indice[]) => { @@ -94,8 +97,12 @@ export const GameProvider: React.FC = ({ children }) => { setOnlyFalse(newOnlyFalse) } + const setWinnerData = (winner: Player) =>{ + setWinner(winner) + } + return ( - + {children} ); diff --git a/cryptide_project/src/Pages/EndGame.tsx b/cryptide_project/src/Pages/EndGame.tsx index 5984075..dea7588 100644 --- a/cryptide_project/src/Pages/EndGame.tsx +++ b/cryptide_project/src/Pages/EndGame.tsx @@ -21,17 +21,20 @@ import { Link } from 'react-router-dom'; /* lang */ import { FormattedMessage } from 'react-intl'; +import { useGame } from '../Contexts/GameContext'; function EndGame() { + const {winner, person} =useGame() + console.log(winner) const theme = useTheme(); return (
-

Dummy a gagné !

-

Le tueur était Bob

+

{winner?.name} a gagné !

+

Le tueur était {person?.getName()}

@@ -42,9 +45,12 @@ function EndGame() {
- - - + {/* + + + + */} +
diff --git a/cryptide_project/src/Pages/InGame.tsx b/cryptide_project/src/Pages/InGame.tsx index db37efe..73bccaf 100644 --- a/cryptide_project/src/Pages/InGame.tsx +++ b/cryptide_project/src/Pages/InGame.tsx @@ -43,10 +43,15 @@ const InGame = ({locale, changeLocale}) => { const [showChoiceBar, setShowChoiceBar] = useState(false); const [showTurnBar, setShowTurnBar] = useState(false); const [turnBarText, setTurnBarText] = useState(""); + const [playerTouched, setPlayerTouched] = useState(-2) const handleNodeClick = (shouldShowChoiceBar: boolean) => { setShowChoiceBar(shouldShowChoiceBar); }; + + const handleSetPlayerTouched = (newPlayerTouched: number) => { + setPlayerTouched(newPlayerTouched); + }; const handleShowTurnBar = (shouldShowTurnBar: boolean) => { @@ -114,18 +119,7 @@ const InGame = ({locale, changeLocale}) => {
{showTurnBar && }
- -
- -
- +
@@ -172,6 +166,7 @@ const InGame = ({locale, changeLocale}) => {
+{/* @@ -179,15 +174,14 @@ const InGame = ({locale, changeLocale}) => {

Il y a {players.length} joueurs

- {/* affichage d'une liste responsive dynamic */} - {/*
- - - -
*/} - +
+ */} +
+ +
+ { - let label = p.getName() + "\n" + p.getAge() + "\n" + let label = p.getName() + "\n" + p.getAge() + "🎂" + "\n" for (let i = 0; i { if (person.getFriends().length == nbEdges){ return diff --git a/cryptide_project/src/model/IndiceTester/NbEdgesIndiceTester.ts b/cryptide_project/src/model/IndiceTester/NbEdgesIndiceTester.ts index 8276c57..6956d48 100644 --- a/cryptide_project/src/model/IndiceTester/NbEdgesIndiceTester.ts +++ b/cryptide_project/src/model/IndiceTester/NbEdgesIndiceTester.ts @@ -12,7 +12,7 @@ class NbEdgesIndiceTester implements IndiceTester{ } Works(person: Person): boolean { - return person.getFriends().length >= this.nbEdgesIndice.getNbEdges() + return person.getFriends().length == this.nbEdgesIndice.getNbEdges() } } diff --git a/cryptide_project/src/model/Stub.ts b/cryptide_project/src/model/Stub.ts index eb32c76..2da86a4 100644 --- a/cryptide_project/src/model/Stub.ts +++ b/cryptide_project/src/model/Stub.ts @@ -11,16 +11,17 @@ import Sport from "./Sport" class Stub{ static GenerateIndice(): Indice[]{ - let indice = new NbEdgesIndice(1, 3) - let indice1 = new NbEdgesIndice(2, 4) - let ageIndice = new AgeIndice(3, 0, 14) - let ageIndice1 = new AgeIndice(4, 15, 19) - let ageIndice2 = new AgeIndice(5, 20, 29) - let ageIndice3 = new AgeIndice(6, 30, 100000) + let indice = new NbEdgesIndice(1, 2) + let indice1 = new NbEdgesIndice(2, 3) + let indice2 = new NbEdgesIndice(3, 4) + let ageIndice = new AgeIndice(4, 0, 14) + let ageIndice1 = new AgeIndice(5, 15, 19) + let ageIndice2 = new AgeIndice(6, 20, 29) + let ageIndice3 = new AgeIndice(7, 30, 100000) - let indices: Indice[] = [indice, indice1, ageIndice, ageIndice1, ageIndice2, ageIndice3] + let indices: Indice[] = [indice, indice1, indice2, ageIndice, ageIndice1, ageIndice2, ageIndice3] - let test = 7 + let test = 8 for (let i: Color=0; i<5; i++){ for (let j: Color=0; j<5; j++){ if (j==i){ From 23e4b24d5b5fe9cdd60135349cd49f55026f2c43 Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Thu, 16 Nov 2023 10:19:33 +0100 Subject: [PATCH 3/5] =?UTF-8?q?d=C3=A9but=20du=20bot=20facile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cryptide_project/server/server.js | 12 ++++---- .../src/Components/GraphContainer.tsx | 1 + .../src/Components/PersonStatus.tsx | 16 ++++++++--- .../src/Components/PlayerList.tsx | 2 +- cryptide_project/src/JSONParser.ts | 14 ++++++++++ cryptide_project/src/Pages/EndGame.css | 10 ++++++- cryptide_project/src/Pages/EndGame.tsx | 24 ++++++++++------ cryptide_project/src/Pages/Lobby.css | 6 ++++ cryptide_project/src/Pages/Lobby.tsx | 21 ++++++++++++-- cryptide_project/src/Translations/en.json | 2 +- cryptide_project/src/model/Bot.ts | 13 +++++++++ cryptide_project/src/model/EasyBot.ts | 28 +++++++++++++++++++ cryptide_project/src/model/EdgesCreator.ts | 3 +- cryptide_project/src/model/Human.tsx | 18 ++++++++++++ .../ColorIndiceEdgesCreator.ts | 2 +- .../model/IndiceTester/NbSportIndiceTester.ts | 2 +- .../src/model/Indices/NbSportIndice.ts | 17 ++++++++--- .../src/model/NetworkGenerator.ts | 18 +++++++----- cryptide_project/src/model/Player.ts | 5 ++-- cryptide_project/src/model/Stub.ts | 10 +++++-- 20 files changed, 182 insertions(+), 42 deletions(-) create mode 100644 cryptide_project/src/model/Bot.ts create mode 100644 cryptide_project/src/model/EasyBot.ts create mode 100644 cryptide_project/src/model/Human.tsx diff --git a/cryptide_project/server/server.js b/cryptide_project/server/server.js index 9364c59..46b2b64 100644 --- a/cryptide_project/server/server.js +++ b/cryptide_project/server/server.js @@ -28,20 +28,22 @@ io.on('connection', (socket) => { io.to(room).emit("game created", network, person, indices, Math.floor(Math.random() * map.get(room).length)) }); - socket.on("lobby joined", (room, name) =>{ - socket.join(room) + socket.on("lobby joined", (room, player) =>{ + if (player.type=="Human"){ + socket.join(room) + } if (map.get(room) == undefined){ - map.set(room, [{id: socket.id, name: name}]) + map.set(room, [{type: player.type, id: player.id, name: player.name}]) } else{ const tab = map.get(room) for(let i = 0; i = ({onNodeClick, handleS const solotmp = params.get('solo'); const navigate = useNavigate(); + console.log(person) useEffect(() =>{ touchedPlayer=playerTouched diff --git a/cryptide_project/src/Components/PersonStatus.tsx b/cryptide_project/src/Components/PersonStatus.tsx index 5a3ab8f..959b23b 100644 --- a/cryptide_project/src/Components/PersonStatus.tsx +++ b/cryptide_project/src/Components/PersonStatus.tsx @@ -6,7 +6,9 @@ import { useTheme } from '../Style/ThemeContext'; /* Ressources */ import Person from '../res/img/Person.png' -import leave from '../res/img/bot.png' +import BotImg from '../res/img/bot.png' +import { useGame } from '../Contexts/GameContext'; +import Bot from '../model/Bot'; interface PlayerStatusProps { img: any state: any @@ -14,12 +16,17 @@ interface PlayerStatusProps { index: number setPlayerTouched: (newPlayerTouch: number) => void; playerTouched: number + showCircle: boolean } let touchedPlayer = -1 //@ts-ignore -const PersonStatus: React.FC = ({img = Person, state= Person, name = "Dummy", index, playerTouched, setPlayerTouched}) => { +const PersonStatus: React.FC = ({img = Person, state= Person, name = "Dummy", index, playerTouched, setPlayerTouched, showCircle}) => { const theme=useTheme(); + const {players} = useGame() + if (players[index] instanceof Bot){ + img = BotImg + } const [touchedPlayer, setTouchedPlayer] = useState(-2) useEffect(() =>{ @@ -30,11 +37,12 @@ const PersonStatus: React.FC = ({img = Person, state= Person,
setPlayerTouched(index)}> player
{name}
- {(touchedPlayer == index) ?( + + {(touchedPlayer == index && showCircle) ?(
state
- ): + ): showCircle && (
state diff --git a/cryptide_project/src/Components/PlayerList.tsx b/cryptide_project/src/Components/PlayerList.tsx index 6d01a0f..850f524 100644 --- a/cryptide_project/src/Components/PlayerList.tsx +++ b/cryptide_project/src/Components/PlayerList.tsx @@ -25,7 +25,7 @@ const PlayerList: React.FC = ({ players, playerTouched, setPlay { //@ts-ignore players.map((player, index) => ( - player.id!=socket.id && + player.id!=socket.id && )) }
diff --git a/cryptide_project/src/JSONParser.ts b/cryptide_project/src/JSONParser.ts index 037cf19..fbd8ef2 100644 --- a/cryptide_project/src/JSONParser.ts +++ b/cryptide_project/src/JSONParser.ts @@ -1,3 +1,5 @@ +import EasyBot from "./model/EasyBot"; +import Human from "./model/Human"; import AgeIndice from "./model/Indices/AgeIndice"; import ColorEdgesIndice from "./model/Indices/ColorEdgesIndice"; import ColorIndice from "./model/Indices/ColorIndice"; @@ -7,6 +9,7 @@ import NbSportIndice from "./model/Indices/NbSportIndice"; import SportIndice from "./model/Indices/SportIndice"; import Person from "./model/Person"; import PersonNetwork from "./model/PersonsNetwork"; +import Player from "./model/Player"; class JSONParser{ @@ -78,6 +81,17 @@ class JSONParser{ }); return tabIndice } + + static JSONToPlayer(json: any): Player{ + switch (json.type){ + case "Human": + return new Human(json.id, json.name) + case "EasyBot": + return new EasyBot(json.id, json.name) + default: + throw new Error("PARSER unable to parse player: " + json.type); + } + } } export default JSONParser \ No newline at end of file diff --git a/cryptide_project/src/Pages/EndGame.css b/cryptide_project/src/Pages/EndGame.css index a7e77dd..0d69be8 100644 --- a/cryptide_project/src/Pages/EndGame.css +++ b/cryptide_project/src/Pages/EndGame.css @@ -31,4 +31,12 @@ justify-content: center; flex-direction: column; align-items: center; -} \ No newline at end of file +} + +.playerContainer { + display: flex; + flex-direction: column; + align-items: center; + padding-left: "5px"; + } + \ No newline at end of file diff --git a/cryptide_project/src/Pages/EndGame.tsx b/cryptide_project/src/Pages/EndGame.tsx index dea7588..cf3d932 100644 --- a/cryptide_project/src/Pages/EndGame.tsx +++ b/cryptide_project/src/Pages/EndGame.tsx @@ -26,7 +26,7 @@ import { useGame } from '../Contexts/GameContext'; function EndGame() { - const {winner, person} =useGame() + const {winner, person, players, indices} =useGame() console.log(winner) const theme = useTheme(); return ( @@ -39,19 +39,27 @@ function EndGame() {
+

{indices[players.findIndex((p) => p.id == winner?.id)].ToString("fr")}

-
- {/* - - - - */} +
    + { + players.map((player, index) => ( +
    + {player.id!=winner?.id && + <> + {}} index={index} showCircle={false}/> +
    {indices[players.findIndex((p) => p.id == player?.id)].ToString("fr")}
    + + } +
    + )) + } -
+
diff --git a/cryptide_project/src/Pages/Lobby.css b/cryptide_project/src/Pages/Lobby.css index 5c4bfbd..6a6f132 100644 --- a/cryptide_project/src/Pages/Lobby.css +++ b/cryptide_project/src/Pages/Lobby.css @@ -11,6 +11,7 @@ padding: 20px; margin-right: 10px; margin-left: 30px; + } .lobby-vertical-divider{ @@ -53,4 +54,9 @@ color: gray; font-size: 20px; cursor: pointer; +} + +.centerButton { + display: flex; + justify-content: center; } \ No newline at end of file diff --git a/cryptide_project/src/Pages/Lobby.tsx b/cryptide_project/src/Pages/Lobby.tsx index a6d6930..2d3ff79 100644 --- a/cryptide_project/src/Pages/Lobby.tsx +++ b/cryptide_project/src/Pages/Lobby.tsx @@ -23,6 +23,8 @@ import { useNavigate } from 'react-router-dom'; import { socket } from "../SocketConfig"; import { random } from 'lodash'; import Player from '../model/Player'; +import Human from '../model/Human'; +import EasyBot from '../model/EasyBot'; @@ -38,10 +40,14 @@ function Lobby() { const params = new URLSearchParams(window.location.search); const room = params.get('room'); + function addBot(){ + socket.emit("lobby joined", room, new EasyBot("botId" + Math.floor(Math.random() * 1000), "Bot" + Math.floor(Math.random() * 100)).toJson()) + } + useEffect(() => { if (first){ first = false - socket.emit("lobby joined", room, "test name" + Math.floor(Math.random() * 10)) + socket.emit("lobby joined", room, new Human(socket.id, "Test" + Math.floor(Math.random() * 100)).toJson()) return () => { socket.off('game created'); @@ -78,9 +84,10 @@ function Lobby() { socket.on("new player", (tab) =>{ const tmpTab: Player[] = [] for (const p of tab){ - tmpTab.push(new Player(p.id, p.name)) + tmpTab.push(JSONParser.JSONToPlayer(p)) + console.log(tmpTab) } - setPlayersData(tab.map((p: any) => new Player(p.id, p.name))) + setPlayersData(tmpTab) }) const [codeShowed, setCodeShowed] = useState(true); @@ -111,6 +118,14 @@ function Lobby() { {players.map((player, index) => ( ))} +
+ +
diff --git a/cryptide_project/src/Translations/en.json b/cryptide_project/src/Translations/en.json index 250600f..c81eafc 100644 --- a/cryptide_project/src/Translations/en.json +++ b/cryptide_project/src/Translations/en.json @@ -59,7 +59,7 @@ "nb_friends_indice_end": "friends", "nb_sports_indice_start": "The suspect is playing", - "nb_sports_indice_end": "sport", + "nb_sports_indice_end": "sport(s)", "sport_start": "The suspect plays at least", "sport_end": "" diff --git a/cryptide_project/src/model/Bot.ts b/cryptide_project/src/model/Bot.ts new file mode 100644 index 0000000..453a5b4 --- /dev/null +++ b/cryptide_project/src/model/Bot.ts @@ -0,0 +1,13 @@ +import PersonNetwork from "./PersonsNetwork"; +import Player from "./Player"; + +abstract class Bot extends Player{ + + constructor( id: string, name: string){ + super(id, name); + } + + abstract playRound(personNetwork : PersonNetwork, players: Player): [number, boolean] +} + +export default Bot \ No newline at end of file diff --git a/cryptide_project/src/model/EasyBot.ts b/cryptide_project/src/model/EasyBot.ts new file mode 100644 index 0000000..89fa803 --- /dev/null +++ b/cryptide_project/src/model/EasyBot.ts @@ -0,0 +1,28 @@ +import Bot from "./Bot"; +import Indice from "./Indices/Indice"; +import PersonNetwork from "./PersonsNetwork"; +import Player from "./Player"; + +class EasyBot extends Bot{ + + public indice: Indice | undefined + + constructor(id: string, name: string){ + super(id, name) + } + + toJson() { + return { + type: "EasyBot", + id: this.id, + name: this.name, + }; + } + + playRound(personNetwork: PersonNetwork, players: Player): [number, boolean] { + return [1, false] + } + +} + +export default EasyBot \ No newline at end of file diff --git a/cryptide_project/src/model/EdgesCreator.ts b/cryptide_project/src/model/EdgesCreator.ts index 8885158..80a0919 100644 --- a/cryptide_project/src/model/EdgesCreator.ts +++ b/cryptide_project/src/model/EdgesCreator.ts @@ -11,9 +11,8 @@ class EdgesCreator{ CreateWorkingEdge(personNetwork: PersonNetwork, choosenPerson: Person, indice: Indice, indices: Indice[]){ let creator = IndiceEdgesFactory.Create(indice) - const nbMaxEdge = Math.floor(Math.random() * 5) + 1 - creator.createWorkingEdges(personNetwork, choosenPerson, indices) + const nbMaxEdge = creator.createWorkingEdges(personNetwork, choosenPerson, indices) if (choosenPerson.getFriends().length < nbMaxEdge){ for (const p of personNetwork.getPersons()){ diff --git a/cryptide_project/src/model/Human.tsx b/cryptide_project/src/model/Human.tsx new file mode 100644 index 0000000..5c50dc9 --- /dev/null +++ b/cryptide_project/src/model/Human.tsx @@ -0,0 +1,18 @@ +import Player from "./Player"; + +class Human extends Player{ + + constructor(id: string, name: string){ + super(id, name) + } + + toJson() { + return { + type: "Human", + id: this.id, + name: this.name, + }; + } +} + +export default Human \ No newline at end of file diff --git a/cryptide_project/src/model/IndiceEdgesCreator.ts/ColorIndiceEdgesCreator.ts b/cryptide_project/src/model/IndiceEdgesCreator.ts/ColorIndiceEdgesCreator.ts index f68ee4a..69de618 100644 --- a/cryptide_project/src/model/IndiceEdgesCreator.ts/ColorIndiceEdgesCreator.ts +++ b/cryptide_project/src/model/IndiceEdgesCreator.ts/ColorIndiceEdgesCreator.ts @@ -40,7 +40,7 @@ class ColorIndiceEdgesCreator implements IndiceEdgesCreator{ if (testEdgeWork < indices.length){ p.addFriend(person) person.addFriend(p) - return 1 + return Math.floor(Math.random() * 4) } } } diff --git a/cryptide_project/src/model/IndiceTester/NbSportIndiceTester.ts b/cryptide_project/src/model/IndiceTester/NbSportIndiceTester.ts index a0cf2dc..2f88e41 100644 --- a/cryptide_project/src/model/IndiceTester/NbSportIndiceTester.ts +++ b/cryptide_project/src/model/IndiceTester/NbSportIndiceTester.ts @@ -13,7 +13,7 @@ class NbSportIndiceTester implements IndiceTester{ } Works(person: Person): boolean { - return this.nbSportIndice.getNbSport() == person.getSports().length + return this.nbSportIndice.getNbSport().includes(person.getSports().length) } } diff --git a/cryptide_project/src/model/Indices/NbSportIndice.ts b/cryptide_project/src/model/Indices/NbSportIndice.ts index a34bcbb..eab219e 100644 --- a/cryptide_project/src/model/Indices/NbSportIndice.ts +++ b/cryptide_project/src/model/Indices/NbSportIndice.ts @@ -2,21 +2,30 @@ import { GetJsonFile } from "../EnumExtender"; import Indice from "./Indice"; class NbSportIndice extends Indice { - private nbSport: number; + private nbSport: number[]; - constructor(id: number, nbSport: number) { + constructor(id: number, nbSport: number[]) { super(id); this.nbSport = nbSport; } - public getNbSport(): number{ + public getNbSport(): number[]{ return this.nbSport } // Implémentation de la méthode abstraite ToString(lang: string): string { let json = GetJsonFile(lang) - return `${json.nb_sports_indice_start} ${this.nbSport} ${json.nb_sports_indice_end}`; + let string = `${json.nb_sports_indice_start}`; + this.nbSport.forEach((i, index) =>{ + if (index == this.nbSport.length - 1){ + string += i + } + else{ + string += ` ${i} ${json.or} ` + } + }) + return string + ` ${json.nb_sports_indice_end}` } toJSON() { diff --git a/cryptide_project/src/model/NetworkGenerator.ts b/cryptide_project/src/model/NetworkGenerator.ts index ebd60bd..41584e7 100644 --- a/cryptide_project/src/model/NetworkGenerator.ts +++ b/cryptide_project/src/model/NetworkGenerator.ts @@ -13,30 +13,34 @@ class NetworkGenerator{ const tabAdo: number[] = [] const tabAdulte: number[] = [] const tabVieux: number[] = [] + const tabTresVieux: number[] = [] const tabPerson: Person[] = [] const tabNames = json.names + /* let id = 0 - for(let i = 0; i < nbPerson/4; i++){ + for(let i = 0; i < nbPerson/5; i++){ const nombreAleatoire = Math.floor(Math.random() * 14) + 1; tabJeune.push(nombreAleatoire) } - for(let i = 0; i < nbPerson/4; i++){ - const nombreAleatoire = Math.floor(Math.random() * 5) + 15; + */ + for(let i = 0; i < nbPerson/3; i++){ + const nombreAleatoire = Math.floor(Math.random() * 9) + 12; tabAdo.push(nombreAleatoire) } - for(let i = 0; i < nbPerson/4; i++){ + for(let i = 0; i < nbPerson/3; i++){ const nombreAleatoire = Math.floor(Math.random() * 10) + 20; tabAdulte.push(nombreAleatoire) } - for(let i = 0; i < nbPerson/4; i++){ - const nombreAleatoire = Math.floor(Math.random() * 31) + 30; + for(let i = 0; i < nbPerson/3; i++){ + const nombreAleatoire = Math.floor(Math.random() * 30) + 30; tabVieux.push(nombreAleatoire) } - const tabAge: number[][] = [tabJeune, tabAdo, tabAdulte, tabVieux] + + const tabAge: number[][] = [tabAdo, tabAdulte, tabVieux] let tmpTabSport=[...tabSports] let tmpTabColor = [...tabColor] diff --git a/cryptide_project/src/model/Player.ts b/cryptide_project/src/model/Player.ts index 4af4485..2f51004 100644 --- a/cryptide_project/src/model/Player.ts +++ b/cryptide_project/src/model/Player.ts @@ -1,5 +1,4 @@ -class Player{ - +abstract class Player{ public id: string public name: string; @@ -7,6 +6,8 @@ class Player{ this.id=id this.name=name } + + abstract toJson(): any } export default Player \ No newline at end of file diff --git a/cryptide_project/src/model/Stub.ts b/cryptide_project/src/model/Stub.ts index 2da86a4..f699d22 100644 --- a/cryptide_project/src/model/Stub.ts +++ b/cryptide_project/src/model/Stub.ts @@ -48,8 +48,14 @@ class Stub{ } for (let i=1; i<3; i++){ - indices.push(new NbSportIndice(test, i)) - test++ + for (let j=0; j<3; j++){ + if (j==i){ + continue + } + indices.push(new NbSportIndice(test, [i, j])) + test++ + } + } return indices } From 58c3ec79ed3c0f0d34314e4fafa63e862cf42ee8 Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Mon, 20 Nov 2023 11:59:00 +0100 Subject: [PATCH 4/5] =?UTF-8?q?Fin=20du=20bot=20facile=20+=20normalement?= =?UTF-8?q?=20ce=20sera=20tr=C3=A8s=20simple=20d'impl=C3=A9menter=20le=20b?= =?UTF-8?q?ot=20avanc=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cryptide_project/server/server.js | 13 +- .../src/Components/GraphContainer.tsx | 182 ++++++++++++++++-- .../src/Components/PersonStatus.tsx | 1 - cryptide_project/src/Pages/Lobby.tsx | 19 +- cryptide_project/src/model/Bot.ts | 21 +- cryptide_project/src/model/EasyBot.ts | 90 ++++++++- .../src/model/NetworkGenerator.ts | 2 +- cryptide_project/src/model/Pair.ts | 15 ++ 8 files changed, 303 insertions(+), 40 deletions(-) create mode 100644 cryptide_project/src/model/Pair.ts diff --git a/cryptide_project/server/server.js b/cryptide_project/server/server.js index 3c1c3ec..8fbe62a 100644 --- a/cryptide_project/server/server.js +++ b/cryptide_project/server/server.js @@ -24,8 +24,8 @@ server.listen(3002, () => { io.on('connection', (socket) => { console.log(socket.id); - socket.on('network created', (network, person, indices, room) =>{ - io.to(room).emit("game created", network, person, indices, Math.floor(Math.random() * map.get(room).length)) + socket.on('network created', (network, person, indices, room, start) =>{ + io.to(room).emit("game created", network, person, indices, start) }); socket.on("lobby joined", (room, player) =>{ @@ -62,8 +62,8 @@ io.on('connection', (socket) => { io.to(askingPlayer.id).emit("already asked", nodeId, askedPlayer) }) - socket.on("ask player", (nodeId, playerId, askingPlayer, askingPlayerIndex) =>{ - io.to(playerId).emit("asked", nodeId, askingPlayer, askingPlayerIndex) + socket.on("ask player", (nodeId, playerId, askingPlayer) =>{ + io.to(playerId).emit("asked", nodeId, askingPlayer) }) socket.on("asked all 1by1", (id, playerId) =>{ @@ -80,15 +80,14 @@ io.on('connection', (socket) => { for (let i = 0; i{ - console.log(playerIndex) - io.to(room).emit("node checked", id, works, color, playerIndex) + io.to(room).emit("node checked", id, works, color, playerIndex, socket.id) }) socket.on("put correct background", (id) =>{ diff --git a/cryptide_project/src/Components/GraphContainer.tsx b/cryptide_project/src/Components/GraphContainer.tsx index 21932f1..eb1af59 100644 --- a/cryptide_project/src/Components/GraphContainer.tsx +++ b/cryptide_project/src/Components/GraphContainer.tsx @@ -41,15 +41,20 @@ let askedWrong = false let solo: boolean = true let mapIndexPersons: Map = new Map() let touchedPlayer = -1 +let botIndex = -1 +let askedWrongBot = false +let botTurnToCube = false +let lastSocketId= "" const MyGraphComponent: React.FC = ({onNodeClick, handleShowTurnBar, handleTurnBarTextChange, playerTouched, setPlayerTouched, changecptTour, solo}) => { let cptTour: number = 0 - const { indices, indice, person, personNetwork, setNodeIdData, players, askedPersons, setActualPlayerIndexData, room, actualPlayerIndex, turnPlayerIndex, setWinnerData } = useGame(); + const { indices, indice, person, personNetwork, setNodeIdData, players, askedPersons, setActualPlayerIndexData, room, actualPlayerIndex, turnPlayerIndex, setTurnPlayerIndexData, setWinnerData } = useGame(); const params = new URLSearchParams(window.location.search); const navigate = useNavigate(); + const [lastIndex, setLastIndex] = useState(-1) useEffect(() =>{ @@ -69,7 +74,6 @@ let cptTour: number = 0 socket.emit("put imossible grey", socket.id) } }, [playerTouched]) - let playerIndex: number = turnPlayerIndex let index = 0 @@ -80,7 +84,95 @@ let cptTour: number = 0 } } let thisPlayerIndex = index - + + useEffect(() =>{ + if (actualPlayerIndex==0){ + const bot = players[lastIndex] + if(bot instanceof Bot && botIndex != lastIndex){ + botIndex = lastIndex + if (personNetwork!=null){ + const [choosedPlayerIndex, personIndex] = bot.playRound(personNetwork, players) + const person = personNetwork.getPersons().find((p) => p.getId() == personIndex) + if (choosedPlayerIndex == players.length && person != undefined){ + console.log(lastIndex + " All in sur => " + personNetwork.getPersons().find((p) => p.getId() == personIndex)?.getName()) + let nextPlayerIndex = lastIndex + 1 + if (nextPlayerIndex == players.length){ + nextPlayerIndex = 0 + } + let playerIndex = lastIndex + 1 + let i = 0 + socket.emit("node checked", personIndex, true, lastIndex, room, lastIndex) + while(playerIndex != lastIndex){ + i++ + if (playerIndex == players.length){ + playerIndex = 0 + } + const tester = IndiceTesterFactory.Create(indices[playerIndex]) + const works = tester.Works(person) + socket.emit("asked all 1by1", person.getId(), players[playerIndex].id) + if (i==players.length){ + socket.emit("node checked", personIndex, works, playerIndex, room, nextPlayerIndex) + } + else{ + socket.emit("node checked", personIndex, works, playerIndex, room, lastIndex) + } + if(!works){ + socket.emit("node checked", personIndex, works, playerIndex, room, nextPlayerIndex) + return + } + playerIndex ++ + } + socket.emit("end game", lastIndex, room) + } + else{ + if (person!=undefined){ + if (players[choosedPlayerIndex] instanceof Bot){ + console.log("BOT") + const tester = IndiceTesterFactory.Create(indices[choosedPlayerIndex]) + const works = tester.Works(person) + if (works){ + playerIndex = lastIndex + 1 + if(playerIndex == players.length){ + playerIndex = 0 + } + console.log(lastIndex + " interroge " + choosedPlayerIndex + " a propos de " + person.getName() + " et dit oui") + socket.emit("node checked", personIndex, true, choosedPlayerIndex, room, playerIndex) + } + else{ + console.log(lastIndex + " interroge " + choosedPlayerIndex + " a propos de " + person.getName() + " et dit non") + socket.emit("node checked", personIndex, false, choosedPlayerIndex, room, lastIndex) + const ind = bot.placeSquare(personNetwork, players) + console.log(lastIndex + " pose carré sur " + personNetwork.getPersons()[ind].getName()) + playerIndex = lastIndex + 1 + if(playerIndex == players.length){ + playerIndex = 0 + } + socket.emit("node checked", ind, false, lastIndex, room, playerIndex) + } + } + else{ + console.log(choosedPlayerIndex + " => Pas bot" ) + socket.emit("ask player", personIndex, players[choosedPlayerIndex].id, players[lastIndex]) + console.log(lastIndex + " interroge " + +choosedPlayerIndex + " sur " + personNetwork.getPersons()[personIndex].getName()) + const tester = IndiceTesterFactory.Create(indices[choosedPlayerIndex]) + if (!tester.Works(person)){ + const ind = bot.placeSquare(personNetwork, players) + console.log(lastIndex + " pose carré sur " + personNetwork.getPersons()[ind].getName()) + playerIndex = lastIndex + 1 + if(playerIndex == players.length){ + playerIndex = 0 + } + socket.emit("node checked", ind, false, lastIndex, room, playerIndex) + } + } + } + } + } + } + } + }, [lastIndex]) + + if (first){ first = false @@ -88,6 +180,14 @@ let cptTour: number = 0 for(let i = 0; i{ + if (p instanceof Bot && personNetwork!=null){ + p.index=index + p.initiateMap(personNetwork) + } + }) + } setActualPlayerIndexData(index) if (playerIndex == thisPlayerIndex){ handleTurnBarTextChange("À vous de jouer") @@ -142,9 +242,10 @@ let cptTour: number = 0 } }) - socket.on("node checked",(id, works, askedIndex, newPlayerIndex) => { + socket.on("node checked",(id, works, askedIndex, newPlayerIndex, socketId) => { const node = nodes.get().find((n) => id == n.id) if (node!=undefined){ + onNodeClick(false) playerIndex = newPlayerIndex if (mapIndexPersons.get(askedIndex)?.find((p) => p.getId() == id) == undefined){ @@ -152,13 +253,19 @@ let cptTour: number = 0 const tab = mapIndexPersons.get(askedIndex) if (p!=undefined && tab != undefined){ tab.push(p) + if (actualPlayerIndex == 0){ + players.forEach((player) => { + if (player instanceof Bot){ + player.newInformation(p, askedIndex, works) + } + }) + } } } if (!node.label.includes(colorToEmoji(positionToColor(askedIndex), works))){ networkData.nodes.update({id: id, label: node.label + colorToEmoji(positionToColor(askedIndex), works)}) } - if (playerIndex === thisPlayerIndex){ handleTurnBarTextChange("À vous de jouer") handleShowTurnBar(true) @@ -167,8 +274,10 @@ let cptTour: number = 0 handleShowTurnBar(false) } } + lastSocketId = socketId lastAskingPlayer = 0 lastNodeId = -1 + setLastIndex(newPlayerIndex) }) socket.on("already asked", (nodeId, askedPlayer) =>{ @@ -177,13 +286,14 @@ let cptTour: number = 0 socket.on("asked wrong", () =>{ askedWrong = true + askedWrongBot=true handleShowTurnBar(true) handleTurnBarTextChange("Mauvais choix, posez un carré !") socket.emit("put grey background", socket.id, thisPlayerIndex) }) - socket.on("asked", (nodeId, askingPlayer, askingPlayerIndex) => { + socket.on("asked", (nodeId, askingPlayer) => { if (askingPlayer.id !== lastAskingPlayer || nodeId !== lastNodeId ){ lastAskingPlayer = askingPlayer.id lastNodeId = nodeId @@ -199,20 +309,30 @@ let cptTour: number = 0 if (node != undefined && indice != null){ var tester = IndiceTesterFactory.Create(indice) let maybe = thisPlayerIndex - playerIndex = playerIndex + 1 - if(playerIndex == players.length){ - playerIndex = 0 - } if (tester.Works(pers)){ + playerIndex = playerIndex + 1 + if(playerIndex == players.length){ + playerIndex = 0 + } socket.emit("node checked", nodeId, true, thisPlayerIndex, room, playerIndex) } else{ - maybe = maybe - 1 + maybe = thisPlayerIndex - 1 if(maybe == 0){ maybe = players.length - 1 } - socket.emit("node checked", nodeId, false, thisPlayerIndex, room, maybe) - socket.emit("asked wrong", askingPlayer, room) + let index = players.findIndex((p) => p.id == askingPlayer.id) + if (players[index] instanceof Bot){ + index = playerIndex + 1 + if(index == players.length){ + index = 0 + } + } + if (index != undefined){ + socket.emit("node checked", nodeId, false, thisPlayerIndex, room, index) + socket.emit("asked wrong", askingPlayer, room) + } + } } } @@ -327,8 +447,24 @@ let cptTour: number = 0 } } else if(touchedPlayer != -1 && playerIndex == actualPlayerIndex && touchedPlayer p.getId() == params.nodes[0]) + if (person != undefined){ + if (test.Works(person)){ + let nextPlayerIndex = actualPlayerIndex + 1 + if (nextPlayerIndex == players.length){ + nextPlayerIndex = 0 + } + socket.emit("node checked", params.nodes[0], true, touchedPlayer, room, nextPlayerIndex) + } + else{ + socket.emit("node checked", params.nodes[0], false, touchedPlayer, room, actualPlayerIndex) + socket.emit("asked wrong", players[actualPlayerIndex]) + } + } } else{ socket.emit("ask player", params.nodes[0], players[touchedPlayer].id, players.find((p) => p.id === socket.id, actualPlayerIndex)) @@ -337,6 +473,7 @@ let cptTour: number = 0 } } else if(playerIndex == actualPlayerIndex && touchedPlayer==players.length){ + botIndex = -1 const person = personNetwork?.getPersons().find((p) => p.getId() == params.nodes[0]) if (person != undefined){ const indiceTester = IndiceTesterFactory.Create(indices[actualPlayerIndex]) @@ -347,7 +484,8 @@ let cptTour: number = 0 let playerIndex = actualPlayerIndex + 1 if (indiceTester.Works(person)){ - socket.emit("node checked", params.nodes[0], true, actualPlayerIndex, room, nextPlayerIndex) + let i = 0 + socket.emit("node checked", params.nodes[0], true, actualPlayerIndex, room, actualPlayerIndex) while(playerIndex != actualPlayerIndex){ if (playerIndex == players.length){ playerIndex = 0 @@ -356,18 +494,28 @@ let cptTour: number = 0 const works = tester.Works(person) await delay(500); socket.emit("asked all 1by1", person.getId(), players[playerIndex].id) - socket.emit("node checked", params.nodes[0], works, playerIndex, room, nextPlayerIndex) + if (works){ + socket.emit("node checked", params.nodes[0], true, playerIndex, room, actualPlayerIndex) + } if(!works){ + socket.emit("node checked", params.nodes[0], works, playerIndex, room, nextPlayerIndex) socket.emit("put correct background", socket.id) touchedPlayer=-1 return } + if (i == players.length - 1){ + socket.emit("put correct background", socket.id) + await delay(1000) + socket.emit("end game", actualPlayerIndex, room) + return + } playerIndex ++ + i++ } touchedPlayer=-1 socket.emit("put correct background", socket.id) await delay(1000) - socket.emit("end game", thisPlayerIndex, room) + socket.emit("end game", actualPlayerIndex, room) } } } diff --git a/cryptide_project/src/Components/PersonStatus.tsx b/cryptide_project/src/Components/PersonStatus.tsx index 959b23b..a66429d 100644 --- a/cryptide_project/src/Components/PersonStatus.tsx +++ b/cryptide_project/src/Components/PersonStatus.tsx @@ -31,7 +31,6 @@ const PersonStatus: React.FC = ({img = Person, state= Person, const [touchedPlayer, setTouchedPlayer] = useState(-2) useEffect(() =>{ setTouchedPlayer(playerTouched) - console.log(touchedPlayer) }, [playerTouched]) return (
setPlayerTouched(index)}> diff --git a/cryptide_project/src/Pages/Lobby.tsx b/cryptide_project/src/Pages/Lobby.tsx index f977e29..54bd477 100644 --- a/cryptide_project/src/Pages/Lobby.tsx +++ b/cryptide_project/src/Pages/Lobby.tsx @@ -5,7 +5,6 @@ import { useTheme } from '../Style/ThemeContext'; /* res */ import PlayerItemList from '../Components/PlayerItemList' import PersonImg from '../res/img/Person.png'; -import Bot from '../res/img/bot.png'; import param from '../res/icon/param.png'; import cible from '../res/icon/cible.png'; @@ -25,6 +24,7 @@ import { random } from 'lodash'; import Player from '../model/Player'; import Human from '../model/Human'; import EasyBot from '../model/EasyBot'; +import Bot from '../model/Bot'; @@ -61,19 +61,20 @@ function Lobby() { const network: PersonNetwork = JSONParser.JSONToNetwork(jsonNetwork) const choosenOne: Person = network.getPersons().filter((i) => i.getId() == jsonPerson.id)[0] const choosenIndices : Indice[] = JSONParser.JSONToIndices(jsonIndicesString) - let index = 0 for (let i=0; i[]> + constructor( id: string, name: string){ super(id, name); + this.actualNetwork = new Map[]>() + this.index = -1 + } - abstract playRound(personNetwork : PersonNetwork, players: Player): [number, boolean] + abstract playRound(personNetwork : PersonNetwork, players: Player[]): [number, number] + + abstract placeSquare(personNetwork : PersonNetwork, players: Player[]): number + + abstract newInformation(person: Person, playerIndex: number, works: boolean): void + + abstract initiateMap(personNetwork: PersonNetwork): void } export default Bot \ No newline at end of file diff --git a/cryptide_project/src/model/EasyBot.ts b/cryptide_project/src/model/EasyBot.ts index 89fa803..cb5f38f 100644 --- a/cryptide_project/src/model/EasyBot.ts +++ b/cryptide_project/src/model/EasyBot.ts @@ -1,12 +1,16 @@ +import { DataSet } from "vis-network"; +import { colorToEmoji, positionToColor } from "../ColorHelper"; import Bot from "./Bot"; +import IndiceTesterFactory from "./Factory/IndiceTesterFactory"; +import NodePerson from "./Graph/NodePerson"; import Indice from "./Indices/Indice"; +import Pair from "./Pair"; +import Person from "./Person"; import PersonNetwork from "./PersonsNetwork"; import Player from "./Player"; class EasyBot extends Bot{ - public indice: Indice | undefined - constructor(id: string, name: string){ super(id, name) } @@ -19,8 +23,86 @@ class EasyBot extends Bot{ }; } - playRound(personNetwork: PersonNetwork, players: Player): [number, boolean] { - return [1, false] + playRound(personNetwork: PersonNetwork, players: Player[]): [number, number] { + if (this.indice==undefined){ + return [-1, -1] + } + let rand = Math.random() + const filterPlayers = players.filter((p) => p.id != this.id) + if (rand < 0.75){ + const possibleCombinaison = new Map() + filterPlayers.forEach((p, index) =>{ + possibleCombinaison.set(index, []) + }) + personNetwork.getPersons().forEach((p) =>{ + players.forEach((player, index) =>{ + if (index!=this.index && (!this.actualNetwork.get(p)?.includes(new Pair(index, true) || !this.actualNetwork.get(p)?.includes(new Pair(index, false))))){ + const t = possibleCombinaison.get(index) + if( t==undefined){ + possibleCombinaison.set(index, [p.getId()]) + } + else{ + t.push(p.getId()) + } + } + }) + }) + let r = this.index + while(r==this.index){ + r = Math.floor(Math.random() * players.length) + } + const tab = possibleCombinaison.get(r) + if (tab!= undefined){ + const randIndex = Math.floor(Math.random() * tab.length) + const tester = IndiceTesterFactory.Create(this.indice) + const pers = personNetwork.getPersons().find((p) => p.getId() == tab[randIndex]) + if (pers != undefined) + return [r, tab[randIndex]] + } + } + else{ + const filterNodes = [] + const possibleNodes: number[] = [] + const indiceTester=IndiceTesterFactory.Create(this.indice) + personNetwork.getPersons().forEach((p) => { + let works = true + for(let i = 0; i{ + const tab = this.actualNetwork.get(p) + if (!indiceTester.Works(p) && (!tab?.includes(new Pair(this.index, true) || !tab?.includes(new Pair(this.index, false))))){ + tabFilterPerson.push(p) + } + }) + return tabFilterPerson[Math.floor(Math.random() * tabFilterPerson.length)].getId() + } + + newInformation(person: Person, playerIndex: number, works: boolean): void { + this.actualNetwork.get(person)?.push(new Pair(playerIndex, works)) + } + initiateMap(personNetwork: PersonNetwork): void { + personNetwork.getPersons().forEach((p) =>{ + this.actualNetwork.set(p, []) + }) } } diff --git a/cryptide_project/src/model/NetworkGenerator.ts b/cryptide_project/src/model/NetworkGenerator.ts index 41584e7..a4bb545 100644 --- a/cryptide_project/src/model/NetworkGenerator.ts +++ b/cryptide_project/src/model/NetworkGenerator.ts @@ -7,7 +7,7 @@ class NetworkGenerator{ static GenerateNetwork(nbPerson: number): PersonNetwork{ let json = require("../res/names.json") - const tabSports: Sport[] = [0, 1, 2, 3, 4, 5, 5, 5, 5] + const tabSports: Sport[] = [0, 1, 2, 3, 4, 5, 5, 5] const tabColor: Color[] = [0, 1, 2, 3, 4] const tabJeune: number[] = [] const tabAdo: number[] = [] diff --git a/cryptide_project/src/model/Pair.ts b/cryptide_project/src/model/Pair.ts new file mode 100644 index 0000000..1e56a7f --- /dev/null +++ b/cryptide_project/src/model/Pair.ts @@ -0,0 +1,15 @@ +class Pair{ + public first: T1 + public second: T2 + + constructor(first: T1, second: T2){ + this.first=first + this.second=second + } + + equals(other: Pair): boolean { + return this.first === other.first && this.second === other.second; + } +} + +export default Pair \ No newline at end of file From 5087f20c5c0df4e2c1020afa541ae6a74d2cd126 Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Mon, 20 Nov 2023 14:44:27 +0100 Subject: [PATCH 5/5] test --- cryptide_project/server/server.js | 4 ++-- .../src/Components/GraphContainer.tsx | 7 ++++-- cryptide_project/src/Pages/Lobby.tsx | 23 +++++++++++++++++++ cryptide_project/src/SocketConfig.ts | 2 +- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cryptide_project/server/server.js b/cryptide_project/server/server.js index 8fbe62a..ba01cfe 100644 --- a/cryptide_project/server/server.js +++ b/cryptide_project/server/server.js @@ -7,7 +7,7 @@ const app = express(); const server = http.createServer(app); const io = socketIO(server, { cors: { - origin: ["http://localhost:3000", "http://localhost:3001"], // Remplacez par l'URL de votre application React + origin: ["http://172.20.10.4:3000", "http://localhost:3000"], // Remplacez par l'URL de votre application React methods: ["GET", "POST"], credentials: true } @@ -80,7 +80,7 @@ io.on('connection', (socket) => { for (let i = 0; i p.id === socket.id, actualPlayerIndex)) - socket.emit("put correct background", socket.id) + if (touchedPlayer > 0){ + console.log(touchedPlayer) + socket.emit("ask player", params.nodes[0], players[touchedPlayer].id, players.find((p) => p.id === socket.id, actualPlayerIndex)) + socket.emit("put correct background", socket.id) + } touchedPlayer=-1 } } diff --git a/cryptide_project/src/Pages/Lobby.tsx b/cryptide_project/src/Pages/Lobby.tsx index 54bd477..ecdacc8 100644 --- a/cryptide_project/src/Pages/Lobby.tsx +++ b/cryptide_project/src/Pages/Lobby.tsx @@ -27,6 +27,7 @@ import EasyBot from '../model/EasyBot'; import Bot from '../model/Bot'; +let gameStarted = false function Lobby() { const theme=useTheme(); @@ -79,6 +80,7 @@ function Lobby() { setPersonNetworkData(network) setIndicesData(choosenIndices) first = true + gameStarted = true navigate('/game?solo=false'); }); @@ -90,6 +92,27 @@ function Lobby() { setPlayersData(tmpTab) }) + socket.on("player left", (tab, socketId) => { + if (gameStarted){ + const i = players.findIndex((p) => p.id == socketId) + if (i != undefined){ + let player = players[i] + player = new EasyBot("125", "BOT125") + if (player instanceof Bot){ + player.indice = indices[i] + } + } + } + else{ + const tmpTab: Player[] = [] + for (const p of tab){ + tmpTab.push(JSONParser.JSONToPlayer(p)) + } + setPlayersData(tmpTab) + } + const index = players + }) + const [codeShowed, setCodeShowed] = useState(true); diff --git a/cryptide_project/src/SocketConfig.ts b/cryptide_project/src/SocketConfig.ts index 2fea1de..339e25f 100644 --- a/cryptide_project/src/SocketConfig.ts +++ b/cryptide_project/src/SocketConfig.ts @@ -1,6 +1,6 @@ import { io } from "socket.io-client"; -const socket = io("http://localhost:3002"); +const socket = io("http://172.20.10.4:3002"); export {socket} \ No newline at end of file