Created
November 1, 2023 18:35
-
-
Save zachduda/45b19189d5d9499d047d77bada9a335f to your computer and use it in GitHub Desktop.
Mountaineer Metrics Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// v1 -- 2023 -- Written for MountaineerMetrics.com | |
function id(id) { | |
return document.getElementById(id); | |
} | |
function resetBtns() { | |
id('btn_2023').style.fontSize = null; | |
id('btn_2022').style.fontSize = null; | |
id('btn_2021').style.fontSize = null; | |
} | |
let year = 2023; | |
function update(year = "2023", timer = false) { | |
if (year !== 2023 && timer) { | |
return; | |
} | |
if (!timer) { | |
id('data').innerHTML = "Loading scores for " + year + "...<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"; | |
} | |
resetBtns(); | |
id('btn_' + year).style.fontSize = "24px"; | |
const root_ep = "https://mountaineermetrics.com/" + year + "_season.php"; | |
let overall = ""; | |
let full = []; | |
let data_html = ""; | |
let Data = {}; | |
let Games = {}; | |
fetch(root_ep, { | |
method: 'GET', | |
type: 'text/plain', | |
}).then(function(response) { | |
return response.text() | |
}).then(function(raw) { | |
let pstr = ""; | |
console.log(raw); | |
var str_array = raw.split('\r'); | |
let game_id = 1; | |
for (var i = 0; i < str_array.length; i++) { | |
// Trim the excess whitespace. | |
let row = str_array[i].trim(); | |
if (row == "") { | |
continue; | |
} | |
full.push(row); | |
//if(pstr.includes("Overall") || row.includes("Overall")) { | |
if (i == 1) { | |
Data.Year = row.replaceAll(" Football Schedule", "").trim(); | |
} | |
if (i == 3) { | |
Data.Overall = row.replaceAll("Overall ", "").trim(); | |
} | |
if (i == 4) { | |
Data.Conference = row.replaceAll("Conference ", "").trim(); | |
} | |
if (i == 5) { | |
Data.Streak = row.replaceAll("Streak ", "").trim(); | |
} | |
if (i == 6) { | |
Data.Home = row.replaceAll("Home ", "").trim(); | |
} | |
if (i == 7) { | |
Data.Away = row.replaceAll("Away ", "").trim(); | |
} | |
if (i == 8) { | |
Data.Neutral = row.replaceAll("Neutral ", "").trim(); | |
} | |
if (i >= 11) { // Begin game lines | |
let Game = {}; | |
Game.Month = row.substring(0, 3); | |
Game.Day = row.substring(4, 6).trim(); | |
Game.Dow = row.substring(6, 12).replaceAll("(", "").replaceAll(")", "").trim(); | |
// Trim what was parsed out of row. | |
row = row.replaceAll(Game.Month, "").replaceAll(Game.Day, "").replaceAll(Game.Dow, "").replaceAll("()", "").trim(); | |
Game.Time = row.replaceAll(".", "").substring(0, 7).replaceAll("ET", "").replaceAll("MT", "").replaceAll("CT", "").trim(); | |
// Trim time out of row to continue parse | |
let away_ind = row.indexOf("Away"); | |
let home_ind = row.indexOf("Home"); | |
let neu_ind = row.indexOf("Neutral"); | |
// Row trim everything before Away/Home and parse | |
// Championship won't return Home/Away, instead returns Neutral | |
if (neu_ind > 0) { | |
row = row.substr(neu_ind + 8, row.length); | |
Game.Loc = "TBD"; | |
} else | |
if (home_ind <= -1) { | |
Game.Loc = "Away"; | |
row = row.substr(away_ind + 5, row.length); | |
} else | |
if (away_ind <= -1) { | |
Game.Loc = "Home"; | |
row = row.substr(home_ind + 5, row.length); | |
} | |
// Get scores first to leave the location left. | |
let check_score_str = row.substr(row.length - 7, row.length).trim(); | |
if (check_score_str.indexOf("-") >= 0) { | |
// - means theres a score in that space. | |
if (check_score_str.indexOf("W") >= 0) { | |
Game.Won = true; | |
} else if (check_score_str.indexOf("L") >= 0) { | |
Game.Won = false; | |
} | |
Game.Score = check_score_str.replaceAll("W", "").replaceAll("L", "").trim(); | |
} else { | |
Game.Score = "TBD"; | |
} | |
row = row.replaceAll("(Kidd Brewer Stadium)", "").replaceAll("BOONE, N.C.", "").replaceAll(check_score_str, "").trim(); | |
Game.Opponent = row; | |
switch (Game.Dow) { | |
case "Mon": | |
Game.Dow = "Monday"; | |
break; | |
case "Tue": | |
Game.Dow = "Tuesday"; | |
break; | |
case "Wed": | |
Game.Dow = "Wednesday"; | |
break; | |
case "Thu": | |
Game.Dow = "Thursday"; | |
break; | |
case "Fri": | |
Game.Dow = "Friday"; | |
break; | |
case "Sat": | |
Game.Dow = "Saturday"; | |
break; | |
case "Sun": | |
Game.Dow = "Sunday"; | |
break; | |
} | |
Games[game_id] = Game; | |
data_html += "<h4 style='font-weight:750'>" + Game.Opponent + "</h4>"; | |
if (Game.Score == "TBD") { | |
data_html += "<ul style='font-family:monospace;'>"; | |
data_html += "<li>Scheduled for " + Game.Month + " " + Game.Day + ", " + Data.Year + "</li>"; | |
if (Game.Loc == "Home") { | |
data_html += "<li><b>Home</b> Game</li>"; | |
} | |
if (Game.Loc == "Away") { | |
data_html += "<li><b>Away</b> Game</li>"; | |
} | |
data_html += "</ul><hr>"; | |
} else { | |
data_html += "<ul style='font-family:monospace;'>"; | |
data_html += "<li>Played on " + Game.Month + " " + Game.Day + ", " + Data.Year + "</li>"; | |
if (Game.Loc == "Home") { | |
data_html += "<li><b>Home</b> Game</li>"; | |
} | |
if (Game.Loc == "Away") { | |
data_html += "<li><b>Away</b> Game</li>"; | |
} | |
data_html += "<li><h4 style='padding:0;margin:0;padding:5px;font-weight:800;font-family:Arial;'>🏈 Scores:</h4></li><ul>"; | |
if (Game.Won) { | |
data_html += "<li>App <span style='color:green;font-weight:800;'>WON</span> this game</li>"; | |
} else { | |
data_html += "<li>App <span style='color:red;font-weight:800;'>LOST</span> this game</li>"; | |
} | |
data_html += "<li>The final score was <b style='color:blue'>" + Game.Score + "</b></li>"; | |
data_html += "</ul></ul><hr>"; | |
} | |
game_id++; | |
} | |
} | |
Data.Games = Games; | |
console.log(Data); | |
id("data").innerHTML = data_html; | |
}).catch(function(error) { | |
console.error("Error: " + error); | |
}); | |
} | |
update(); | |
setInterval(function() { | |
update('2023', true) | |
}, 30000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment