Ejemplo n.º 1
0
 def _perform_extraction(self, barb_type, line):
     soup = BeautifulSoup(line, 'html.parser')
     rows = soup.findAll('tr')
     barbs = []
     for row in rows:
         cells = row.findAll('td')
         if len(cells) == 9:
             distance, points, x, y = 0, 0, 0, 0
             i = 0
             try:
                 for cell in cells:
                     data = cell.text.strip()
                     if i == 1:
                         distance = float(data)
                     elif i == 5:
                         x, y = self._parse_for_coords(data, barb_type)
                     elif i == 8:
                         points = int(str(data).replace(',', ''))
                     i += 1
             except Exception as e:
                 print('Encountered exception  :' + str(e))
             villa = Villa(x, y, points=points, distance=distance)
             if distance <= self.max_distance:
                 if villa.get_points() > 500: villa.set_ignore(True)
                 barbs.append(villa)
             else:
                 print("Ignoring {} as distance {} > {}".format(
                     villa, distance, self.max_distance))
         else:
             print(
                 'TW changed the order of showing barbs... Cannot proceed with info extraction'
             )
     return barbs
 def read_in_villas_to_be_farmed(self):
     raw_farm_villas_list = read_generic_config(self.current_world_config,
                                                'farming')
     raw_farm_villas_list = [
         Villa(**villa) for villa in raw_farm_villas_list
     ]
     raw_farm_villas_list = self.filter_villas_to_farm(raw_farm_villas_list)
     # Un-comment only whilst weeding up the duplicates, as set destroy order of villages from config
     # print('Pre-conv to set size: '+str(len(raw_farm_villas_list)))
     # raw_farm_villas_list = set(raw_farm_villas_list)
     print('Final number of villas being farmed: ' +
           str(len(raw_farm_villas_list)))
     return raw_farm_villas_list
def operate_on_coord(x, y):
    url_to_hit = url.format(x=x, y=y)
    co_ord = "{}|{}".format(x, y)
    if co_ord in already_visited_coords:
        # print('Hitting already read-in co-ord: ' + co_ord)
        return
    already_visited_coords.add(co_ord)
    global counter
    counter += 1
    print("{}. {} => {}".format(counter, co_ord, url_to_hit))
    req = requests.get(url_to_hit)
    if "Barbarian" in req.text:
        print("This is a Barbarian villa!!")
        skimmed_barbs.append(Villa(x, y))
Ejemplo n.º 4
0
    def orchestrator(self, verbose=False):
        link = generate_link_twstats_barbs_list(self.mode, self.world, self.x,
                                                self.y)
        barb_lister = _ExtractBarbsList(link, max_distance=self.max_distance)
        barbs = barb_lister.extract_barbs_from_site()
        if verbose: [print(barb) for barb in barbs]

        config_villas = barb_lister.read_in_villas_to_be_farmed(
            read_config_world_level(self.base_working_dir,
                                    self.world,
                                    mode=self.mode))
        config_villas = OrderedSet(config_villas)
        if verbose: [print(barb) for barb in config_villas]

        print(
            'Number of villas in barb list from site before comparing with config : '
            + str(len(barbs)))
        [barbs.remove(villa) for villa in config_villas if villa in barbs]
        barbs = barbs.difference(
            barb_lister.get_ignored_barbs()
        )  # removing barbs which have already been set to ignore mode in config list
        # print('Found {} in existing attack list'.format(villa))
        print(
            'Number of new villas in site barb list after comparing with config : '
            + str(len(barbs)))
        [print(barb) for barb in barbs]
        base_villa = Villa(x=self.x, y=self.y)
        if barbs:
            print('Creating as-is config to be added in config.json:-')
            print("Base villa: " + str(base_villa))
            for villa in barbs:
                distance = base_villa.get_distance_from_another_villa(villa)
                axe, lcav, scouts = 0, 11, 1
                if distance <= 5 or villa.get_points() <= 100:
                    axe, lcav = 101, 5
                villa.set_axes(axe)
                villa.set_lcav(lcav)
                villa.set_scouts(scouts)
                villa.set_name("Barb")
            print("The new list of barbs:-")
            pprint(barbs)
            worker = WorkerProcessor(self.base_working_dir, self.world,
                                     self.mode)
            delta = worker.orchestrate_addition(barbs)
            return delta
        else:
            print('Discovered no new barbs in the tool!')
        return 0
    def compute_villas_already_being_attacked(self):
        attack_in_progress_villas = []
        self.interactor.load_page(
            self.base_screen_url.format(
                screen=self.helper.extract_screen('home')))
        xpath_all_own_commands_count = '//*[@id="commands_outgoings"]/table/tbody/tr[1]/th[1]/span'
        try:
            attacks_already_count = self._parse_number_of_outgoing_attacks(
                self.driver.find_element_by_xpath(
                    xpath_all_own_commands_count).text) + 2
            print("Number of already ongoing attacks : " +
                  str(attacks_already_count - 2))

            xpath = '//*[@id="commands_outgoings"]/table/tbody/tr[{}]/td[1]/span/span/a/span[2]'  # 2 onwards
            prefix = 'Attack on '  # Attack on Bonus village (510|530) K55
            for i in range(2, attacks_already_count):
                text = self.driver.find_element_by_xpath(xpath.format(i)).text
                if text.startswith(prefix):
                    x, y = self._parse_attack_cmd_for_coords(text)
                    attack_in_progress_villas.append(Villa(x, y))
        except Exception as e:
            print('Seems like there are no ongoin attacks..')
        return attack_in_progress_villas
def read_in_villas_to_be_farmed(config):
    raw_farm_villas_list = config['farming']
    raw_farm_villas_list = [Villa(**villa) for villa in raw_farm_villas_list]
    return _filter_villas_to_farm(raw_farm_villas_list)
 def generate_central_village_info(self):
     x = read_generic_config(self.current_world_config, 'villa')['x']
     y = read_generic_config(self.current_world_config, 'villa')['y']
     name = read_generic_config(self.current_world_config, 'villa')['name']
     points = read_generic_config(self.current_world_config, 'villa')['points']
     return Villa(x, y, None, name, points)