Skip to content
Snippets Groups Projects
Commit c39518bf authored by AceZephyr's avatar AceZephyr
Browse files

Reconnection

parent a5c5bae8
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,11 @@
.filter(c => c[0] === "token")[0];
return t ? t[1] : null;
}
function hideJoinButton() {
document.querySelector("#join-game").style.display = "none";
}
const socket = io({
query: {
gameId: window.location.href.split("/").filter(e => e.length > 0).pop(),
......@@ -37,6 +42,7 @@
BOARD1.updateBoard(args.boardDisplay, args.boardLock, args.boardColors);
document.querySelector("#join-game").style.display = "none";
displayPlayerScores(args.playerScores);
hideJoinButton();
});
socket.on("game stop", () => {
......@@ -45,7 +51,7 @@
document.querySelector("#join-game").onclick = () => {
socket.emit("join game");
document.querySelector("#join-game").style.display = "none";
hideJoinButton();
}
function updateToServer(x, y, num) {
......
......@@ -61,15 +61,19 @@ class Game {
this.sendBoard();
}
sendBoard() {
// do not send board state if game is not in progress
if (this.state === ROOM_PLAYING) {
this.sendAll("board state", {
packageBoardState() {
return {
boardDisplay: this.boardDisplay,
boardLock: this.boardLock,
boardColors: this.boardColors,
playerScores: this.playerScores
});
};
}
sendBoard() {
// do not send board state if game is not in progress
if (this.state === ROOM_PLAYING) {
this.sendAll("board state", this.packageBoardState());
}
}
......@@ -94,10 +98,6 @@ class Game {
return true;
}
validateCell(args) {
return this.boardSolution[args.y][args.x] === args.num;
}
userJoin(clientId, user) {
if (this.state === ROOM_OPEN && this.playerUsers.length < 2) {
this.playerUsers.push(user);
......@@ -131,15 +131,13 @@ class Game {
userLeave(client, user) {
this.usersToClients.delete(user);
if (this.playerUsers.includes(user)) {
this.kill();
} else if (this.connectedClients.includes(client)) {
if (this.connectedClients.includes(client)) {
this.connectedClients.splice(this.connectedClients.indexOf(client), 1);
}
}
sendAll(name, message = null) {
for (let clientId of this.connectedClients) {
send(name, clients, message = null) {
for (let clientId of clients) {
io.in(clientId).fetchSockets().then(sockets => {
sockets.forEach(socket => {
socket.emit(name, message)
......@@ -148,6 +146,10 @@ class Game {
}
}
sendAll(name, message = null) {
this.send(name, this.connectedClients, message);
}
constructor(id, difficulty) {
this.id = id;
this.difficulty = difficulty;
......@@ -156,9 +158,9 @@ class Game {
this.boardLock = [];
this.boardColors = [];
this.connectedClients = [];
this.usersToClients = new Map();
this.playerUsers = [];
this.playerScores = {};
this.usersToClients = new Map();
this.state = ROOM_OPEN;
}
}
......@@ -284,6 +286,10 @@ io.on("connection", (socket) => {
game.clientJoin(socketId);
console.log(`Client connected: ${socketId} to game ${gameId}`);
if (game.playerUsers.includes(user)) {
socket.emit("board state", game.packageBoardState());
}
socket.on("cell input", args => {
if (game.playerUsers.includes(user)) {
game.input(socketId, user, args.x, args.y, args.num);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment