Better leader last_action calculation
This commit is contained in:
parent
3c405344e3
commit
1bcb9d4bb1
3 changed files with 21 additions and 14 deletions
16
app.rb
16
app.rb
|
@ -7,20 +7,12 @@ get '/' do
|
|||
@raids = Raid.order('created_at DESC').page(params[:page])
|
||||
leaders = Leader.all
|
||||
last_update = leaders.first ? leaders.first.created_at : DateTime.new(0)
|
||||
rebuild_leaders if last_update + 300 < DateTime.now
|
||||
|
||||
@top20 = leaders.map { |l| { user: l.leader, date: nil } }
|
||||
|
||||
@top20.each_with_index do |hash, i|
|
||||
@raids.each do |raid|
|
||||
if raid.attacker == hash[:user] || raid.defender == hash[:user]
|
||||
@top20[i][:date] = raid.created_at
|
||||
break
|
||||
end
|
||||
end
|
||||
if last_update + 300 < DateTime.now
|
||||
rebuild_leaders
|
||||
end
|
||||
|
||||
@top20 = @top20.select { |h| h[:date] }.sort_by { |h| h[:date] } + @top20.reject { |h| h[:date] }
|
||||
leaders = Leader.all.includes(:last_attack, :last_defense)
|
||||
@top20 = leaders.select { |l| l.last_action }.sort_by { |l| l.last_action } + leaders.reject { |l| l.last_action }
|
||||
|
||||
erb :index
|
||||
end
|
||||
|
|
|
@ -1,3 +1,18 @@
|
|||
class Leader < ActiveRecord::Base
|
||||
has_one :last_attack, foreign_key: :attacker, primary_key: :leader, class_name: 'Raid'
|
||||
has_one :last_defense, foreign_key: :defender, primary_key: :leader, class_name: 'Raid'
|
||||
|
||||
def last_action
|
||||
if last_attack == nil && last_defense == nil
|
||||
@last_action = nil
|
||||
elsif last_defense == nil
|
||||
@last_action = last_attack.created_at
|
||||
elsif last_attack == nil
|
||||
@last_action = last_defense.created_at
|
||||
else
|
||||
@last_action = (last_attack.created_at > last_defense.created_at) ? last_attack.created_at : last_defense.created_at
|
||||
end
|
||||
|
||||
return @last_action
|
||||
end
|
||||
end
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
<div class="col-md-3">
|
||||
<div class="well well-lg text-center"><strong><%= Time.now.utc.strftime('%H:%M on %m-%d-%Y') %></strong></div>
|
||||
<ul class="list-group">
|
||||
<% @top20.each do |user| %>
|
||||
<li class="list-group-item"><%= user[:user] %><span class="pull-right"><small><%= user[:date].strftime('%H:%M on %m-%d-%Y') if user[:date] %></small></li>
|
||||
<% @top20.each do |leader| %>
|
||||
<li class="list-group-item"><%= leader.leader %><span class="pull-right"><small><%= leader.last_action.strftime('%H:%M on %m-%d-%Y') if leader.last_action %></small></li>
|
||||
<% end %>
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue