1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-25 01:14:43 +00:00

Swap page is now working and also moved swaping logic from tournament model to its own class

This commit is contained in:
2016-01-27 14:13:49 +00:00
parent 96c7cd446c
commit a8b96c96e8
10 changed files with 80 additions and 73 deletions

View File

@@ -1,6 +1,6 @@
class TournamentsController < ApplicationController
before_action :set_tournament, only: [:weigh_in_sheet,:error,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:remove_delegate,:school_delegate,:delegate,:matches,:weigh_in,:weigh_in_weight,:create_custom_weights,:show,:edit,:update,:destroy,:up_matches,:no_matches,:team_scores,:brackets,:generate_matches,:bracket,:all_brackets]
before_filter :check_access_manage, only: [:weigh_in_sheet,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:school_delegate,:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches]
before_action :set_tournament, only: [:swap,:weigh_in_sheet,:error,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:remove_delegate,:school_delegate,:delegate,:matches,:weigh_in,:weigh_in_weight,:create_custom_weights,:show,:edit,:update,:destroy,:up_matches,:no_matches,:team_scores,:brackets,:generate_matches,:bracket,:all_brackets]
before_filter :check_access_manage, only: [:swap,:weigh_in_sheet,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:school_delegate,:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches]
before_filter :check_access_destroy, only: [:destroy,:delegate,:remove_delegate]
before_filter :check_tournament_errors, only: [:generate_matches]
before_filter :check_for_matches, only: [:up_matches,:bracket,:all_brackets]
@@ -9,6 +9,15 @@ class TournamentsController < ApplicationController
end
def swap
@wrestler = Wrestler.find(params[:wrestler][:originalId])
respond_to do |format|
if SwapWrestlers.new.swapWrestlers(params[:wrestler][:originalId], params[:wrestler][:swapId])
format.html { redirect_to @wrestler, notice: 'Wrestler was successfully swaped.' }
format.json { render action: 'show', status: :created, location: @wrestler }
end
end
end
def remove_teampointadjust
if params[:teampointadjust]
@@ -246,7 +255,7 @@ class TournamentsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def tournament_params
params.require(:tournament).permit(:name, :address, :director, :director_email, :tournament_type, :weigh_in_ref, :user_id, :date)
params.require(:tournament).permit(:name, :address, :director, :director_email, :tournament_type, :weigh_in_ref, :user_id, :date, :originalId, :swapId)
end
#Check for tournament owner

View File

@@ -1,16 +1,9 @@
class WrestlersController < ApplicationController
before_action :set_wrestler, only: [:show, :edit, :update, :destroy, :swap]
before_action :set_wrestler, only: [:show, :edit, :update, :destroy]
before_filter :check_access, only: [:new,:create,:update,:destroy,:edit]
def swap
respond_to do |format|
if @wrestler.tournament.swapWrestlers(@wrestler.id, params[:wrestler][:swapId])
format.html { redirect_to @wrestler, notice: 'Wrestler was successfully swaped.' }
format.json { render action: 'show', status: :created, location: @wrestler }
end
end
end
# GET /wrestlers/1
# GET /wrestlers/1.json

View File

@@ -7,14 +7,14 @@ class Match < ActiveRecord::Base
after_update do
if self.finished == 1 && self.winner_id != nil
if self.w1 && self.w2
if self.finished == 1 && self.winner_id != nil
if self.w1 && self.w2
wrestler1.touch
wrestler2.touch
end
advance_wrestlers
calcSchoolPoints
end
end
end
WIN_TYPES = ["Decision", "Major", "Tech Fall", "Pin", "Forfeit", "Injury Default", "Default", "DQ"]
@@ -136,14 +136,5 @@ class Match < ActiveRecord::Base
end
end
def swapWrestlers(wrestler1_id,wrestler2_id)
if self.w1 == wrestler1_id
self.w1 = wrestler2_id
self.save
elsif self.w2 == wrestler1_id
self.w2 = wrestler2_id
self.save
end
end
end

View File

@@ -0,0 +1,42 @@
class SwapWrestlers
attr_accessor :wrestler1_id, :wrestler2_id
def swapWrestlers(wrestler1_id,wrestler2_id)
w1 = Wrestler.find(wrestler1_id)
w2 = Wrestler.find(wrestler2_id)
#placeholder guy
w3 = Wrestler.new
w3.weight_id = w1.weight_id
w3.original_seed = w1.original_seed
w3.seed = w1.seed
swapWrestlerMatches(w1.allMatches,w1.id,w3.id)
#Swap wrestler 1 and wrestler 2
swapWrestlerMatches(w2.allMatches,w2.id,w1.id)
w1.seed = w2.seed
swapWrestlerMatches(w3.allMatches,w3.id,w2.id)
w2.seed = w3.seed
w1.save
w2.save
end
def swapWrestlerMatches(matchesToSwap,w1_id,w2_id)
matchesToSwap.each do |m|
if m.bracket_position == "Pool"
if m.w1 == w1_id
m.w1 = w2_id
m.save
elsif m.w2 == w1_id
m.w2 = w2_id
m.save
end
end
end
end
end

View File

@@ -9,7 +9,7 @@ class Tournament < ActiveRecord::Base
has_many :wrestlers, through: :weights
has_many :matches, dependent: :destroy
has_many :delegates, class_name: "TournamentDelegate"
validates :date, :name, :tournament_type, :address, :director, :director_email , presence: true
def self.search(search)
@@ -136,36 +136,6 @@ class Tournament < ActiveRecord::Base
end
end
def swapWrestlers(wrestler1_id,wrestler2_id)
w1 = Wrestler.find(wrestler1_id)
w2 = Wrestler.find(wrestler2_id)
#placeholder guy
w3 = Wrestler.new
w3.weight_id = w1.weight_id
w3.original_seed = w1.original_seed
w3.seed = w1.seed
swapWrestlerMatches(w1.allMatches,wrestler1_id,w3.id)
#Swap wrestler 1 and wrestler 2
swapWrestlerMatches(w2.allMatches,wrestler2_id,wrestler1_id)
w1.seed = w2.seed
swapWrestlerMatches(w3.allMatches,w3.id,wrestler2_id)
w2.seed = w3.seed
w1.save
w2.save
end
def swapWrestlerMatches(matchesToSwap,w1_id,w2_id)
matchesToSwap.each do |m|
if m.bracket_position == "Pool"
m.swapWrestlers(w1_id,w2_id)
end
end
end
end

View File

@@ -64,7 +64,7 @@ class Weight < ActiveRecord::Base
end
def swapWrestlers(wrestler1_id,wrestler2_id)
self.tournament.swapWrestlers(wrestler1_id,wrestler2_id)
SwapWrestlers.new.swapWrestlers(wrestler1_id,wrestler2_id)
end
def returnPoolNumber(wrestler)

View File

@@ -4,16 +4,16 @@ class Wrestler < ActiveRecord::Base
has_one :tournament, through: :weight
has_many :matches, through: :weight
has_many :deductedPoints, class_name: "Teampointadjust"
attr_accessor :poolNumber, :poolAdvancePoints, :swapId
attr_accessor :poolNumber, :poolAdvancePoints, :originalId, :swapId
validates :name, :weight_id, :school_id, presence: true
before_destroy do
self.tournament.destroyAllMatches
# self.tournament.destroyAllMatches
end
before_create do
self.tournament.destroyAllMatches
# self.tournament.destroyAllMatches
end

View File

@@ -16,14 +16,15 @@
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<% if @tournament %>
<%= f.hidden_field :school_id, :value => @school.id %>
<% else %>
<div class="field">
<%= f.label 'School' %><br>
<%= f.collection_select :school_id, @school.tournament.schools, :id, :name %>
</div>
<% if can? :manage, @wrestler.tournament %>
<% if @tournament %>
<%= f.hidden_field :school_id, :value => @school.id %>
<% else %>
<div class="field">
<%= f.label 'School' %><br>
<%= f.collection_select :school_id, @school.tournament.schools, :id, :name %>
</div>
<% end %>
<% end %>
<div class="field">
<%= f.label 'Weight Class' %><br>
@@ -56,8 +57,8 @@
<% if can? :manage, @wrestler.tournament %>
<br><br>
<h3>Swap Bracket Position</h3>
<%= form_for @wrestler, :url => url_for(:controller => 'wrestlers', :action => 'swap', :method => "post") do |f| %>
<%= form_for(Wrestler.new, url: swap_wrestlers_path(@wrestler.tournament)) do |f| %>
<%= f.hidden_field :originalId, :value => @wrestler.id %>
<div class="field">
<%= f.label 'Swap With' %><br>
<%= f.collection_select :swapId, @wrestler.weight.wrestlers, :id, :name %>

View File

@@ -15,7 +15,7 @@ Wrestling::Application.routes.draw do
post "/weights/:id" => "weights#show"
patch "/wrestlers/:id/swap" => "wrestlers#swap"
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
@@ -51,6 +51,7 @@ Wrestling::Application.routes.draw do
post 'tournaments/:id/teampointadjust' => 'tournaments#teampointadjust'
delete 'tournaments/:id/:teampointadjust/remove_teampointadjust' => 'tournaments#remove_teampointadjust'
get 'tournaments/:id/error' => 'tournaments#error'
post "/tournaments/:id/swap" => "tournaments#swap", :as => :swap_wrestlers
# Example of regular route:
# get 'products/:id' => 'catalog#view'

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160112152946) do
ActiveRecord::Schema.define(version: 20160126173424) do
create_table "delayed_jobs", force: :cascade do |t|
t.integer "priority", default: 0, null: false