mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-25 01:14:43 +00:00
Use json for tournament backups and imports
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
class WrestlingdevImporter
|
class WrestlingdevImporter
|
||||||
def initialize( tournament, import_text )
|
|
||||||
@tournament = tournament
|
##### Note, the json contains id's for each row in the tables as well as it's associations
|
||||||
@import_text = import_text
|
##### this ignores those ids and uses this tournament id and then looks up associations based on name
|
||||||
@attribute_separator = ";;"
|
##### and this tournament id
|
||||||
@model_separator = ",,"
|
def initialize(tournament, import_json)
|
||||||
|
@tournament = tournament
|
||||||
|
@import_data = JSON.parse(import_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
def import
|
def import
|
||||||
if Rails.env.production?
|
if Rails.env.production?
|
||||||
self.delay(:job_owner_id => @tournament.id, :job_owner_type => "Importing a backup").import_raw
|
self.delay(job_owner_id: @tournament.id, job_owner_type: "Importing a backup").import_raw
|
||||||
else
|
else
|
||||||
import_raw
|
import_raw
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -18,168 +20,89 @@ class WrestlingdevImporter
|
|||||||
@tournament.curently_generating_matches = 1
|
@tournament.curently_generating_matches = 1
|
||||||
@tournament.save
|
@tournament.save
|
||||||
destroy_all
|
destroy_all
|
||||||
parse_text
|
parse_data
|
||||||
@tournament.curently_generating_matches = nil
|
@tournament.curently_generating_matches = nil
|
||||||
@tournament.save
|
@tournament.save
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy_all
|
def destroy_all
|
||||||
@tournament.mats.reload.each do | mat |
|
@tournament.mats.destroy_all
|
||||||
mat.destroy
|
@tournament.matches.destroy_all
|
||||||
end
|
@tournament.schools.each do |school|
|
||||||
@tournament.matches.each do | match |
|
school.wrestlers.destroy_all
|
||||||
match.destroy
|
|
||||||
end
|
|
||||||
@tournament.schools.each do | school |
|
|
||||||
school.wrestlers.each do | wrestler |
|
|
||||||
wrestler.destroy
|
|
||||||
end
|
|
||||||
school.destroy
|
school.destroy
|
||||||
end
|
end
|
||||||
@tournament.weights.each do | weight |
|
@tournament.weights.destroy_all
|
||||||
weight.destroy
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_text
|
def parse_data
|
||||||
@import_text.each_line do |line|
|
parse_tournament(@import_data["tournament"]["attributes"])
|
||||||
line_array = line.split(@model_separator,-1)
|
parse_schools(@import_data["tournament"]["schools"])
|
||||||
if line_array[0] == "--Tournament--"
|
parse_weights(@import_data["tournament"]["weights"])
|
||||||
line_array.shift
|
parse_mats(@import_data["tournament"]["mats"])
|
||||||
parse_tournament(line_array)
|
parse_wrestlers(@import_data["tournament"]["wrestlers"])
|
||||||
elsif line_array[0] == "--Schools--"
|
parse_matches(@import_data["tournament"]["matches"])
|
||||||
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
|
end
|
||||||
|
|
||||||
def parse_tournament(tournament_attributes)
|
def parse_tournament(attributes)
|
||||||
tournament_array = tournament_attributes[0].split(@attribute_separator,-1)
|
attributes.except!("id")
|
||||||
@tournament.name = tournament_array[0]
|
@tournament.update(attributes)
|
||||||
@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 |
|
|
||||||
puts 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.pool_placement = wrestler_array[11]
|
|
||||||
new_wrestler.pool_placement_tiebreaker = wrestler_array[12]
|
|
||||||
new_wrestler.save
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_matches(matches)
|
|
||||||
matches.each do | match |
|
|
||||||
puts 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.overtime_type = match_array[16]
|
|
||||||
new_match.save
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_schools(schools)
|
def parse_schools(schools)
|
||||||
schools.each do | school |
|
schools.each do |school_attributes|
|
||||||
puts school
|
school_attributes.except!("id")
|
||||||
school_array = school.split(@attribute_separator,-1)
|
School.create(school_attributes.merge(tournament_id: @tournament.id))
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_weights(weights)
|
def parse_weights(weights)
|
||||||
weights.each do | weight |
|
weights.each do |weight_attributes|
|
||||||
puts weight
|
weight_attributes.except!("id")
|
||||||
weight_array = weight.split(@attribute_separator,-1)
|
Weight.create(weight_attributes.merge(tournament_id: @tournament.id))
|
||||||
new_weight = Weight.new
|
|
||||||
new_weight.tournament_id = @tournament.id
|
|
||||||
new_weight.max = weight_array[0]
|
|
||||||
new_weight.save
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_mats(mats)
|
def parse_mats(mats)
|
||||||
mats.each do | mat |
|
mats.each do |mat_attributes|
|
||||||
puts mat
|
mat_attributes.except!("id")
|
||||||
mat_array = mat.split(@attribute_separator,-1)
|
Mat.create(mat_attributes.merge(tournament_id: @tournament.id))
|
||||||
new_mat = Mat.new
|
|
||||||
new_mat.tournament_id = @tournament.id
|
|
||||||
new_mat.name = mat_array[0]
|
|
||||||
new_mat.save
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
def parse_wrestlers(wrestlers)
|
||||||
|
wrestlers.each do |wrestler_attributes|
|
||||||
|
school = School.find_by(name: wrestler_attributes["school"]["name"], tournament_id: @tournament.id)
|
||||||
|
weight = Weight.find_by(max: wrestler_attributes["weight"]["max"], tournament_id: @tournament.id)
|
||||||
|
wrestler_attributes.except!("id", "school", "weight")
|
||||||
|
Wrestler.create(wrestler_attributes.merge(
|
||||||
|
school_id: school&.id,
|
||||||
|
weight_id: weight&.id
|
||||||
|
))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_matches(matches)
|
||||||
|
matches.each do |match_attributes|
|
||||||
|
next unless match_attributes # Skip if match_attributes is nil
|
||||||
|
|
||||||
|
weight = Weight.find_by(max: match_attributes.dig("weight", "max"), tournament_id: @tournament.id)
|
||||||
|
mat = Mat.find_by(name: match_attributes.dig("mat", "name"), tournament_id: @tournament.id)
|
||||||
|
|
||||||
|
w1 = Wrestler.find_by(name: match_attributes["w1_name"], weight_id: weight&.id) if match_attributes["w1_name"]
|
||||||
|
w2 = Wrestler.find_by(name: match_attributes["w2_name"], weight_id: weight&.id) if match_attributes["w2_name"]
|
||||||
|
winner = Wrestler.find_by(name: match_attributes["winner_name"], weight_id: weight&.id) if match_attributes["winner_name"]
|
||||||
|
|
||||||
|
match_attributes.except!("id", "weight", "mat", "w1_name", "w2_name", "winner_name", "tournament_id")
|
||||||
|
|
||||||
|
Match.create(match_attributes.merge(
|
||||||
|
tournament_id: @tournament.id,
|
||||||
|
weight_id: weight&.id,
|
||||||
|
mat_id: mat&.id,
|
||||||
|
w1: w1&.id,
|
||||||
|
w2: w2&.id,
|
||||||
|
winner_id: winner&.id
|
||||||
|
))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|||||||
@@ -1,22 +1,23 @@
|
|||||||
--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>
|
"tournament": {
|
||||||
--THEN IN A DOCUMENT, RIGHT CLICK PASTE--<br>
|
"attributes": <%= @tournament.attributes.to_json %>,
|
||||||
--WE RECOMMEND USING NOTEPAD (NOT MICROSOFT WORD) TO SAVE THIS TEXT ON WINDOWS--<br>
|
"schools": <%= @tournament.schools.map(&:attributes).to_json %>,
|
||||||
<br>
|
"weights": <%= @tournament.weights.map(&:attributes).to_json %>,
|
||||||
<br>
|
"mats": <%= @tournament.mats.map(&:attributes).to_json %>,
|
||||||
--TO RESTORE PASTE THIS TEXT INTO THE FORM ON THE TOURNAMENT HOME PAGE--<br>
|
"wrestlers": <%= @tournament.wrestlers.map { |wrestler| wrestler.attributes.merge(
|
||||||
<br>
|
{
|
||||||
<br>
|
"school": wrestler.school&.attributes,
|
||||||
<% attribute_separator = ";;" %>
|
"weight": wrestler.weight&.attributes
|
||||||
<% 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 %>
|
) }.to_json %>,
|
||||||
<br>
|
"matches": <%= @tournament.matches.sort_by(&:bout_number).map { |match| match.attributes.merge(
|
||||||
--Schools--<% @tournament.schools.each do |school| %><%= model_separator %><%= school.name %><%= attribute_separator %><%= school.score %><% end %>
|
{
|
||||||
<br>
|
"w1_name": Wrestler.find_by(id: match.w1)&.name,
|
||||||
--Weights--<% @tournament.weights.each do |weight| %><%= model_separator %><%= weight.max %><% end %>
|
"w2_name": Wrestler.find_by(id: match.w2)&.name,
|
||||||
<br>
|
"winner_name": Wrestler.find_by(id: match.winner_id)&.name,
|
||||||
--Mats--<% @tournament.mats.each do |mat| %><%= model_separator %><%= mat.name %><% end %>
|
"weight": Weight.find_by(id: match.weight_id)&.attributes,
|
||||||
<br>
|
"mat": Mat.find_by(id: match.mat_id)&.attributes
|
||||||
--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 %><%= attribute_separator %><%= wrestler.pool_placement %><%= attribute_separator %><%= wrestler.pool_placement_tiebreaker %><% end %>
|
}
|
||||||
<br>
|
) }.to_json %>
|
||||||
--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 %><%= attribute_separator %><%= match.overtime_type %><% end %>
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user