From b171f5835e85b37f827d8668b1e6a7210236f155 Mon Sep 17 00:00:00 2001 From: "aurian.jault" Date: Tue, 7 Mar 2023 17:24:00 +0100 Subject: [PATCH 1/3] Theorie sur l'archi du server / client --- archi.trash | 19 ++++++++++ structModel.trash | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 archi.trash create mode 100644 structModel.trash diff --git a/archi.trash b/archi.trash new file mode 100644 index 0000000..db42964 --- /dev/null +++ b/archi.trash @@ -0,0 +1,19 @@ +# Coté client +- l'utilisateur récup le deck actuel + - lorsqu'il fait une action -> set/hyperSet + - envoie un event + - idRoom + - idPlayer + - deck + - carteJoué + + +# Coté serveur + +- Crée le deck de base + - le donne a tous les joueurs + - A chaque action fais par un joueur + - Verif si le packet envoyé est le même que celui local + - verif si action est valable -> set ou pas + - envoie un event "updateDeck" + - update les points diff --git a/structModel.trash b/structModel.trash new file mode 100644 index 0000000..bd3fe29 --- /dev/null +++ b/structModel.trash @@ -0,0 +1,88 @@ +room { + id= "INT", + player = [cle,valeur], // cle -> player || valeur -> nombre de points (possible de JSON ça) + deck = Deck + settings = + { + isHyperSet = BOOL, + isPublic = BOOL, + nbCards = INT, + nbAttribs = INT, + nbPlayers = INT + } +} + + +fonctions = +{ + checkFull() + { + return player.length === setting.nbPlayers + } + + // utile pour enchainer les appel quand t'es sur de toi + getRoom(id) + { + let res = rooms.filter(element => e.id===id) + if(res) + { + return res; + } + else{ + return false; + } + } + + createRoom(socket) + { + if(!getRoom(socket.id)) + { + rooms.push({ + id=id, + player=socket.nom, + deck = new Deck().create(), + settings{ + isHyperSet = socket.hyper, + isPublic = socket.public, + nbCards = socket.cards, + nbAttribs = socket.attribs, + nbPlayer = 1 + } + }) + } + } + + + // LeaveRoom + leaveRoom(){ + let room = getRoom(rooms,id); + if(room.player.length === 1) + { + // Trouver une solution pour remove une room + } + else + { + let index = room.player.indexOf(idUser) + room.player.slice(index,1) + } + } + + + joinRoom(id) + { + let room = getRoom(id); + if(!room) + { + return false; + } + if(room.setting.isPublic && room.player.length < room.setting.nbPlayer) + { + room.player.push(id) + return true + } + return false; + } + + + +} From d787e3db794bc71c2245e5b636460fca0008a616 Mon Sep 17 00:00:00 2001 From: "aurian.jault" Date: Wed, 15 Mar 2023 14:38:54 +0100 Subject: [PATCH 2/3] Ajout MAJ des joueurs + suppression des joueurs a la deconnexion --- client/index.html | 4 ++- client/main.js | 32 ++++++++++++++++---- index.js | 74 ++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 93 insertions(+), 17 deletions(-) diff --git a/client/index.html b/client/index.html index cae5a96..97ca3e8 100644 --- a/client/index.html +++ b/client/index.html @@ -19,7 +19,9 @@ - + +
+
diff --git a/client/main.js b/client/main.js index 70b707c..a2fa3fa 100644 --- a/client/main.js +++ b/client/main.js @@ -4,18 +4,38 @@ const form = document.querySelector("#form"); socket.on("connect", () => { - console.log(socket); -}); + socket.on("players",(socket) => { + data = new Map(JSON.parse(socket)); + console.log(data); + const player = document.querySelector("#players") + player.innerHTML='' + data.forEach((it,key) => { + let p = document.createElement("p"); + p.innerHTML = `joueur : ${key} points : ${it}`; + player.appendChild(p); + }) + }) +}) + + + form.addEventListener('submit',function(e){ e.preventDefault(); value = form.querySelector("input[name=pseudo]").value; - console.log(`[Client Side] inputValue : ${value}`); + if(value) { - socket.emit('testing',value); - console.log(`emitted`); - value= "VIDE"; + perso = { + id: value, + nom: socket.id, + test: "coucou" + } + data = JSON.stringify(perso); + console.log(`[Client Side] inputValue : ${data}`); + socket.emit('testing',data); } }) + + diff --git a/index.js b/index.js index da41c69..b5fb6f1 100644 --- a/index.js +++ b/index.js @@ -24,27 +24,59 @@ server.listen(21111, () => { console.log('listening on *:21111'); }); -io.on('connection', (socket) => { - console.log(`[Server Side]Socket : ${socket.id}`); - socket.on('testing', (socket) => { + +io.on('connection', (socket1) => { + + socket1.on('disconnect',(socket)=>{ + console.log(`[Server Side] disconnection : ${socket1.id}`) + rooms.forEach(it =>{ + it.players = new Map( + [...it.players] + .filter(([k, v]) => k !== socket1.id ) + ) + roomChanged(it) + }) + }) + + socket1.on('testing', (socket) => { + data = JSON.parse(socket); console.clear(); - console.log(`[Server Side] Socket : ${socket}`) + console.log(`[Server Side] JSON : ${socket}`) + console.log(`[Server Side] data : ${data.id}`) if(rooms.length === 0) { + map = new Map(); + map.set(data.nom,0); rooms.push({ - id: socket, + id: data.id, + players: map }) + roomChanged(rooms[0]) } else { - if(rooms.some(e => e.id === socket)) + res = rooms.filter(e => e.id === data.id); + if(res[0] != null ) { - console.error("Ceci existe deja") + console.log(res) + if(res[0].players.get(data.nom) != null) + { + console.log(`[Server Side] Error : le joueur ${data.nom} est dejà dans la room`); + } + else + { + res[0].players.set(data.nom,0); + roomChanged(res[0]) + } } else{ + map = new Map(); + map.set(data.nom,0); rooms.push({ - id: socket, + id: data.id, + players: map }); + roomChanged(rooms[rooms.length-1]) } } displayAllRooms(rooms); @@ -53,14 +85,36 @@ io.on('connection', (socket) => { + function displayAllRooms(rooms) { console.log(`[Server Side] number of rooms : ${rooms.length}`); console.log(`------------`); rooms.forEach(element => { - console.log(`[Server Side] room :`); - console.log(element); + console.log(`[Server Side] room : ${element.id}`); + displayAllPlayers(element.players); console.log(`------------`); + }); +} +function displayAllPlayers(players) +{ + console.log("[Server Side]: Players :") + players.forEach((element,key) => { + console.log(`[Server Side]: ${key} -> ${element}`); + }); +} + +function roomChanged(room) +{ + if(room.players.size === 0) + { + rooms = rooms.filter((it)=>{ + it !== room + }) + } + room.players.forEach((element,key) => { + io.to(key).emit('players',JSON.stringify(Array.from(room.players.entries()))) }); } + From 47cbf84cdc5fe695154159edb3bd0c4d977a24e6 Mon Sep 17 00:00:00 2001 From: "aurian.jault" Date: Mon, 20 Mar 2023 15:14:34 +0100 Subject: [PATCH 3/3] coucou --- client/main.js | 2 +- index.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/client/main.js b/client/main.js index a2fa3fa..69d6935 100644 --- a/client/main.js +++ b/client/main.js @@ -33,7 +33,7 @@ form.addEventListener('submit',function(e){ } data = JSON.stringify(perso); console.log(`[Client Side] inputValue : ${data}`); - socket.emit('testing',data); + socket.emit('createRoom',data); } }) diff --git a/index.js b/index.js index b5fb6f1..ad4aef1 100644 --- a/index.js +++ b/index.js @@ -36,9 +36,11 @@ io.on('connection', (socket1) => { ) roomChanged(it) }) + console.clear() + displayAllRooms(rooms) }) - socket1.on('testing', (socket) => { + socket1.on('createRoom', (socket) => { data = JSON.parse(socket); console.clear(); console.log(`[Server Side] JSON : ${socket}`)