mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-25 01:14:43 +00:00
Preliminary import/export feature
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
class TournamentsController < ApplicationController
|
class TournamentsController < ApplicationController
|
||||||
before_action :set_tournament, only: [:bout_sheets,: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_action :set_tournament, only: [:import,:export,:bout_sheets,: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_action :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_action :check_access_manage, only: [:import,:export,: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_action :check_access_destroy, only: [:destroy,:delegate,:remove_delegate]
|
before_action :check_access_destroy, only: [:destroy,:delegate,:remove_delegate]
|
||||||
before_action :check_tournament_errors, only: [:generate_matches]
|
before_action :check_tournament_errors, only: [:generate_matches]
|
||||||
before_action :check_for_matches, only: [:up_matches,:bracket,:all_brackets]
|
before_action :check_for_matches, only: [:up_matches,:bracket,:all_brackets]
|
||||||
@@ -9,6 +9,20 @@ class TournamentsController < ApplicationController
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def export
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def import
|
||||||
|
import_text = params[:tournament][:import_text]
|
||||||
|
respond_to do |format|
|
||||||
|
if WrestlingdevImporter.new(@tournament,import_text).import
|
||||||
|
format.html { redirect_to "/tournaments/#{@tournament.id}", notice: 'Import is on-going. This will take 1-5 minutes.' }
|
||||||
|
format.json { render action: 'show', status: :created, location: @tournament }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def swap
|
def swap
|
||||||
@wrestler = Wrestler.find(params[:wrestler][:originalId])
|
@wrestler = Wrestler.find(params[:wrestler][:originalId])
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ class Tournament < ActiveRecord::Base
|
|||||||
|
|
||||||
validates :date, :name, :tournament_type, :address, :director, :director_email , presence: true
|
validates :date, :name, :tournament_type, :address, :director, :director_email , presence: true
|
||||||
|
|
||||||
|
attr_accessor :import_text
|
||||||
|
|
||||||
def self.search(search)
|
def self.search(search)
|
||||||
where("date LIKE ? or name LIKE ?", "%#{search}%", "%#{search}%")
|
where("date LIKE ? or name LIKE ?", "%#{search}%", "%#{search}%")
|
||||||
end
|
end
|
||||||
|
|||||||
172
app/services/tournament_services/wrestlingdev_importer.rb
Normal file
172
app/services/tournament_services/wrestlingdev_importer.rb
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
class WrestlingdevImporter
|
||||||
|
def initialize( tournament, import_text )
|
||||||
|
@tournament = tournament
|
||||||
|
@import_text = import_text
|
||||||
|
@attribute_separator = ";;"
|
||||||
|
@model_separator = ",,"
|
||||||
|
end
|
||||||
|
|
||||||
|
def import
|
||||||
|
@tournament.curently_generating_matches = 1
|
||||||
|
@tournament.save
|
||||||
|
destroy_all
|
||||||
|
parse_text
|
||||||
|
@tournament.curently_generating_matches = nil
|
||||||
|
@tournament.save
|
||||||
|
end
|
||||||
|
if Rails.env.production?
|
||||||
|
handle_asynchronously :import
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy_all
|
||||||
|
@tournament.mats.reload.each do | mat |
|
||||||
|
mat.destroy
|
||||||
|
end
|
||||||
|
@tournament.matches.each do | match |
|
||||||
|
match.destroy
|
||||||
|
end
|
||||||
|
@tournament.schools.each do | school |
|
||||||
|
school.wrestlers.each do | wrestler |
|
||||||
|
wrestler.destroy
|
||||||
|
end
|
||||||
|
school.destroy
|
||||||
|
end
|
||||||
|
@tournament.weights.each do | weight |
|
||||||
|
weight.destroy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_text
|
||||||
|
@import_text.each_line do |line|
|
||||||
|
line_array = line.split(@model_separator,-1)
|
||||||
|
if line_array[0] == "--Tournament--"
|
||||||
|
line_array.shift
|
||||||
|
parse_tournament(line_array)
|
||||||
|
elsif line_array[0] == "--Schools--"
|
||||||
|
line_array.shift
|
||||||
|
parse_schools(line_array)
|
||||||
|
elsif line_array[0] == "--Weights--"
|
||||||
|
line_array.shift
|
||||||
|
parse_weights(line_array)
|
||||||
|
elsif line_array[0] == "--Mats--"
|
||||||
|
line_array.shift
|
||||||
|
parse_mats(line_array)
|
||||||
|
elsif line_array[0] == "--Wrestlers--"
|
||||||
|
line_array.shift
|
||||||
|
parse_wrestlers(line_array)
|
||||||
|
elsif line_array[0] == "--Matches--"
|
||||||
|
line_array.shift
|
||||||
|
parse_matches(line_array)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_tournament(tournament_attributes)
|
||||||
|
tournament_array = tournament_attributes[0].split(@attribute_separator,-1)
|
||||||
|
@tournament.name = tournament_array[0]
|
||||||
|
@tournament.address = tournament_array[1]
|
||||||
|
@tournament.director = tournament_array[2]
|
||||||
|
@tournament.director_email = tournament_array[3]
|
||||||
|
@tournament.tournament_type = tournament_array[4]
|
||||||
|
@tournament.weigh_in_ref = tournament_array[5]
|
||||||
|
@tournament.user_id = tournament_array[6]
|
||||||
|
#@tournament.curently_generating_matches = tournament_array[7]
|
||||||
|
@tournament.date = tournament_array[8]
|
||||||
|
@tournament.save
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_wrestlers(wrestlers)
|
||||||
|
wrestlers.each do | wrestler |
|
||||||
|
wrestler_array = wrestler.split(@attribute_separator,-1)
|
||||||
|
new_wrestler = Wrestler.new
|
||||||
|
new_wrestler.name = wrestler_array[0]
|
||||||
|
|
||||||
|
school_id = School.where("name = ? and tournament_id = ?",wrestler_array[1],@tournament.id).first.id
|
||||||
|
weight_id = Weight.where("max = ? and tournament_id = ?",wrestler_array[2],@tournament.id).first.id
|
||||||
|
new_wrestler.school_id = school_id
|
||||||
|
new_wrestler.weight_id = weight_id
|
||||||
|
|
||||||
|
new_wrestler.bracket_line = wrestler_array[3]
|
||||||
|
new_wrestler.original_seed = wrestler_array[4]
|
||||||
|
new_wrestler.season_win = wrestler_array[5]
|
||||||
|
new_wrestler.season_loss = wrestler_array[6]
|
||||||
|
new_wrestler.criteria = wrestler_array[7]
|
||||||
|
new_wrestler.extra = wrestler_array[8]
|
||||||
|
new_wrestler.offical_weight = wrestler_array[9]
|
||||||
|
new_wrestler.pool = wrestler_array[10]
|
||||||
|
new_wrestler.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_matches(matches)
|
||||||
|
matches.each do | match |
|
||||||
|
@tournament.reload
|
||||||
|
@tournament.mats.reload
|
||||||
|
match_array = match.split(@attribute_separator,-1)
|
||||||
|
new_match = Match.new
|
||||||
|
weight_id = Weight.where("max = ? and tournament_id = ?",match_array[10],@tournament.id).first.id
|
||||||
|
if match_array[0].size > 0
|
||||||
|
w1_id = Wrestler.where("name = ? and weight_id = ?",match_array[0],weight_id).first.id
|
||||||
|
end
|
||||||
|
if match_array[1].size > 0
|
||||||
|
w2_id = Wrestler.where("name = ? and weight_id = ?",match_array[1],weight_id).first.id
|
||||||
|
end
|
||||||
|
if match_array[4].size > 0
|
||||||
|
winner_id = Wrestler.where("name = ? and weight_id = ?",match_array[4],weight_id).first.id
|
||||||
|
end
|
||||||
|
if match_array[15].size > 0
|
||||||
|
# mat_id = Mat.where("name = ? and tournament_id = ?",match_array[15],@tournament.id).first.id
|
||||||
|
end
|
||||||
|
new_match.w1 = w1_id if match_array[0].size > 0
|
||||||
|
new_match.w2 = w2_id if match_array[1].size > 0
|
||||||
|
new_match.winner_id = winner_id if match_array[4].size > 0
|
||||||
|
new_match.w1_stat = match_array[2]
|
||||||
|
new_match.w2_stat = match_array[3]
|
||||||
|
new_match.win_type = match_array[5]
|
||||||
|
new_match.score = match_array[6]
|
||||||
|
new_match.tournament_id = @tournament.id
|
||||||
|
new_match.round = match_array[7]
|
||||||
|
new_match. finished = match_array[8]
|
||||||
|
new_match.bout_number = match_array[9]
|
||||||
|
new_match.weight_id = weight_id
|
||||||
|
new_match.bracket_position = match_array[11]
|
||||||
|
new_match.bracket_position_number = match_array[12]
|
||||||
|
new_match.loser1_name = match_array[13]
|
||||||
|
new_match.loser2_name = match_array[14]
|
||||||
|
# new_match.mat_id = mat_id if match_array[15].size > 0
|
||||||
|
new_match.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_schools(schools)
|
||||||
|
schools.each do | school |
|
||||||
|
school_array = school.split(@attribute_separator,-1)
|
||||||
|
new_school = School.new
|
||||||
|
new_school.tournament_id = @tournament.id
|
||||||
|
new_school.name = school_array[0]
|
||||||
|
new_school.score = school_array[1]
|
||||||
|
new_school.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_weights(weights)
|
||||||
|
weights.each do | weight |
|
||||||
|
weight_array = weight.split(@attribute_separator,-1)
|
||||||
|
new_weight = Weight.new
|
||||||
|
new_weight.tournament_id = @tournament.id
|
||||||
|
new_weight.max = weight_array[0]
|
||||||
|
new_weight.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_mats(mats)
|
||||||
|
mats.each do | mat |
|
||||||
|
mat_array = mat.split(@attribute_separator,-1)
|
||||||
|
new_mat = Mat.new
|
||||||
|
new_mat.tournament_id = @tournament.id
|
||||||
|
new_mat.name = mat_array[0]
|
||||||
|
new_mat.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
@@ -40,6 +40,7 @@
|
|||||||
<li><%= link_to "Create High School Weights (106-285)" , "/tournaments/#{@tournament.id}/create_custom_weights?customValue=hs",data: { confirm: 'Are you sure? This will delete all current weights.' } %></li>
|
<li><%= link_to "Create High School Weights (106-285)" , "/tournaments/#{@tournament.id}/create_custom_weights?customValue=hs",data: { confirm: 'Are you sure? This will delete all current weights.' } %></li>
|
||||||
<li><strong>Tournament Actions</strong></li>
|
<li><strong>Tournament Actions</strong></li>
|
||||||
<li><%= link_to "Generate Brackets" , "/tournaments/#{@tournament.id}/generate_matches", data: { confirm: 'Are you sure? This will delete all current matches.' } %></li>
|
<li><%= link_to "Generate Brackets" , "/tournaments/#{@tournament.id}/generate_matches", data: { confirm: 'Are you sure? This will delete all current matches.' } %></li>
|
||||||
|
<li><%= link_to "Export Data" , "/tournaments/#{@tournament.id}/export?print=true", target: :_blank %></li>
|
||||||
</ul>
|
</ul>
|
||||||
<% end %>
|
<% end %>
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
11
app/views/tournaments/_import_form.html.erb
Normal file
11
app/views/tournaments/_import_form.html.erb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<% if can? :manage, @tournament %>
|
||||||
|
<br><br>
|
||||||
|
<h3>Import Data</h3>
|
||||||
|
<%= form_for(Tournament.new, url: import_url(@tournament)) do |f| %>
|
||||||
|
<div class="field">
|
||||||
|
<%= f.label 'Import text' %><br>
|
||||||
|
<%= f.text_area :import_text, cols: "30", rows: "20" %>
|
||||||
|
</div>
|
||||||
|
<%= submit_tag "Import", :class=>"btn btn-success", data: { confirm: 'Are you sure? This will delete everything for the current tournament and restore it with the backup text pasted below.' }%>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
22
app/views/tournaments/export.html.erb
Normal file
22
app/views/tournaments/export.html.erb
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
--COPY AND PASTE ALL OF THIS TEXT INTO A FILE ON YOUR COMPUTER--<br>
|
||||||
|
--CLICK ANYWHERE ON THIS PAGE, PRESS CTRL + A (OR COMMAND + A ON MAC) TO SELECT ALL, THEN PRESS CTRL + C (OR COMMAND + C ON MAC) TO COPY--<br>
|
||||||
|
--THEN IN A DOCUMENT, RIGHT CLICK PASTE--<br>
|
||||||
|
--WE RECOMMEND USING NOTEPAD (NOT MICROSOFT WORD) TO SAVE THIS TEXT ON WINDOWS--<br>
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
--TO RESTORE PASTE THIS TEXT INTO THE FORM ON THE TOURNAMENT HOME PAGE--<br>
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
<% attribute_separator = ";;" %>
|
||||||
|
<% model_separator = ",," %>
|
||||||
|
--Tournament--<%= model_separator %><%= @tournament.name %><%= attribute_separator %><%= @tournament.address %><%= attribute_separator %><%= @tournament.director %><%= attribute_separator %><%= @tournament.director_email %><%= attribute_separator %><%= @tournament.tournament_type %><%= attribute_separator %><%= @tournament.weigh_in_ref %><%= attribute_separator %><%= @tournament.user_id %><%= attribute_separator %><%= @tournament.curently_generating_matches %><%= attribute_separator %><%= @tournament.date %>
|
||||||
|
<br>
|
||||||
|
--Schools--<% @tournament.schools.each do |school| %><%= model_separator %><%= school.name %><%= attribute_separator %><%= school.score %><% end %>
|
||||||
|
<br>
|
||||||
|
--Weights--<% @tournament.weights.each do |weight| %><%= model_separator %><%= weight.max %><% end %>
|
||||||
|
<br>
|
||||||
|
--Mats--<% @tournament.mats.each do |mat| %><%= model_separator %><%= mat.name %><% end %>
|
||||||
|
<br>
|
||||||
|
--Wrestlers--<% @tournament.wrestlers.each do |wrestler| %><%= model_separator %><%= wrestler.name %><%= attribute_separator %><%= wrestler.school.name %><%= attribute_separator %><%= wrestler.weight.max %><%= attribute_separator %><%= wrestler.bracket_line %><%= attribute_separator %><%= wrestler.original_seed %><%= attribute_separator %><%= wrestler.season_win %><%= attribute_separator %><%= wrestler.season_loss %><%= attribute_separator %><%= wrestler.criteria %><%= attribute_separator %><%= wrestler.extra %><%= attribute_separator %><%= wrestler.offical_weight %><%= attribute_separator %><%= wrestler.pool %><% end %>
|
||||||
|
<br>
|
||||||
|
--Matches--<% @tournament.matches.sort_by{|match| match.bout_number}.each do |match| %><%= model_separator %><%= Wrestler.where("id = ?",match.w1).first.name if match.w1 %><%= attribute_separator %><%= Wrestler.where("id = ?",match.w2).first.name if match.w2 %><%= attribute_separator %><%= match.w1_stat %><%= attribute_separator %><%= match.w2_stat %><%= attribute_separator %><%= Wrestler.where("id = ?",match.winner_id).first.name if match.winner_id %><%= attribute_separator %><%= match.win_type %><%= attribute_separator %><%= match.score %><%= attribute_separator %><%= match.round %><%= attribute_separator %><%= match.finished %><%= attribute_separator %><%= match.bout_number %><%= attribute_separator %><%= Weight.where("id = ?",match.weight_id).first.max if match.weight_id %><%= attribute_separator %><%= match.bracket_position %><%= attribute_separator %><%= match.bracket_position_number %><%= attribute_separator %><%= match.loser1_name %><%= attribute_separator %><%= match.loser2_name %><%= attribute_separator %><%= Mat.where("id = ?",match.mat_id).first.name if match.mat_id %><% end %>
|
||||||
@@ -130,7 +130,8 @@
|
|||||||
</table>
|
</table>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
<br>
|
||||||
|
<%= render 'import_form' %>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,8 @@ Wrestling::Application.routes.draw do
|
|||||||
delete 'tournaments/:id/:teampointadjust/remove_teampointadjust' => 'tournaments#remove_teampointadjust'
|
delete 'tournaments/:id/:teampointadjust/remove_teampointadjust' => 'tournaments#remove_teampointadjust'
|
||||||
get 'tournaments/:id/error' => 'tournaments#error'
|
get 'tournaments/:id/error' => 'tournaments#error'
|
||||||
post "/tournaments/:id/swap" => "tournaments#swap", :as => :swap_wrestlers
|
post "/tournaments/:id/swap" => "tournaments#swap", :as => :swap_wrestlers
|
||||||
|
get 'tournaments/:id/export' => "tournaments#export"
|
||||||
|
post "/tournaments/:id/import" => "tournaments#import", :as => :import
|
||||||
|
|
||||||
post 'weights/:id/re_gen' => 'weights#re_gen', :as => :regen_weight
|
post 'weights/:id/re_gen' => 'weights#re_gen', :as => :regen_weight
|
||||||
post "/wrestlers/update_pool" => "wrestlers#update_pool"
|
post "/wrestlers/update_pool" => "wrestlers#update_pool"
|
||||||
|
|||||||
Reference in New Issue
Block a user