Limit leaderboard rebuild
This commit is contained in:
parent
cfce9d80ab
commit
ca58bc03f1
7 changed files with 105 additions and 73 deletions
83
app.rb
83
app.rb
|
@ -5,22 +5,24 @@ require 'mechanize'
|
||||||
require 'nokogiri'
|
require 'nokogiri'
|
||||||
require './environments'
|
require './environments'
|
||||||
|
|
||||||
|
require './models/raid'
|
||||||
|
require './models/leader'
|
||||||
|
require './helpers/raids_helper'
|
||||||
|
require './jobs/leaders'
|
||||||
|
|
||||||
set :public_folder, 'public'
|
set :public_folder, 'public'
|
||||||
|
|
||||||
# CONTROLLER
|
# CONTROLLER
|
||||||
get '/' do
|
get '/' do
|
||||||
@raids = Raid.all.reverse
|
@raids = Raid.all.reverse
|
||||||
|
|
||||||
mechanize = Mechanize.new
|
leaders = Leader.all
|
||||||
page = mechanize.get('http://trugul.com/highscores')
|
last_update = leaders.first ? leaders.first.created_at : DateTime.new(0)
|
||||||
highscores = page.search "//table[@id='highscores_table']//tr[@class='clickable']/td[2]"
|
pp last_update
|
||||||
|
pp DateTime.now
|
||||||
|
rebuild_leaders if last_update + 300 < DateTime.now
|
||||||
|
|
||||||
require 'pp'
|
@top20 = leaders.map { |l| { user: l.leader, date: nil } }
|
||||||
@top20 = []
|
|
||||||
highscores.each do |td_user|
|
|
||||||
user = td_user.text.scan(/[A-Za-z0-9]+/)
|
|
||||||
@top20 << { user: user.first, date: nil }
|
|
||||||
end
|
|
||||||
|
|
||||||
@top20.each_with_index do |hash, i|
|
@top20.each_with_index do |hash, i|
|
||||||
@raids.each do |raid|
|
@raids.each do |raid|
|
||||||
|
@ -33,9 +35,6 @@ get '/' do
|
||||||
|
|
||||||
@top20 = @top20.select { |h| h[:date] }.sort_by { |h| h[:date] } + @top20.reject { |h| h[:date] }
|
@top20 = @top20.select { |h| h[:date] }.sort_by { |h| h[:date] } + @top20.reject { |h| h[:date] }
|
||||||
|
|
||||||
# get last attack for each
|
|
||||||
# order by date
|
|
||||||
|
|
||||||
erb :index
|
erb :index
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -52,63 +51,3 @@ post '/' do
|
||||||
{ :message => 'Raid failed to save' }.to_json
|
{ :message => 'Raid failed to save' }.to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#HELPERS
|
|
||||||
def player_style(player)
|
|
||||||
"color:#{players[player]};font-weight: bold" if players.keys.include?(player)
|
|
||||||
end
|
|
||||||
|
|
||||||
def players
|
|
||||||
{
|
|
||||||
'greggnic' => '#3c763d',
|
|
||||||
'atomaka' => '#31708f',
|
|
||||||
'mafiaman' => '#a94442'
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def money_style(value)
|
|
||||||
value.to_f < 0 ? 'color:#a94442' : 'color:#3c763d'
|
|
||||||
end
|
|
||||||
|
|
||||||
def readable_number(value)
|
|
||||||
if value.to_f < 0
|
|
||||||
negative = true
|
|
||||||
value *= -1 if value < 0
|
|
||||||
else
|
|
||||||
negative = false
|
|
||||||
end
|
|
||||||
|
|
||||||
numbers.each do |number, symbol|
|
|
||||||
if value.to_f / number.to_f >= 1
|
|
||||||
new_value = negative ? '-' : ''
|
|
||||||
new_value += sprintf('%.2f', value.to_f / number.to_f) + '<strong>' + symbol + '</strong>'
|
|
||||||
return new_value
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
value = 0.0 unless value
|
|
||||||
return sprintf('%.2f', value) + " "
|
|
||||||
end
|
|
||||||
|
|
||||||
def numbers
|
|
||||||
{
|
|
||||||
'1000000000000000' => 'Q',
|
|
||||||
'1000000000000' => 'T',
|
|
||||||
'1000000000' => 'B',
|
|
||||||
'1000000' => 'M',
|
|
||||||
'1000' => 'K'
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
# MODEL
|
|
||||||
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
|
|
||||||
|
|
8
db/migrate/20150425172329_create_leaders.rb
Normal file
8
db/migrate/20150425172329_create_leaders.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
class CreateLeaders < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
create_table :leaders do |t|
|
||||||
|
t.string :leader
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,13 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20150415152327) do
|
ActiveRecord::Schema.define(version: 20150425172329) do
|
||||||
|
|
||||||
|
create_table "leaders", force: :cascade do |t|
|
||||||
|
t.string "leader"
|
||||||
|
t.datetime "created_at"
|
||||||
|
t.datetime "updated_at"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "raids", force: :cascade do |t|
|
create_table "raids", force: :cascade do |t|
|
||||||
t.string "attacker"
|
t.string "attacker"
|
||||||
|
|
45
helpers/raids_helper.rb
Normal file
45
helpers/raids_helper.rb
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
def player_style(player)
|
||||||
|
"color:#{players[player]};font-weight: bold" if players.keys.include?(player)
|
||||||
|
end
|
||||||
|
|
||||||
|
def players
|
||||||
|
{
|
||||||
|
'greggnic' => '#3c763d',
|
||||||
|
'atomaka' => '#31708f',
|
||||||
|
'mafiaman' => '#a94442'
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def money_style(value)
|
||||||
|
value.to_f < 0 ? 'color:#a94442' : 'color:#3c763d'
|
||||||
|
end
|
||||||
|
|
||||||
|
def readable_number(value)
|
||||||
|
if value.to_f < 0
|
||||||
|
negative = true
|
||||||
|
value *= -1 if value < 0
|
||||||
|
else
|
||||||
|
negative = false
|
||||||
|
end
|
||||||
|
|
||||||
|
numbers.each do |number, symbol|
|
||||||
|
if value.to_f / number.to_f >= 1
|
||||||
|
new_value = negative ? '-' : ''
|
||||||
|
new_value += sprintf('%.2f', value.to_f / number.to_f) + '<strong>' + symbol + '</strong>'
|
||||||
|
return new_value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
value = 0.0 unless value
|
||||||
|
return sprintf('%.2f', value) + " "
|
||||||
|
end
|
||||||
|
|
||||||
|
def numbers
|
||||||
|
{
|
||||||
|
'1000000000000000' => 'Q',
|
||||||
|
'1000000000000' => 'T',
|
||||||
|
'1000000000' => 'B',
|
||||||
|
'1000000' => 'M',
|
||||||
|
'1000' => 'K'
|
||||||
|
}
|
||||||
|
end
|
20
jobs/leaders.rb
Normal file
20
jobs/leaders.rb
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
require 'sinatra/activerecord'
|
||||||
|
require 'mechanize'
|
||||||
|
require 'nokogiri'
|
||||||
|
|
||||||
|
require './models/leader'
|
||||||
|
|
||||||
|
def rebuild_leaders
|
||||||
|
puts "rebuilding_leaders called"
|
||||||
|
Leader.destroy_all
|
||||||
|
|
||||||
|
mechanize = Mechanize.new
|
||||||
|
page = mechanize.get('http://trugul.com/highscores')
|
||||||
|
leaders = page.search "//table[@id='highscores_table']//tr[@class='clickable']/td[2]"
|
||||||
|
|
||||||
|
leaders.each do |td_user|
|
||||||
|
user = td_user.text.scan(/[A-Za-z0-9]+/).first
|
||||||
|
|
||||||
|
Leader.new(leader: user).save
|
||||||
|
end
|
||||||
|
end
|
3
models/leader.rb
Normal file
3
models/leader.rb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
class Leader < ActiveRecord::Base
|
||||||
|
|
||||||
|
end
|
11
models/raid.rb
Normal file
11
models/raid.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
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
|
Loading…
Reference in a new issue