-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayWinRate.html
278 lines (218 loc) · 8.32 KB
/
displayWinRate.html
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
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
var personaId = "";
//"63ce2034-8a40-4c9e-b83d-2735c7206bd0"; // Chronopolize
var matchesToGet = 400; // should be multiple of 100
$(document).ready(function() {
// UrL Param code from https://www.sitepoint.com/url-parameters-jquery/
$.urlParam = function(name) {
var results = new RegExp('[\?&]' + name + '=([^]*)').exec(window.location.href);
if (results == null) {
return null;
} else {
return results[1] || 0;
}
}
if ($.urlParam("persona") != null) { //if there is a persona passed in, use that one
personaId = $.urlParam("persona");
requestMatchData();
} else {
alert("No personaId given.\n" + "Example usage: " + "displayWinRate.html?persona=1b767f0b-7a7f-4470-834a-410d23d8dd88")
}
});
function requestMatchData() {
var url = window.location.href;
var params = url.split('?');
var numberOfBatches = matchesToGet / 100;
//load requests
var requests = []
for (var i = 0; i < numberOfBatches; i++) {
offset = i * 100;
var reqURL = "https://ashesescalation-api-tachyon.stardock.net/v1/products/2641/matches?personaid=" + personaId + "&offset=" + offset + "&count=100"
requests.push($.ajax({
url: reqURL,
delay: 1000 * i,
type: "GET",
success: function(data) {
processData(data);
},
dataType: "json",
error: function() {
console.log("Request to Ashes API failed")
}
}));
}
$.when.apply(null, requests).done(function() {
console.log("All done!");
computeStats();
// printResults();
displayResults();
});
}
var playerName;
var results = {};
var rankedGameCount = 0;
var rankedWins = 0;
var rankedWinPercent;
function processData(data) {
console.log("Recieved a response");
// Process the match data
for (var i = 0; i < data.length; i++) {
var match = data[i];
//Determine the result of the match.
if (match.dataString.type == "1v1Ranked") {
var player, opponent;
if (match.participants[0].personaId == personaId) {
player = match.participants[0];
opponent = match.participants[1];
} else {
player = match.participants[1];
opponent = match.participants[0];
}
var isVictory = player.dataInteger.result == 1 ? true : false;
var opponentName = opponent.name;
playerName = player.name; //lazy way of getting the player's name.
// console.log("Game result: " + isVictory + " vs" + opponentName);
// Update results:
if (results[opponentName] == null) {
results[opponentName] = {
wins: 0,
losses: 0,
numGames: 0
}
}
if (isVictory) {
rankedWins += 1;
results[opponentName].wins += 1;
} else {
results[opponentName].losses += 1;
}
rankedGameCount += 1;
results[opponentName].numGames += 1;
}
}
}
function computeStats() {
// Calculate relevant stats:
for (var opponent in results) {
if (results.hasOwnProperty(opponent)) {
var record = results[opponent];
record.winPercent = Math.round(1.0 * record.wins / record.numGames * 100);
}
}
rankedWinPercent = Math.round(1.0 * rankedWins / rankedGameCount * 100)
}
function printResults() {
console.log("---");
console.log("Player name: " + playerName);
console.log("From last " + rankedGameCount + " ranked games, Win rate: " + rankedWinPercent + "%")
for (var opponent in results) {
if (results.hasOwnProperty(opponent)) {
var record = results[opponent];
console.log("vs " + opponent + ": " + record.wins + "W " + record.losses + "L " + record.winPercent + "%")
}
}
}
function displayResults() {
$(".loadingMessage").hide();
var html = []
html.push("<div class ='infoText'>" + "Player: " + playerName +"</div>");
html.push("<div class ='infoText'>" + "From last " + rankedGameCount + " ranked games, Win rate: " + rankedWinPercent + "%" + "</div>");
html.push("<table class = 'matchupTable'>");
for (var opponent in results) {
if (results.hasOwnProperty(opponent)) {
var record = results[opponent];
// Do a Baeysian average for performance rating
var bConstant = 2;
var bWinPercent = Math.round(1.0 * (record.wins + bConstant) / (record.numGames + 2 * bConstant) * 100);
var val = bWinPercent;
var red = new Color(190, 20, 20),
mid = new Color(160, 140, 0),
green = new Color(0, 160, 0);
var start, end;
if (val >= 50) {
start = mid,
end = green;
val = Math.min(val, 75);
val = val - 50;
} else {
start = red;
end = mid;
val = Math.max(val, 25);
val = val - 25;
}
var startColors = start.getColors(),
endColors = end.getColors();
var r = interpolate(startColors.r, endColors.r, 25, val);
var g = interpolate(startColors.g, endColors.g, 25, val);
var b = interpolate(startColors.b, endColors.b, 25, val);
var colorSpanTag = "<span style='color:" + "rgb(" + r + "," + g + "," + b + ")'>";
html.push(
"<tr>" +
"<td>" + wrap("vs " + opponent, colorSpanTag) + ":" + "</td>" +
"<td>" + wrap(record.wins + "-" + record.losses, colorSpanTag) + "</td>" +
"<td>" + wrap(record.winPercent + "%", colorSpanTag) + "</td> " +
"</tr>");
}
}
html.push("</table");
// console.log(html.join(""));
$("#body").append(html.join(""));
}
//Below color code taken from: https://stackoverflow.com/questions/11849308/generate-colors-between-red-and-green-for-an-input-range
function interpolate(start, end, steps, count) {
var s = start,
e = end,
final = s + (((e - s) / steps) * count);
return Math.floor(final);
}
function Color(_r, _g, _b) {
var r, g, b;
var setColors = function(_r, _g, _b) {
r = _r;
g = _g;
b = _b;
};
setColors(_r, _g, _b);
this.getColors = function() {
var colors = {
r: r,
g: g,
b: b
};
return colors;
};
}
//End of Color code
//Helper functions
function wrap(target, str) {
var x = str;
if (str.length >= 2 && str.charAt(0) == "<") { //trim angle brackets if input seems to be a tag
x = str.substring(1, str.length - 1);
}
return "<" + x + ">" + target + "</" + x + ">";
}
</script>
<style>
.matchupTable {
border-collapse: collapse;
margin-top: 5px;
margin-left: 10px;
}
.infoText {
margin: 5px 0px 0px 10px;
}
tr,
td {
border: 1px solid rgb(160, 160, 255);
padding: 5px;
}
</style>
</head>
<body>
<div class='loadingMessage'>If results do not show after 30 seconds, try refreshing the page.</div>
<div id='body'></div>
</body>