trugul-bot/app.rb

107 lines
2.2 KiB
Ruby
Raw Normal View History

2015-04-15 11:00:32 -04:00
require 'sinatra'
require 'sinatra/activerecord'
require 'json'
2015-04-23 16:09:59 -04:00
require 'mechanize'
require 'nokogiri'
2015-04-15 11:00:32 -04:00
require './environments'
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
mechanize = Mechanize.new
page = mechanize.get('http://trugul.com/highscores')
highscores = page.search "//table[@id='highscores_table']//tr[@class='clickable']/td[2]"
2015-04-23 16:32:57 -04:00
require 'pp'
2015-04-23 16:57:54 -04:00
@top20 = []
2015-04-23 16:09:59 -04:00
highscores.each do |td_user|
2015-04-23 16:34:36 -04:00
user = td_user.text.scan(/[A-Za-z0-9]+/)
2015-04-23 16:57:54 -04:00
@top20 << { user: user.first, date: nil }
2015-04-23 16:32:57 -04:00
end
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-23 16:32:57 -04:00
# get last attack for each
# order by 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
2015-04-23 14:46:51 -04:00
#HELPERS
2015-04-23 15:22:13 -04:00
def player_style(player)
2015-04-23 15:32:44 -04:00
"color:#{players[player]};font-weight: bold" if players.keys.include?(player)
2015-04-23 15:28:48 -04:00
end
def players
{
2015-04-23 15:32:24 -04:00
'greggnic' => '#3c763d',
'atomaka' => '#31708f',
'mafiaman' => '#a94442'
2015-04-23 15:28:48 -04:00
}
2015-04-23 15:22:13 -04:00
end
2015-04-23 14:46:51 -04:00
def readable_number(value)
2015-04-23 21:45:22 -04:00
negative = false
2015-04-23 22:18:31 -04:00
negative = true if value.to_f < 0
value.gsub!(/-/, '')
2015-04-23 14:46:51 -04:00
numbers.each do |number, symbol|
if value.to_f / number.to_f > 1
2015-04-23 15:09:39 -04:00
return sprintf('%.2f', value.to_f / number.to_f) + '<strong>' + symbol + '</strong>'
2015-04-23 14:46:51 -04:00
end
end
2015-04-23 15:09:39 -04:00
value = 0.0 unless value
2015-04-23 21:45:22 -04:00
value = value * -1 if negative
2015-04-23 15:09:39 -04:00
return sprintf('%.2f', value) + "_"
2015-04-23 14:46:51 -04:00
end
def numbers
{
'1000000000000000' => 'Q',
'1000000000000' => 'T',
'1000000000' => 'B',
'1000000' => 'M',
'1000' => 'K'
}
end
# MODEL
2015-04-15 11:00:32 -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
end