-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
68 lines (52 loc) · 1.75 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
"use strict";
var gameport = process.env.PORT || 8080;
var io = require('socket.io');
var express = require('express');
var UUID = require('node-uuid');
var http = require('http');
var app = express();
var server = http.createServer(app);
server.listen(gameport, "localhost", function () {
console.log(':: Server :: Listening on port ' + gameport);
});
app.use(express.static('./'));
//Allow accessing to homepage
app.get('/', function (req, res) {
console.log('Loading the homepage /index.html');
res.sendFile('/index.html', {
root: __dirname
});
});
//Expose all assets and urls under conndectFour/
app.get('/*', function (req, res, next) {
var file = req.params[0];
res.sendFile(__dirname + '/' + file);
});
//Setup sockets to listen on port
var sio = io.listen(server);
//Set up gameServer model instance
var gameServer = require("./js/gameServer.js");
//Listen for connections via socket.io
sio.on('connection', function (client) {
//assign UUID to user
client.userID = UUID();
//Send user their UUID on connection
client.emit('onconnection', {
id: client.userID
});
console.log(':: Socket.io :: player ' + client.userID + ' connected');
gameServer.findGame(client);
//Listen for game moves
client.on('playerMove', function (data) {
gameServer.playerMove(client.userID, data);
});
//Ensure both users disconnect when one disconnects
client.on('disconnect', function () {
//Useful to know when soomeone disconnects
console.log(':: Socket.io :: client disconnected ' + client.userID + ' ' + client.game_id);
//Disconnect users
if (client.game && client.game.id) {
gameServer.endGame(client.game.id, client.userID);
}
});
});