Fix for Blaseball API changes & use fetch instead of XMLHttpRequest
This commit is contained in:
parent
442641e568
commit
17231816fa
1 changed files with 41 additions and 41 deletions
58
main.js
58
main.js
|
@ -1,46 +1,39 @@
|
||||||
"use strict"
|
"use strict"
|
||||||
|
|
||||||
let request = new XMLHttpRequest()
|
let api_response = fetch(
|
||||||
request.open(
|
|
||||||
"GET",
|
|
||||||
"https://cors-proxy.blaseball-reference.com/database/allTeams",
|
"https://cors-proxy.blaseball-reference.com/database/allTeams",
|
||||||
true)
|
).then(function(response) {
|
||||||
request.send();
|
if (response.ok) {
|
||||||
|
response.json().then(function(teams) {
|
||||||
request.onload = function() {
|
let totals = teams.map(team =>
|
||||||
let teams = JSON.parse(this.response)
|
team.lineup.length + team.rotation.length
|
||||||
let totals = teams.map(team => team.lineup.length + team.rotation.length)
|
)
|
||||||
|
|
||||||
let results = document.getElementById("results")
|
let results = document.getElementById("results")
|
||||||
let results_shadowed = document.getElementById("results_shadowed")
|
let results_shadowed = document.getElementById("results_shadowed")
|
||||||
let shadows = document.getElementById("shadows")
|
|
||||||
|
|
||||||
display_teams(
|
display_teams(totals, teams, results)
|
||||||
totals,
|
|
||||||
teams,
|
|
||||||
results,
|
|
||||||
team => team.lineup.length,
|
|
||||||
team => team.rotation.length,
|
|
||||||
)
|
|
||||||
// Results are ready so we can show them now
|
// Results are ready so we can show them now
|
||||||
results.style.display = "block"
|
results.style.display = "block"
|
||||||
|
|
||||||
// Now get the shadowed results ready to display
|
// Now get the shadowed results ready to display
|
||||||
display_teams(
|
display_teams(
|
||||||
totals.map((total, pos) =>
|
totals.map((total, pos) => total + teams[pos].shadows.length),
|
||||||
total + teams[pos].bullpen.length + teams[pos].bench.length),
|
|
||||||
teams,
|
teams,
|
||||||
results_shadowed,
|
results_shadowed
|
||||||
team => team.lineup.length + team.bullpen.length,
|
|
||||||
team => team.rotation.length + team.bench.length,
|
|
||||||
)
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
let shadows = document.getElementById("shadows")
|
||||||
// Set the toggle function for & display the checkbox
|
// Set the toggle function for & display the checkbox
|
||||||
shadows.oninput = checkbox_toggle
|
shadows.oninput = checkbox_toggle
|
||||||
shadows.style.display = "block"
|
shadows.style.display = "block"
|
||||||
|
|
||||||
document.getElementById("progress").remove()
|
document.getElementById("progress").remove()
|
||||||
}
|
} else {
|
||||||
|
alert("Error fetching Blaseball API data: " + api_response.status)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
function checkbox_toggle() {
|
function checkbox_toggle() {
|
||||||
results_shadowed.style.display = "block"
|
results_shadowed.style.display = "block"
|
||||||
|
@ -53,26 +46,33 @@ function checkbox_toggle() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function display_teams(totals, teams, div_container, hitters_fn, pitchers_fn) {
|
function display_teams(totals, teams, div_container) {
|
||||||
const ENDING = x => (Math.abs(x) === 1 ? "" : "s")
|
const ENDING = x => (Math.abs(x) === 1 ? "" : "s")
|
||||||
// Make the headers & lists for teams
|
// Make the headers & lists for teams
|
||||||
totals.slice().sort((x, y) => x - y)
|
totals.slice().sort((x, y) => x - y)
|
||||||
.filter((total, pos, array) => !pos || total !== array[pos - 1])
|
.filter((total, pos, array) => !pos || total !== array[pos - 1])
|
||||||
.forEach(function(total) {
|
.forEach(function(total) {
|
||||||
div_container.insertAdjacentHTML("afterbegin",
|
div_container.insertAdjacentHTML(
|
||||||
|
"afterbegin",
|
||||||
`<h1>${total} player${ENDING(total)}</h1>
|
`<h1>${total} player${ENDING(total)}</h1>
|
||||||
<ul id="${div_container.id}_tlist${total}"></ul>`)
|
<ul id="${div_container.id}_tlist${total}"></ul>`
|
||||||
|
)
|
||||||
})
|
})
|
||||||
// Put the teams in the lists
|
// Put the teams in the lists
|
||||||
teams.forEach(function(team, pos) {
|
teams.forEach(function(team, pos) {
|
||||||
let hitters = hitters_fn(team)
|
let hitters = team.lineup.length
|
||||||
let pitchers = pitchers_fn(team)
|
let pitchers = team.rotation.length
|
||||||
|
let shadow_string = ""
|
||||||
|
if (div_container.id == "results_shadowed") {
|
||||||
|
let shadows = team.shadows.length
|
||||||
|
shadow_string = `, ${shadows} shadowed player${ENDING(shadows)}`
|
||||||
|
}
|
||||||
document.getElementById(div_container.id + "_tlist" + totals[pos])
|
document.getElementById(div_container.id + "_tlist" + totals[pos])
|
||||||
.insertAdjacentHTML(
|
.insertAdjacentHTML(
|
||||||
"beforeend",
|
"beforeend",
|
||||||
`<li>${team.fullName}
|
`<li>${team.fullName}
|
||||||
(${hitters} hitter${ENDING(hitters)},
|
(${hitters} hitter${ENDING(hitters)},
|
||||||
${pitchers} pitcher${ENDING(pitchers)})</li>`
|
${pitchers} pitcher${ENDING(pitchers)}${shadow_string})</li>`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue