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

Cut the runtime by queueing the changes in memory and deferring database writes

Made some unspecified behavior explicit
This commit is contained in:
RJ Osborne
2015-05-24 23:04:22 -04:00
parent 98879c16b9
commit 091b7c4181
5 changed files with 24 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
class Pool class Pool
def initialize(weight) def initialize(weight)
@weight = weight @weight = weight
@tournament = @weight.tournament
@pool = 1 @pool = 1
end end
@@ -23,7 +24,7 @@ class Pool
bouts = b.map bouts = b.map
bouts.each do |bout| bouts.each do |bout|
if bout[0] != nil and bout[1] != nil if bout[0] != nil and bout[1] != nil
match = Match.new( match = @tournament.matches.create(
w1: bout[0].id, w1: bout[0].id,
w2: bout[1].id, w2: bout[1].id,
weight_id: @weight.id, weight_id: @weight.id,

View File

@@ -16,13 +16,11 @@ class Tournament < ActiveRecord::Base
def createCustomWeights(value) def createCustomWeights(value)
self.weights.destroy_all self.weights.destroy_all
if value == 'hs' if value == 'hs'
@weights = [106,113,120,132,138,145,152,160,170,182,195,220,285] Weight::HS_WEIGHT_CLASSES.each do |w|
end self.weights.create(max: w)
@weights.each do |w| end
newWeight = Weight.new else
newWeight.max = w raise "Unspecified behavior"
newWeight.tournament_id = self.id
newWeight.save
end end
end end

View File

@@ -42,6 +42,7 @@ class Tournamentmatchgen
end end
def saveMatches def saveMatches
@tournament.save!
@matches.each do |m| @matches.each do |m|
m.tournament_id = @tournament.id m.tournament_id = @tournament.id
m.save m.save

View File

@@ -4,6 +4,8 @@ class Weight < ActiveRecord::Base
attr_accessor :pools attr_accessor :pools
HS_WEIGHT_CLASSES = [106,113,120,132,138,145,152,160,170,182,195,220,285]
before_save do before_save do
self.tournament.destroyAllMatches self.tournament.destroyAllMatches
end end
@@ -43,7 +45,7 @@ class Weight < ActiveRecord::Base
end end
def twoPoolNumbers(wrestlers) def twoPoolNumbers(wrestlers)
pool = 1 pool = 1
wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w| wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w|
if w.original_seed == 1 if w.original_seed == 1
@@ -65,8 +67,8 @@ class Weight < ActiveRecord::Base
end end
return wrestlers return wrestlers
end end
def fourPoolNumbers(wrestlers) def fourPoolNumbers(wrestlers)
pool = 1 pool = 1
wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w| wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w|
if w.original_seed == 1 if w.original_seed == 1
@@ -98,20 +100,20 @@ class Weight < ActiveRecord::Base
if self.wrestlers.size > 6 && self.wrestlers.size <= 8 if self.wrestlers.size > 6 && self.wrestlers.size <= 8
return "twoPoolsToSemi" return "twoPoolsToSemi"
elsif self.wrestlers.size > 8 && self.wrestlers.size <= 10 elsif self.wrestlers.size > 8 && self.wrestlers.size <= 10
return "twoPoolsToFinal" return "twoPoolsToFinal"
elsif self.wrestlers.size == 11 || self.wrestlers.size == 12 elsif self.wrestlers.size == 11 || self.wrestlers.size == 12
return "fourPoolsToQuarter" return "fourPoolsToQuarter"
elsif self.wrestlers.size > 12 && self.wrestlers.size <= 16 elsif self.wrestlers.size > 12 && self.wrestlers.size <= 16
return "fourPoolsToSemi" return "fourPoolsToSemi"
end end
end end
def poolRounds(matches) def poolRounds(matches)
@matchups = matches.select{|m| m.weight_id == self.id} @matchups = matches.select{|m| m.weight_id == self.id}
@poolMatches = @matchups.select{|m| m.bracket_position == nil} @poolMatches = @matchups.select{|m| m.bracket_position == nil}
return @poolMatches.sort_by{|m| m.round}.last.round return @poolMatches.sort_by{|m| m.round}.last.round
end end
def totalRounds(matches) def totalRounds(matches)
@matchups = matches.select{|m| m.weight_id == self.id} @matchups = matches.select{|m| m.weight_id == self.id}
@lastRound = matches.sort_by{|m| m.round}.last.round @lastRound = matches.sort_by{|m| m.round}.last.round
@@ -125,5 +127,5 @@ class Weight < ActiveRecord::Base
end end
return @count return @count
end end
end end

View File

@@ -82,13 +82,19 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest
refute_nil @tournament refute_nil @tournament
end end
test "tournament can be set to high school weight classes" do
@tournament.weights.destroy_all
@tournament.createCustomWeights("hs")
assert_equal Weight::HS_WEIGHT_CLASSES.size, @tournament.weights.size
end
test "tests bout_number matches round" do test "tests bout_number matches round" do
@matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first @matchup_to_test = @genMatchups.select{|m| m.bout_number == 4000}.first
assert_equal 4, @matchup_to_test.round assert_equal 4, @matchup_to_test.round
end end
test "tests bout_numbers are generated with smallest weight first regardless of id" do test "tests bout_numbers are generated with smallest weight first regardless of id" do
@weight = @tournament.weights.map.sort_by{|x|[x.max]}.first @weight = @tournament.weights.order(:max).limit(1).first
@matchup = @genMatchups.select{|m| m.bout_number == 1000}.first @matchup = @genMatchups.select{|m| m.bout_number == 1000}.first
assert_equal @weight.max, @matchup.weight_max assert_equal @weight.max, @matchup.weight_max
end end