Getting at least one tournament working!
This commit is contained in:
parent
73cff897a3
commit
d08362326d
@ -39,9 +39,12 @@ class MashTournamentsController < ApplicationController
|
|||||||
@mash_tournament = MashTournament.new(
|
@mash_tournament = MashTournament.new(
|
||||||
:requester => Requester.new(:ip => request.remote_ip)
|
:requester => Requester.new(:ip => request.remote_ip)
|
||||||
)
|
)
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if @mash_tournament.save && @mash_tournament.create_rounds_for_contenders(8)
|
if @mash_tournament.save && (
|
||||||
|
@mash_tournament.builder.create_rounds_for_contenders(8) == 3
|
||||||
|
) && (
|
||||||
|
@mash_tournament.builder.fill_in_next_round.length == 4
|
||||||
|
)
|
||||||
flash[:notice] = "Let's start mashing!"
|
flash[:notice] = "Let's start mashing!"
|
||||||
format.html { redirect_to :controller => 'mashes', :action => 'new' }
|
format.html { redirect_to :controller => 'mashes', :action => 'new' }
|
||||||
else
|
else
|
||||||
|
@ -24,27 +24,18 @@ class MashesController < ApplicationController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def update
|
||||||
return if not already_registered?(request.remote_ip)
|
return if not already_registered?(request.remote_ip)
|
||||||
|
|
||||||
mash_params = params[:mash].clone
|
|
||||||
logger.info("Got params: #{params.inspect}")
|
logger.info("Got params: #{params.inspect}")
|
||||||
|
|
||||||
requester = Requester.find_by_ip(request.remote_ip)
|
@mash = Mash.find(params[:id])
|
||||||
tournament = requester.mash_tournaments.first
|
@mash.winner = Map.find((params[:mash][:winner_id] || -1).to_i)
|
||||||
mash_params[:tournament_id] = tournament.id
|
|
||||||
|
|
||||||
logger.info("Creating mash with: #{mash_params.inspect}")
|
|
||||||
@mash = Mash.new(mash_params)
|
|
||||||
|
|
||||||
@winner = Map.find(@mash.winner_id.to_i)
|
|
||||||
@winner.points += 1
|
|
||||||
|
|
||||||
logger.info("About to save #{@mash.inspect}")
|
logger.info("About to save #{@mash.inspect}")
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if @winner.save && @mash.save
|
if @mash.save
|
||||||
flash[:notice] = 'Mash was successfully created.'
|
flash[:notice] = 'Mashed!'
|
||||||
format.html { redirect_to(:action => 'new') }
|
format.html { redirect_to(:action => 'new') }
|
||||||
else
|
else
|
||||||
format.html { render :action => 'new' }
|
format.html { render :action => 'new' }
|
||||||
|
@ -5,11 +5,9 @@ class Mash < ActiveRecord::Base
|
|||||||
has_one :tournament, :class_name => 'MashTournament'
|
has_one :tournament, :class_name => 'MashTournament'
|
||||||
has_one :round, :class_name => 'MashTournamentRound'
|
has_one :round, :class_name => 'MashTournamentRound'
|
||||||
|
|
||||||
named_scope :unplayed, {
|
named_scope :unplayed, :conditions => [%{
|
||||||
:conditions => %{
|
|
||||||
winner_id IS NULL
|
winner_id IS NULL
|
||||||
AND map_a_id IS NOT NULL
|
AND map_a_id IS NOT NULL
|
||||||
AND map_b_id IS NOT NULL
|
AND map_b_id IS NOT NULL
|
||||||
}
|
}]
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
@ -4,11 +4,21 @@ class MashTournament < ActiveRecord::Base
|
|||||||
has_many :rounds, :class_name => 'MashTournamentRound'
|
has_many :rounds, :class_name => 'MashTournamentRound'
|
||||||
|
|
||||||
def next_unplayed_mash
|
def next_unplayed_mash
|
||||||
self.mashes.unplayed.first
|
mash = self.mashes.unplayed.first
|
||||||
|
if not mash
|
||||||
|
filled = self.builder.fill_in_next_round
|
||||||
|
if filled
|
||||||
|
return self.mashes.unplayed.first
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return mash
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def done?
|
def done?
|
||||||
true
|
self.rounds.collect(&:done?).uniq == [true]
|
||||||
end
|
end
|
||||||
|
|
||||||
def round(number = 0)
|
def round(number = 0)
|
||||||
@ -16,4 +26,10 @@ class MashTournament < ActiveRecord::Base
|
|||||||
:conditions => {:number => number}
|
:conditions => {:number => number}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def builder
|
||||||
|
MashTournamentBuilder.new(
|
||||||
|
self, MashTournament, MashTournamentRound, Map, Mash
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
<ul>
|
|
||||||
<% @mash_tournament.mashes.each do |mash| %>
|
|
||||||
<% if mash.map_a.present? && mash.map_b.present? && mash.winner.present? %>
|
|
||||||
<li><%= mash.map_a.name %> vs. <%= mash.map_b.name %>, winner = <%= mash.winner.name %></li>
|
|
||||||
<% end %>
|
|
||||||
<% end %>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
|
|
||||||
<%= link_to 'New!', :controller => 'mash_tournaments', :action => 'new' %>
|
|
11
rails/map-mash/app/views/mash_tournaments/show.html.haml
Normal file
11
rails/map-mash/app/views/mash_tournaments/show.html.haml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
- @mash_tournament.rounds.reverse.each do |round|
|
||||||
|
%h2 Round #{round.number}
|
||||||
|
- round.mashes.each do |mash|
|
||||||
|
%ul
|
||||||
|
- if mash.map_a.present? && mash.map_b.present? && mash.winner.present?
|
||||||
|
%li
|
||||||
|
#{mash.map_a.name} vs. #{mash.map_b.name}, winner =
|
||||||
|
%strong #{mash.winner.name}
|
||||||
|
|
||||||
|
|
||||||
|
= link_to 'New!', :controller => 'mash_tournaments', :action => 'new'
|
@ -1,32 +0,0 @@
|
|||||||
<% content_for :head_js do %>
|
|
||||||
<script type="text/javascript">
|
|
||||||
$(function() {
|
|
||||||
$('.mash-image').click(function(elem) {
|
|
||||||
$('#mash_winner_id').val($(elem.target).attr('data-map-id'));
|
|
||||||
$('#new_mash').submit();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
<h1>Mash the Maps!</h1>
|
|
||||||
<p>Choose your fave.</p>
|
|
||||||
|
|
||||||
<% form_for(@mash) do |f| %>
|
|
||||||
<%= f.error_messages %>
|
|
||||||
|
|
||||||
<div id="maps">
|
|
||||||
<div id="map_a" class="mash-image">
|
|
||||||
<image src="<%= url_for @map_a %>.png" data-map-id="<%= @map_a.id %>" />
|
|
||||||
</div>
|
|
||||||
<div id="map_b" class="mash-image">
|
|
||||||
<image src="<%= url_for @map_b %>.png" data-map-id="<%= @map_b.id %>" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<%= f.hidden_field :map_a_id, :value => @map_a.id %>
|
|
||||||
<%= f.hidden_field :map_b_id, :value => @map_b.id %>
|
|
||||||
<%= f.hidden_field :winner_id, :value => -1 %>
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
<%= link_to 'Back', mashes_path %>
|
|
22
rails/map-mash/app/views/mashes/new.html.haml
Normal file
22
rails/map-mash/app/views/mashes/new.html.haml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
- content_for :head_js do
|
||||||
|
:javascript
|
||||||
|
$(function() {
|
||||||
|
$('.mash-image').click(function(elem) {
|
||||||
|
$('#mash_winner_id').val($(elem.target).attr('data-map-id'));
|
||||||
|
$('form[id^="edit_mash_"]').submit();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
%h1 Mash the Maps!
|
||||||
|
%p Choose your fave.
|
||||||
|
|
||||||
|
- form_for(@mash) do |f|
|
||||||
|
= f.error_messages
|
||||||
|
|
||||||
|
#maps
|
||||||
|
#map_a.mash-image
|
||||||
|
%image{:src => "#{url_for @mash.map_a}.png", :'data-map-id' => @mash.map_a.id}
|
||||||
|
#map_b.mash-image
|
||||||
|
%image{:src => "#{url_for @mash.map_b}.png", :'data-map-id' => @mash.map_b.id}
|
||||||
|
|
||||||
|
= f.hidden_field :winner_id, :value => '?'
|
@ -82,14 +82,14 @@ class MashTournamentBuilder
|
|||||||
previous_winners = previous.mashes.collect(&:winner_id)
|
previous_winners = previous.mashes.collect(&:winner_id)
|
||||||
pool = @map_model.all(
|
pool = @map_model.all(
|
||||||
:order => 'RANDOM()',
|
:order => 'RANDOM()',
|
||||||
:conditions => ['id in ?', previous_winners]
|
:conditions => {:id => previous_winners}
|
||||||
)
|
)
|
||||||
|
|
||||||
filled_in = []
|
filled_in = []
|
||||||
|
|
||||||
round.mashes.each do |mash|
|
round.mashes.each do |mash|
|
||||||
mash.map_a = pool.pop.id
|
mash.map_a = pool.pop
|
||||||
mash.map_b = pool.pop.id
|
mash.map_b = pool.pop
|
||||||
mash.save!
|
mash.save!
|
||||||
|
|
||||||
filled_in << mash
|
filled_in << mash
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
require 'spec_helper'
|
|
||||||
|
|
||||||
describe "/mash_tournaments/show.html.erb" do
|
|
||||||
include MashTournamentsHelper
|
|
||||||
before(:each) do
|
|
||||||
assigns[:mash_tournament] = @mash_tournament = stub_model(MashTournament)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "renders attributes in <p>" do
|
|
||||||
render
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in New Issue
Block a user