var API = "https://api.nhle.com/stats/rest/fr/team/summary"; var expansions = { 1970: [19,20], 1972: [21,22], 1974: [23,24], 1979: [25,26,27,28], 1991: [29], 1992: [30,31], 1993: [32,33], 1998: [34], 1999: [35], 2000: [36,37], 2017: [38], 2021: [39], 2024: [40], } var removals = { 1978: [13], 2024: [28], } function query(d,q) { var url = new URLSearchParams({ "isAggregate": true, "isGame": true, "start": 0, "limit": -1, "sort": '[{"property":"pointPct","direction":"DESC"},{"property":"wins","direction":"DESC"},{"property":"losses","direction":"ASC"},{"property":"otLosses","direction":"DESC"},{"property":"ties","direction":"DESC"},{"property":"goalsFor","direction":"DESC"},{"property":"goalsAgainst","direction":"ASC"},{"property":"franchiseId","direction":"ASC"}]', "cayenneExp": q, }); return fetch(API+"?"+url, { method: 'GET', }) .then(res => res.json()) .then(res => { res.div = d; return res; }) } var teams = [ [1,5,6,10,11,12], [13,14,15,16,17,18] ]; var rankings = {}; var season = "19671968"; function sim(s) { var year = parseInt(s.substr(0,4)); //console.log("Season %d-%d", year, year+1); var promises = []; for(var d = 0; d < 2; d++) promises[d] = query(d,"gameTypeId=2 and franchiseId in ("+teams[d].join(",")+") and opponentFranchiseId in ("+teams[d].join(",")+") and seasonId="+s) .then(res=>{ //console.log("Division %d", res.div+1); ///*if(year>=2023)*/console.table(res.data, ["franchiseId","franchiseName","gamesPlayed","wins","losses","otLosses","ties","goalsFor","goalsAgainst","points","pointPct"]); var exps = expansions[year+1] ? Math.floor((expansions[year+1].length - (removals[year+1]?removals[year+1].length:0) - (teams[0].length-teams[1].length)) /2): 0; // var start = res.div == 0 ? res.data.length - (exps < 2 ? 2 - exps : 0) : 0; var end = res.div == 0 ? res.data.length : (exps > 2 ? exps : 2); var msg = res.div == 0 ? "relegated to Division 2" : "promoted to Division 1"; var arr = []; for(var i = start; i < end; i++) { //console.log("The %s are %s", res.data[i].franchiseName, msg); arr.push(res.data[i].franchiseId); } return arr; }); Promise.all(promises).then(a=>{ rankings[year] = structuredClone(teams); if(year < 2024) { year++; if(year == 2004) year++; a.forEach((v,i)=>{ for(var j = 0; j < v.length; j++){ teams[1-i].push(v[j]); teams[i].splice(teams[i].indexOf(v[j]),1); } }); if(expansions[year]) { for(var j = 0; j < expansions[year].length; j++){ teams[1].push(expansions[year][j]); } } sim((year)+""+(year+1)); } else console.log(JSON.stringify(rankings)); }); } sim(season);