-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
105 lines (89 loc) · 3.09 KB
/
app.js
File metadata and controls
105 lines (89 loc) · 3.09 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var _ = require('lodash');
var settings = require('./settings.js');
var path = require('path')
var people = require('./People.js')
var Rooms = require('./Rooms.js')
Rooms.io = io;
app.use(express.static(settings.STATIC_DIR));
app.use('/node_modules', express.static('node_modules'))
app.get('/', function(req, res) {
return res.sendFile(path.join(__dirname, 'static/index.html'))
})
app.get('/rooms', function(req, res) {
var rooms = Rooms.getRooms();
return res.json(rooms);
})
io.on('connection', function(client) {
console.log("Client connected, id: ", client.id);
client.on('joinRoom', function(data) {
people[client.id] = data.name || "Guest#" + randomIntBetween(1000, 9999);
var roomName = 'rooms/' + data.roomId;
client.join(roomName);
if (!Rooms.getGame(data.roomId).owner) {
Rooms.assignGameOwner(data.roomId, client.id);
}
var game = Rooms.getGame(data.roomId);
if (game.started && game.sets[people[client.id]] === undefined) {
game.sets[people[client.id]] = 0;
}
io.to(roomName).emit('message', {timestamp: new Date(), server: true, message: people[client.id] + " has joined the room!"});
client.emit('gameStatus', Rooms.getGame(data.roomId))
io.emit('updateRooms', {rooms: Rooms.getRooms()});
})
client.on('leaveRoom', function(data) {
var roomName = 'rooms/' + data.roomId;
client.leave(roomName);
io.to(roomName).emit('message', {timestamp: new Date(), server: true, message: people[client.id] + " has left the room."})
io.emit('updateRooms', {rooms: Rooms.getRooms()});
})
client.on('messageSent', function(message) {
var room = _.find(client.rooms, function(room) {
return room.indexOf('rooms/') > -1;
})
io.to(room).emit('message', {timestamp: new Date(), sender: people[client.id], message: message});
})
client.on('startGame', function(roomId) {
var roomName = 'rooms/' + roomId;
Rooms.startGame(roomId);
var game = Rooms.getGame(roomId);
if (client.id === game.owner) {
io.to(roomName).emit('gameStatus', Rooms.getGame(roomId))
}
})
client.on('submitSet', function(data) {
var setIsValid = Rooms.validateSet(data.roomId, data.set);
if (setIsValid) {
io.to('rooms/' + data.roomId).emit('correctSet', {
scorer: people[client.id],
set: data.set,
sets: Rooms.getGame(data.roomId).sets,
timestamp: new Date()
})
setTimeout(function() {
Rooms.processSet(client.id, data.roomId, data.set);
var game = Rooms.getGame(data.roomId);
io.to('rooms/' + data.roomId).emit('gameStatus', game);
}, 1000);
}
else {
io.to('rooms/' + data.roomId).emit('failSet', {
failure: people[client.id],
socketId: client.id
})
}
})
client.on('disconnect', function(data) {
Rooms.playerLeft(client.id);
io.sockets.emit('updateRooms', {rooms: Rooms.getRooms()})
})
})
server.listen(settings.PORT_NUMBER, function() {
console.log("Server running at port " + settings.PORT_NUMBER);
});
function randomIntBetween(min,max) {
return Math.floor(Math.random()*(max-min+1)+min);
}