From 66aed81910ba84e829ecc9c0d11bed104c4283d0 Mon Sep 17 00:00:00 2001 From: tkimia Date: Thu, 24 Apr 2014 16:20:21 -0400 Subject: math wizardry --- lib/scheduling/elimination.rb | 56 ++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 24 deletions(-) (limited to 'lib/scheduling') diff --git a/lib/scheduling/elimination.rb b/lib/scheduling/elimination.rb index 1ac696d..ca48f14 100644 --- a/lib/scheduling/elimination.rb +++ b/lib/scheduling/elimination.rb @@ -51,10 +51,16 @@ module Scheduling def graph(current_user) matches = @tournament_stage.matches_ordered + numTeams = @tournament_stage.tournament.min_teams_per_match + logBase = numTeams + # depth of SVG tree - depth = Math.log2(matches.count).floor+1; + depth = Math.log(matches.count*(logBase-1),logBase).floor+1; + # height of SVG - height = [200 * 2**Math.log2(matches.count).floor + 100, 500].max; + matchHeight = 50*logBase; + height = [(matchHeight+50) * logBase**(depth-1) + 100, 500].max; + lastrx = 0 lastry = 0 lastrh = 0 @@ -73,10 +79,12 @@ module Scheduling STRING (1..matches.count).each do |i| - rh = 100/(2**(depth-1)+1) - 5 + matchDepth = Math.log(i*(logBase-1), logBase).floor+1 + base = (logBase**(matchDepth-1)/(logBase-1)).ceil + rh = 100 / (logBase**(depth-1)+1) - 100/height; rw = 100/(depth+1) - 5 - rx = 50/(depth+1) + 100/(depth+1)*(depth-(Math.log2(i).floor+1)) - ry = ( 100/(2**(Math.log2(i).floor)+1) + rh * 1.1 * (2**Math.log2(i).ceil-i)) + rx = 50/(depth+1) + 100/(depth+1)*(depth-matchDepth) + ry = 100/(logBase**(matchDepth-1)+1) * (i-base+1) - rh/2 str += "\t\n" str += "\t\t\n" - if matches[i].teams.first - str += "\t\tTeam #{matches[i].teams.first.id}\n" - end - - str += "\t\t VS \n" + str += "/>\n" - color = (matches[i].teams[1] and matches[i].teams[1].users.include?(current_user)) ? "#BCED91" : "white" - str += "\t\t\n" - if matches[i].teams[1] - str += "\t\tTeam #{matches[i].teams[1].id}\n" + t = 1 + while t <= numTeams + color = (matches[i].teams[t-1] and matches[i].teams[t-1].users.include?(current_user)) ? "#BCED91" : "white" + str += "\t\t\n" + if matches[i].teams[t-1] + str += "\t\tTeam #{matches[i].teams[t-1].id}\n" + end + if (t < numTeams) + str += "\t\t VS \n" + end + t = t + 1 end if i > 1 - str += "\t\t\n" - end - if Math.log2(i+1) == Math.log2(i+1).ceil - lastrx = rx - lastry = ry - lastrh = rh - lastrw = rw + parent = (i+logBase-2)/logBase + pDepth = Math.log(parent*(logBase-1), logBase).floor+1 + pBase = (logBase**(pDepth-1)/(logBase-1)).ceil + lastrx = 50/(depth+1) + 100/(depth+1)*(depth-pDepth) + lastry = 100/(logBase**(pDepth-1)+1) * (parent-pBase+1) - rh/2 + str += "\t\t\n" end str += "\n" end @@ -126,5 +133,6 @@ STRING return str end + end end -- cgit v1.2.3-2-g168b