Esempio n. 1
0
 def attempt_research(self, unit_type, smith_data=None):
     if not smith_data:
         result = self.wrapper.get_action(village_id=self.village_id,
                                          action="smith")
         smith_data = Extractor.smith_data(result)
     if not smith_data or unit_type not in smith_data['available']:
         self.logger.warning(
             "Unit %s does not appear to be available or smith not built yet"
             % unit_type)
         return
     data = smith_data['available'][unit_type]
     if 'can_research' in data and data['can_research']:
         if 'research_error' in data and data['research_error']:
             return False
         if 'error_buildings' in data and data['error_buildings']:
             return False
         res = self.wrapper.get_api_action(village_id=self.village_id,
                                           action="research",
                                           params={"screen": "smith"},
                                           data={
                                               "tech_id": unit_type,
                                               "source": self.village_id,
                                               'h': self.wrapper.last_h
                                           })
         if res:
             self.logger.info("Started research of %s" % unit_type)
             self.resman.update(res['game_data'])
             return True
     self.logger.info("Research of %s not yet possible" % unit_type)
Esempio n. 2
0
    def attempt_upgrade(self):
        self.logger.debug("Managing Upgrades")
        if self._research_wait > time.time():
            self.logger.debug("Smith still busy for %d seconds" %
                              int(self._research_wait - time.time()))
            return
        unit_levels = self.wanted_levels
        if not unit_levels:
            self.logger.debug("Not upgrading because nothing is requested")
            return
        result = self.wrapper.get_action(village_id=self.village_id,
                                         action="smith")
        smith_data = Extractor.smith_data(result)
        if not smith_data:
            self.logger.debug("Error reading smith data")
            return False
        for unit_type in unit_levels:
            if not smith_data or unit_type not in smith_data['available']:
                self.logger.warning(
                    "Unit %s does not appear to be available or smith not built yet"
                    % unit_type)
                continue
            wanted_level = unit_levels[unit_type]
            current_level = int(smith_data['available'][unit_type]['level'])
            data = smith_data['available'][unit_type]

            if current_level < wanted_level and 'can_research' in data and data[
                    'can_research']:
                if 'research_error' in data and data['research_error']:
                    self.logger.debug(
                        "Skipping research of %s because of research error" %
                        unit_type)
                    continue
                if 'error_buildings' in data and data['error_buildings']:
                    self.logger.debug(
                        "Skipping research of %s because of building error" %
                        unit_type)
                    continue

                attempt = self.attempt_research(unit_type,
                                                smith_data=smith_data)
                if attempt:
                    self.logger.info(
                        "Started smith upgrade of %s %d -> %d" %
                        (unit_type, current_level, current_level + 1))
                    self.wrapper.reporter.report(
                        self.village_id, "TWB_UPGRADE",
                        "Started smith upgrade of %s %d -> %d" %
                        (unit_type, current_level, current_level + 1))
                    return True
        return False
Esempio n. 3
0
 def attempt_upgrade(self, unit_levels):
     self.logger.debug("Managing Upgrades")
     result = self.wrapper.get_action(village_id=self.village_id,
                                      action="smith")
     smith_data = Extractor.smith_data(result)
     for unit_type in unit_levels:
         if not smith_data or unit_type not in smith_data['available']:
             self.logger.warning(
                 "Unit %s does not appear to be available or smith not built yet"
                 % unit_type)
             continue
         wanted_level = unit_levels[unit_type]
         current_level = int(smith_data['available'][unit_type]['level'])
         if current_level < wanted_level:
             attempt = self.attempt_research(unit_type,
                                             smith_data=smith_data)
             if attempt:
                 self.logger.info(
                     "Started smith upgrade of %s %d -> %d" %
                     (unit_type, current_level, current_level + 1))
                 return True
     return False