From 1a4991aef1e1a7214e4f2cb060c2cff3a5f8440e Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Tue, 28 Apr 2015 10:12:01 -0400 Subject: [PATCH] First iteration of stats --- app.rb | 15 +++++++++++++++ models/raid.rb | 20 ++++++++++++++++++++ views/bossfight.erb | 15 +++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 views/bossfight.erb diff --git a/app.rb b/app.rb index 179ec2d..6e5cf2a 100644 --- a/app.rb +++ b/app.rb @@ -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 diff --git a/models/raid.rb b/models/raid.rb index 5a08511..3ac8dd3 100644 --- a/models/raid.rb +++ b/models/raid.rb @@ -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 diff --git a/views/bossfight.erb b/views/bossfight.erb new file mode 100644 index 0000000..fc06190 --- /dev/null +++ b/views/bossfight.erb @@ -0,0 +1,15 @@ +<% if @first_negative %> + Raider who broke the walls: <%= @first_negative.attacker %>
+<%end%> + +<% if @soldiers_lost_to_date %> + Soldiers defeated: <%= @soldiers_lost_to_date %>
+<%end%> + +<% require 'pp' %> +<% if @soldiers_by_attacker %> +Participants: +<% @soldiers_by_attacker.each do |raid| %> + <% pp raid %> +<%end%> + <%end%>