First iteration of stats

This commit is contained in:
Andrew Tomaka 2015-04-28 10:12:01 -04:00
parent 478ce9ea37
commit 1a4991aef1
3 changed files with 50 additions and 0 deletions

15
app.rb
View file

@ -30,3 +30,18 @@ post '/' do
{ :message => 'Raid failed to save' }.to_json
end
end
get '/bossfight' do
puts "RAIDS FOR MAFIAMAN"
user_raids = Raid.for_user('mafiaman')
puts "FIRST NEGATIVE"
@first_negative = user_raids.first_negative
unless @first_negative == nil
puts "SOLDIERS LOST"
@soldiers_lost_to_date = user_raids.to_date(@first_negative.created_at).soldiers_lost
puts "SOLDIERS BY ATTACKER"
@soldiers_by_attacker = user_raids.to-date(@first_negative.created_at).by_attacker.soldiers_lost
end
erb :bossfight
end

View file

@ -8,4 +8,24 @@ class Raid < ActiveRecord::Base
value = value.to_s.gsub(/[$,]/, '').to_f
write_attribute(:soldiers, value)
end
def self.for_user(username)
where('defender = ?', username)
end
def self.first_negative
where('money < 0').order(:created_at).first
end
def self.to_date(date)
where('created_at < ?', date)
end
def self.soldiers_lost
sum(:soldiers)
end
def self.by_attacker
group(:attacker)
end
end

15
views/bossfight.erb Normal file
View file

@ -0,0 +1,15 @@
<% if @first_negative %>
Raider who broke the walls: <%= @first_negative.attacker %><br/>
<%end%>
<% if @soldiers_lost_to_date %>
Soldiers defeated: <%= @soldiers_lost_to_date %><br/>
<%end%>
<% require 'pp' %>
<% if @soldiers_by_attacker %>
Participants:
<% @soldiers_by_attacker.each do |raid| %>
<% pp raid %>
<%end%>
<%end%>