-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (71 loc) · 2.59 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
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);
app.use(express.static('static'));
// store in memory
var games = {};
var playerColors = ['rgba(200, 0, 0, 0.8)', 'rgba(0, 0, 200, 0.8)', 'rgba(0, 200, 0, 0.8)', 'rgba(0, 200, 200, 0.8)'];
function getOrCreateGame(gameName) {
var game = games[gameName];
if (game) return game;
console.log('creating new game', gameName);
return games[gameName] = {
hostSocket: null,
players: [],
};
}
function leaveGame(gameName, leavingPlayer) {
var game = games[gameName];
if (!game) return console.log('no game with name', gameName, 'to leave');
game.players = game.players.filter(function(player) { return player.player !== leavingPlayer; });
if (game.hostSocket) game.hostSocket.emit('player left', leavingPlayer);
}
io.on('connection', function(socket) {
var hostedGames = [];
var joinedGames = [];
console.log('user connected');
socket.on('disconnect', function() {
console.log('user disconnected');
console.log('unhosting games', hostedGames);
hostedGames.forEach(function(g) { games[g].hostSocket = null });
console.log('leaving games', joinedGames);
joinedGames.forEach(function(jg) { leaveGame(jg.gameName, jg.player); });
});
socket.on('orientation data', function(data) {
var game = getOrCreateGame(data.gameName);
if (game.hostSocket) game.hostSocket.emit('orientation data', data);
});
socket.on('host game', function(data) {
console.log('host game', data);
var game = getOrCreateGame(data.gameName);
game.hostSocket = socket;
hostedGames.push(data.gameName);
game.players.forEach(function(p) { socket.emit('new player joined', p.player); });
});
socket.on('join game', function(data) {
console.log('join game', data);
var game = getOrCreateGame(data.gameName);
var num = 1;
game.players.forEach(function(p) { if (p.player.num === num) num += 1; });
var player = {
num: num,
playerName: 'Player ' + num,
playerColor: playerColors[(num - 1) % playerColors.length],
};
game.players.push({
player: player,
socket: socket
});
joinedGames.push({ gameName: data.gameName, player: player });
console.log('new player joined', player);
if (game.hostSocket) game.hostSocket.emit('new player joined', player);
socket.emit('game joined', player);
});
});
var port = process.env.PORT || 5000;
server.listen(port, function(err) {
if (err) return console.log(err.stack);
console.log('listening on port', port);
})