-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcs.js
executable file
·383 lines (324 loc) · 10.3 KB
/
cs.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
var fs = require('fs');
var math = require('mathjs');
var request = require('request');
var jsdom = require('jsdom').jsdom;
var document = jsdom('<html></html>', {});
var window = document.defaultView;
var $ = require('jquery')(window);
math.config({
number: 'BigNumber',
precision: 20
});
var API = "";
var logInput = false;
exports.setup = setup;
exports.processSteamIds = processSteamIds;
function setup(options) {
API = options.API;
logInput = options.logInput;
}
function processSteamIds(steamContents, callback) {
if (!isValidInput(steamContents)) {
callback([],[]);
return;
}
var steamID, friends, playerInfo, friendGroups;
steamIDs = steamContents.trim().replace(/\\(.)/mg, "$1");
if (logInput) {
fs.appendFile('./csgo-player-stats-inputs.txt', steamIDs, function (err) {
if (err)
console.log("Error when writing to file - " + err);
});
}
getInformationFromAPI(steamIDs, function(players, banData) {
processData(players, banData, callback);
});
}
function isValidInput(input) {
if (API === "") {
console.log("Error: API key is not set up");
return false;
}
if (input.length < 20)
return false;
return true;
}
function processData(players, banData, callback) {
var playerStats = getPlayerStats(players);
var banStats = getBanStats(banData, players);
callback(playerStats, banStats);
}
function getPlayerStats(players) {
var playerStats = [];
for (var i = 0; i < players.length; i++) {
var player = players[i];
var pFriends = getFriends(player, players);
var premadeSize = pFriends.length+1;
var stats = getStats(player.getGameResults());
var playerData = {
nick : player.getNick(),
timePlayed : stats[0],
kdRatio : stats[1],
accuracy : stats[2],
friendsWith : genFriendOutput(pFriends),
premadeSize : premadeSize
};
playerStats.push(playerData);
}
playerStats.sort(sortFunction);
return playerStats;
}
function getFriends(player, players) {
var friendList = player.getFriendResults();
var nick = player.getNick();
var result = [];
if (friendList === undefined)
return result;
for (var i = 0; i < players.length; i++) {
var otherPlayer = players[i];
if ((friendList.indexOf((otherPlayer.getCommunityid())) !== -1) && otherPlayer.getNick() !== nick) {
result.push(otherPlayer.getNick());
}
}
return result;
}
function getStats(playerStats) {
results=[];
if (playerStats.length < 500) {
results.push("<font color =\"#FF9900\"><b>Private</b></font>");
results.push("<font color =\"#FF9900\"><b>Private</b></font>");
results.push("<font color =\"#FF9900\"><b>Private</b></font>");
return results;
}
var obj = $.parseJSON(playerStats);
var statList = obj.playerstats.stats;
var gameTimeM, totKills, totDeaths, shotsAK, hitsAK, shotsM4, hitsM4;
for (var i = 0; i < statList.length; i++) {
var stat = statList[i];
switch(stat.name) {
case "total_time_played":
gameTimeM = stat.value;
break;
case "total_kills":
totKills = stat.value;
break;
case "total_deaths":
totDeaths = stat.value;
break;
case "total_shots_ak47":
shotsAK = stat.value;
break;
case "total_hits_ak47":
hitsAK = stat.value;
break;
case "total_shots_m4a1":
shotsM4 = stat.value;
break;
case "total_hits_m4a1":
hitsM4 = stat.value;
break;
}
}
gameTime = Math.floor(gameTimeM/(60*60));
KDRatio = Math.round((totKills/totDeaths)*100)/100;
AccAk = Math.round((hitsAK/shotsAK)*100)/100;
AccM4 = Math.round((hitsM4/shotsM4)*100)/100;
Acc = Math.round(((AccAk+AccM4)/2)*100);
results.push(colorTime(gameTime));
results.push(colorKD(KDRatio));
results.push(colorAcc(Acc));
return results;
}
function genFriendOutput(pF) {
var output = "";
for (var i = 0; i < pF.length; i++) {
output += pF[i]+", ";
}
return output.slice(0, -2);
}
function getBanStats(banData, players) {
var playersBanStats = [];
var banListPlayers = $.parseJSON(banData).players;
for (var i = 0; i < banListPlayers.length; i++) {
var player = banListPlayers[i];
var playerBanInfo = {
nick : communityIdToNick(players, player.SteamId),
isCommunityBanned : "",
isVACBanned : "",
numberOfVACBans : player.NumberOfVACBans,
daysSinceLastBan : player.DaysSinceLastBan,
numberOfGameBans : player.NumberOfGameBans
};
if (player.CommunityBanned === false)
playerBanInfo.isCommunityBanned = "<FONT COLOR=\"GREEN\"><b>No</b></FONT>";
else
playerBanInfo.isCommunityBanned = "<FONT COLOR=\"RED\"><b>Yes</b></FONT>";
if (player.VACBanned === false)
playerBanInfo.isVACBanned = "<FONT COLOR=\"GREEN\"><b>No</b></FONT>";
else
playerBanInfo.isVACBanned = "<FONT COLOR=\"RED\"><b>Yes</b></FONT>";
playersBanStats.push(playerBanInfo);
}
playersBanStats.sort(sortFunction);
return playersBanStats;
}
function sortFunction(a, b) {
var aComp = a.nick.toLowerCase();
var bComp = b.nick.toLowerCase();
if (aComp === bComp)
return 0;
return (aComp < bComp) ? -1 : 1;
}
function communityIdToNick(players, communityId) {
for (var i = 0; i < players.length; i++) {
var player = players[i];
if ((player.getCommunityid()) === communityId) {
return player.getNick();
}
}
return "Unknown";
}
function colorTime(time) {
if (time < 100) {
if (time < 50) {
return "<font color=\"red\"><b>"+time+"</b></font>";
} else {
return "<font color=\"brown\"><b>"+time+"</b></font>";
}
}
return time;
}
function colorKD(KD) {
if (KD > 1.5) {
if (KD > 2) {
return "<font color=\"red\"><b>"+KD+"</b></font>";
} else {
return "<font color=\"brown\"><b>"+KD+"</b></font>";
}
}
return KD;
}
function colorAcc(acc) {
if (acc > 20) {
if (acc > 30) {
return "<font color=\"red\"><b>"+acc+"%</b></font>";
} else {
return "<font color=\"brown\"><b>"+acc+"%</b></font>";
}
}
return acc+"%";
}
function getInformationFromAPI(steamIds, callback) {
var allPlayersInfo = steamIds.split("\n");
var URLResults = collectURLs(allPlayersInfo);
var players = URLResults.players;
var nodesFriends = URLResults.nodesFriends;
var nodesGameStats = URLResults.nodesGameStats;
// Request all the URLs
var URLRequests = nodesFriends.concat(nodesGameStats);
__request(URLRequests, function(responses) {
var i, response;
for (i = 0; i < nodesFriends.length-1; i++) {
response = responses[nodesFriends[i]].body;
players[i].setFriendResults(response);
}
for (i = 0; i < nodesGameStats.length; i++) {
response = responses[nodesGameStats[i]].body;
players[i].setGameResults(response);
}
var banResults = responses[nodesFriends[nodesFriends.length-1]].body;
callback(players, banResults);
});
}
function collectURLs(allPlayersInfo) {
var players = [], nodesFriends = [], nodesGameStats = [], nodesBanStats = [], banIDs = "";
for (var i = 0; i < allPlayersInfo.length; i++) {
var playerInfo = allPlayersInfo[i].replace(" ", " ").replace(" ", " ");
if (isValidPlayerInfo(playerInfo)) {
var nickAndSteamid = getNickAndSteamid(playerInfo);
var player = new Player(nickAndSteamid.nick, nickAndSteamid.steamid, steamToCommunityId(nickAndSteamid.steamid));
players.push(player);
nodesFriends.push('http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key='+API+'&steamid='+player.getCommunityid()+'&relationship=friend');
nodesGameStats.push('http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key='+API+'&steamid='+player.getCommunityid());
banIDs += (","+player.getCommunityid());
}
}
nodesFriends.push('http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key='+API+'&steamids='+banIDs);
return {
players: players,
nodesFriends : nodesFriends,
nodesGameStats : nodesGameStats
};
}
function isValidPlayerInfo(pInfo) {
return pInfo.charAt(0) === '#' && pInfo.charAt(1) === " " &&
(isNum(pInfo.charAt(2)) || isNum(pInfo.charAt(3)));
}
function getNickAndSteamid(info) {
var nickAndSteam = info.split(/"(.*)" (STEAM.*?) /);
var player = {
nick : nickAndSteam[1],
steamid : nickAndSteam[2]
};
return player;
}
// Convert a 32-bit steamID to a 64-bit steamID, using the mathjs library
function steamToCommunityId(steamId) {
var parts = steamId.split(":");
var result = math.eval("("+parts[2]+"*2)+76561197960265728+"+parts[1]);
return math.string(result);
}
// "Class" representing a player
function Player (nick, steamId, communityid) {
this.nick = nick;
this.steamId = steamId;
this.communityid = communityid;
this.friendResults = "";
this.gameResults = [];
this.getNick = function() {
return this.nick;
};
this.getSteamid = function() {
return this.steamId;
};
this.getCommunityid = function() {
return this.communityid;
};
this.setFriendResults = function(res) {
this.friendResults = res;
};
this.setGameResults = function(res) {
this.gameResults = res;
};
this.getFriendResults = function() {
return this.friendResults;
};
this.getGameResults = function() {
return this.gameResults;
};
}
function isNum(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
/**
* Handle multiple requests at once
* @param urls [array]
* @param callback [function]
* @requires request module for node ( https://github.com/mikeal/request )
*
* Modified from: https://gist.github.com/natos/2001487
*/
var __request = function (urls, callback) {
'use strict';
var results = {}, c = 0;
var handler = function (error, response, body) {
var url = response.request.uri.href;
results[url] = { error: error, response: response, body: body };
if (++c === urls.length) {
callback(results);
}
};
for (var i = 0; i < urls.length; i++) {
request(urls[i], handler);
}
};