-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathowapi.js
251 lines (217 loc) · 7.36 KB
/
owapi.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
// OWAPI interface object
// usage:
// 1. set id to player BattleTag
// 2. set onSuccess and onFail callback functions
// 3. call getStats() method
// 4. object field filled with stats. Read them in onSuccess callback or process error in onFail
var OWAPI = {
id: "", // player battletag
display_name: "", // first part of battletag before '#'
level: 0,
time_played: 0,
sr_by_class: {},
playtime_by_class: {},
top_heroes: [],
private_profile: false,
onSuccess: undefined, // Set to actual callback function before use
onFail: undefined, // Set to actual callback function before use
can_retry: false, // will be set to true on "soft" errors (like timeouts)
// settings
owapi_timeout: 15000, // 15 sec timeout for OWAPI requests
top_hero_max_ratio: 4.0, // ratio of hero playtime to detect top heroes
region: "eu", // eu, us, kr
// internal
is_processed: false, // to prevent multiple callback on single request
// public methods
getStats: function() {
// reset
this.sr_by_class = {};
this.level = 0;
this.time_played = 0;
this.sr_by_class = {};
this.playtime_by_class = {};
this.top_heroes = [];
this.private_profile = false;
this.id = format_player_id( this.id );
this.display_name = format_player_name( this.id );
this.is_processed = false;
var xhttp = new XMLHttpRequest();
xhttp.onload = function() {
if (this.readyState == 4 ) {
if ( this.status == 200) {
try {
var stats_obj = JSON.parse(this.responseText);
if ( stats_obj[OWAPI.region] === null ) {
OWAPI.can_retry = false;
throw new Error("Player has no stats in region "+OWAPI.region.toUpperCase());
}
if ( stats_obj[OWAPI.region].stats.competitive !== null ) {
if ( ! stats_obj[OWAPI.region].stats.competitive.hasOwnProperty('overall_stats') ) {
OWAPI.can_retry = false;
throw new Error("Player has no stats in region "+OWAPI.region.toUpperCase());
}
var sr_value = stats_obj[OWAPI.region].stats.competitive.overall_stats.tank_comprank;
if ( (sr_value !== undefined) && (sr_value !== null) ) {
OWAPI.sr_by_class["tank"] = Number(sr_value);
}
sr_value = stats_obj[OWAPI.region].stats.competitive.overall_stats.damage_comprank;
if ( (sr_value !== undefined) && (sr_value !== null) ) {
OWAPI.sr_by_class["dps"] = Number(sr_value);
}
sr_value = stats_obj[OWAPI.region].stats.competitive.overall_stats.support_comprank;
if ( (sr_value !== undefined) && (sr_value !== null) ) {
OWAPI.sr_by_class["support"] = Number(sr_value);
}
OWAPI.level = stats_obj[OWAPI.region].stats.competitive.overall_stats.prestige*100 + stats_obj[OWAPI.region].stats.competitive.overall_stats.level,
OWAPI.time_played = Number(stats_obj[OWAPI.region].stats.competitive.game_stats.time_played);
var hero_stats = OWAPI.parseHeroStats( stats_obj[OWAPI.region].heroes );
OWAPI.playtime_by_class = OWAPI.calculatePlaytimeByClass( hero_stats );
OWAPI.top_heroes = OWAPI.calculateTopHeroes( hero_stats );
}
if (typeof OWAPI.onSuccess == "function") {
if ( ! OWAPI.is_processed ) {
OWAPI.onSuccess.call();
}
OWAPI.is_processed = true;
}
}
catch (err) {
if(typeof OWAPI.onFail == "function") {
if ( ! OWAPI.is_processed ) {
OWAPI.onFail.call( OWAPI, err.message );
}
OWAPI.is_processed = true;
}
}
} else {
var msg = "";
switch (this.status) {
case 404: msg = "Player not found (incorrect BattleTag)";
OWAPI.can_retry = false;
break;
case 429: msg = "Too many stats requests, try later";
OWAPI.can_retry = true;
break;
case 403: msg = "Player has private profile";
OWAPI.can_retry = false;
OWAPI.private_profile = true;
break;
default: msg = "Can't get player stats (HTTP "+this.status+": "+this.statusText+")";
OWAPI.can_retry = true;
}
if(typeof OWAPI.onFail == "function") {
if ( ! OWAPI.is_processed ) {
OWAPI.onFail.call( OWAPI, msg );
}
OWAPI.is_processed = true;
}
}
}
};
xhttp.ontimeout = function() {
var msg = "OWAPI timeout";
OWAPI.can_retry = true;
if(typeof OWAPI.onFail == "function") {
if ( ! OWAPI.is_processed ) {
OWAPI.onFail.call( OWAPI, msg );
}
OWAPI.is_processed = true;
}
};
xhttp.onerror = function() {
var msg = "OWAPI error - "+this.statusText;
if(typeof OWAPI.onFail == "function") {
if ( ! OWAPI.is_processed ) {
OWAPI.onFail.call( OWAPI, msg );
}
OWAPI.is_processed = true;
}
};
xhttp.open("GET", "https://owapi.net/api/v3/u/"+this.id+"/blob", true);
xhttp.timeout = OWAPI.owapi_timeout;
xhttp.send();
},
// private methods
// returns array of objects (hero, playtime) sorted by playtime
parseHeroStats: function( heroes_node ) {
var hero_playtime = heroes_node.playtime.competitive;
var hero_playtime_sorted = [];
for (var hero_name in hero_playtime ) {
var current_hero_playtime = Math.round(hero_playtime[hero_name] * 100) / 100;
hero_playtime_sorted.push( { hero: hero_name, playtime: current_hero_playtime} );
}
hero_playtime_sorted.sort( function(hero1, hero2) {
return hero2.playtime - hero1.playtime;
});
return hero_playtime_sorted;
},
// returns top 2 hero classes
calculateTopClasses: function calculate_top_classes( hero_playtime ) {
var class_playtime = {
dps: 0,
tank: 0,
support: 0
};
var total_hero_playtime = 0;
for ( i=0; i<hero_playtime.length; i++ ) {
var hero_class = hero_classes[hero_playtime[i].hero];
if ( hero_class === undefined ) {
throw new Error("Unknown hero: "+hero_playtime[i].hero);
}
class_playtime[hero_class] += hero_playtime[i].playtime;
total_hero_playtime += hero_playtime[i].playtime;
}
if ( total_hero_playtime == 0 ) {
return [];
}
var class_playtime_arr = Object.entries(class_playtime);
class_playtime_arr.sort( function(item1, item2){
return item2[1]-item1[1];
});
var top_classes = [];
var top_class = class_playtime_arr.shift();
if( top_class !== undefined ) {
top_classes.push( top_class[0] );
}
top_class = class_playtime_arr.shift();
if( top_class !== undefined ) {
if( Number(top_class[1])/this.time_played >= OWAPI.top_class_min_fraction ) {
top_classes.push( top_class[0] );
}
}
return top_classes;
},
// returns top 4 heroes by playtime
calculateTopHeroes: function ( hero_playtime ) {
var top_heroes = [];
if ( hero_playtime.length != 0 ) {
top_heroes.push( hero_playtime[0] );
}
for ( i=1; i<hero_playtime.length; i++ ) {
if ( hero_playtime[i-1].playtime / hero_playtime[i].playtime < OWAPI.top_hero_max_ratio ) {
top_heroes.push( hero_playtime[i] );
} else {
break;
}
}
while (top_heroes.length > 4) {
top_heroes.pop();
}
return top_heroes;
},
calculatePlaytimeByClass: function ( hero_playtime ) {
var class_playtime = {
dps: 0,
tank: 0,
support: 0
};
for ( i=0; i<hero_playtime.length; i++ ) {
var hero_class = hero_classes[hero_playtime[i].hero];
if ( hero_class === undefined ) {
throw new Error("Unknown hero: "+hero_playtime[i].hero);
}
class_playtime[hero_class] += hero_playtime[i].playtime;
}
return class_playtime;
}
}