forked from houseofbits/nodejs-uno-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
48 lines (39 loc) · 1.66 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let express = require('express');
let application = express();
let server = require('http').Server(application);
let GameServiceFactory = require('./node_src/GameServiceFactory.js');
let GameServiceRepository = require('./node_src/GameServiceRepository.js');
let UnitTest = require('./node_src/UnitTest.js');
//Perform unit tests on some required logic
let unitTest = new UnitTest();
application.get('/', function(request, response){
response.sendFile(__dirname + '/client/index.html');
});
application.use('/client', express.static(__dirname + '/client'));
//server.listen(3000);
server.listen(80);
console.log('============= Server started ===============');
let io = require('socket.io')(server, {});
let gameServiceRepository = new GameServiceRepository();
let gameServiceFactory = new GameServiceFactory();
io.sockets.on('connection', function(socket) {
console.log('Socket connection');
socket.on('create', function(room) {
console.log('Join room: ' + room + ' socketId: ' + socket.id);
socket.join(room);
let gameService = gameServiceRepository.findById(room);
if(!gameService){
gameService = gameServiceFactory.create("UNO", room);
gameServiceRepository.insert(gameService);
}
socket.use(function(packet){
gameService.handleAction(socket, packet[0], packet[1]);
Object.keys(io.sockets.sockets).forEach(function(id) {
let data = gameService.getClientResponseData(id);
if(data){
io.to(id).emit('state', data);
}
});
});
});
});