-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathapp.js
147 lines (109 loc) · 4.42 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
137
138
139
140
141
142
143
144
145
146
147
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/default.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) {
doLogin(socket, userId);
});
function doLogin(socket, userId) {
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.broadcast.emit('gameadd', {gameId: game.id, gameState:game});
});
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('resign', function(msg) {
console.log("resign: " + msg);
delete users[activeGames[msg.gameId].users.white].games[msg.gameId];
delete users[activeGames[msg.gameId].users.black].games[msg.gameId];
delete activeGames[msg.gameId];
socket.broadcast.emit('resign', msg);
});
socket.on('disconnect', function(msg) {
console.log(msg);
if (socket && socket.userId && socket.gameId) {
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.emit('dashboardlogin', {games: activeGames});
});
});
http.listen(port, function() {
console.log('listening on *: ' + port);
});