I am currently creating multiple games by adding objects of type Game
as properties of an object called games
. The names of these properties are set programmatically - for example, the first game will be a property of games
called game1
. Sockets joining this game will join the room room1
.
Is this the right approach? It feels like there might be a cleaner way.
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Game = require('./Game.js');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.sendFile('/index.html');
});
var games = {
//Games get put here
};
var gameCount = 0;
io.on('connection', function(socket) {
var i = gameCount;
var arrayOfGames = [];
while(i){
var gameInfo = {};
gameInfo.name = games["game"+i]["getName"]();
gameInfo.no = games["game"+i]["getNo"]();
arrayOfGames.push(gameInfo);
i--;
}
socket.emit('populate game list',arrayOfGames);
socket.on('create game',function(options){
gameCount++;
options.no = gameCount;
games["game"+ gameCount] = new Game(options);
socket.join('room' + gameCount);
socket.room = 'room'+gameCount;
});
socket.on('join game',function(no){
socket.join('room'+no);
socket.room = 'room'+no;
});
});
var port = Number(process.env.PORT || 3000);
http.listen(port, function() {
console.log('listening on *:3000');
});
I can show the client side code and class code if you request, but I don't feel it's relevant here.