Skip to content

Instantly share code, notes, and snippets.

@JFickel
Created November 12, 2012 21:32
Show Gist options
  • Save JFickel/4062047 to your computer and use it in GitHub Desktop.
Save JFickel/4062047 to your computer and use it in GitHub Desktop.
Tournament App
%p#notice= notice
%p
%b Name:
= @tournament.name
%p
%b Game:
= @tournament.game
%p
%b Starts at:
= @tournament.starts_at
- if @tournament.start == true
%b Tournament has started!
- @admins.each do |admin|
- if @current_user == admin
- @admin_access = true
- if @admin_access
= link_to 'Manual Start', tournament_url(id: @tournament.id, tournament: {start: true}), method: 'put', class: 'btn'
- if @tournament.start == true
- @hash_of_arrays_of_round_slots.each do |round, slot_array|
%h2
= "Round #{round}:"
- slot_array = slot_array.sort_by { |slot| slot.pair }
- @new_slot_array = []
- slot_array.each_cons(2) do |slot_pair|
- if slot_pair.first.pair == slot_pair.last.pair
- @new_slot_array << slot_pair
- slot_array.each do |tested_slot|
- @slot_array_with_exception = slot_array.reject { |rejected_slot| tested_slot == rejected_slot }
- if !@slot_array_with_exception.any? { |slot| tested_slot.pair == slot.pair }
- @new_slot_array << [tested_slot]
- @new_slot_array = @new_slot_array.sort_by do |slot_coupling|
- slot_coupling.first.pair
- @finished_slot_array = []
- @new_slot_array.each do |slot_coupling|
- if slot_coupling.count > 1
- slot_coupling = slot_coupling.sort_by { |slot| slot.top ? 0 : 1 }
- @finished_slot_array << slot_coupling
- else
- @finished_slot_array << slot_coupling
- @finished_slot_array.each do |slot_pair_array|
.slot_pair{ :class => "col-#{slot_pair_array.first.round}" }
= "THIS IS A SLOT PAIR ARRAY"
- slot_pair_array.each do |slot|
%slot
- @slot_player = User.find_by_id(slot.user_id)
= image_tag @slot_player.image.url(:medium), size: '60x60'
= link_to @slot_player.nickname, user_path(@slot_player)
= @slot_player.full_name
= slot.inspect
%h2 Your Match:
- if @opponent_pending
= "opponent is pending"
- else
= "Your opponent is:"
= image_tag @opponent.image.url(:medium), size: '60x60'
= link_to @opponent.nickname, user_path(@opponent)
= @opponent.full_name
- @tournament_slot = @current_slot
- if !@opponent_slot.replay.present? && !@current_slot.replay.present?
= form_for @tournament_slot do |f|
= f.label :replay
= f.file_field :replay
= f.submit 'Upload Replay', class: 'btn btn-success'
%h2 Result:
- if @current_slot.present? && @opponent_slot.present?
- if @opponent_slot.replay.present? || @current_slot.replay.present?
= "winner is #{@replay.game.winner.name}"
= link_to('Replay Link', @replay_url)
- else
= "result is pending..."
%h3 Previous Opponents:
%ul
- @previous_opponents_slots.each_with_index do |slot, i|
%li
- @previous_opponent = User.find_by_id(slot.user_id)
- @round = slot.round
= "Round: #{@round + 1}"
= "Replay:"
= link_to('Download Link', @previous_replay_links[i])
= image_tag @previous_opponent.image.url(:medium), size: '60x60'
= link_to @previous_opponent.nickname, user_path(@previous_opponent)
= @previous_opponent.full_name
%p
%b Admins:
%ul
- @admins.each do |admin|
%li
= image_tag admin.image.url(:medium), size: '60x60'
= link_to admin.nickname, user_path(admin)
= admin.full_name
%p
%b Signed up:
%ul
- @signed_up_users.each do |user|
%li
= image_tag user.image.url(:medium), size: '60x60'
= link_to user.nickname, user_path(user)
= user.full_name
= link_to 'Edit', edit_tournament_path(@tournament)
= link_to 'Back', tournaments_path
def show
@tournament = Tournament.find(params[:id])
@admin_slots = TournamentSlot.where(tournament_id: @tournament.id, admin: true)
@admins = []
@admin_slots.each do |slot|
@user = User.find_by_id(slot.user_id)
@admins << @user
end
## 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)
@rounds = Math.log2(slots.count).ceil
@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
@remaining_rounds = @rounds - 2
## Initializing Rounds
## 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, top: true)
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, top: true)
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, top: true)
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
## View Variables
@uninitialized_slots = TournamentSlot.where(tournament_id: @tournament.id, round: nil)
@rounds = Math.log2(@uninitialized_slots.count).ceil
@hash_of_arrays_of_round_slots = {}
@rounds.times do |round|
@round_number = round
@hash_of_arrays_of_round_slots[@round_number + 1] = TournamentSlot.where(tournament_id: @tournament.id, round: @round_number)
end
@hash_of_arrays_of_round_slots.each do |round, slot_array|
slot_array.each do |slot|
if slot.winner != true && slot.user_id.to_i == @current_user.id
@current_slot = slot
@current_slots = TournamentSlot.where(tournament_id: @current_slot.tournament_id, round: @current_slot.round, pair: slot.pair)
@current_slots.each do |slot|
if slot.user_id != @current_slot.user_id
@opponent_slot = slot
end
end
if @opponent_slot.present?
@opponent = User.where(id: @opponent_slot.user_id).first
else
@opponent_pending = true
end
end
end
end
if @current_slot.present?
@previous_opponents_slots = []
@previous_replay_links = []
@previous_slots = TournamentSlot.where(tournament_id: @current_slot.tournament_id, user_id: @current_slot.user_id, winner: true)
@previous_slots.each do |slot|
@two_slots = TournamentSlot.where(pair: slot.pair, round: slot.round)
@two_slots.each do |slot|
if slot.replay.present?
@previous_replay_links << slot.replay.url
end
if slot.user_id.to_i != @current_user.id
@previous_opponents_slots << slot
end
end
end
end
@tournament_slots = TournamentSlot.where(tournament_id: @tournament.id, round: nil)
@signed_up_users = []
@tournament_slots.each do |slot|
@user = User.find_by_id(slot.user_id)
@signed_up_users << @user
end
if @current_slot.present? && @opponent_slot.present?
if @opponent_slot.replay.present?
@replay = Tassadar::SC2::Replay.new(open(@opponent_slot.replay.url))
@replay_url = @opponent_slot.replay.url
end
if @current_slot.replay.present?
@replay = Tassadar::SC2::Replay.new(open(@current_slot.replay.url))
@replay_url = @current_slot.replay.url
end
end
if @current_slot.present? && @opponent_slot.present?
if @opponent_slot.replay.present? || @current_slot.replay.present?
@current_players = []
@current_slots.each do |slot|
@player = User.find_by_id(slot.user_id)
@current_players << @player
end
@current_players.each do |player|
if @replay.game.winner.name == player.sc2_character
@winner = player
end
end
@winner_slot = TournamentSlot.where(tournament_id: @current_slot.tournament_id, round: @current_slot.round, user_id: @winner.id).first
@winner_slot.winner = true
@winner_slot.save
@winner_pair = @winner_slot.pair
@winner_pair += 1
@new_pair = @winner_pair/2
@new_round = @winner_slot.round + 1
@slot_already_exists = TournamentSlot.where(tournament_id: @current_slot.tournament_id, round: @new_round, user_id: @winner_slot.user_id, pair: @new_pair)
if !@slot_already_exists.present?
if @winner_slot.pair.odd?
TournamentSlot.create(tournament_id: @current_slot.tournament_id, round: @new_round, user_id: @winner_slot.user_id, pair: @new_pair, top: true)
else
TournamentSlot.create(tournament_id: @current_slot.tournament_id, round: @new_round, user_id: @winner_slot.user_id, pair: @new_pair)
end
redirect_to(@tournament) and return
end
end
end
## StringIO Fix
OpenURI::Buffer.send :remove_const, 'StringMax' if OpenURI::Buffer.const_defined?('StringMax')
OpenURI::Buffer.const_set 'StringMax', 0
respond_to do |format|
format.html
format.json { render json: @tournament }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment