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 From dd9a7a6b55fd3a6a494ba41fc133b1f1056c66a9 Mon Sep 17 00:00:00 2001 From: tkimia Date: Thu, 24 Apr 2014 17:10:24 -0400 Subject: fixed an off-by-one error when moving teams into matches --- lib/scheduling/elimination.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/scheduling') diff --git a/lib/scheduling/elimination.rb b/lib/scheduling/elimination.rb index ca48f14..e66a29a 100644 --- a/lib/scheduling/elimination.rb +++ b/lib/scheduling/elimination.rb @@ -32,7 +32,7 @@ module Scheduling # if the match is full, move to the next match, otherwise move to the next team if (team_num == self.tournament.min_teams_per_match) match_num -= 1 - team_num = 0 + team_num = 1 else team_num += 1 end -- cgit v1.2.3-2-g168b From bd1bfe729030312fc678a3705c881744aa650c63 Mon Sep 17 00:00:00 2001 From: tkimia Date: Thu, 24 Apr 2014 18:08:26 -0400 Subject: text in graph is a bit better --- lib/scheduling/elimination.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/scheduling') diff --git a/lib/scheduling/elimination.rb b/lib/scheduling/elimination.rb index e66a29a..4e368b5 100644 --- a/lib/scheduling/elimination.rb +++ b/lib/scheduling/elimination.rb @@ -108,7 +108,7 @@ STRING t = 1 while t <= numTeams - color = (matches[i].teams[t-1] and matches[i].teams[t-1].users.include?(current_user)) ? "#BCED91" : "white" + color = (matches[i].teams[t-1] and matches[i].teams[t-1].users.include?(current_user)) ? "#5BC0DE" : "white" str += "\t\t\n" if matches[i].teams[t-1] str += "\t\tTeam #{matches[i].teams[t-1].id}\n" -- cgit v1.2.3-2-g168b From 575c8a552cbcf11934b32f4d24636cdc18094f53 Mon Sep 17 00:00:00 2001 From: tkimia Date: Thu, 24 Apr 2014 18:37:18 -0400 Subject: number of matches has new formula that works for all teams/match --- lib/scheduling/elimination.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/scheduling') diff --git a/lib/scheduling/elimination.rb b/lib/scheduling/elimination.rb index 4e368b5..cf0d625 100644 --- a/lib/scheduling/elimination.rb +++ b/lib/scheduling/elimination.rb @@ -17,7 +17,7 @@ module Scheduling def create_matches num_teams = (self.tournament.players.count/self.tournament.min_players_per_team).floor - num_matches = num_teams - 1 + num_matches = (Float(num_teams - tournament.min_teams_per_match)/(tournament.min_teams_per_match - 1)).ceil + 1 for i in 1..num_matches self.tournament_stage.matches.create(status: 0, submitted_peer_evaluations: 0) end -- cgit v1.2.3-2-g168b