2012年12月5日 星期三

ruby for gladiatus in Ubuntu

~$ sudo apt-get install ruby
~$ sudo apt-get install rubygems
~$ sudo gem install selenium-webdriver (換瀏覽器版本時,重新執行此指令)
~$ sudo gem install oj  #for 不要出現 [WARNING] MultiJson is using the default adapter (ok_json). We recommend loading a different JSON library to improve performance.

~$ vi attack.rb
~$ vi glad-methods.rb

attack.rb
#!/usr/bin/ruby

# -*- coding: utf-8 -*-

require 'rubygems'
require 'selenium-webdriver'
load 'glad-methods.rb'

if    ARGV[0] == "1"
  puts "login1"
  login1
elsif ARGV[0] == "5"
  puts "login5"
  login5
elsif ARGV[0] == "7"
  puts "login7"
  login7
end

def attack
 
  #為了防備中午12:00~13:00公司internet忙碌,造成程式response time太久而當機
  #此時程式如果起來,就sleep 0.5~1 小時
 
  currentTime = Time::now
  if currentTime.hour == 12
    #12:30前程式起來,則延遲1小時才動作
    if currentTime.min <= 30
      sleep 3600
    #12:30後程式起來,則延遲0.5小時才動作
    else
      sleep 1800
    end
  end
 
  getLevel
  show_hp
  get_time_of_next_attack 
 
  @attack_number = @driver.find_element(:id, "expeditionpoints_value").text.match(/\d+/).to_s.to_i
  @boundroumi_number = @driver.find_element(:id, "dungeonpoints_value").text.match(/\d+/).to_s.to_i
 
  puts "expedition points : #{@attack_number}"
  puts "dungeon points : #{@boundroumi_number}"
  #puts "arena time : #{@arena_time}"
  #puts "cosmo arena time : #{@cosmo_arena_time}"

  if @attack_number > 1 and @exp_time == 30 and @hp > 10
    exp_attack
  end
 
  if @boundroumi_number > 1 and @mpou_time == 30
    mpou_attack
  end

  #if @arena_time == 30 and @hp > 10
  #  arena_attack
  #end

  if @cosmo_arena_time == 30
    cosmoarena_attack
  end

#  puts @attack_number
#  puts @exp_time
  if @hp <= 10 && (@boundroumi_number > 1 || @attack_number > 1)
    puts "Needs energy (HP), give him something to eat or I will let him work-fight in dungeon to replace some energy"
    work_time_left
    if @left_time > 0
      puts "still working on, wait for #{@left_time}"
      sleep @left_time
    else
      max_job_per_hour
    end
    @total_time = 0
  end

  if @boundroumi_number == 1 && @attack_number == 1 ||  #level >= 10
     @mpou_time-30 == 36000 && @attack_number == 1      #level <  10
    work_time_left
    if @left_time > 0
      puts "still working on, wait for #{@left_time}"
      sleep @left_time
    else
      gladjob_eight
      #max_job_per_hour
      #puts "max_job_per_hour"
    end
    @total_time = 0
  end

  #`killall -9 firefox`
  puts "#{@total_time} seconds left to login again..."
  sleep @total_time
end

i = 0

loop do
  attack
end

glad-methods.rb
# -*- coding: utf-8 -*- @total_time = 0 def login @driver = Selenium::WebDriver.for :firefox @driver.get "http://s1.gladiatus.gr/game/" @driver.find_element(:id, "login_username").click @driver.find_element(:id, "login_username").clear @driver.find_element(:id, "login_username").send_keys "xxx" @driver.find_element(:id, "login_password").clear @driver.find_element(:id, "login_password").send_keys "yyy" @driver.find_element(:id, "loginsubmit").click end def login1 @driver = Selenium::WebDriver.for :firefox @wait = Selenium::WebDriver::Wait.new(:timeout => 10) @driver.get "http://s1.gladiatus.tw/game/" @driver.find_element(:id, "login_username").click @driver.find_element(:id, "login_username").clear @driver.find_element(:id, "login_username").send_keys "xxx" @driver.find_element(:id, "login_password").clear @driver.find_element(:id, "login_password").send_keys "yyy" @driver.find_element(:id, "loginsubmit").click end def login5 @driver = Selenium::WebDriver.for :firefox @wait = Selenium::WebDriver::Wait.new(:timeout => 10) @driver.get "http://s5.gladiatus.tw/game/" @driver.find_element(:id, "login_username").click @driver.find_element(:id, "login_username").clear @driver.find_element(:id, "login_username").send_keys "xxx" @driver.find_element(:id, "login_password").clear @driver.find_element(:id, "login_password").send_keys "yyy" @driver.find_element(:id, "loginsubmit").click end def login7 @driver = Selenium::WebDriver.for :firefox @wait = Selenium::WebDriver::Wait.new(:timeout => 10) @driver.get "http://s7.gladiatus.tw/game/" @driver.find_element(:id, "login_username").click @driver.find_element(:id, "login_username").clear @driver.find_element(:id, "login_username").send_keys "xxx" @driver.find_element(:id, "login_password").clear @driver.find_element(:id, "login_password").send_keys "yyy" @driver.find_element(:id, "loginsubmit").click end def get_time_of_next_attack #@driver.find_element(:link, "神諭").click #@driver.find_element(:link, "概覽").click # From Attacks cooldown = @wait.until {@driver.find_elements(:id, "cooldown_bar_text_expedition")} time = cooldown.first.text hours = time.match(/\d+/) minutes = time.match(/:\d+/)#.gsub(":", "") seconds = time.match(/:\d+:\d+/) hours = hours.to_s.to_i minutes = minutes.to_s.slice(1..3).to_i seconds = seconds.to_s.slice(4..5).to_i @exp_time = 0 @exp_time = hours*60*60 + minutes * 60 + seconds + 30 if time =~ /-/ work_time_left else puts "Expedition: #{@exp_time-30} second(s) " end # From Mpountroumi cooldown = @wait.until {@driver.find_elements(:id, "cooldown_bar_text_dungeon")} time = cooldown.first.text hours = time.match(/\d+/) minutes = time.match(/:\d+/)#.gsub(":", "") seconds = time.match(/:\d+:\d+/) hours = hours.to_s.to_i minutes = minutes.to_s.slice(1..3).to_i seconds = seconds.to_s.slice(4..5).to_i @mpou_time = 0 @mpou_time = hours*60*60 + minutes * 60 + seconds + 30 if time =~ /-/ work_time_left else puts "Mpountroumi: #{@mpou_time-30} second(s) " end # From Arena #cooldown = @wait.until {@driver.find_elements(:id, "cooldown_bar_text_arena")} #time = cooldown.first.text #hours = time.match(/\d+/) #minutes = time.match(/:\d+/)#.gsub(":", "") #seconds = time.match(/:\d+:\d+/) #hours = hours.to_s.to_i #minutes = minutes.to_s.slice(1..3).to_i #seconds = seconds.to_s.slice(4..5).to_i #@arena_time = 0 #@arena_time = hours*60*60 + minutes * 60 + seconds + 30 #if time =~ /-/ # work_time_left #else # puts "Arena: #{@arena_time-30} second(s)" #end # From Cosmoarena cooldown = @wait.until {@driver.find_elements(:id, "cooldown_bar_text_ct")} time = cooldown.first.text hours = time.match(/\d+/) minutes = time.match(/:\d+/)#.gsub(":", "") seconds = time.match(/:\d+:\d+/) hours = hours.to_s.to_i minutes = minutes.to_s.slice(1..3).to_i seconds = seconds.to_s.slice(4..5).to_i @cosmo_arena_time = 0 @cosmo_arena_time = hours*60*60 + minutes * 60 + seconds + 30 if time =~ /-/ work_time_left else puts "Cosmo Arena: #{@cosmo_arena_time-30} second(s) " end @times = [] @times << @mpou_time @times << @exp_time #@times << @arena_time @times << @cosmo_arena_time @times.sort! @total_time = @times[0] #if @mpou_time < @exp_time # @total_time = @mpou_time #else # @total_time = @exp_time #end #puts "ATTACK!" if @total_time == 30 end def show_hp @hp = @wait.until {@driver.find_element(:id, "header_values_hp_percent").text} @hp = @hp.to_s.gsub("%","").to_i puts "HP: #{@hp}%" if @hp < 10 puts "eat" #eat end end def getLevel level = @wait.until {@driver.find_elements(:class, "headervalue_small")} @charLevel = level[3].text.to_i #array index start from 0, level is the 4th, so the index is 3 puts "" puts "Level = #{@charLevel}" end def exp_attack puts "遠征 attack" if ARGV[1] != nil target = ARGV[1] else target = 2 end @wait.until {@driver.find_element(:class, "cooldown_bar_link")}.click #@wait.until {@driver.find_element(:xpath, "(//button[@value=''])[#{target}]")}.click button = @wait.until {@driver.find_element(:xpath, "(//button[@value=''])[#{target}]")} while button == nil puts "loop for finding 遠征 button" button = @wait.until {@driver.find_element(:xpath, "(//button[@value=''])[#{target}]")} end button.click end def mpou_attack puts "地下城 attack" @wait.until {@driver.find_element(:xpath, "(//a[@class='cooldown_bar_link'])[2]")}.click start_boundroumi = @wait.until {@driver.find_elements(:class, "button1")} #start_boundroumi.first.click if start_boundroumi.first.attribute('value') == "Κανονικό" while start_boundroumi.first == nil start_boundroumi = @driver.find_elements(:class, "button1") puts "loop for fingding 地下城 button1" end start_boundroumi.first.click if start_boundroumi.first.attribute('value') == "平庸級" #@wait.until {@driver.find_elements(:xpath, "(//img[@onclick])")}.first.click onclick = @driver.find_elements(:xpath, "(//img[@onclick])") while onclick == nil puts "loop for finding 地下城 img onclick" onclick = @driver.find_elements(:xpath, "(//img[@onclick])") end onclick.first.click end def arena_attack puts "競技場 attack" if @charLevel < 10 @wait.until {@driver.find_element(:xpath, "(//a[@class='cooldown_bar_link'])[2]")}.click #< level 10 else @wait.until {@driver.find_element(:xpath, "(//a[@class='cooldown_bar_link'])[3]")}.click #>= level 10 end #@driver.find_element(:link, "Αρένα Επαρχιών").click @wait.until {@driver.find_element(:link, "跨域羅馬競技場")}.click @arena_table = [] (2..6).each do |i| @arena_table << [i, @wait.until {@driver.find_element(:xpath, "/html/body/div[2]/div/div[5]/div/div[2]/div[2]/table/tbody/tr[2]/td/div[2]/div/table/tbody/tr[#{i}]/td[2]").text}] #puts #{@arena_table} end #@arena_table.sort_by! { |row| row[1] } @arena_table.sort_by { |row| row[1] } #puts "To mikrotero level einai tis grammis #{@arena_table[0][0] - 1} me level #{@arena_table[0][1]}" puts "To mikrotero level einai tis grammis #{@arena_table[0][0] - 1} me level #{@arena_table[0][1]}" @driver.find_element(:xpath, "/html/body/div[2]/div/div[5]/div/div[2]/div[2]/table/tbody/tr[2]/td/div[2]/div/table/tbody/tr[#{@arena_table[0][0]}]/td[4]/span").click end def cosmoarena_attack puts "特瑪競技場 attack" @wait.until {@driver.find_element(:xpath, "(//a[@class='cooldown_bar_link'])[4]")}.click sleep 2 #@driver.find_element(:link, "Αρένα Κοσμοσυρροής").click @wait.until {@driver.find_element(:link, "跨域特瑪競技場")}.click sleep 2 @cosmo_table = [] (2..6).each do |i| @cosmo_table << [i, @wait.until {@driver.find_element(:xpath, "/html/body/div[2]/div/div[5]/div/div[2]/div[2]/table/tbody/tr[2]/td/div[2]/div/table/tbody/tr[#{i}]/td[2]").text}] end #@cosmo_table.sort_by! { |row| row[1] } @cosmo_table.sort_by { |row| row[1] } puts "To mikrotero level einai tis grammis #{@cosmo_table[0][0] - 1} me level #{@cosmo_table[0][1]}" @driver.find_element(:xpath, "/html/body/div[2]/div/div[5]/div/div[2]/div[2]/table/tbody/tr[2]/td/div[2]/div/table/tbody/tr[#{@cosmo_table[0][0]}]/td[4]/span").click #@driver.find_element(:xpath, "/html/body/div[2]/div/div[5]/div/div[2]/div[2]/table/tbody/tr[2]/td/div[2]/div/table/tbody/tr[3]/td[4]/span").click end def gladjob_one @driver.execute_script("switchMenu(1)") @wait.until {@driver.find_element(:link, "職業仲介所")}.click @wait.until {@driver.find_element(:id, "content")}.click @wait.until {@driver.find_element(:id, "job_row_2")}.click unless @wait.until {@driver.find_elements(:id, "ticker1")}.size == 1 Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, "workTime")).select_by(:text, "1 小時") @wait.until {@driver.find_element(:id, "doWork")}.click end #`killall -9 firefox` puts "sleep 3720 (one hour) for work in 馬廄..." sleep 3720 end def gladjob_eight @driver.execute_script("switchMenu(1)") @wait.until {@driver.find_element(:link, "職業仲介所")}.click @wait.until {@driver.find_element(:id, "content")}.click @wait.until {@driver.find_element(:id, "job_row_2")}.click unless @wait.until {@driver.find_elements(:id, "ticker1")}.size == 1 Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, "workTime")).select_by(:text, "8 小時") @wait.until {@driver.find_element(:id, "doWork")}.click end #`killall -9 firefox` puts "sleep 28830 (eight hours) for work in 馬廄..." sleep 28830 end def show_tagma_enemies login @driver.find_element(:link, "Τάγμα").click @driver.find_element(:id, "war_master_div").click @driver.find_elements(:xpath, "//div[@class='report_statistic']//table//tr").each do |table_line| #puts table_line.find_elements(:xpath, "//td").first.text #first.find_element(:xpath, "//a"). puts table_line.text[0..-3] end end def max_job_per_hour @driver.execute_script("switchMenu(1)") @wait.until {@driver.find_element(:link, "職業仲介所")}.click @wait.until {@driver.find_element(:id, "content")}.click @wait.until {@driver.find_element(:id, "job_row_4")}.click unless @driver.find_elements(:id, "ticker1").size == 1 Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, "workTime")).select_by(:text, "2 小時") @wait.until {@driver.find_element(:id, "doWork")}.click end #`killall -9 firefox` puts "sleep 7200 (two hours) for work as a 屠夫..." sleep 7200 end def work_time_left @driver.execute_script("switchMenu(1)") @wait.until {@driver.find_element(:link, "職業仲介所")}.click left = @wait.until {@driver.find_elements(:id, "ticker1")} if left.first == nil @left_time = 0 else time = left.first.text hours = time.match(/\d+/) minutes = time.match(/:\d+/)#.gsub(":", "") seconds = time.match(/:\d+:\d+/) hours = hours.to_s.to_i minutes = minutes.to_s.slice(1..3).to_i seconds = seconds.to_s.slice(4..5).to_i @left_time = 0 @left_time = hours*60*60 + minutes * 60 + seconds puts "still working, wait for #{time}" sleep @left_time end end def eat @driver.execute_script("switchMenu(1)") @wait.until {@driver.find_element(:link, "概覽")}.click #heals = @wait.until {@driver.find_elements(:xpath,"//img[@src='7263/img/item/7_16.gif']")} heals = @wait.until {@driver.find_elements(:xpath,"//img[contains(@src,'7263/img/item/7_')]")} #puts "heals = #{heals.first.attribute("src")}" gladiator = @wait.until {@driver.find_element(:id,"p8_1_1")} if heals.length > 0 @driver.action.drag_and_drop(heals.first,gladiator).perform show_hp #@hp = @wait.until {@driver.find_element(:id, "header_values_hp_percent").text} #@hp = @hp.to_s.gsub("%","").to_i end end

沒有留言:

張貼留言