Skip to content

Commit

Permalink
Add minileague sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
slmnio committed Nov 15, 2021
1 parent 2f8c3d1 commit 835efe7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
25 changes: 21 additions & 4 deletions website/src/components/broadcast/Standings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,34 @@ export default {
if (!this.stage) return this.allMatches;
return this.allMatches.filter(match => match.match_group && match.match_group.toLowerCase() === this.stage.toLowerCase());
},
settings() {
blocks() {
if (!this.event || !this.event.blocks) return null;
try {
const blocks = JSON.parse(this.event.blocks);
return blocks.settings || null;
return blocks || null;
} catch (e) {
return null;
}
},
settings() {
if (!this.blocks) return null;
return this.blocks?.settings || null;
},
useOMW() {
return this.settings?.useOMW && this.stageMatches.every(m => [m.score_1, m.score_2].some(s => s === m.first_to));
},
standingsSort() {
try {
const sorters = this.blocks.standingsSort;
if (sorters && sorters.length) {
const sorter = sorters.find(s => s.group === this.stage);
return sorter?.sort;
}
} catch (e) {
return null;
}
return null;
},
standings() {
if (!this.stageMatches || !this.event) return [];
if (!this.stageMatches.some(m => m.match_group)) return []; // make sure there's matches to analyse
Expand Down Expand Up @@ -200,9 +216,10 @@ export default {
teams = teams.sort(sortFunction);
console.log("[standings teams]", teams);
// console.log("[standings teams]", teams);
const standings = sortTeamsIntoStandings(teams.map(t => ({ ...t, ...t.standings })), {
useOMW: this.useOMW
useOMW: this.useOMW,
sort: this.standingsSort
});
// console.log("[new standings]", standings);
Expand Down
82 changes: 76 additions & 6 deletions website/src/utils/scenarios.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export function sortByMatchDiff(a, b) {
return 0;
}

export function miniLeagueMatchDiff(a, b) {
const [aMatchDiff, bMatchDiff] = [a, b].map(x => x.standings.minileague.wins - x.standings.minileague.losses);
if (aMatchDiff < bMatchDiff) return 1;
if (aMatchDiff > bMatchDiff) return -1;
return 0;
}

export function sortByHeadToHead(a, b) {
// console.log("h2h", a.standings, b.standings);

Expand Down Expand Up @@ -154,7 +161,7 @@ export function sortIntoGroups2(sortFunction, standings, maxInGroup) {
if (group.length <= 1) continue; // don't bother sorting if it's just one

if (maxInGroup && group.length > maxInGroup) {
// console.log(`[i] cannot sort this group because ${group.length} is too big for max ${maxInGroup} for this function`);
console.log(`[i] cannot sort this group because ${group.length} is too big for max ${maxInGroup} for this function`, { group, standings });
continue;
}

Expand Down Expand Up @@ -264,8 +271,71 @@ export function sortWithinGroups(sortFunction, standings) {
return standings.map(group => group.sort(sortFunction));
}

function miniLeaguePrep(standings) {
for (const group of standings) {
if (group.length <= 1) continue;
console.log("minileagueprep", group);

const groupIDs = group.map(g => g.id);

group.forEach(team => {
// console.log("minileague setup", team.id, groupIDs, team.standings.h2h);
team.standings.minileague = {
wins: 0,
losses: 0
};
groupIDs.forEach(opponentID => {
const diff = team.standings.h2h[opponentID];
if (diff === 1) team.standings.minileague.wins++;
if (diff === -1) team.standings.minileague.losses++;
});
});

console.log(group.sort(miniLeagueMatchDiff).map(t => `|${t.code.padStart(6, " ")} ${t.standings.minileague.wins}-${t.standings.minileague.losses}`).join("\n"));

/*
* Set up a minileague
* - Take all the head to heads from all the teams in the tied group
* - Sort it by whatever required
* */
}
return standings;
}

function getSortMethod(stringMethod) {
if (stringMethod === "MatchDiff") return { method: sortByMatchDiff, max: null };
if (stringMethod === "MapDiff") return { method: sortByMapDiff, max: null };
if (stringMethod === "HeadToHead") return { method: sortByHeadToHead, max: 2 };
if (stringMethod === "MapWins") return { method: sortByMapWins, max: null };
if (stringMethod === "OMW") return { method: sortByOMW, max: null };
if (stringMethod === "MiniLeague") return { prep: miniLeaguePrep, method: miniLeagueMatchDiff, max: null };
return null;
}

export function sortTeamsIntoStandings(teams, settings = {}) {
// console.log("[standings]", "starting sort", teams);
const log = false;
if (log) console.log("[standings]", "starting sort", teams, settings);

if (settings.sort) {
// Custom sort
console.log("[standings]", `Sorting in custom order: ${settings.sort}`);
let standings = [teams];
for (const mode of settings.sort) {
const _method = getSortMethod(mode);
if (!_method) continue;
const { prep, method, max } = _method;

if (prep) standings = prep(standings);

if (max) {
standings = sortIntoGroups2(method, standings, max);
} else {
standings = sortIntoGroups2(method, standings);
}
}
return standings;
}

let standings = sortIntoGroups2(sortByMatchDiff, [teams]);
// if (i === 4050) console.log(standings);
// sortMatches(sortByMatchWins, scenario.teams, standings);
Expand All @@ -285,20 +355,20 @@ export function sortTeamsIntoStandings(teams, settings = {}) {
}

if (!standings.every(s => s.length === 1)) {
console.log("[standings]", "not converged, trying map diff", standings);
if (log) console.log("[standings]", "not converged, trying map diff", standings);
standings = sortIntoGroups2(sortByMapDiff, standings);
}
if (!standings.every(s => s.length === 1)) {
console.log("[standings]", "not converged, trying head to head", standings);
if (log) console.log("[standings]", "not converged, trying head to head", standings);
// i don't know why [standings] works here but not for the other one
standings = sortIntoGroups2(sortByHeadToHead, standings, 2);
}
if (!standings.every(s => s.length === 1)) {
console.log("[standings]", "not converged, trying map wins", standings);
if (log) console.log("[standings]", "not converged, trying map wins", standings);
standings = sortIntoGroups2(sortByMapWins, standings);
}
if (!standings.every(s => s.length === 1) && settings.useOMW) {
console.log("[standings]", "not converged, trying opponent winrate", standings);
if (log) console.log("[standings]", "not converged, trying opponent winrate", standings);
standings = sortIntoGroups2(sortByOMW, standings);
}
if (!standings.every(s => s.length === 1)) {
Expand Down

0 comments on commit 835efe7

Please sign in to comment.