標籤

4GL (1) 人才發展 (10) 人物 (3) 太陽能 (4) 心理 (3) 心靈 (10) 文學 (31) 生活常識 (14) 光學 (1) 名句 (10) 即時通訊軟體 (2) 奇狐 (2) 音樂 (2) 產業 (5) 郭語錄 (3) 無聊 (3) 統計 (4) 新聞 (1) 經濟學 (1) 經營管理 (42) 解析度 (1) 遊戲 (5) 電學 (1) 網管 (10) 廣告 (1) 數學 (1) 機率 (1) 雜趣 (1) 證券 (4) 證券期貨 (1) ABAP (15) AD (1) agentflow (4) AJAX (1) Android (1) AnyChart (1) Apache (14) BASIS (4) BDL (1) C# (1) Church (1) CIE (1) CO (38) Converter (1) cron (1) CSS (23) DMS (1) DVD (1) Eclipse (1) English (1) excel (5) Exchange (4) Failover (1) FI (57) File Transfer (1) Firefox (2) FM (2) fourjs (1) gladiatus (1) google (1) Google Maps API (2) grep (1) Grub (1) HR (2) html (23) HTS (8) IE (1) IE 8 (1) IIS (1) IMAP (3) Internet Explorer (1) java (3) JavaScript (22) jQuery (6) JSON (1) K3b (1) LED (3) Linux (112) Linux Mint (4) Load Balance (1) Microsoft (2) MIS (2) MM (51) MSSQL (1) MySQL (27) Network (1) NFS (1) Office (1) Oracle (125) Outlook (3) PDF (6) Perl (59) PHP (33) PL/SQL (1) PL/SQL Developer (1) PM (3) Postfix (2) postfwd (1) PostgreSQL (1) PP (50) python (1) QM (1) Red Hat (4) Reporting Service (28) ruby (11) SAP (234) scp (1) SD (16) sed (1) Selenium-WebDriver (5) shell (5) SQL (4) SQL server (8) SQuirreL SQL Client (1) SSH (2) SWOT (3) Symantec (2) T-SQL (7) Tera Term (2) tip (1) tiptop (22) Tomcat (6) Trouble Shooting (1) Tuning (5) Ubuntu (33) ufw (1) utf-8 (1) VIM (11) Virtual Machine (2) vnc (3) Web Service (2) wget (1) Windows (19) Windows (1) WM (6) youtube (1) yum (2)

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

沒有留言:

張貼留言