def check_execution(self): if not self.ready: return self.ready = False c = True cn = {} for k, cond in self.cond_instances.items(): c = c and cond.is_true() cn[k] = cond.is_true() logger.log_note("check condition", "Job: %s\n Fulfilled: %s\n %s\n %s" % (self['job'], cn, self['conditions'], self.cond_instances)) if c: logger.log_note("execute job", "Execute job: %s" % self['job']) for cond in self.cond_instances.values(): cond.terminate() self['job'].execute(self.village) if 'repeat' in self['job'] and self['job']['repeat']: self.current_state = self.wait_for_conditions elif 'quest_event' in self['job'] and isinstance(self['job'], JobBuild): self.wait_for_build['quest_event'] = self['job']['quest_event'] self.wait_for_build['building'] = self['job'].get_build_id() self.wait_for_build['level'] = self['job']['level'] self.current_state = self.wait_for_build else: self.current_state = self.terminated self.village.fire_event( Event(self.village, 'job_completed', datetime.now(), job=self['job']) ) else: self.save_data() self.ready = True
def execute(self, village): if not self['next_field'] in village.resource_fields: self.next_field(village) logger.log_note('build field', 'Build field %s' % (self['next_field'],)) village.build_building(self['next_field'][1], self['next_field'][2]+1) pages = village.refresh('resources') if not village.events.build: #any( event.building == self['next_field'][1] and event.level == self['next_field'][2]+1 for event in village.events.build ): logger.log_error('build failed', pages['resources'].find('html').html(), title='Could not build %s level %s.' % (db.buildings[self['next_field'][1]]['gname'], self['next_field'][2])) self.next_field(village)
def action_quest(account, action, quest= None): params = {} if action is not None: params["action"] = action if quest is not None: params["questTutorialId"] = quest response = account.ajax_cmd("quest", params) if response.text == "": logger.log_error("ajax failed", "Ajax request failed at tutorial: action: %s, quest: %s" % (action, quest)) logger.log_note("quest", response.text, title="Execute %s %s" % (action, quest))
def fire_event(self, event): logger.log_note("event fired", "event %s got fired! %s" % (event.name, event)) pages = self.event_refresh_pages.get(event.name, None) if pages is not None: try: self.refresh(*pages) except Exception as e: print("could not refresh due to connection issues.") print(traceback.format_exc()) for event_handler in self.event_handlers: event_handler.__on_event__(event) for trigger in self.triggers: trigger.__on_event__(event) for condition in self.conditions: condition.__on_event__(event)
def request_POST(self, url, params, add_headers = {}, createDom=True): headers = dict(self.default_headers) headers['Content-Type'] = 'application/x-www-form-urlencoded' headers.update(add_headers) # perform login result = self.session.post(self.url + url, data=params, headers=headers) logger.log_note("POST request", "POST issued for %s with %s" % (url, params)) try: if createDom: return HtmlDom().createDom(result.text) except: logger.log_error("POST failed", result.text, title="failed reading page in POST %s" % url) return result
def refresh(self, *page_names): ''' Refreshes all pages related with this village, or only with the given pages. ''' if self.suppress_refresh: return logger.log_note('refresh', 'refreshing %s' % str(page_names)) pages = {} page_names = page_names or self.url_mapping.keys() for page_name in page_names: doc = self.account.request_GET(self.url_mapping[page_name]) if 'type="password"' in doc.find("html").html(): logger.log_info("logout", "Logout detected") self.account.clear_cookie() self.account.login() self.account.save_cookie() self.next_refresh_time = datetime.now() + timedelta(seconds=30) return pages[page_name] = doc self.read_content(pages) self.read_events(pages) if not self.resources: self.account.login() self.refresh(*page_names) print(self.events.build) if not self.resources or not self.production or not self.storage_capacity: logger.log_error("page error", [ "%s: %s" % (t, p.find("html").html().strip()) for t,p in pages.items() ], 'Could not fetch village data') self.save_status() self.new_refresh_time() return pages
os.environ['HTTP_PROXY'] = '' """ http://217.76.38.69:3128 http://81.198.230.182:8080 """ proxies = [ "http://37.239.46.26:80" ] #proxies = [ ] server = (3, 'de') account = Account(server, name) logger.log_name = name logger.log_note("start", "Started the bot") #db.users.remove({}) # wipe users #db.states.remove({}) jobs_from_db = True if account.get_db() is None or account.get_db()['activated'] == False: logger.log_info("register account", "need to register this account first: ensure the mailserver is running") if account.get_db() is None: account.init_db(email, password, nation, proxies) account.load_db() action.action_register(account) jobs_from_db = False
def execute(self, account = None): logger.log_note("execute action", "Execute: %s(%s)" % (self.func_action.__name__, self.params)) self.func_action(account or self.account, *self.params)