Repository navigation
Expand file tree
/
Copy pathlobby.js
More file actions
234 lines (202 loc) · 6.03 KB
/
Copy pathlobby.js
File metadata and controls
234 lines (202 loc) · 6.03 KB
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
var request = require("request");
var crypto = require('crypto');
var util = require('util');
var fs = require('fs');
var SLUG_LEN = 5;
var STORE = {
filename: 'lobby.json',
bind: function (id, def) {
if (!STORE.data[id]) {
def.id = id;
STORE.data[id] = def;
}
global[id] = STORE.data[id];
return STORE.data[id];
},
flush: function (cb) {
console.log('flushing game state');
console.log('real', {wants: wantsAsync ? wantsAsync.length : 0, recent: recentAsync ? recentAsync.length : 0});
try {
fs.writeFileSync(STORE.filename,
JSON.stringify(STORE.data,
function (k, v) {
if (k == 'socket') return ''; //discard
else return v;
}));
} catch (e) {
return cb(e);
}
return cb();
},
data: {}
}
try {
console.log('reloading party');
STORE.data = JSON.parse(fs.readFileSync(STORE.filename, 'utf8'));
console.log('loaded', STORE.data);
for (var i in STORE.data) //JSON loses IDs otherwise
STORE.data[i].id = i;
} catch (e) {
console.log('could not load party, restarting from scratch');
}
setInterval(function () { //backup every 5 min
STORE.flush(function (err) {
console.log('game state backup', err ? 'failed' : 'succeeded', err);
});
}, 5 * 60 * 1000);
//persistent global state
var reloads =
[
['wantsMatch', {}],
['wantsAsync', []],
['recentAsync', []],
['handshakes', {}],
['games', {}]
];
function init(app, io, sendMandrill, db) {
reloads.forEach(function (p) {
STORE.bind(p[0], p[1]);
});
console.log('wantsAsync', global.wantsAsync, wantsAsync);
var adminSockets = [];
var playerSockets = {};
io.sockets
.on('connection', function (socket) {
socket.connected = true;
console.log('connected', socket.id);
socket.once('getPIDLobby', function (msg, fn) {
console.log('connecting lobby...', msg, fn ? true : false);
if (!socket) {
console.error('no socket..');
return;
}
if (msg.playerID && msg.playerID != 'undefined') {
console.log('connecting', 'existing PID', msg, socket ? true : false);
console.log('lobby');
socket.playerID = msg.playerID;
playerSockets[msg.playerID] = socket;
return fn(msg.playerID);
} else {
crypto.randomBytes(SLUG_LEN, function(ex, buf) {
var slug = buf.toString('hex');
console.log('connecting', 'gensym PID', slug);
socket.playerID = slug;
playerSockets[slug] = socket;
fn(slug);
});
}
});
lobby(socket, db);
join(socket);
socket.on('disconnect', function () {
socket.connected = false;
if (socket.playerID && playerSockets[socket.playerID])
delete playerSockets[socket.playerID];
if (adminSockets.indexOf(socket) != -1)
adminSockets.splice(adminSockets.indexOf(socket), 1);
console.log('disconnected', socket.playerID, socket.id);
});
})
return module.exports;
};
function shutdown (cb) {
console.log('shutdown party');
STORE.flush(function (err) {
if (err) console.log('failure storing game state', err);
console.log('saved game state');
cb();
});
}
function join(socket) {
socket.on('joinLobbyGame', function (msg, fn) {
var game = games[msg.matchID];
if (game.players.indexOf(msg.playerID) == -1) game.players.push(msg.playerID);
if (game.players.length == 2) {
if (wantsAsync.indexOf(game) != -1) {
var spliced = wantsAsync.splice(wantsAsync.indexOf(game), 1);
console.log('spliced', spliced);
recentAsync.push(spliced[0]);
} // else resuming old
}
fn();
});
}
function lobby(socket, db) {
socket.once('getLobby', function (msg) {
console.log('get lobby evt!');
var now = new Date().getTime();
//FIXME memoize..
function summarizeGame(match) {
console.log('match', match);
var game = games[match.matchID];
console.log('game', game);
var isOriginalActive =
(game && game.match && game.match.original && game.match.original.socket && game.match.original.socket.connected)
|| (match.creator && match.creator.socket && match.creator.socket.connected);
var isPartnerActive =
game && game.match && game.match.partner && game.match.partner.socket && game.match.partner.socket.connected;
var res = {
playerCount: game.players.length,
matchID: match.matchID,
active: (isOriginalActive ? 1 : 0) + (isPartnerActive ? 1 : 0),
creator: game ?
{ active: isOriginalActive,
playerID: game.players.indexOf(msg.playerID) > -1 ? msg.playerID : null,
}
: match.creator ? //made but not entered yet
{ active: isOriginalActive,
playerID: match.creator.playerID == msg.playerID ? msg.playerID : null,
} : null,
partner: game && game.match && game.match.partner ?
{ active: isPartnerActive,
playerID: game.match.partner.playerID == msg.playerID ? msg.playerID : null,
} : null,
time: now - match.time
};
return res;
}
var lobby = {want: wantsAsync.slice(-15).map(summarizeGame),
old: recentAsync.slice(-10).map(summarizeGame)};
console.log('sending lobby', lobby);
socket.emit('gotLobby', lobby);
socket.once('createAsyncGame', function (msg, fn) {
console.log('create game');
var maxLen = 400;
var blurb = '';
crypto.randomBytes(SLUG_LEN, function(ex, buf) {
var slug = buf.toString('hex');
var url =
msg.baseURI
+ "/match.html?matchID=" + slug
+ "&playerID=" + msg.playerID
+ "&name=" + encodeURIComponent('' + msg.name)
+ "&email=" + encodeURIComponent('' + msg.email);
var match = {
time: new Date().getTime(),
creator: {
playerID: msg.playerID,
p: '1',
m: slug,
name: msg.name,
description: null,
email: msg.email,
text: blurb,
a: msg.a,
emailNotification: msg.emailNotification,
url: url
},
matchID: slug,
player1Doc: msg.doc
};
wantsMatch[slug] = match;
wantsAsync.push(match);
fn({matchID: match.matchID, url: url})
});
});
});
}
///////////////
module.exports = {
init: init,
shutdown: shutdown
};