Created
April 9, 2018 20:00
-
-
Save MattSandy/da1f0f3b09aebf4ecf08d57587e78a35 to your computer and use it in GitHub Desktop.
Barebones game server with move tracking and rooms using Node and Socket.io
This file contains hidden or 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
var app = require('express')(); | |
var server = require('http').Server(app); | |
var io = require('socket.io')(server); | |
var games = []; | |
var lookup = []; | |
server.listen(3000); | |
app.get('/', function (req, res) { | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
io.on('connection', function (socket) { | |
//where the magic happens | |
socket.emit('event', { hello: 'world' }); | |
socket.on('move', function (data) { | |
console.log(games); | |
try { | |
if(games[lookup[socket.id]].client === socket.id) { | |
//client can go | |
if((games[lookup[socket.id]].moves % 2) === 1) { | |
games[lookup[socket.id]].moves++; | |
io.to(lookup[socket.id]).emit('move',"CLIENT MADE A MOVE"); | |
} | |
} else if(games[lookup[socket.id]].host === socket.id) { | |
//host can go | |
if((games[lookup[socket.id]].moves % 2) === 0) { | |
games[lookup[socket.id]].moves++; | |
io.to(lookup[socket.id]).emit('move',"HOST MADE A MOVE"); | |
} | |
} else { | |
//something went wrong | |
console.log("something went wrong"); | |
} | |
} catch(err) { | |
//the client is not in a room; | |
} | |
}); | |
socket.on('rooms', function (data) { | |
//send the possible rooms by getting property names of io.sockets.adapter.rooms | |
//searches property names for room_, and returns only those results | |
var rooms = Object.getOwnPropertyNames(io.sockets.adapter.rooms).find(function(el) { | |
return el.match('room_'); | |
}); | |
socket.emit('rooms', { data: rooms }); | |
}); | |
socket.on('host', function(data) { | |
//becoming a host requested | |
//global var game keeps track of client and moves | |
games["room_" + socket.id] = { | |
host: socket.id, | |
client: false, | |
moves: 0 | |
}; | |
//add the room to the lookup table for socket ids | |
lookup[socket.id] = "room_" + socket.id; | |
//put host in a room with the host's socket id | |
socket.join("room_" + socket.id); | |
}); | |
socket.on('join', function(data) { | |
try { | |
if(!games[data.game].client) { | |
//set client to prevent others from joining | |
games[data.game].client = socket.id; | |
//add the room to the lookup table for socket ids | |
lookup[socket.id] = data.game; | |
socket.join(data.game); | |
} else { | |
socket.emit('oops', { message: 'room client already connected' }); | |
} | |
} catch(err) { | |
socket.emit('oops', { message: 'Game not found' }); | |
} | |
}); | |
}); |
This file contains hidden or 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
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io.connect('/'); | |
socket.emit('rooms'); | |
socket.on('rooms', function (data) { | |
console.log(data); | |
}); | |
socket.on('move', function (data) { | |
console.log("MOVE"); | |
console.log(data); | |
}); | |
socket.on('oops', function (data) { | |
console.log("ERROR"); | |
console.log(data); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment