-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
325 lines (256 loc) · 11.6 KB
/
server.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Setting up libraries and configuration
require('dotenv').config(); // Loads variables from .env file into process.env
const express = require('express'); // The require() function includes the code for Express
const app = express(); // Initialize the Express library
const http = require('http'); // Get access to Node's http methods!
const https = require('https'); // ... and https methods!
const server = http.Server(app); // Initialize an Express HTTP server
const io = require('socket.io')(server); // Include and initialize SocketIO
const port = process.env.PORT || 8000; // Set the default port number to 8000, or use Heroku's settings (process.env.PORT)
// Use Express to serve everything in the "public" folder as static files
app.use(express.static('public'));
// Pass GITHUB_CLIENT_ID to client when requested (using AJAX for now)
// TODO (later): mess around with templating engines and Express .render()?
app.get('/github-client', function (req, res) {
console.log('Request received for /github-client route. Sending response: GITHUB_CLIENT_ID');
res.end(process.env.GITHUB_CLIENT_ID);
});
// Handle GitHub authentication at this route, then pass token back to client
app.get('/github-auth', authenticateUser);
function authenticateUser (req, res) {
// TO DO: use "state" param and verify it for extra security!
// Make a POST request to https://github.com/login/oauth/access_token
let githubResponseBody = '';
let postRequestBody = 'client_id=' + process.env.GITHUB_CLIENT_ID + '&client_secret=' + process.env.GITHUB_CLIENT_SECRET + '&code=' + req.query.code;
let request = https.request({
hostname: 'github.com',
path: '/login/oauth/access_token',
port: '443',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postRequestBody),
'Accept': 'application/json'
}
}, function(response) {
response.on('data', function(chunk) {
githubResponseBody += chunk;
});
response.on('end', function() {
// TODO (later): check the scopes, because users can authorize less than what my app requested!
// Send GitHub access token back to client
res.end( JSON.parse(githubResponseBody).access_token );
});
});
request.write(postRequestBody);
request.end();
}
// Activate the server and listen on our specified port number
server.listen(port, function() {
// Display this message in the server console once the server is active
console.log('Listening on port ' + port);
});
/* ------------------------------------------------------------
GAME STATE:
{
nextTurnTimestamp,
turnIndex,
currentGist: {id, url, owner},
players:
[
{id, login,avatar_url}, { ... }, { ... }, ...
],
editor:
{
content,
cursorAndSelection: { cursor: {column, row}, range: { end: {column, row}, start: {column, row} },
scroll: {scrollLeft, scrollTop}
}
}
-------------------------------------------------------------- */
let gameState = {
nextTurnTimestamp: null,
turnIndex: 0,
currentGist: null,
players: [],
editor:
{
content: '// Type JavaScript here!',
cursorAndSelection: null,
scroll: null
}
};
const turnDuration = 60000;
let timerId = null;
/* ----------------------------------------------------------------------------------------------------------------------------------------------
EVENTS:
Event Name Sent By Sent To Data Description
------------------ ---------- ------------------ -------------------------- -----------------------------------------------------------------
playerJoined Client Server {login, avatar_url} When new player completes login process
playerJoined Server All other clients {id, login, avatar_url} Update other clients with new player data
gameState Server One client See game state model! Initialize game state for new player that just logged in,
and trigger new gist creation if game is just starting!
playerLeft Server All other clients id Update other clients to remove disconnected player
turnChange Server All clients onDisconnect (Boolean) Trigger clients to change the turn
newGist Client Server {id, url, owner} Broadcast new Gist data
editorTextChange Client Server "just a string!" Broadcast changes to code editor content
editorScrollChange Client Server {scrollLeft, scrollTop} Broadcast changes to code editor content
editorCursorChange Client Server { Broadcast cursor moves or selection changes
cursor: {column, row},
range: {
end: {column, row},
start: {column, row}
}
}
disconnect Client Server ... When clients disconnect from server (SocketIO function)
connection Client Server ... When clients connect to server (SocketIO function)
------------------------------------------------------------------------------------------------------------------------------------------------- */
// When a user connects over websocket,
io.on('connection', function (socket) {
console.log('\nA user connected! (But not yet logged in.)\n');
// When a player logs in,
socket.on('playerJoined', function (playerData) {
console.log('\n* * * * # # # # User logged in! # # # # * * * * *');
console.log('\t\t > > > ' + playerData.login + ' < < <\n');
// Add new player
gameState.players.push({id: socket.id, login: playerData.login, avatar_url: playerData.avatar_url});
// If there is 1 player logged in, START THE GAME!!!
if (gameState.players.length === 1) {
timerId = startTurnTimer(timerId, turnDuration);
}
// Initialize new player
socket.emit('gameState', gameState);
// Broadcast new player data to all OTHER clients
socket.broadcast.emit('playerJoined', {id: socket.id, login: playerData.login, avatar_url: playerData.avatar_url});
});
// When a player disconnects,
socket.on('disconnect', function() {
console.log('\nA user disconnected!\n');
// If disconnected player was logged in,
if (getPlayerById(socket.id, gameState.players) !== -1) {
console.log('\n\t User removed from list of logged-in players. ID: ' + socket.id);
// Broadcast the disconnected player's ID to update all other clients
socket.broadcast.emit( 'playerLeft', socket.id );
// Temporarily save ID of current player (before removing from player list, for a later check!)
let currentPlayerId = getCurrentPlayer().id;
// Update turnIndex only if disconnected player comes BEFORE current player in the players array, and there are still players in the game:
if ( getPlayerIndexById(socket.id, gameState.players) < gameState.turnIndex && gameState.players.length > 1) {
gameState.turnIndex--;
}
// Remove disconnected player from player list
removePlayer(socket.id, gameState.players);
// If no logged-in players are left, reset the game!
if (gameState.players.length === 0) {
console.log('\nNo players left. Turning off the turn timer!\n');
// Turn off the timer
clearInterval(timerId);
// Otherwise, if there are players left, and the disconnected player was the current player, restart timer and change the turn!
} else if (socket.id === currentPlayerId) {
console.log('\nCURRENT PLAYER disconnected! Restarting turn/timer.\n');
// Turn off the timer
clearInterval(timerId);
// Restart the timer
timerId = startTurnTimer(timerId, turnDuration);
// Change the turn, including the onDisconnect=true flag to signal clients NOT to fork/edit the Gist on this turn change
changeTurn(true);
}
} else {
console.log('\n\t User was not yet logged in, so no action taken.\n');
}
});
// When "editorTextChange" event received, update editor state and broadcast it back out
socket.on('editorTextChange', function (data) {
// Double check that this user is allowed to type (in case of client-side tampering with the JS!)
if ( socket.id === getCurrentPlayer().id ) {
// Update saved state of the shared text editor
gameState.editor.content = data;
// Broadcast updated editor content to other clients
socket.broadcast.emit('editorTextChange', gameState.editor.content);
}
});
// When "editorCursorChange" event received, update editor state and broadcast it back out
socket.on('editorCursorChange', function (data) {
// Double check that this user is allowed to broadcast (in case of client-side tampering with the JS!)
if (socket.id === getCurrentPlayer().id ) {
// Update saved state of the shared text editor
gameState.editor.cursorAndSelection = data;
// Broadcast data to other clients
socket.broadcast.emit('editorCursorChange', gameState.editor.cursorAndSelection);
}
});
// When "editorScrollChange" event received, update editor state and broadcast it back out
socket.on('editorScrollChange', function (data) {
// Double check that this user is allowed to broadcast (in case of client-side tampering with the JS!)
if (socket.id === getCurrentPlayer().id ) {
// Update saved state of the shared text editor
gameState.editor.scroll = data;
// Broadcast data to other clients
socket.broadcast.emit('editorScrollChange', gameState.editor.cursorAndSelection);
}
});
// When "newGist" event received, update state and broadcast it back out
socket.on('newGist', function (data) {
// Double check that this user is allowed to broadcast a new gist!
if (socket.id === getPreviousPlayer().id) {
console.log('\nnewGist event received!\n');
//console.log(data);
gameState.currentGist = data;
// Broadcast data to other clients
socket.broadcast.emit('newGist', data);
}
});
}); // End of SocketIO part of the code
/* -------------------------------------------------
FUNCTIONS
---------------------------------------------------- */
function changeTurn(onDisconnect) {
gameState.turnIndex = (gameState.turnIndex + 1) % gameState.players.length;
// Broadcast turnChange to ALL clients, with disconnected player ID (which exists only if current player disconnected):
io.emit('turnChange', onDisconnect);
}
// Initializes the turn and turn timer, returns timerId
function startTurnTimer(timerId, turnDuration) {
console.log('\nInitializing turn timer!');
// Initialize or reset time remaining
gameState.nextTurnTimestamp = Date.now() + turnDuration;
console.log( 'Next turn at: ' + new Date(gameState.nextTurnTimestamp).toString().substring(16,25) );
// Every time the timer goes off, update timestamp and change turn!
timerId = setInterval(() => {
console.log('\n >>>>>>>>>>> ' + new Date().toString().substring(16,25) + ' - Time to change turns! <<<<<<<<<<\n');
// Update time of next turn change
gameState.nextTurnTimestamp = Date.now() + turnDuration;
changeTurn();
}, turnDuration); // TO DO: enable user-specified turn length
return timerId;
}
// Helper functions, just in case:
function getCurrentPlayer() {
return gameState.players[gameState.turnIndex];
}
function getPreviousPlayer() {
let previousPlayerIndex = (gameState.turnIndex + gameState.players.length - 1) % gameState.players.length;
return gameState.players[previousPlayerIndex];
}
function getPlayerById(id, playerList){
for (let i = 0; i < playerList.length; i++) {
if (playerList[i].id === id) {
return playerList[i];
}
}
return -1;
}
function getPlayerIndexById(id, playerList) {
for (let i = 0; i < playerList.length; i++) {
if (playerList[i].id === id) {
return i;
}
}
return -1;
}
function removePlayer(id, playerList) {
for (let i = 0; i < playerList.length; i++) {
if (playerList[i].id === id) {
playerList.splice(i, 1);
}
}
}