travian_bot 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -1,57 +1,67 @@
1
1
  require 'selenium-webdriver'
2
2
  require 'chronic'
3
- require 'yaml'
3
+
4
+ Dir.glob(File.join(File.dirname(__FILE__), '/application/*.rb')).sort.each { |lib| load lib }
5
+
4
6
 
5
7
  class TravianBot
6
8
  class Application
7
9
  class << self
8
-
10
+ include Connection
11
+ include Queue
12
+ include Display
13
+ include Buildings
14
+
15
+ # Is executed by the travinbot shell script.
9
16
  def run!(*arguments)
10
- login
11
- building_queue
12
- close
17
+ h1('Welcome to your TravianBot')
18
+ @game = login
19
+
20
+ current_building_queue
21
+ current_troop_movements
22
+ current_avaible_buildings
23
+
24
+ @game.quit
13
25
 
14
26
  return 1
15
27
  end
16
28
 
17
29
  private
18
-
19
- # Load the credentials from ENV['HOME']/.travian_bot
20
- def get_credentials
21
- credentials = YAML::load(File.open("#{ENV['HOME']}/.travian_bot"))
22
-
23
- [credentials['travian_bot']['url'], credentials['travian_bot']['usr'], credentials['travian_bot']['pwd']]
30
+ def current_avaible_buildings
31
+ h2 'Avaible buildings'
32
+ buildings = avaible_buildings(@game)
33
+
34
+ buildings.each do |building|
35
+ puts building.to_s
36
+ end
37
+ new_line
24
38
  end
25
-
26
- # Login in to travian page
27
- def login
28
- url, user, password = get_credentials
29
- @driver = Selenium::WebDriver.for :firefox
30
- @driver.navigate.to(url)
31
- name_input = @driver.find_element(:name, 'name')
32
- password_input = @driver.find_element(:name, 'password')
33
- login_button = @driver.find_element(:id, 's1')
34
-
35
- name_input.send_keys(user)
36
- password_input.send_keys(password)
37
- login_button.submit
38
- end
39
-
40
- # Close the selenium connection
41
- def close
42
- @driver.quit
39
+
40
+ def current_building_queue
41
+ h2('Current building queue')
42
+ buildings = building_queue(@game)
43
+
44
+ if buildings.empty?
45
+ text 'nothing building'
46
+ else
47
+ buildings.each do |building|
48
+ puts building.to_s
49
+ end
50
+ new_line
51
+ end
43
52
  end
44
53
 
45
- # Check if building something
46
- def building_queue
47
- begin
48
- building_pipe = @driver.find_elements(:xpath, "//table[@id='building_contract']/tbody/tr")
49
-
50
- building_pipe.each do |entry|
51
- puts entry.text
54
+ def current_troop_movements
55
+ h2('Current troop movement')
56
+ troops = troop_movement(@game)
57
+
58
+ if troops.empty?
59
+ text 'No troops movement'
60
+ else
61
+ troops.each do |troop|
62
+ puts troop.to_s
52
63
  end
53
- rescue Selenium::WebDriver::Error::NoSuchElementError
54
- puts 'Nothing in building queue'
64
+ new_line
55
65
  end
56
66
  end
57
67
 
@@ -0,0 +1,44 @@
1
+ require 'travian_bot/application/navigate'
2
+
3
+ class TravianBot
4
+ class Application
5
+ module Buildings
6
+ include TravianBot::Application::Navigate
7
+
8
+ def avaible_buildings(selenium)
9
+ begin
10
+ elements = selenium.find_elements(:xpath, '//map[@id="rx"]/area')
11
+ resources = elements.inject([]) do |out, element|
12
+ out << element.attribute('alt')
13
+
14
+ out
15
+ end
16
+ rescue Selenium::WebDriver::Error::NoSuchElementError
17
+ resources = []
18
+ end
19
+
20
+ to_village_page(selenium)
21
+ show_building_level(selenium)
22
+
23
+ begin
24
+ elements = selenium.find_elements(:xpath, '//div[@id="village_map"]/map/area')
25
+ buildings = elements.inject([]) do |out, element|
26
+ out << element.attribute('alt')
27
+
28
+ out
29
+ end
30
+ rescue Selenium::WebDriver::Error::NoSuchElementError
31
+ buildings = []
32
+ end
33
+
34
+ resources + buildings
35
+ end
36
+
37
+ private
38
+ def show_building_level(selenium)
39
+ link = selenium.find_element(:xpath, '//img[@id="lswitch"]')
40
+ link.click
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,31 @@
1
+ require 'yaml'
2
+
3
+ class TravianBot
4
+ class Application
5
+ module Connection
6
+ # Login in to travian page
7
+ def login
8
+ url, user, password = get_credentials
9
+ driver = Selenium::WebDriver.for :firefox
10
+ driver.navigate.to(url)
11
+ # Get the form fields.
12
+ name_input = driver.find_element(:name, 'name')
13
+ password_input = driver.find_element(:name, 'password')
14
+ login_button = driver.find_element(:id, 's1')
15
+ # Login
16
+ name_input.send_keys(user)
17
+ password_input.send_keys(password)
18
+ login_button.submit
19
+
20
+ driver
21
+ end
22
+
23
+ # Load the credentials from ENV['HOME']/.travian_bot
24
+ def get_credentials
25
+ credentials = YAML::load(File.open("#{ENV['HOME']}/.travian_bot"))
26
+
27
+ [credentials['travian_bot']['url'], credentials['travian_bot']['usr'], credentials['travian_bot']['pwd']]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ class TravianBot
2
+ class Application
3
+ module Display
4
+ def h1(text)
5
+ puts ' '
6
+ puts "#{text}"
7
+ puts "=" * text.length
8
+ puts ' '
9
+ puts ' '
10
+ end
11
+
12
+ def h2(text)
13
+ puts "#{text}"
14
+ puts "-" * text.length
15
+ puts ' '
16
+ end
17
+
18
+ def text(text)
19
+ puts text
20
+ puts ' '
21
+ end
22
+
23
+ def new_line
24
+ puts ' '
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ class TravianBot
2
+ class Application
3
+ module Navigate
4
+ def to_resources_page(selenium)
5
+ to_page(selenium, 'resources')
6
+ end
7
+
8
+ def to_village_page(selenium)
9
+ to_page(selenium, 'village')
10
+ end
11
+
12
+ def to_map_page(selenium)
13
+ to_page(selenium, 'map')
14
+ end
15
+
16
+ def to_stats_page(selenium)
17
+ to_page(selenium, 'stats')
18
+ end
19
+
20
+ def to_reports_page(selenium)
21
+ to_page(selenium, 'reports')
22
+ end
23
+
24
+ private
25
+ def to_page(selenium, target)
26
+ wait = Selenium::WebDriver::Wait.new
27
+ link = selenium.find_element(:xpath, "//ul[@id='navigation']/li[@class='#{target}']/a")
28
+ link.click
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ require 'selenium-webdriver'
2
+
3
+ class TravianBot
4
+ class Application
5
+ module Queue
6
+ # Check if building something
7
+ def building_queue(selenium)
8
+ begin
9
+ building_pipe = selenium.find_elements(:xpath, "//table[@id='building_contract']/tbody/tr")
10
+
11
+ building_pipe.inject([]) do |out, entry|
12
+ out << entry.text
13
+
14
+ out
15
+ end
16
+ rescue Selenium::WebDriver::Error::NoSuchElementError
17
+ []
18
+ end
19
+ end
20
+
21
+ # Show the troop movement.
22
+ def troop_movement(selenium)
23
+ begin
24
+ troop_movement = selenium.find_elements(:xpath, "//table[@id='movements']/tbody/tr")
25
+
26
+ troop_movement.inject([]) do |out, movement|
27
+ out << movement.text
28
+
29
+ out
30
+ end
31
+ rescue Selenium::WebDriver::Error::NoSuchElementError
32
+ []
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
data/travian_bot.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "travian_bot"
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Roman Simecek"]
@@ -28,6 +28,11 @@ Gem::Specification.new do |s|
28
28
  "bin/travianbot",
29
29
  "lib/travian_bot.rb",
30
30
  "lib/travian_bot/application.rb",
31
+ "lib/travian_bot/application/buildings.rb",
32
+ "lib/travian_bot/application/connection.rb",
33
+ "lib/travian_bot/application/display.rb",
34
+ "lib/travian_bot/application/navigate.rb",
35
+ "lib/travian_bot/application/queue.rb",
31
36
  "test/helper.rb",
32
37
  "test/test_travian_bot.rb",
33
38
  "travian_bot.gemspec"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: travian_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: selenium-webdriver
16
- requirement: &2153096620 !ruby/object:Gem::Requirement
16
+ requirement: &2156060440 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153096620
24
+ version_requirements: *2156060440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: chronic
27
- requirement: &2153095920 !ruby/object:Gem::Requirement
27
+ requirement: &2156056740 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2153095920
35
+ version_requirements: *2156056740
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &2153095200 !ruby/object:Gem::Requirement
38
+ requirement: &2156050160 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2153095200
46
+ version_requirements: *2156050160
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &2153094300 !ruby/object:Gem::Requirement
49
+ requirement: &2156047020 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 1.6.4
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2153094300
57
+ version_requirements: *2156047020
58
58
  description: A little bot for travian.
59
59
  email: roman.simecek@cyt.ch
60
60
  executables:
@@ -74,6 +74,11 @@ files:
74
74
  - bin/travianbot
75
75
  - lib/travian_bot.rb
76
76
  - lib/travian_bot/application.rb
77
+ - lib/travian_bot/application/buildings.rb
78
+ - lib/travian_bot/application/connection.rb
79
+ - lib/travian_bot/application/display.rb
80
+ - lib/travian_bot/application/navigate.rb
81
+ - lib/travian_bot/application/queue.rb
77
82
  - test/helper.rb
78
83
  - test/test_travian_bot.rb
79
84
  - travian_bot.gemspec
@@ -92,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
97
  version: '0'
93
98
  segments:
94
99
  - 0
95
- hash: 295626495803354199
100
+ hash: -1529457768905218778
96
101
  required_rubygems_version: !ruby/object:Gem::Requirement
97
102
  none: false
98
103
  requirements: