From 42d6e3b1cc05ef5172081682b53675e4827254d3 Mon Sep 17 00:00:00 2001 From: tkimia Date: Tue, 29 Apr 2014 16:41:35 -0400 Subject: elimination works SOMETIMES... other times it just can't put down a damned winner --- lib/scheduling/elimination.rb | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'lib/scheduling/elimination.rb') diff --git a/lib/scheduling/elimination.rb b/lib/scheduling/elimination.rb index 44ce81e..a95101c 100644 --- a/lib/scheduling/elimination.rb +++ b/lib/scheduling/elimination.rb @@ -7,6 +7,7 @@ module Scheduling @tournament_stage = tournament_stage end + def create_matches num_teams = (tournament.players.count/tournament.min_players_per_team).floor num_matches = (Float(num_teams - tournament.min_teams_per_match)/(tournament.min_teams_per_match - 1)).ceil + 1 @@ -15,29 +16,42 @@ module Scheduling end match_num = num_matches-1 - team_num = 0 + team_num = 1 tournament.players.shuffle # for each grouping of min_players_per_team tournament.players.each_slice(tournament.min_players_per_team) do |team_members| + # create a new team in the current match + tournament_stage.matches.order(:id)[match_num].teams.push(Team.create(users: team_members)) + # if the match is full, move to the next match, otherwise move to the next team if (team_num == tournament.min_teams_per_match) + tournament_stage.matches[match_num].update(status: 1); match_num -= 1 team_num = 1 else team_num += 1 end - # create a new team in the current match - tournament_stage.matches[match_num].teams.push(Team.create(users: team_members)) end end def finish_match(match) + require 'pp' + puts('>'*80) + pp match + puts('>'*80) + logBase = match.tournament_stage.tournament.min_teams_per_match matches = match.tournament_stage.matches_ordered cur_match_num = matches.invert[match] unless cur_match_num == 1 - match.winner.matches.push(matches[cur_match_num/2]) + match.winner.matches.push(matches[(cur_match_num+logBase-2)/logBase]) + end + if matches[(cur_match_num+logBase-2)/logBase].teams.count == match.tournament_stage.tournament.min_teams_per_match + puts(80*'><') + puts "making the status 1" + puts(80*'><') + matches[(cur_match_num+logBase-2)/logBase].status = 1 end end @@ -89,16 +103,16 @@ module Scheduling str += "\t\t