Commit defeee15 authored by Dennis Willers's avatar Dennis Willers 🏀

Ermittlung wie viele Spieler in einem Raum sind und wie viele Spieler welchen Team beigetreten sind

parent 79550d8f
Pipeline #395 passed with stages
in 2 minutes and 8 seconds
{
"events": {
"restart": "sh -c 'lsof -i :${PORT:-3000} -t | xargs kill'",
"crash": "sh -c 'lsof -i :${PORT:-3000} -t | xargs kill'"
},
"delay": 1500
}
...@@ -11,9 +11,28 @@ Http.listen(3000, () => { ...@@ -11,9 +11,28 @@ Http.listen(3000, () => {
}); });
var currentCard = {}; var currentCard = {};
var clients = {}; // Value = ClientID / Key = [SessionID, Team] / Team = 0 (total), 1 (red) or 2 (blue)
var clientsInSession = {}; // Value = SessionID / Key = [total, red, blue}
function getSumClients (sessionName, team, action) { // Team = 0 (total), 1 (red) or 2 (blue) / Action = 1 (add) or -1 (remove)
if (!clientsInSession.hasOwnProperty(sessionName)) {
clientsInSession[sessionName] = [0, 0, 0];
}
if (team === 0) {
clientsInSession[sessionName] = [clientsInSession[sessionName][0] + action, clientsInSession[sessionName][1], clientsInSession[sessionName][2]];
}
if (team === 1) {
clientsInSession[sessionName] = [clientsInSession[sessionName][0] + action, clientsInSession[sessionName][1] + action, clientsInSession[sessionName][2]];
}
if (team === 2) {
clientsInSession[sessionName] = [clientsInSession[sessionName][0] + action, clientsInSession[sessionName][1], clientsInSession[sessionName][2] + action];
}
return {total: clientsInSession[sessionName][0], red: clientsInSession[sessionName][1], blue: clientsInSession[sessionName][2]};
}
Socketio.on("connection", socket => { Socketio.on("connection", socket => {
console.log('new Socket connect'); console.log('new Socket connect');
clients[socket.id] = [null, null];
for (var key in currentCard) { for (var key in currentCard) {
socket.emit(key, currentCard[key]) socket.emit(key, currentCard[key])
...@@ -43,6 +62,8 @@ Socketio.on("connection", socket => { ...@@ -43,6 +62,8 @@ Socketio.on("connection", socket => {
socket.emit(data.sessionName+":newRound", false); socket.emit(data.sessionName+":newRound", false);
Socketio.emit(data.sessionName+":endRound", true); Socketio.emit(data.sessionName+":endRound", true);
socket.emit(data.sessionName+":endRound", true); socket.emit(data.sessionName+":endRound", true);
let sumClients = getSumClients(data.sessionName, null, 0);
Socketio.emit(data.sessionName+":sessionPlayers", sumClients);
}); });
socket.on("canPressTabooh", data => { socket.on("canPressTabooh", data => {
console.log('CanPressTabooh from: ', data.sessionName, data.canPressTabooh); console.log('CanPressTabooh from: ', data.sessionName, data.canPressTabooh);
...@@ -54,5 +75,33 @@ Socketio.on("connection", socket => { ...@@ -54,5 +75,33 @@ Socketio.on("connection", socket => {
Socketio.emit(data.sessionName+":historyChanged", data); Socketio.emit(data.sessionName+":historyChanged", data);
socket.emit(data.sessionName+":historyChanged", data); socket.emit(data.sessionName+":historyChanged", data);
}); });
socket.on("sessionPlayers", data => {
const client = clients[socket.id];
console.log('CLIENT: ', client, data.team);
if (data.sessionName === null && client[0] !== null) {
let sumClients = getSumClients(client[0], client[1], -1);
Socketio.emit(client[0]+":sessionPlayers", sumClients);
clients[socket.id] = [null, null];
}
else {
if (data.sessionName !== client[0] || data.team !== client[1]) {
let sumClients = getSumClients(client[0], client[1], -1);
Socketio.emit(client[0]+":sessionPlayers", sumClients);
clients[socket.id] = [data.sessionName, data.team];
sumClients = getSumClients(data.sessionName, data.team, data.newAction);
Socketio.emit(data.sessionName+":sessionPlayers", sumClients);
socket.emit(data.sessionName+":sessionPlayers", sumClients);
}
}
});
socket.on("disconnect", () => {
const client = clients[socket.id];
console.log("DISCONNECT CLIENT: ", client);
if (client !== null && client[0] !== null) {
const sumClients = getSumClients(client[0], client[1], -1);
Socketio.emit(client[0]+":sessionPlayers", sumClients);
}
delete clients[socket.id];
});
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment