function parseBracket(bracket, rounds) { //Build the graph for (var i=0; i< bracket.length; i++) { //Ignore missing ids if (bracket[i]==undefined) continue; //Round for cosmetic purposes TODO: Group by round within the same depth. bracket[i]["round"] = rounds[bracket[i]["round"]]; bracket[i]['stage_name'] = bracket[i]['stage_name']; //Get children for win and loss graphs bracket[i]["refswins"]=[]; bracket[i]["refslosses"]=[]; if(bracket[i]["team1O"]!=undefined) { if (bracket[i]["team1O"].indexOf("L") >= 0) bracket[i]["refslosses"].push(bracket[i]["team1O"].substring(1, bracket[i]["team1O"].length )); if (bracket[i]["team1O"].indexOf("W") >= 0) bracket[i]["refswins"].push(bracket[i]["team1O"].substring(1, bracket[i]["team1O"].length )); } if(bracket[i]["team2O"]!=undefined) { if (bracket[i]["team2O"].indexOf("L") >= 0) bracket[i]["refslosses"].push(bracket[i]["team2O"].substring(1, bracket[i]["team2O"].length )); if (bracket[i]["team2O"].indexOf("W") >= 0) bracket[i]["refswins"].push(bracket[i]["team2O"].substring(1, bracket[i]["team2O"].length )); } //Make sure that winner's brackets come first. Maybe optional. if(bracket[i]["refswins"].length==2 && bracket[i]["refswins"][1]["round"]>bracket[i]["refswins"][0]["round"]) { bracket[i]["refswins"][1] = [bracket[i]["refswins"][0], bracket[i]["refswins"][0]=bracket[i]["refswins"][1]][0]; //Swap variables } //Draw the edge from parent to me for both loser and winner graphs if(bracket[i]["refswins"].length>0) for( var j=0; j0) for( var j=0; jcurbracket["bo"]/2) { curbracket["winner"] = curbracket["team1"]; curbracket["loser"] = curbracket["team2"]; } if(curbracket["score2"]>curbracket["bo"]/2) { curbracket["winner"] = curbracket["team2"]; curbracket["loser"] = curbracket["team1"]; } //propagate the winners while (curbracket["parent"]!=undefined) { if(curbracket["parent"]["team1O"]=="W"+curbracket["id"] && curbracket["winner"]!=undefined){ curbracket["parent"]["team1"] = curbracket["winner"]; } if(curbracket["parent"]["team2O"]=="W"+curbracket["id"] && curbracket["winner"]!=undefined){ curbracket["parent"]["team2"] = curbracket["winner"]; } curbracket=curbracket["parent"]; curdepth++; } //TODO: Find the correct depth when depth is uneven on both sides. bracket[i]['depth']= curdepth; if (curdepth>maxdepth) maxdepth=curdepth; //and the losers curbracket=bracket[i]; while (curbracket["loserparent"]!=undefined) { if (curbracket["loserparent"]['depth']) curbracket['depth']= curbracket['loserparent']['depth']; if(curbracket["loserparent"]["team1O"]=="L"+curbracket["id"] && curbracket["loser"]!=undefined){ curbracket["loserparent"]["team1"] = curbracket["loser"]; } if(curbracket["loserparent"]["team2O"]=="L"+curbracket["id"] && curbracket["loser"]!=undefined){ curbracket["loserparent"]["team2"] = curbracket["loser"]; } curbracket=curbracket["loserparent"]; curdepth++; } } //Find out how many islands are there var islands =[]; for (var i=0; i< bracket.length; i++) { if(bracket[i]==undefined) continue; var found=-1; for (var j=0;j=0) || (bracket[i]['team1O']!=undefined && bracket[i]['team1O'].substr(0,1)=="W" && islands[j].indexOf(parseInt(bracket[i]['team1O'].substr(1)))>=0) || (bracket[i]['team2O']!=undefined && bracket[i]['team2O'].substr(0,1)=="W" && islands[j].indexOf(parseInt(bracket[i]['team2O'].substr(1)))>=0)) { console.log('eureka'); islands[j].push(i); if(found>=0) { islands[found].push.apply(islands[found], islands[j]); islands.splice(j,1); j--; } else found=j; } } if (found==-1) { islands.push([i]); console.log('Not found: pushing ' + i); } } //Find out the order in which we need to display them. var parentlist=[] for (var i=0;i= 0) { parentlist[i] = k; break; } } } if( parentlist[i]>=0) break; if (bracket[islands[i][j]]['team2O'] != undefined && bracket[islands[i][j]]['team2O'].substr(0,1) == "L") { for (var k = 0; k < islands.length; k++) { if (k != i && islands[k].indexOf(parseInt(bracket[islands[i][j]]['team2O'].substr(1))) >= 0) { parentlist[i] = k; break; } } } if( parentlist[i]>=0) break; } } var p = -1; var q = 0; while(parentlist.indexOf(p)>=0) { for (var i=0;ibracket[i]['depth']) bracket[i]['depth']= bracket[i]['loserparent']['depth']; if(depth[bracket[i]['depth']]==undefined) depth[bracket[i]['depth']]=[]; depth[bracket[i]['depth']].push(bracket[i]); } return [depth, maxdepth]; } //This function returns the number of leaves that are either descendant of the node's older son or of any of their ancestors' elder sons that are not the node's direct ancestors. //That will yield the amount function findHigherLeaves(node, bracket) { var ans = 0; if (node['refswins'] == undefined || node['refswins'].length ==0) //Leaf nodes have to count themselves for displacement ans += 1; else if (node['refswins'].length == 1) // If your node only has one child (straight line), the height is the same as the child return findHigherLeaves(bracket[node['refswins'][0]],bracket); else //If it's a non-leaf node with 2 children, it needs a half-size displacement to fit between the 2 children, plus the amount of grandchildren from his oldest child. ans += findLeaves(bracket[node['refswins'][0]], bracket) + 0.5; while(node['parent']!=undefined) //And it also adds 1 per each leaf that's a leaf descendant of the eldest children of any of his ancestors { prevnode=node; node=node['parent']; if(bracket[node['refswins'][0]]['id'] != prevnode['id']) ans += findLeaves(bracket[node['refswins'][0]], bracket); } return ans; } function findLeaves(node, bracket) { var ans=0; if (node['refswins']==undefined || node['refswins'].length==0) return 1; if (node['refswins'].length==1) return(findLeaves(bracket[node['refswins'][0]],bracket)); for (var i=0; i< node['refswins'].length;i++) ans+=findLeaves(bracket[node['refswins'][i]], bracket); return ans; } function sortByTime(a,b) { return new Date(a['time']).getTime() - new Date(a['time']).getTime(); } //Sort by score DESCENDING function sortByScore(a,b) { return b['score']- a['score']; } function buildSchedule (data){ var schedule = {}; schedule['id'] = data['groupstage']['id']; data['matches'].sort(sortByTime); var curdate = ""; //Add list of dates. for (var i=0; igroupstage['bo']/2 || matcheswon>groupstage['bo']/2 ) { //Give them the score they deserve //scoring scheme goes from worse loss to best win, so if it's bo5, the scores values would be for 0-3,1-3,2-3,3-2,3-1,3-0 in that order. var score = groupstage['scoringscheme'][matcheswon-matcheslost+groupstage['bo'] - ((matcheswon>matcheslost)?1:0)-(((matcheswon>=matcheslost)&& groupstage['bo']%2==0)?1:0)]; if (teamscores[teams[i]]==undefined) { teamscores[teams[i]] = [0, 0, 0]; //Order is points, wins, losses. //if it's bo there can be draws as well if(groupstage['bo']%2 ==0) teamscores[teams[i]].push(0); //we add draws at the end. } teamscores[teams[i]][0]+=score; if (matcheswon>matcheslost) teamscores[teams[i]][1]++; else if (matcheswon == matcheslost) teamscores[teams[i]][3]++; else teamscores[teams[i]][2]++; } } } return teamscores; }