BUSTED: what a mess. trying to get tournament round generation bits working. ack.

This commit is contained in:
Dan Buch
2012-03-10 08:47:47 -05:00
parent f0297f3101
commit 8ec802a2bf
19 changed files with 241 additions and 387 deletions

View File

@@ -1,85 +1,62 @@
class MashTournamentsController < ApplicationController
# GET /mash_tournaments
# GET /mash_tournaments.xml
def index
@mash_tournaments = MashTournament.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @mash_tournaments }
format.html
end
end
# GET /mash_tournaments/1
# GET /mash_tournaments/1.xml
def show
@mash_tournament = MashTournament.find(params[:id])
begin
@mash_tournament = MashTournament.find(params[:id])
rescue ActiveRecord::RecordNotFound
redirect_to :action => 'new' and return
end
if not @mash_tournament.done?
return if should_start_mashing?(request.remote_ip)
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @mash_tournament }
format.html
end
end
# GET /mash_tournaments/new
# GET /mash_tournaments/new.xml
def new
return if should_start_mashing?(request.remote_ip)
@mash_tournament = MashTournament.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @mash_tournament }
format.html
end
end
# GET /mash_tournaments/1/edit
def edit
@mash_tournament = MashTournament.find(params[:id])
end
# POST /mash_tournaments
# POST /mash_tournaments.xml
def create
@mash_tournament = MashTournament.new(params[:mash_tournament])
return if should_start_mashing?(request.remote_ip)
@mash_tournament = MashTournament.new(
:requester => Requester.new(:ip => request.remote_ip)
)
respond_to do |format|
if @mash_tournament.save
flash[:notice] = 'MashTournament was successfully created.'
format.html { redirect_to(@mash_tournament) }
format.xml { render :xml => @mash_tournament, :status => :created, :location => @mash_tournament }
flash[:notice] = "Let's start mashing!"
format.html { redirect_to :controller => 'mashes', :action => 'new' }
else
format.html { render :action => "new" }
format.xml { render :xml => @mash_tournament.errors, :status => :unprocessable_entity }
end
end
end
# PUT /mash_tournaments/1
# PUT /mash_tournaments/1.xml
def update
@mash_tournament = MashTournament.find(params[:id])
respond_to do |format|
if @mash_tournament.update_attributes(params[:mash_tournament])
flash[:notice] = 'MashTournament was successfully updated.'
format.html { redirect_to(@mash_tournament) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @mash_tournament.errors, :status => :unprocessable_entity }
end
private
def should_start_mashing?(ip)
if requester = Requester.find_by_ip(ip)
redirect_to :controller => 'mashes', :action => 'new'
return true
end
end
# DELETE /mash_tournaments/1
# DELETE /mash_tournaments/1.xml
def destroy
@mash_tournament = MashTournament.find(params[:id])
@mash_tournament.destroy
respond_to do |format|
format.html { redirect_to(mash_tournaments_url) }
format.xml { head :ok }
end
false
end
end

View File

@@ -8,13 +8,16 @@ class MashesController < ApplicationController
alias_method :show, :redirect_to_new
def new
@map_a, @map_b = Map.pair
return if not already_registered?(request.remote_ip)
@mash = Mash.new(
:map_a => @map_a,
:map_b => @map_b,
:winner => @map_a
)
requester = Requester.find_by_ip(request.remote_ip)
@mash = requester.current_tournament.next_unplayed_mash
if not @mash
flash[:notice] = "You're done!"
redirect_to requester.current_tournament, :action => 'show'
return
end
respond_to do |format|
format.html
@@ -22,18 +25,18 @@ class MashesController < ApplicationController
end
def create
return if not already_registered?(request.remote_ip)
mash_params = params[:mash].clone
logger.info("Got params: #{params.inspect}")
requester = Requester.find_or_initialize_by_ip(request.remote_ip)
requester.save!
requester.reload
logger.info("Setting mash.requester_id from #{requester.inspect}")
requester = Requester.find_by_ip(request.remote_ip)
tournament = requester.mash_tournaments.first
mash_params[:tournament_id] = tournament.id
logger.info("Creating mash with: #{mash_params.inspect}")
@mash = Mash.new(mash_params)
@mash.requester_id = requester.id
@winner = Map.find(@mash.winner_id.to_i)
@winner.points += 1
@@ -48,4 +51,13 @@ class MashesController < ApplicationController
end
end
end
def already_registered?(ip)
if not Requester.find_by_ip(ip)
redirect_to :controller => :mash_tournaments, :action => 'new'
return false
end
true
end
end

View File

@@ -2,6 +2,8 @@ require 'fastercsv'
class Map < ActiveRecord::Base
belongs_to :mash, :class_name => 'Mash'
def self.from_city_name(city_name)
self.find_or_initialize_by_name(city_name).save!
end

View File

@@ -2,5 +2,14 @@ class Mash < ActiveRecord::Base
has_one :map_a, :class_name => 'Map'
has_one :map_b, :class_name => 'Map'
has_one :winner, :class_name => 'Map'
belongs_to :requester
belongs_to :tournament, :class_name => 'MashTournament'
belongs_to :round, :class_name => 'MashTournamentRound'
named_scope :unplayed, {
:conditions => %{
winner_id IS NULL
AND map_a_id IS NOT NULL
AND map_b_id IS NOT NULL
}
}
end

View File

@@ -1,2 +1,106 @@
class MashTournament < ActiveRecord::Base
belongs_to :requester
has_many :mashes
has_many :rounds, :class_name => 'MashTournamentRound'
after_create :create_rounds
after_save :maybe_fill_in_next_round
def next_unplayed_mash
self.mashes.unplayed.first
end
def done?
true
end
def round(number = 0)
MashTournamentRound.find_by_mash_tournament_id(self.id,
:conditions => {:number => number}
)
end
private
def create_rounds
n_contenders = Map.count
while n_contenders % 4 != 0
n_contenders -= 1
end
round = 0
while n_contenders > 2
create_round(round, n_contenders)
n_contenders = n_contenders / 2
round += 1
end
assign_maps_for_round_zero
self.total_rounds = round - 1
end
def create_round(round_number, n_contenders)
round = MashTournamentRound.new(
:mash_tournament_id => self.id,
:number => round_number,
:mash_count => n_contenders / 2
)
round.save!
n_contenders.times do
Mash.new(
:mash_tournament_id => self.id,
:mash_tournament_round_id => round.id
).save!
end
end
def maybe_fill_in_next_round
self.rounds.sort(&:number).each do |round|
if not round.done?
assign_maps_for_round(round)
return
end
end
end
def assign_maps_for_round(round)
previous = MashTournamentRound.find_by_mash_tournament_id_and_number(
self.id, round.number - 1
)
previous_winners = previous.mashes.collect(&:winner_id)
pool = Map.all(
:order => 'RANDOM()',
:conditions => ['id in ?', previous_winners]
)
round.mashes.each do |mash|
mash.update_attributes(
:map_a_id => pool.pop.id,
:map_b_id => pool.pop.id
)
end
end
def assign_maps_for_round_zero
round = MashTournamentRound.for_round(self.id, 0)
pool = Map.all(
:order => 'RANDOM()',
:limit => round.mash_count * 2
)
logger.info("Populating mashes from pool: #{pool.inspect}")
round.mashes.each do |mash|
map_a = pool.pop
map_b = pool.pop
logger.info("Assigning `map_a` from #{map_a.inspect}, " +
"`map_b` from #{map_b.inspect} to mash #{mash.inspect}")
logger.info("`map_a`.`id` = #{map_a.id}, `map_b`.`id` = #{map_b.id} ")
mash.update_attributes(
:map_a_id => map_a.id,
:map_b_id => map_b.id
)
end
end
end

View File

@@ -0,0 +1,8 @@
class MashTournamentRound < ActiveRecord::Base
belongs_to :tournament, :class_name => 'MashTournament'
has_many :mashes
def self.for_round(tournament, round_number)
self.find_by_mash_tournament_id_and_number(tournament, round_number)
end
end

View File

@@ -1,3 +1,7 @@
class Requester < ActiveRecord::Base
has_many :mashes
has_many :mash_tournaments
def current_tournament
self.mash_tournaments.last
end
end

View File

@@ -1,11 +1,9 @@
<h1>New mash_tournament</h1>
<h1>New Map Mash tournament!</h1>
<% form_for(@mash_tournament) do |f| %>
<%= f.error_messages %>
<p>
<%= f.submit 'Create' %>
<%= f.submit 'Start' %>
</p>
<% end %>
<%= link_to 'Back', mash_tournaments_path %>

View File

@@ -1,3 +1,8 @@
<ul>
<% @mash_tournament.mashes.each do |mash| %>
<li><%= mash.map_a.name %> vs. <%= mash.map_b.name %>, winner = <%= mash.winner.name %></li>
<% end %>
</ul>
<%= link_to 'Edit', edit_mash_tournament_path(@mash_tournament) %> |
<%= link_to 'Back', mash_tournaments_path %>
<%= link_to 'New!', :controller => 'mash_tournaments', :action => 'new' %>