-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStart.py
134 lines (98 loc) · 3.89 KB
/
Start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import time
import json
from datetime import datetime
from datetime import timedelta
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.chrome.options import Options as Chrome_options
from Building import Building
from Utils import wait,wait_until,click,pressEscape,dated_message,string_to_delta_time,date_to_string,click_ac
import Events
import Files
import Game
def load_init_actions(game):
if(game.settings['player']['reapVillages']):
if(game.settings['player']['captain']):
game.add_action(datetime.now(),game.village_loot_captain,None)
if(game.settings['player']['upgrade_villages']):
for currentCity in game.cities:
game.add_action(datetime.now(), game.upgrade_villages, currentCity)
else:
for currentCity in game.cities:
game.add_action(datetime.now(),game.village_loot,currentCity)
if(game.settings['player']['culture']):
if(game.settings['player']['admin']):
game.add_action(datetime.now(),game.culture,None)
for currentCity in game.cities:
if(game.settings['player']['manageSenate']):
game.add_action(datetime.now(),game.upgrade_buildings,currentCity)
if(game.settings['player']['academy']):
game.add_action(datetime.now(),game.academy,currentCity)
if(game.settings['player']['max_hours_to_run'] != 0.0):
deltaTime = timedelta(minutes=int(game.settings['player']['max_hours_to_run']))
newTime = datetime.now()+deltaTime
game.add_action(newTime,game.end,None)
def play_grepolis(flag, update_function, finish_function):
queue = Events.ActionsQueue()
files = Files.Files("settings.json","log.txt")
# Start browser and login
browser = execute_game_session(files.get_settings(), flag, update_function)
game = Game.Game(browser,files,queue,flag,update_function,finish_function)
game.load_settings()
try:
game.get_cities()
load_init_actions(game)
while 1:
if(game.do_next_action() == 1):
break
except Exception as e:
print('Something went wrong:')
print(e)
game.end()
# logs in, manages the game and closes the browser
def execute_game_session(settings, flag, update_function):
# setup web browser
exePath = settings['webDriver']["executablePath"]
try:
browser = webdriver.Chrome(exePath)
except Exception as e:
print(e)
print('Failed to start webdriver.')
flag.set(True)
return
browser.maximize_window()
try:
update_function('Logging in')
login_and_select_world(browser, settings['player'])
except Exception as e:
print(e)
browser.quit()
print('Something went wrong in the login.')
return browser
# logs the user in and navigates to the game world
def login_and_select_world(browser,player):
#browser.maximize_window()
browser.get(player['server'])
time.sleep(1)
# find username and password inputs
usernameInput = browser.find_element_by_id('login_userid')
passwordInput = browser.find_element_by_id('login_password')
# enter user name and password
usernameInput.send_keys(player['username'])
time.sleep(1)
passwordInput.send_keys(player['password'])
time.sleep(1)
#press login button
loginButton = browser.find_element_by_id('login_Login')
loginButton.click()
time.sleep(5)
#select world
worldButton = browser.find_elements_by_class_name('world_name')
worldButton[player['world_number']].find_element_by_css_selector('div').click()
time.sleep(2)
# exit any pop ups
pressEscape(browser)
time.sleep(1)
pressEscape(browser)