lib/travian_bot/application.rb in travian_bot-0.1.2 vs lib/travian_bot/application.rb in travian_bot-0.2.0

- old
+ new

@@ -1,58 +1,68 @@ require 'selenium-webdriver' require 'chronic' -require 'yaml' +Dir.glob(File.join(File.dirname(__FILE__), '/application/*.rb')).sort.each { |lib| load lib } + + class TravianBot class Application class << self - + include Connection + include Queue + include Display + include Buildings + + # Is executed by the travinbot shell script. def run!(*arguments) - login - building_queue - close + h1('Welcome to your TravianBot') + @game = login + + current_building_queue + current_troop_movements + current_avaible_buildings + + @game.quit return 1 end private - - # Load the credentials from ENV['HOME']/.travian_bot - def get_credentials - credentials = YAML::load(File.open("#{ENV['HOME']}/.travian_bot")) - - [credentials['travian_bot']['url'], credentials['travian_bot']['usr'], credentials['travian_bot']['pwd']] + def current_avaible_buildings + h2 'Avaible buildings' + buildings = avaible_buildings(@game) + + buildings.each do |building| + puts building.to_s + end + new_line end - - # Login in to travian page - def login - url, user, password = get_credentials - @driver = Selenium::WebDriver.for :firefox - @driver.navigate.to(url) - name_input = @driver.find_element(:name, 'name') - password_input = @driver.find_element(:name, 'password') - login_button = @driver.find_element(:id, 's1') - - name_input.send_keys(user) - password_input.send_keys(password) - login_button.submit + + def current_building_queue + h2('Current building queue') + buildings = building_queue(@game) + + if buildings.empty? + text 'nothing building' + else + buildings.each do |building| + puts building.to_s + end + new_line + end end - # Close the selenium connection - def close - @driver.quit - end - - # Check if building something - def building_queue - begin - building_pipe = @driver.find_elements(:xpath, "//table[@id='building_contract']/tbody/tr") - - building_pipe.each do |entry| - puts entry.text + def current_troop_movements + h2('Current troop movement') + troops = troop_movement(@game) + + if troops.empty? + text 'No troops movement' + else + troops.each do |troop| + puts troop.to_s end - rescue Selenium::WebDriver::Error::NoSuchElementError - puts 'Nothing in building queue' + new_line end end # Get the ending time of a string def wait_till(input)