Merge pull request 'rooms' (#1) from rooms into master

Reviewed-on: #1
master
Aurian JAULT 2 years ago
commit 7c7ec9a0ba

@ -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

@ -19,7 +19,9 @@
</div>
<button class="btn btn-primary" id="start" type="submit">Créer un salon privée</button>
</form>
</div>
<div id="players">
</div>
</div>
<script src="/socket.io/socket.io.js"></script>

@ -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('createRoom',data);
}
})

@ -24,27 +24,61 @@ 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)
})
console.clear()
displayAllRooms(rooms)
})
socket1.on('createRoom', (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 +87,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())))
});
}

@ -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;
}
}
Loading…
Cancel
Save