Skip to content

Instantly share code, notes, and snippets.

@JFickel
Created November 2, 2012 18:49
Show Gist options
  • Save JFickel/4003535 to your computer and use it in GitHub Desktop.
Save JFickel/4003535 to your computer and use it in GitHub Desktop.
## Check if Tournament has already started
@tournament_not_started = true
if TournamentSlot.where(tournament_id: @tournament.id, round: 0).present?
@tournament_not_started = false
end
## Tournament Start
if @tournament.start && @tournament_not_started
slots = TournamentSlot.where(tournament_id: @tournament.id, round: nil)
if slots.count > 2
rounds = 2
end
if slots.count > 4
rounds = 3
end
if slots.count > 8
rounds = 4
end
if slots.count > 16
rounds = 5
end
if slots.count > 32
rounds = 6
end
if slots.count > 64
rounds = 7
end
if slots.count > 128
rounds = 8
end
if slots.count > 256
rounds = 9
end
if slots.count > 512
rounds = 10
end
if slots.count > 1024
rounds = 11
end
@rounds = rounds
@bracket_size = 2**rounds
@after_filter_round_size = @bracket_size/2
filter_round_challengers = slots.count - @after_filter_round_size
filter_round_size = filter_round_challengers * 2
## Filter Round
round = 0
@filter_round_participants = slots.first(filter_round_size)
@first_round_participants = slots - slots.first(filter_round_size)
@filter_round_participants.each_slice(2).with_index do |slot, index|
TournamentSlot.create(tournament_id: slot.first.tournament_id, user_id: slot.first.user_id, round: round, pair: index + 1)
TournamentSlot.create(tournament_id: slot.last.tournament_id, user_id: slot.last.user_id, round: round, pair: index + 1)
end
## First Round
round = 1
if @first_round_participants.count.even?
@pair_origin = filter_round_challengers/2
@first_round_participants.each_slice(2).with_index do |slot, index|
TournamentSlot.create(tournament_id: slot.first.tournament_id, user_id: slot.first.user_id, round: round, pair: index + 1 + @pair_origin)
TournamentSlot.create(tournament_id: slot.last.tournament_id, user_id: slot.last.user_id, round: round, pair: index + 1 + @pair_origin)
end
end
if @first_round_participants.count.odd?
@pair_origin = filter_round_challengers/2
@odd_individual = @first_round_participants.shift
TournamentSlot.create(tournament_id: @odd_individual.tournament_id, user_id: @odd_individual.user_id, round: 1, pair: @pair_origin + 1)
@new_pair_origin = @pair_origin + 1
@first_round_participants.each_slice(2).with_index do |slot, index|
TournamentSlot.create(tournament_id: slot.first.tournament_id, user_id: slot.first.user_id, round: round, pair: index + 1 + @new_pair_origin)
TournamentSlot.create(tournament_id: slot.last.tournament_id, user_id: slot.last.user_id, round: round, pair: index + 1 + @new_pair_origin)
end
end
end
@filter_round_slots = TournamentSlot.where(tournament_id: @tournament.id, round: 0)
@first_round_slots = TournamentSlot.where(tournament_id: @tournament.id, round: 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment