forked from odota/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playerCache.js
231 lines (228 loc) · 6.96 KB
/
playerCache.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
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
var config = require('./config');
var zlib = require('zlib');
var compute = require('./compute');
var computePlayerMatchData = compute.computePlayerMatchData;
var aggregator = require('./aggregator');
var async = require('async');
var constants = require('./constants');
var utility = require('./utility');
var filter = require('./filter');
var reduceAggregable = utility.reduceAggregable;
var enabled = config.ENABLE_PLAYER_CACHE;
var cEnabled = config.CASSANDRA_PLAYER_CACHE;
var redis;
var cassandra;
if (enabled)
{
redis = require('./redis');
if (cEnabled)
{
cassandra = require('./cassandra');
}
}
//CREATE KEYSPACE yasp WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1': 1 };
//CREATE TABLE yasp.player_caches (account_id bigint, match_id bigint, match text, PRIMARY KEY(account_id, match_id));
function readCache(account_id, options, cb)
{
if (enabled)
{
if (cEnabled)
{
console.time('readcache');
var query = 'SELECT match FROM player_caches WHERE account_id = ?';
return cassandra.execute(query, [account_id],
{
prepare: true
}, function(err, results)
{
if (err)
{
console.log(err);
return cb(err);
}
if (!results.rows || !results.rows.length)
{
return cb();
}
var matches = results.rows.map(function(m)
{
return JSON.parse(m.match);
});
//get array of matches, filter, agg and return results
var filtered = filter(matches, options.js_select);
var aggData = aggregator(filtered, options.js_agg);
console.timeEnd('readcache');
cb(err,
{
aggData: aggData
});
});
}
else
{
console.time('readcache');
redis.get(new Buffer("player:" + account_id), function(err, result)
{
var cache = result ? JSON.parse(zlib.inflateSync(result)) : null;
console.timeEnd('readcache');
//console.log(result ? result.length : 0, JSON.stringify(cache).length);
return cb(err, cache);
});
}
}
else
{
return cb();
}
}
function writeCache(account_id, cache, cb)
{
if (enabled)
{
if (cEnabled)
{
console.time("writecache");
console.log("saving player cache to cassandra %s", account_id);
var arr = cache.raw.map(function(m)
{
return reduceAggregable(m);
});
//upsert matches into store
return async.each(arr, function(m, cb)
{
var query = 'INSERT INTO player_caches (account_id, match_id, match) VALUES (?, ?, ?)';
cassandra.execute(query, [m.account_id, m.match_id, JSON.stringify(m)],
{
prepare: true
}, cb);
}, function(err)
{
console.timeEnd("writecache");
return cb(err);
});
}
else
{
console.time("writecache");
console.log("saving player cache to redis %s", account_id);
redis.ttl("player:" + account_id, function(err, ttl)
{
if (err)
{
return cb(err);
}
cache = {
aggData: cache.aggData
};
redis.setex(new Buffer("player:" + account_id), Number(ttl) > 0 ? Number(ttl) : 24 * 60 * 60 * config.UNTRACK_DAYS, zlib.deflateSync(JSON.stringify(cache)), function(err)
{
console.timeEnd("writecache");
cb(err);
});
});
}
}
else
{
return cb();
}
}
function updateCache(match, cb)
{
if (enabled)
{
var players = match.players;
if (match.pgroup && players)
{
players.forEach(function(p)
{
//add account id to each player so we know what caches to update
p.account_id = match.pgroup[p.player_slot].account_id;
//add hero_id to each player so we update records with hero played
p.hero_id = match.pgroup[p.player_slot].hero_id;
});
}
async.eachSeries(players, function(player_match, cb)
{
if (player_match.account_id && player_match.account_id !== constants.anonymous_account_id)
{
readCache(player_match.account_id,
{}, function(err, cache)
{
if (err)
{
return cb(err);
}
//if player cache doesn't exist, skip
if (cache)
{
//join player with match to form player_match
for (var key in match)
{
player_match[key] = match[key];
}
computePlayerMatchData(player_match);
if (cEnabled)
{
writeCache(player_match.account_id,
{
raw: [player_match]
}, cb);
}
else
{
cache.aggData = aggregator([player_match], null, cache.aggData);
writeCache(player_match.account_id, cache, cb);
}
}
else
{
return cb();
}
});
}
else
{
return cb();
}
}, cb);
}
else
{
return cb();
}
}
function countPlayerCaches(cb)
{
if (enabled)
{
if (cEnabled)
{
cassandra.execute('SELECT DISTINCT COUNT(account_id) FROM player_caches', [],
{
prepare: true
}, function(err, result)
{
result = result && result.rows && result.rows[0] && result.rows[0].count ? result.rows[0].count.toNumber() : 0;
return cb(err, result);
});
}
else
{
redis.keys("player:*", function(err, result)
{
cb(err, result.length);
});
}
}
else
{
return cb(null, 0);
}
}
module.exports = {
readCache: readCache,
writeCache: writeCache,
updateCache: updateCache,
countPlayerCaches: countPlayerCaches,
};