-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
148 lines (143 loc) · 8.27 KB
/
script.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
function toS(milliseconds) {
let seconds = milliseconds / 1000;
return seconds.toFixed(3);
}
function getId(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
var match = url.match(regExp);
return (match&&match[7].length==11)? match[7] : false;
}
document.addEventListener("keydown", function(event) {
const key = event.key;
if (key === "Escape") {
$('.videoContainer').fadeOut();
document.getElementById('video').removeAttribute('src');
}
});
function watchVideo(videoId) {
$('#video').attr('src', 'https://www.youtube.com/embed/' + videoId + '?autoplay=1&origin=https://sd0e.github.io/Hyparkour&modestbranding=1&rel=0&loop=1');
$('.videoContainer').fadeIn();
}
function difference(record, best) {
let difference = best - record;
return difference.toFixed(3);
}
async function submit(mcUsername) {
const k = $('#apiKeyInput').val();
if (k != '') {
console.log("Hyparkour - See Hypixel parkour records");
$('.playerImage').attr('src', 'loading.gif');
$('.notFound').hide();
$('.unknown').hide();
$('.problem').hide();
$('.tooFrequent').hide();
$('.playerName').text('Loading...');
$('.lobbies').html('');
const headURL = `https://mc-heads.net/minecraft/profile/` + mcUsername;
const headfetchResult = fetch(headURL)
const headresponse = await headfetchResult;
const headData = await headresponse.json();
let uuid = headData.id;
let head = `https://mc-heads.net/head/` + uuid + `.png`;
$('.playerImage').attr('src', head);
const hypixelURL = `https://api.hypixel.net/player?key=` + k + `&name=` + mcUsername;
const hypixelfetchResult = fetch(hypixelURL)
const hypixelresponse = await hypixelfetchResult;
const hypixelData = await hypixelresponse.json();
if (hypixelData.player != null) {
let parkour = hypixelData.player.parkourCompletions;
let parkourCheckpoints = hypixelData.player.parkourCheckpointBests;
$('.playerName').text(hypixelData.player.displayname);
if (parkour == undefined) {
$('.notFound').show();
} else {
const fetchResult = fetch('info.json')
const response = await fetchResult;
const jsonData = await response.json();
const speedrunsURL = 'https://www.speedrun.com/api/v1/games/v1pejmd8/records';
const speedrunsfetchResult = fetch(speedrunsURL)
const speedrunsresponse = await speedrunsfetchResult;
const speedrunsjsonData = await speedrunsresponse.json();
runs = speedrunsjsonData.data;
Object.keys(jsonData).forEach(function (key) {
$('body').css('background-image', 'none');
let name = key;
let otherData = jsonData[key];
let displayName = otherData.displayName;
let hypixelName = otherData.hypixelName;
let speedrunLevel = otherData.speedrunLevel;
Object.keys(runs).forEach(function(key2) {
let otherDataSpeedruns = runs[key2];
let runId = getId(otherDataSpeedruns.runs[0].run.videos.links[0].uri);
let runInfo = otherDataSpeedruns.runs[0].run;
if (otherDataSpeedruns.level == speedrunLevel) {
let recordTime = otherDataSpeedruns.runs[0].run.times.primary_t;
// Do general parkour info here
Object.keys(parkour).forEach(function(key8) {
let parkourData = parkour[key8];
if (key8 == hypixelName) {
var bestTime = 100000000000;
Object.keys(parkourData).forEach(function(key9) {
if (parkourData[key9].timeTook < bestTime) {
bestTime = parkourData[key9].timeTook;
}
});
var lengthOfParkours = Object.keys(parkourData).length;
var existingLength = 0;
Object.keys(parkourData).forEach(function(key10) {
existingLength = existingLength + parkourData[key10].timeTook;
});
let averageTime = existingLength/lengthOfParkours;
if ($('.' + name).length == false) {
$('.lobbies').append(`
<div class="lobby" style="background: url(backgrounds/` + name + `.png) no-repeat center center;">
<div class="lobbyInfoContainer">
<img src="icons/` + name + `.png" alt="Lobby Icon" class="lobbyIcon">
<span class="lobbyName">` + displayName + `</span>
</div><br>
<span class="statContainer"><span class="completed stat">` + Object.keys(parkourData).length + `</span> successfully completed run(s).</span><br>
<span class="statContainer">The run(s) took an average of <span class="averageTime stat">` + toS(averageTime) + `s</span> to complete.</span><br>
<span class="statContainer">Their fastest run of <span class="fastestRun stat">` + toS(bestTime) + `s</span> was <span class="runDifference stat">` + difference(recordTime, toS(bestTime)) + `s</span> slower than the record of <span class="recordRun stat">` + recordTime + `s</span>.</span><br><br>
<div class="checkpoints ` + name + `"></div>
<small class="attribution"><a class="link" onclick="watchVideo('` + runId + `');" target="_blank">Watch a video of the record</a> - <a class="link" href="https://www.speedrun.com/mcm_hsp/` + name + `" target="_blank">View this lobby's records on speedrun.com</a></small>
</div>
`);
}
}
});
Object.keys(parkourCheckpoints).forEach(function(key3) {
let parkourCheckpointsData = parkourCheckpoints[key3];
if (key3 == hypixelName) {
let length = Object.keys(parkourCheckpointsData).length;
for (i = 0; i < length; i++) {
let legibleNumber = i + 1;
let checkpointClass = "chkpnt_" + legibleNumber + "_" + name;
if ($('.' + checkpointClass).length == false) {
$('.checkpoints.' + name).append('<span class="statContainer ' + checkpointClass + '">Checkpoint ' + legibleNumber + ' Best: <span class="averageTime stat">' + toS(parkourCheckpointsData[i]) + 's</span></span><br>');
}
}
}
});
}
});
});
}
$(".records").fadeIn();
} else {
if (hypixelData.success == false) {
$('.playerName').text(mcUsername.charAt(0).toUpperCase() + mcUsername.slice(1));
if (hypixelData.cause === 'You have already looked up this name recently') {
$('.tooFrequent').show();
} else {
$('.problem').show();
}
$('.records').fadeIn();
} else {
let fallbackName = headData.name;
$('.playerName').text(fallbackName);
$('.unknown').show();
$('.records').fadeIn();
}
}
}
}