Created
August 9, 2018 08:54
-
-
Save MikeShi42/13b11837c02ad20807d44cd40e8371e4 to your computer and use it in GitHub Desktop.
competitive-2048 index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const socketio = require('socket.io'); | |
const http = require('http'); | |
const app = express(); | |
const server = http.Server(app); | |
const io = socketio(server); // Attach socket.io to our server | |
app.use(express.static('public')); // Serve our static assets from /public | |
server.listen(3000, () => console.log('server started')); | |
const connections = [null, null]; | |
// Handle a socket connection request from web client | |
io.on('connection', function (socket) { | |
// Find an available player number | |
let playerIndex = -1; | |
for (var i in connections) { | |
if (connections[i] === null) { | |
playerIndex = i; | |
} | |
} | |
// Tell the connecting client what player number they are | |
socket.emit('player-number', playerIndex); | |
// Ignore player 3 | |
if (playerIndex == -1) return; | |
connections[playerIndex] = socket; | |
// Tell everyone else what player number just connected | |
socket.broadcast.emit('player-connect', playerIndex); | |
socket.on('actuate', function (data) { | |
const { grid, metadata } = data; // Get grid and metadata properties from client | |
const move = { | |
playerIndex, | |
grid, | |
metadata, | |
}; | |
// Emit the move to all other clients | |
socket.broadcast.emit('move', move); | |
}); | |
socket.on('disconnect', function() { | |
console.log(`Player ${playerIndex} Disconnected`); | |
connections[playerIndex] = null; | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment