-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmatch.js
More file actions
157 lines (138 loc) · 4.44 KB
/
Copy pathmatch.js
File metadata and controls
157 lines (138 loc) · 4.44 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
var request = require("request");
var crypto = require('crypto');
var util = require('util');
var fs = require('fs');
var SLUG_LEN = 5;
var STORE = {
filename: 'matches.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(games);
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 matches');
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 =
[
['games', {}]
];
function init(app, io, sendMandrill, db) {
reloads.forEach(function (p) {
STORE.bind(p[0], p[1]);
});
var playerSockets = {};
io.sockets
.on('connection', function (socket) {
socket.connected = true;
console.log('connected', socket.id);
socket.once('getPID', function (msg, fn) {
if (msg.playerID) {
console.log('connecting', 'existing PID', msg);
console.log('match');
socket.playerID = msg.playerID;
playerSockets[msg.playerID] = socket;
//init game
var game = getGame(msg.matchID, msg.playerID);
return fn({playerID: msg.playerID, messages: game.messages, playerNumber: game.players.indexOf(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;
//init game
var game = getGame(msg.matchID, slug);
fn({playerID: slug, messages: game.messages, playerNumber: game.players.indexOf(slug)});
});
}
});
socket.on('disconnect', function () {
socket.connected = false;
if (socket.playerID && playerSockets[socket.playerID])
delete playerSockets[socket.playerID];
console.log('disconnected', socket.playerID, socket.id);
});
socket.on('playerWrite', function (msg, fn) {
console.log('playerWrite', msg.playerID);
var game = getGame(msg.matchID, msg.playerID);
var playerNumber = game.players.indexOf(msg.playerID);
var entry = {playerNumber: playerNumber, text: msg.text, i: game.messages.length};
game.messages.push(entry);
fn({i: entry.i});
game.players.forEach(function (partnerID) {
try {
console.log('broadcast', msg.playerID, '->', partnerID);
playerSockets[partnerID].emit('entryUpdate',
{self: playerNumber == game.players.indexOf(partnerID),
playerNumber: playerNumber,
text: entry.text,
i: entry.i});
} catch (e) {
console.error('bad broadcast', e, {playerID: msg.playerID, partnerID: msg.partnerID});
}
});
});
});
return module.exports;
};
function shutdown (cb) {
console.log('shutdown party');
STORE.flush(function (err) {
if (err) console.log('failure storing game state (match.js)', err);
else console.log('saved game state');
cb();
});
}
///////////////
function getGame(matchID, playerID) {
if (!matchID || !playerID) throw 'getGame missing args';
console.log('gaming', matchID, playerID);
var game = games[matchID];
if (!game) {
console.log('creating game', {matchID: matchID, playerID: playerID});
game = {players: [playerID], messages: []};
games[matchID] = game;
}
if (game.players.indexOf(playerID) == -1)
game.players.push(playerID);
return game;
}
///////////////
///////////////
module.exports = {
init: init,
shutdown: shutdown
};