trugul-bot/app.rb

50 lines
1.1 KiB
Ruby
Raw Normal View History

2015-04-15 11:00:32 -04:00
require 'sinatra'
require 'sinatra/activerecord'
require 'json'
require './environments'
2015-04-25 13:17:47 -04:00
require './models/raid'
require './models/leader'
require './helpers/raids_helper'
require './jobs/leaders'
2015-04-15 11:00:32 -04:00
set :public_folder, 'public'
2015-04-23 14:46:51 -04:00
# CONTROLLER
2015-04-15 11:00:32 -04:00
get '/' do
2015-04-23 14:46:51 -04:00
@raids = Raid.all.reverse
2015-04-23 16:09:59 -04:00
2015-04-25 13:17:47 -04:00
leaders = Leader.all
last_update = leaders.first ? leaders.first.created_at : DateTime.new(0)
rebuild_leaders if last_update + 300 < DateTime.now
2015-04-23 16:32:57 -04:00
2015-04-25 13:17:47 -04:00
@top20 = leaders.map { |l| { user: l.leader, date: nil } }
2015-04-23 16:32:57 -04:00
2015-04-23 16:57:54 -04:00
@top20.each_with_index do |hash, i|
2015-04-23 16:32:57 -04:00
@raids.each do |raid|
2015-04-23 16:57:54 -04:00
if raid.attacker == hash[:user] || raid.defender == hash[:user]
@top20[i][:date] = raid.created_at
2015-04-23 16:32:57 -04:00
break
end
end
2015-04-23 16:09:59 -04:00
end
2015-04-23 16:57:54 -04:00
@top20 = @top20.select { |h| h[:date] }.sort_by { |h| h[:date] } + @top20.reject { |h| h[:date] }
2015-04-15 11:00:32 -04:00
erb :index
end
post '/' do
response['Access-Control-Allow-Origin'] = 'http://trugul.com'
@raid = Raid.new(params[:raid])
if @raid.save
content_type :json
{ :message => 'Raid saved' }.to_json
else
content_type :json
{ :message => 'Raid failed to save' }.to_json
end
end