-
Notifications
You must be signed in to change notification settings - Fork 61
/
app.js
128 lines (96 loc) · 3.92 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
var express = require('express');
var app = express();
app.use(express.static('public'));
app.use(express.static('dashboard'));
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
var lobbyUsers = {};
var users = {};
var activeGames = {};
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.get('/dashboard/', function(req, res) {
res.sendFile(__dirname + '/dashboard/dashboard.html');
});
io.on('connection', function(socket) {
console.log('new connection ' + socket);
socket.on('login', function(userId) {
console.log(userId + ' joining lobby');
socket.userId = userId;
if (!users[userId]) {
console.log('creating new user');
users[userId] = {userId: socket.userId, games:{}};
} else {
console.log('user found!');
Object.keys(users[userId].games).forEach(function(gameId) {
console.log('gameid - ' + gameId);
});
}
socket.emit('login', {users: Object.keys(lobbyUsers), games: Object.keys(users[userId].games)});
lobbyUsers[userId] = socket;
socket.broadcast.emit('joinlobby', socket.userId);
});
socket.on('invite', function(opponentId) {
console.log('got an invite from: ' + socket.userId + ' --> ' + opponentId);
socket.broadcast.emit('leavelobby', socket.userId);
socket.broadcast.emit('leavelobby', opponentId);
var game = {
id: Math.floor((Math.random() * 100) + 1),
board: null,
users: {white: socket.userId, black: opponentId}
};
socket.gameId = game.id;
activeGames[game.id] = game;
users[game.users.white].games[game.id] = game.id;
users[game.users.black].games[game.id] = game.id;
console.log('starting game: ' + game.id);
lobbyUsers[game.users.white].emit('joingame', {game: game, color: 'white'});
lobbyUsers[game.users.black].emit('joingame', {game: game, color: 'black'});
delete lobbyUsers[game.users.white];
delete lobbyUsers[game.users.black];
});
socket.on('resumegame', function(gameId) {
console.log('ready to resume game: ' + gameId);
socket.gameId = gameId;
var game = activeGames[gameId];
users[game.users.white].games[game.id] = game.id;
users[game.users.black].games[game.id] = game.id;
console.log('resuming game: ' + game.id);
if (lobbyUsers[game.users.white]) {
lobbyUsers[game.users.white].emit('joingame', {game: game, color: 'white'});
delete lobbyUsers[game.users.white];
}
if (lobbyUsers[game.users.black]) {
lobbyUsers[game.users.black] && lobbyUsers[game.users.black].emit('joingame', {game: game, color: 'black'});
delete lobbyUsers[game.users.black];
}
});
socket.on('move', function(msg) {
socket.broadcast.emit('move', msg);
activeGames[msg.gameId].board = msg.board;
console.log(msg);
});
socket.on('disconnect', function(msg) {
console.log(msg);
console.log(socket.userId + ' disconnected');
console.log(socket.gameId + ' disconnected');
delete lobbyUsers[socket.userId];
socket.broadcast.emit('logout', {
userId: socket.userId,
gameId: socket.gameId
});
});
/////////////////////
// Dashboard messages
/////////////////////
socket.on('dashboardlogin', function() {
console.log('dashboard joined');
socket.gameses = ["hi", "hi"];
socket.emit('dashboardlogin', {games: activeGames});
});
});
http.listen(port, function() {
console.log('listening on *: ' + port);
});