This repository was archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
136 lines (111 loc) · 3.93 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Include all of the required node modules
var http = require('http'),
path = require('path'),
express = require('express'),
// Create a working node app
app = express(),
server = http.createServer(app),
// Instantiate a websocket server
io = require('socket.io').listen(server),
// Link all of our files together
router = require('./server/router'),
lobbies = {};
// Set the main directory for client files to the client directory
app.set("view options", {
layout: false
});
app.use(express.static(path.resolve(__dirname, 'client')));
app.get('/changelog', function(req, res) {
res.sendfile(__dirname + '/client/changelog.html');
});
// Configure the socket
io.configure(function() {
// Don't log unimportant (to us) stuff to the console
io.set('log level', 0);
// Require a handshake before proceeding with a client
io.set('authorization', function(handshakeData, callback) {
callback(null, true); // error first callback style
});
});
function createLobbyList() {
var lobbyList = {};
for (var lobby in lobbies) {
if (lobbies.hasOwnProperty(lobby)) {
lobbyList[lobby] = {
'playerCount': Object.keys(lobbies[lobby].clients).length
}
}
}
return lobbyList;
}
function forEachSocket(callback) {
var clients = io.sockets.clients();
for (var i = clients.length - 1; i >= 0; i--) {
callback(clients[i]);
}
}
// Define what happens when a user connects to the server
io.on('connection', function(socket) {
// Log the new socket's ID to the server's console
console.log('Socket connected:', socket.id);
// When signing in, send them all the lobbies
socket.on('signIn', function(data) {
socket.name = data.name;
socket.isInLobby = true;
socket.emit('lobbyList', createLobbyList());
});
socket.on('newLobby', function() {
var lobby = router.createLobby(5),
listOfLobbies;
lobbies[lobby.id] = lobby;
listOfLobbies = createLobbyList();
forEachSocket(function(socket) {
if (socket.isInLobby) {
socket.emit('lobbyList', listOfLobbies);
}
});
});
// When the player is ready to play, add them to an open lobby
socket.on('play', function(data) {
router.findLobby(lobbies, data.lobbyId).addPlayer(socket);
socket.isInLobby = false;
var listOfLobbies = createLobbyList();
forEachSocket(function(socket) {
if (socket.isInLobby) {
socket.emit('lobbyList', listOfLobbies);
}
});
});
// Process data received from the socket
socket.on('message', function(message) {
if (!socket.player) {
router.findOpenLobby(lobbies).addPlayer(socket);
}
socket.player.parseMessage(message);
});
// Define what happens when a user disconnects
socket.on('disconnect', function() {
// Log the socket's disconnection w/ ID to the console
console.log('Socket disconnected:', socket.id);
if (socket.player) {
var listOfLobbies;
console.log('And left the lobby:', socket.player.lobby.id);
socket.player.lobby.removePlayer(socket);
if (!Object.keys(socket.player.lobby.clients).length) {
socket.player.lobby.remove(lobbies);
}
listOfLobbies = createLobbyList();
forEachSocket(function(socket) {
if (socket.isInLobby) {
socket.emit('lobbyList', listOfLobbies);
}
});
}
});
});
// Set the server to listen for clients on localhost:3000
server.listen(process.env.PORT || 3000, process.env.IP || '0.0.0.0', function() {
var addr = server.address();
// Log the server's address to the console
console.log('Server listening at', addr.address + ':' + addr.port);
});