diff --git a/.gitignore b/.gitignore index ae37a1c..5c49670 100644 --- a/.gitignore +++ b/.gitignore @@ -44,11 +44,11 @@ thumb sketch ### modules ### -yarn.lock -package-lock.json +**/yarn.lock +**/package-lock.json # db -socialgraph.db +**/socialgraph.db # build cryptide_project/build/ diff --git a/cryptide_project/server/server.js b/cryptide_project/server/server.js index 05bdb0e..6d3511d 100644 --- a/cryptide_project/server/server.js +++ b/cryptide_project/server/server.js @@ -37,59 +37,70 @@ io.on('connection', (socket) => { io.to(playerId).emit("join during game", networkPerson, person, indices, start, map.get(room).tab, nodes) }) - socket.on("lobby joined", (room, player) =>{ - if (lastSocketJoined != player.id){ - lastSocketJoined=player.id - const game = map.get(room) - if (game !== undefined){ - if (game.tab.length == 6 && !game.started){ - io.to(socket.id).emit("room full") - return - } - if (game.started){ - for(const u of game.tab){ - if(u.type !== "User" && u.pseudo===player.pseudo){ - u.type = "User" - u.id=socket.id - io.to(game.tab[game.actualPlayer].id).emit("give network", socket.id) - io.to(room).emit("player joined ingame", game.tab) - socket.join(room) - return + socket.on("join back game", (player) => { + for (const k of map.keys()){ + const tab = map.get(k) + for (let i = 0; i{ + + const game = map.get(room) + if (game !== undefined){ + if (game.tab.length == 6 && !game.started){ + io.to(socket.id).emit("room full") + return } - - - if (game == undefined){ - map.set(room, {tab: [{type: player.type, id: socket.id, pseudo: player.pseudo, profilePicture: player.profilePicture}], started: false, actualPlayer: 0, lastWorks: false}) - socket.join(room) - } - else{ - const tab = game.tab - for(let i = 0; i ({ key, value })) - const playerJson = JSON.stringify(playerArray); - io.emit("request lobbies", playerJson) + + if (player.type!=="User"){ + tab.push({type: player.type, id: player.id, pseudo: player.pseudo, profilePicture: player.profilePicture}) + } + else{ + tab.push({type: player.type, id: socket.id, pseudo: player.pseudo, profilePicture: player.profilePicture}) + socket.join(room) + } } + io.to(room).emit("new player", map.get(room)) + const playerArray = Array.from(map.entries()).map(([key, value]) => ({ key, value })) + const playerJson = JSON.stringify(playerArray); + io.emit("request lobbies", playerJson) + }) socket.on("request lobbies", () => { @@ -134,15 +145,17 @@ io.on('connection', (socket) => { }) socket.on("who plays", (room) => { - let player = map.get(room).actualPlayer - if (map.get(room).tab[player].type != "User"){ - player = player + 1 - if (player == map.get(room).tab.length){ - player=0 + if (map.get(room) !== undefined){ + let player = map.get(room).actualPlayer + if (map.get(room).tab[player].type != "User"){ + player = player + 1 + if (player == map.get(room).tab.length){ + player=0 + } } + console.log(player) + io.to(room).emit("who plays", player, map.get(room).lastWorks) } - console.log(player) - io.to(room).emit("who plays", player, map.get(room).lastWorks) }) socket.on("disconnect", () =>{ diff --git a/cryptide_project/src/Components/GraphContainer.tsx b/cryptide_project/src/Components/GraphContainer.tsx index f56071e..6e39fbb 100644 --- a/cryptide_project/src/Components/GraphContainer.tsx +++ b/cryptide_project/src/Components/GraphContainer.tsx @@ -61,6 +61,7 @@ let cptPlayerLeft = 0 let firstPlayer = 0 let cptBug = 0 let cptUseEffect = 0 +let testPlayers: Player[] = [] const MyGraphComponent: React.FC = ({onNodeClick, handleShowTurnBar, handleTurnBarTextChange, playerTouched, setPlayerTouched, changecptTour, solo, isDaily, isEasy, addToHistory, showLast, setNetwork, setNetworkEnigme, setPlayerIndex, askedWrong, setAskedWrong}) => { @@ -102,8 +103,14 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS }, [elapsedTime, endgame]); + useEffect(() => { + testPlayers = players + console.log(testPlayers) + }, [players]) + useEffect(() =>{ touchedPlayer=playerTouched + console.log(playerTouched) if (touchedPlayer == -1){ if (!askedWrongLocal){ socket.emit("put correct background", socket.id) @@ -157,7 +164,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS useEffect(() =>{ cptBug=0 if (actualPlayerIndex==firstPlayer){ - const bot = players[lastIndex] + const bot = testPlayers[lastIndex] if(bot instanceof Bot && botIndex != lastIndex){ botIndex = lastIndex if (personNetwork!=null){ @@ -181,7 +188,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS } const tester = IndiceTesterFactory.Create(indices[playerIndex]) const works = tester.Works(person) - socket.emit("asked all 1by1", person.getId(), players[playerIndex].id) + socket.emit("asked all 1by1", person.getId(), testPlayers[playerIndex].id) if (i==players.length){ socket.emit("node checked", personIndex, works, playerIndex, room, nextPlayerIndex) } @@ -190,7 +197,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS } if(!works){ socket.emit("node checked", personIndex, works, playerIndex, room, nextPlayerIndex) - const ind = bot.placeSquare(personNetwork, players) + const ind = bot.placeSquare(personNetwork, testPlayers) if (ind == -1 ){ socket.emit("can't put square", lastIndex, room) return @@ -209,7 +216,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS } else{ if (person!=undefined){ - if (players[choosedPlayerIndex] instanceof Bot){ + if (testPlayers[choosedPlayerIndex] instanceof Bot){ console.log("BOT") const tester = IndiceTesterFactory.Create(indices[choosedPlayerIndex]) const works = tester.Works(person) @@ -224,7 +231,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS 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) + const ind = bot.placeSquare(personNetwork, testPlayers) if (ind == -1 ){ socket.emit("can't put square", playerIndex, room) return @@ -239,11 +246,11 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS } else{ console.log(choosedPlayerIndex + " => Pas bot" ) - socket.emit("ask player", personIndex, players[choosedPlayerIndex].id, players[lastIndex]) + socket.emit("ask player", personIndex, testPlayers[choosedPlayerIndex].id, testPlayers[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) + const ind = bot.placeSquare(personNetwork, testPlayers) if (ind == -1 ){ socket.emit("can't put square", playerIndex, room) return @@ -382,7 +389,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS tmpTab.push(JSONParser.JSONToPlayer(p)) } else{ - tmpTab.push(players[ind]) + tmpTab.push(testPlayers[ind]) } ind ++ } @@ -399,12 +406,11 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS tmpTab.push(JSONParser.JSONToPlayer(p)) } else{ - tmpTab.push(players[ind]) + tmpTab.push(testPlayers[ind]) } ind ++ } if (i==firstPlayer){ - console.log(tmpTab) for(let index = 0; index < tmpTab.length; index++){ const test = tmpTab[index] if (test instanceof User){ @@ -508,7 +514,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS if (p!=undefined && tab != undefined){ tab.push(p) if (actualPlayerIndex == 0){ - players.forEach((player) => { + testPlayers.forEach((player) => { if (player instanceof Bot){ player.newInformation(p, askedIndex, works) } @@ -522,7 +528,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS cptHistory++ if (cptHistory % 2 == 0){ lastNodes.push(node) - addToHistory(players[askedIndex].pseudo + " à mis un " + positionToEmoji(askedIndex, works) + " à " + personNetwork.getPersons()[id].getName()) + addToHistory(testPlayers[askedIndex].pseudo + " à mis un " + positionToEmoji(askedIndex, works) + " à " + personNetwork.getPersons()[id].getName()) } } @@ -585,7 +591,7 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS cptBug=0 cptOnAskedWrong ++ if (cptOnAskedWrong % 2 == 0){ - addToHistory(players[askingPlayer].pseudo + " ne peut plus poser de carré") + addToHistory(testPlayers[askingPlayer].pseudo + " ne peut plus poser de carré") playerIndex = askingPlayer + 1 if(playerIndex == players.length){ playerIndex = 0 @@ -630,8 +636,8 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS socket.emit("node checked", nodeId, true, actualPlayerIndex, room, playerIndex) } else{ - let index = players.findIndex((p) => p.id == askingPlayer.id) - if (players[index] instanceof Bot){ + let index = testPlayers.findIndex((p) => p.id == askingPlayer.id) + if (testPlayers[index] instanceof Bot){ index = playerIndex + 1 if(index == players.length){ index = 0 @@ -818,9 +824,9 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS network.on("click", async (params) => { if(params.nodes.length > 0){ - console.log(actualPlayerIndex + " => " + playerIndex) - console.log(playerTouched) + console.log(touchedPlayer) setNodeIdData(params.nodes[0]) + console.log(players) // addToHistory("Le joueur a cliqué") //! TEST DEBUG if (!solo){ if (askedWrongLocal){ @@ -851,7 +857,9 @@ const MyGraphComponent: React.FC = ({onNodeClick, handleS } else if(touchedPlayer != -1 && playerIndex == actualPlayerIndex && touchedPlayer = ({onNodeClick, handleS } else{ if (touchedPlayer >= 0){ - console.log(touchedPlayer) + console.log("CE N'EST PAS UN BOT") //@ts-ignore 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) diff --git a/cryptide_project/src/Components/PersonStatus.tsx b/cryptide_project/src/Components/PersonStatus.tsx index 7ec2942..4bf7415 100644 --- a/cryptide_project/src/Components/PersonStatus.tsx +++ b/cryptide_project/src/Components/PersonStatus.tsx @@ -51,6 +51,7 @@ const PersonStatus: React.FC = ({img = Person, state= Person, function onTouch(){ if (IsActualPlayer && !askedWrong){ setPlayerTouched(index) + setTouchedPlayer(index) } } diff --git a/cryptide_project/src/Pages/Lobbies.tsx b/cryptide_project/src/Pages/Lobbies.tsx index cb305af..da6a2d5 100644 --- a/cryptide_project/src/Pages/Lobbies.tsx +++ b/cryptide_project/src/Pages/Lobbies.tsx @@ -10,6 +10,7 @@ import User from '../model/User'; import { socket } from '../SocketConfig'; import JSONParser from '../JSONParser'; import Person from '../model/Person'; +import { useNavigationType } from 'react-router-dom'; class LobbyDataProps { @@ -26,6 +27,8 @@ class LobbyDataProps { } } +let cptNavigation = 0 + function Lobbies() { const theme=useTheme(); @@ -62,6 +65,14 @@ function Lobbies() { setFirst(first) } + const navigationType = useNavigationType() + cptNavigation++ + if (cptNavigation % 2 == 0){ + if (navigationType.toString() == "POP"){ + socket.emit("player quit") + } + } + if (first){ setFirst(false) socket.emit("request lobbies") diff --git a/cryptide_project/src/Pages/Play.tsx b/cryptide_project/src/Pages/Play.tsx index a0fc893..83106db 100644 --- a/cryptide_project/src/Pages/Play.tsx +++ b/cryptide_project/src/Pages/Play.tsx @@ -108,6 +108,7 @@ function Play() { if (loggedIn){ login() setUserData(user) + socket.emit("join back game", user) } else{ loadImageAsync(defaultImg).then((blob) => { @@ -118,8 +119,19 @@ function Play() { } }) } + else{ + socket.emit("join back game", user) + } }, [isLoggedIn]); + const [goBackRoom, setGoBackRoom] = useState(-1) + + useEffect(() => { + socket.on("join back game", (room) => { + setGoBackRoom(room) + }) + }, []) + const [room, setRoom] = useState(null); const navigate = useNavigate(); @@ -169,6 +181,10 @@ function Play() { } }, [room, navigate]); + const goBack = () => { + navigate("/lobby?room=" + goBackRoom) + } + const [showOverlay, setShowOverlay] = useState(false); @@ -259,7 +275,7 @@ function Play() { - + {goBackRoom != -1 && }
diff --git a/cryptide_project/yarn.lock b/cryptide_project/yarn.lock index b9e5034..1db275c 100644 --- a/cryptide_project/yarn.lock +++ b/cryptide_project/yarn.lock @@ -1,7 +1,6 @@ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 - "@aashutoshrathi/word-wrap@^1.2.3": version "1.2.6" resolved "https://registry.npmjs.org/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz"