Skip to content

Instantly share code, notes, and snippets.

@Xfox1
Last active December 23, 2019 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xfox1/5248cd6b85b9c0ac2c64a27c16ba5cdd to your computer and use it in GitHub Desktop.
Save Xfox1/5248cd6b85b9c0ac2c64a27c16ba5cdd to your computer and use it in GitHub Desktop.
const http = require("http")
const express = require("express");
const app = express();
const socketIo = require("socket.io");
const fs = require("fs");
const server = http.Server(app).listen(8080);
const io = socketIo(server);
const clients = {};
// Serve static resources
app.use(express.static(__dirname + "/../client/"));
app.use(express.static(__dirname + "/../node_modules/"));
app.get("/", (req, res) => {
const stream = fs.createReadStream(__dirname + "/../client/index.html");
stream.pipe(res);
});
var players = {}; // opponent: scoket.id of the opponent, symbol = "X" | "O", socket: player's socket
var unmatched;
// When a client connects
io.on("connection", function(socket) {
let id = socket.id;
console.log("New client connected. ID: ", socket.id);
clients[socket.id] = socket;
socket.on("disconnect", () => {// Bind event for that socket (player)
console.log("Client disconnected. ID: ", socket.id);
delete clients[socket.id];
socket.broadcast.emit("clientdisconnect", id);
});
join(socket); // Fill 'players' data structure
if (opponentOf(socket)) { // If the current player has an opponent the game can begin
socket.emit("game.begin", { // Send the game.begin event to the player
symbol: players[socket.id].symbol
});
opponentOf(socket).emit("game.begin", { // Send the game.begin event to the opponent
symbol: players[opponentOf(socket).id].symbol
});
}
// Event for when any player makes a move
socket.on("make.move", function(data) {
if (!opponentOf(socket)) {
// This shouldn't be possible since if a player doens't have an opponent the game board is disabled
return;
}
// Validation of the moves can be done here
socket.emit("move.made", data); // Emit for the player who made the move
opponentOf(socket).emit("move.made", data); // Emit for the opponent
});
// Event to inform player that the opponent left
socket.on("disconnect", function() {
if (opponentOf(socket)) {
opponentOf(socket).emit("opponent.left");
}
});
});
function join(socket) {
players[socket.id] = {
opponent: unmatched,
symbol: "X",
socket: socket
};
// If 'unmatched' is defined it contains the socket.id of the player who was waiting for an opponent
// then, the current socket is player #2
if (unmatched) {
players[socket.id].symbol = "O";
players[unmatched].opponent = socket.id;
unmatched = null;
} else { //If 'unmatched' is not define it means the player (current socket) is waiting for an opponent (player #1)
unmatched = socket.id;
}
}
function opponentOf(socket) {
if (!players[socket.id].opponent) {
return;
}
return players[players[socket.id].opponent].socket;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment