trugul-bot/models/raid.rb

44 lines
1 KiB
Ruby
Raw Normal View History

2015-04-25 13:17:47 -04:00
class Raid < ActiveRecord::Base
def money=(value)
value = value.to_s.gsub(/[$,]/, '').to_f
write_attribute(:money, value)
end
def soldiers=(value)
value = value.to_s.gsub(/[$,]/, '').to_f
write_attribute(:soldiers, value)
end
2015-04-28 10:12:01 -04:00
2015-04-28 11:51:18 -04:00
def self.defenses_for_user(username)
2015-04-28 10:12:01 -04:00
where('defender = ?', username)
end
2015-04-28 11:51:18 -04:00
def self.attacks_for_user(username)
where('attacker = ?', username)
end
2015-04-28 10:12:01 -04:00
def self.first_negative
where('money < 0').order(:created_at).first
end
2015-04-28 10:19:46 -04:00
def self.soldiers_lost_to_date(date)
2015-04-28 11:02:17 -04:00
where('created_at <= ?', date).sum(:soldiers)
2015-04-28 10:19:46 -04:00
end
2015-04-28 10:26:23 -04:00
2015-04-28 11:36:17 -04:00
def self.contributor_attacks(date)
where('created_at <= ?', date).group(:attacker).count(:id)
end
2015-04-28 11:51:18 -04:00
def self.contributor_defenses(date)
where('created_at <= ?', date).group(:defender).count(:id)
end
2015-04-28 11:36:17 -04:00
def self.contributor_soldiers(date)
2015-04-28 11:02:17 -04:00
where('created_at <= ?', date).group(:attacker).sum(:soldiers)
2015-04-28 10:26:23 -04:00
end
2015-04-28 11:51:18 -04:00
def self.contributor_defenders(date)
where('created_at <= ?', date).group(:defender).sum(:soldiers)
end
2015-04-25 13:17:47 -04:00
end