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

Reducing queries for upcoming matches

This commit is contained in:
2015-03-16 08:06:08 -04:00
parent e8dd332bbc
commit 05c340eb94
4 changed files with 23 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
class Matchup
attr_accessor :w1, :w2, :round, :weight_id, :boutNumber
attr_accessor :w1, :w2, :round, :weight_id, :boutNumber, :w1_name, :w2_name
# def w1
# end
@@ -14,13 +14,8 @@ class Matchup
# end
def weight_max
@wrestler = Wrestler.find(self.w1)
@weight = Weight.find(@wrestler.weight_id)
@weight = Weight.find(self.weight_id)
return @weight.max
end
def w_name(id)
@wrestler = Wrestler.find(id)
return @wrestler.name
end
end

View File

@@ -1,27 +1,23 @@
class Pool
def onePool(wrestlers,weight_id,tournament,matches)
def onePool(wrestlers,weight,tournament,matches)
wrestlers.sort_by{|x|[x.original_seed]}.each do |w|
w.poolNumber = 1
w.save
end
matches = roundRobin(1,tournament,weight_id,matches)
matches = roundRobin(1,tournament,weight,matches,wrestlers)
return matches
end
def twoPools(wrestlers,weight_id,tournament,matches)
def twoPools(wrestlers,weight,tournament,matches)
pool = 1
wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w|
if w.original_seed == 3
w.poolNumber = 2
w.save
elsif w.original_seed == 4
w.poolNumber = 1
w.save
else
w.poolNumber = pool
w.save
end
if pool < 2
pool = pool + 1
@@ -29,30 +25,25 @@ class Pool
pool =1
end
end
matches = roundRobin(1,tournament,weight_id,matches)
matches = roundRobin(2,tournament,weight_id,matches)
matches = roundRobin(1,tournament,weight,matches,wrestlers)
matches = roundRobin(2,tournament,weight,matches,wrestlers)
return matches
end
def fourPools(wrestlers,weight_id,tournament,matches)
def fourPools(wrestlers,weight,tournament,matches)
@pool = 1
wrestlers.sort_by{|x|[x.original_seed]}.reverse.each do |w|
if w.original_seed == 3
w.poolNumber = 3
w.save
elsif w.original_seed == 4
w.poolNumber = 4
w.save
elsif w.original_seed == 1
w.poolNumber = 1
w.save
elsif w.original_seed == 2
w.poolNumber = 2
w.save
else
w.poolNumber = @pool
w.save
end
if @pool < 4
@pool = @pool + 1
@@ -60,15 +51,15 @@ class Pool
@pool =1
end
end
matches = roundRobin(1,tournament,weight_id,matches)
matches = roundRobin(2,tournament,weight_id,matches)
matches = roundRobin(3,tournament,weight_id,matches)
matches = roundRobin(4,tournament,weight_id,matches)
matches = roundRobin(1,tournament,weight,matches,wrestlers)
matches = roundRobin(2,tournament,weight,matches,wrestlers)
matches = roundRobin(3,tournament,weight,matches,wrestlers)
matches = roundRobin(4,tournament,weight,matches,wrestlers)
return matches
end
def roundRobin(pool,tournament_id,weight_id,matches)
@wrestlers = Wrestler.where(weight_id: weight_id, poolNumber: pool).to_a
def roundRobin(pool,tournament_id,weight,matches,wrestlers)
@wrestlers = wrestlers.to_a
@poolMatches = RoundRobinTournament.schedule(@wrestlers).reverse
@poolMatches.each_with_index do |b,index|
@bout = b.map
@@ -76,9 +67,11 @@ class Pool
if b[0] != nil and b[1] != nil
@match = Matchup.new
@match.w1 = b[0].id
@match.w1_name = b[0].name
@match.w2 = b[1].id
@match.w2_name = b[1].name
@match.weight_id = weight.id
@match.round = index + 1
matches << @match
end
end

View File

@@ -20,7 +20,7 @@ class Weight < ActiveRecord::Base
end
def pools
@wrestlers = Wrestler.where(weight_id: self.id)
@wrestlers = self.wrestlers
if @wrestlers.size <= 6
self.pools = 1
elsif (@wrestlers.size > 6) && (@wrestlers.size <= 10)
@@ -49,17 +49,17 @@ class Weight < ActiveRecord::Base
end
def generateMatchups(matches)
@wrestlers = Wrestler.where(weight_id: self.id)
@wrestlers = self.wrestlers
#@wrestlers.sort_by{|w| [w.original_seed]}
if self.pools == 1
@pool = Pool.new
@matches = @pool.onePool(@wrestlers,self.id,self.tournament_id,matches)
@matches = @pool.onePool(@wrestlers,self,self.tournament_id,matches)
elsif self.pools == 2
@pool = Pool.new
@matches = @pool.twoPools(@wrestlers,self.id,self.tournament_id,matches)
@matches = @pool.twoPools(@wrestlers,self,self.tournament_id,matches)
elsif self.pools == 4
@pool = Pool.new
@matches = @pool.fourPools(@wrestlers,self.id,self.tournament_id,matches)
@matches = @pool.fourPools(@wrestlers,self,self.tournament_id,matches)
end
return @matches
end

View File

@@ -20,7 +20,7 @@
<td>Round <%= m.round %></td>
<td><%= m.boutNumber %></td>
<td><%= m.weight_max %> lbs</td>
<td><%= m.w_name(m.w1) %> vs. <%= m.w_name(m.w2) %></td>
<td><%= m.w1_name %> vs. <%= m.w2_name %></td>
</tr>
<% end %>
</tbody>