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
|
#!/usr/bin/env node
/* jshint esnext: true */
var request = require("request-promise");
var pg = require("pg");
var Pool = pg.Pool;
var db_config_raw = {
user: "vainraw",
password: "vainraw",
host: "localhost",
database: "vainsocial-raw",
port: 5433,
max: 10
};
var db_config_web = {
user: "vainweb",
password: "vainweb",
host: "localhost",
database: "vainsocial-web",
port: 5432,
max: 10
};
var pool_raw = new Pool(db_config_raw);
var pool_web = new Pool(db_config_web);
var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var APITOKEN = process.env.VAINSOCIAL_APITOKEN;
if (APITOKEN == undefined) throw "Need a valid API token!";
http.listen(8080);
/* API helper */
/* searches for player name across all shards */
async function findPlayer(name) {
var regions = ["na", "eu", "sg"],
finds = [];
for (let region of regions) {
var options = {
uri: "https://api.dc01.gamelockerapp.com/shards/" + region + "/players",
headers: {
"X-Title-Id": "semc-vainglory",
"Authorization": APITOKEN
},
qs: {
"filter[playerNames]": name
},
json: true,
gzip: true
};
try {
res = await request(options);
finds.push({
"region": res.data[0].attributes.shardId,
"id": res.data[0].id,
"last_update": res.data[0].attributes.createdAt
});
} catch (err) {
// TODO
}
}
if (finds.length == 0)
return undefined;
// due to an API bug, many players are also present in NA
// TODO: get history for all regions in case of region transfer
finds.sort((a, b) => { return a.last_update < b.last_update; });
return finds[0];
}
/* routes */
app.get("/api/player/:name", async (req, res) => {
var name = req.params.name;
var raw = await pool_raw.connect(),
web = await pool_web.connect();
/* search for the player in our db */
var player = await web.query(`
SELECT api_id, shard_id, last_match_created_date
FROM player WHERE name=$1
`, [name]);
var player_id, player_region, grab_start;
/* found */
if (player.rows.length > 0) {
console.log("player '" + name + "' was found in db");
player_id = player.rows[0].api_id;
player_region = player.rows[0].shard_id;
grab_start = player.rows[0].last_match_created_date;
// TODO db does not save time zone offset @stormcaller remove this
if (grab_start != undefined)
grab_start.setMinutes(grab_start.getMinutes() -
(new Date().getTimezoneOffset()));
}
/* not found */
if (player.rows.length == 0) {
console.log("player '" + name + "' not found in db");
/* search in all regions */
player = await findPlayer(name);
if (player == undefined) {
console.log("player '" + name + "' not found in API");
/* give up */
res.sendStatus(404);
return;
}
player_id = player.id;
player_region = player.region;
}
if (grab_start == undefined) {
grab_start = new Date("2017-01-01T00:00:00Z");
}
var timedelta_minutes = ((new Date()) - grab_start) / 1000 / 60;
// TODO make it configurable
if (timedelta_minutes < 30) {
console.log("player '" + name + "' update skipped");
res.sendStatus(304);
return;
}
/* request update job */
var jobid = -1;
// createdAt-start <= x <= createdAt-end
grab_start.setSeconds(grab_start.getSeconds() + 1);
payload = {
"region": player_region,
"params": {
"filter[playerIds]": player_id,
"filter[playerNames]": name, // TODO remove in 2.0 - backwards compat
"filter[createdAt-start]": grab_start.toISOString(),
"filter[gameMode]": "casual,ranked"
}
};
var job = await raw.query(`
UPDATE jobs SET priority=0
WHERE payload=$1 AND status<>'finished'
RETURNING id
`, [payload]);
if (job.rows.length == 0) {
job = await raw.query(`
INSERT INTO jobs(type, payload, priority)
VALUES('grab', $1, 0)
RETURNING id
`, [payload]);
// wake apigrabber up
await raw.query(`NOTIFY grab_open`, []);
console.log("player '" + name + "' new job requested");
}
console.log("player '" + name + "' updating after " + grab_start.toISOString());
jobid = job.rows[0].id;
/* clean up */
raw.release();
web.release();
res.json({
"job_id": jobid,
"player_id": player_id,
"player_region": player_region
});
});
/* internal monitoring */
app.get("/", async (req, res) => {
res.sendFile(__dirname + "/index.html");
});
/* notifications from database */
function listen() {
var client = new pg.Client(db_config_raw);
client.connect();
client.on('notification', (msg) => {
io.emit("job update", msg.channel);
});
client.query("LISTEN grab_open");
client.query("LISTEN process_open");
client.query("LISTEN compile_open");
client.query("LISTEN analyze_open");
client.query("LISTEN grab_running");
client.query("LISTEN process_running");
client.query("LISTEN compile_running");
client.query("LISTEN analyze_running");
client.query("LISTEN grab_finished");
client.query("LISTEN process_finished");
client.query("LISTEN compile_finished");
client.query("LISTEN analyze_finished");
client.query("LISTEN grab_failed");
client.query("LISTEN process_failed");
client.query("LISTEN compile_failed");
client.query("LISTEN analyze_failed");
// keep open forever
}
listen();
|