-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
267 lines (213 loc) · 8.75 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
//For google analytics:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-100029869-1', 'auto');
ga('send', 'pageview');
function clearOutput() {
var parent = document.getElementById("outputDiv");
parent.innerHTML = "";
}
//This method is used to grey out the place filter if the feature is disabled
function setEnabled(id, enabled) {
document.getElementById(id).disabled = !enabled;
}
function appendNewLine(node){
//In wiki markup, you need this extra whitespace to force a new line
var wikiNewLine = document.createTextNode("\u00A0\u00A0");
node.appendChild(wikiNewLine);
//Still need the line break to force the full new lines
var linebreak = document.createElement('br');
node.appendChild(linebreak);
}
function parseTime(time) {
var split = time.toString().split(".");
var totalSeconds = split[0];
var seconds = totalSeconds % 60;
var minutes = Math.floor(totalSeconds/60) % 60;
var hours = Math.floor(totalSeconds/3600) % 24;
var days = Math.floor(totalSeconds / 86400);
var result = "";
if(days != 0) {
result += days + " days, ";
}
if(hours != 0) {
if(minutes < 10) minutes = "0" + minutes;
if(seconds < 10) seconds = "0" + seconds;
result += hours + ":" + minutes + ":" + seconds;;
}
else if(minutes != 0) {
if(seconds < 10) seconds = "0" + seconds;
result += minutes + ":" + seconds;
}
else {
result += seconds;
}
if(split.length == 2) {
result = result + "." + split[1];
}
return result;
}
function getPlaceAsHumanReadable(number) {
if(number == 1) return "WR";
var lastDigit = number % 10;
var secondToLastDigit = Math.round((number % 100)/10);
//For the teens, always end with 'th'
if(secondToLastDigit != 1) {
if(lastDigit == 1) {
return number + "st";
}
if(lastDigit == 2) {
return number + "nd";
}
if(lastDigit == 3) {
return number + "rd";
}
}
return number + "th";
}
function getLink(text, url) {
return "[" + text + "](" + url + ")";
}
//This is used to track async calls so that, once they're all done, we can sort and display
var pendingRequests = 0;
var allRuns = [];
function allRunsFetched() {
//Sort all alphabetically
allRuns.sort(function(a, b){
//Should probably use an internal name for this instead of removing the '**'
return a.textContent.replace("**","").localeCompare(b.textContent.replace("**", ""));
});
var parent = document.getElementById("outputDiv");
//Clear the 'fetching data' status then write the output
clearOutput();
for (var i = 0; i < allRuns.length; i++){
parent.appendChild(allRuns[i]);
appendNewLine(parent);
}
var giveCredit = document.getElementById('showVia').checked
if(giveCredit) {
appendNewLine(parent);
var creditNode = document.createTextNode("generated from speedrun.com data using " + getLink("Mitch3a's script", "https://mitch3b.github.io/SpeedRunSummary/"));
parent.appendChild(creditNode);
}
//Might have a better place to do this, but need to clear out allRuns in case we want to refetch
allRuns = [];
}
function toWikiMarkup(speedrun, elementToWriteTo) {
var AJAX = [];
pendingRequests++;
var showWR = document.getElementById('showWR').checked;
AJAX.push($.getJSON("https://www.speedrun.com/api/v1/games/" + speedrun.run.game + "?callback=?"));
AJAX.push($.getJSON("https://www.speedrun.com/api/v1/categories/" + speedrun.run.category + "?callback=?"));
if(showWR && speedrun.place != 1) {
AJAX.push($.getJSON("https://www.speedrun.com/api/v1/leaderboards/" + speedrun.run.game + "/category/" + speedrun.run.category + "?callback=?"));
}
$.when.apply($, AJAX).done(function(){
//Pull these based on the order we called them in.
//The second index refers to the following structure: [data, statusText, jqXHR]
var gameInfo = arguments[0][0];
var categoryInfo = arguments[1][0];
var gameName = gameInfo.data.names.international;
var category = categoryInfo.data.name;
var place = getPlaceAsHumanReadable(speedrun.place);
var time = parseTime(speedrun.run.times.primary_t);;
var runLink = speedrun.run.weblink;
var categoryLink = categoryInfo.data.weblink;
//Bold lines that are for the world record
var bold = (speedrun.place == 1) ? "**" : "";
elementToWriteTo.nodeValue = bold + getLink(gameName, categoryLink) + " - " + category + ": " + getLink(time, runLink) + " (" + place + ")" + bold;
if(showWR && speedrun.place != 1) {
var leaderboard = arguments[2][0];
//TODO this assumes runs come in order. might want to iterate until finding place = 1.
var wrRun = leaderboard.data.runs[0].run;
var wrTime = parseTime(wrRun.times.primary_t);
var wrLink = wrRun.weblink;
elementToWriteTo.nodeValue += " - wr " + getLink(wrTime, wrLink) + " by ";
//For "Guests", there is a name field, else we use the id to look it up
if(wrRun.players[0].name) {
elementToWriteTo.nodeValue += wrRun.players[0].name;
}
else {
var userUrl = "https://www.speedrun.com/api/v1/users/" + wrRun.players[0].id + "?callback=?";
$.getJSON(userUrl, function(result){
var wrHolder = result.data.names.international;
elementToWriteTo.nodeValue += wrHolder;
});
}
}
pendingRequests--;
if(pendingRequests == 0) {
allRunsFetched();
}
}).fail(function (jqXHR, textStatus, errorThrown) {
alert("Error pulling data for game id: " + speedrun.run.game + ". Please contact mitch3a or report the bug on github here: https://github.com/mitch3b/SpeedRunSummary/issues");
pendingRequests--;
if(pendingRequests ==0) {
allRunsFetched();
}
})
}
$(document).ready(function(){
$("button").click(function(){
var filterOnPlace = document.getElementById('filterByPlace').checked;
var maxPlace = parseInt(document.getElementById('maxPlaceToDisplay').value) || 2147483647; //If empty default to largest int
var parent = document.getElementById("outputDiv");
//Clear the previous results
parent.innerHTML = "Fetching data";
var username = document.getElementById('usernameTextBox').value;
var url = "https://www.speedrun.com/api/v1/users/" + username + "/personal-bests?callback=?";
if(filterOnPlace) {
url += "&top=" + maxPlace;
}
pendingRequests++;
$.getJSON(url, function(result){
parent.innerHTML = "Fetching speedrun details";
//This is just the top level which is just "data"
$.each(result, function(key, datalist){
//This is to iterate the data list
$.each(datalist, function(i, listItem){
var element = document.createTextNode("");
allRuns[i] = element;
toWikiMarkup(listItem, element);
});
});
pendingRequests--;
if(pendingRequests == 0) {
allRunsFetched();
}
}).fail(function (jqXHR, textStatus, errorThrown) {
parent.innerHTML = "Could not retrieve info for user: " + username;
})
});
});
</script>
</head>
<body>
This is an example of fetching best times from speedrun.com for display on a wiki (ie twitch panels). Eventually will add other features (configurable output style?). The eventual goal would be to have uploads to speedrun.com update your twitch page automagically but I'm not sure that's possible given current twitch api.
</br></br>
It will output in the form: </br>
<img src="SpeedrunScreenshot.png"/>
</br></br>
where the time will link to the video and the game will link to the speedrun.com page.
</br></br>
Filter out places above value:
<input type="checkbox" id="filterByPlace" onchange="setEnabled('maxPlaceToDisplay', this.checked)"/>
<input type="text" id="maxPlaceToDisplay" disabled onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
</br>
append world record holder: <input type="checkbox" id="showWR">
<br>
Give this script credit. Put link to it at the end: <input type="checkbox" id="showVia" checked/>
</br>
username: <input name="usernameTextBox" id="usernameTextBox" type="text" />
</br>
<button>Generate wiki markup </button>
<div id="outputDiv"></div>
</body>
</html>