Ejemplo n.º 1
0
    def print_spells(self):
        morgue_parser = MorgueParser(self.character.morgue_file())
        raw_spells = morgue_parser.spells()
        spells = [spell for spell in raw_spells if (len(spell.split()) >= 5)]
        formatted_spells = []
        for spell in spells:
            spell_parts = spell.split()
            spell_parts.reverse()
            hunger, level, failure, power, spell_type, *spell = spell_parts
            formatted_spells.append(
                f"{' '.join(spell)} - {spell_type} - {power} - {failure}")

        return [
            "TakeNRG Listing All Spells TakeNRG",
            "Spell Type Power Failure",
        ] + formatted_spells
def test_parse_spells():
    morgue_file = open("support/kilrfish.txt").read()
    morgue_parser = MorgueParser(morgue_file)
    spells = morgue_parser.spells()
    expected_spells = [
        "Animate Skeleton         Necr           N/A          98%         1    ##.....",
        "Apportation              Tloc           #.....       98%         1    ##.....",
        "Confusing Touch          Hex            #.....       98%         1    ##.....",
        "Corona                   Hex            #......      98%         1    ##.....",
        "Infusion                 Chrm           #...         98%         1    ##.....",
        "Shock                    Conj/Air       #...         98%         1    ##.....",
        "Summon Small Mammal      Summ           #...         98%         1    ##.....",
        "Call Imp                 Summ           #.......     99%         2    ###....",
        "Ensorcelled Hibernation  Hex/Ice        #.....       99%         2    ###....",
        "Shroud of Golubria       Chrm/Tloc      #.....       99%         2    ###....",
        "Song of Slaying          Chrm           #.......     99%         2    ###....",
        "Swiftness                Chrm/Air       #.......     99%         2    ###....",
        "Call Canine Familiar     Summ           #.......     100%        3    ####...",
        "Confuse                  Hex            #.......     100%        3    ####...",
        "Dazzling Spray           Conj/Hex       #.....       100%        3    ####...",
        "Portal Projectile        Hex/Tloc       #.....       100%        3    ####...",
        "Regeneration             Chrm/Necr      #.........   100%        3    ####...",
        "Spectral Weapon          Hex/Chrm       #.......     100%        3    ####...",
        "Static Discharge         Conj/Air       #.......     100%        3    ####...",
        "Summon Guardian Golem    Hex/Summ       #.......     100%        3    ####...",
        "Tukima's Dance           Hex            #.......     100%        3    ####...",
        "Airstrike                Air            #.........   100%        4    #####..",
        "Ice Form                 Ice/Tmut       #.......     100%        4    #####..",
        "Leda's Liquefaction      Hex/Erth       #.........   100%        4    #####..",
        "Summon Ice Beast         Ice/Summ       #.......     100%        4    #####..",
        "Summon Lightning Spire   Summ/Air       #.......     100%        4    #####..",
        "Lightning Bolt           Conj/Air       #.........   100%        5    ######.",
        "Metabolic Englaciation   Hex/Ice        #.........   100%        5    ######.",
        "Bolt of Cold             Conj/Ice       #.........   100%        6    #######",
        "Freezing Cloud           Conj/Ice/Air   #.........   100%        6    #######",
        "Ozocubu's Refrigeration  Ice            #.........   100%        6    #######",
        "Simulacrum               Ice/Necr       #.........   100%        6    #######",
    ]

    assert spells == expected_spells
Ejemplo n.º 3
0
class Character:
    def __init__(self,
                 morgue_filepath=None,
                 morgue_url=None,
                 name=None,
                 local_mode=None):
        self.morgue_filepath = morgue_filepath
        self.morgue_url = morgue_url
        self.name = name

        # self.local_mode = True
        self.local_mode = local_mode

        self._find_morguefile()
        self._find_name()
        self.key = f"{name}/morguefile.txt"
        self.weapons = fetch_weapons(self.non_saved_morgue_file())
        self.morgue_parser = MorgueParser(self.non_saved_morgue_file())

    def __str__(self):
        return self.name

    def __repr__(self):
        return f"{super().__repr__()}: {self.name}"

    # ========================================================================================

    def overview(self):
        morgue_parser = MorgueParser(self.non_saved_morgue_file())
        return morgue_parser.overview()

    def spells(self):
        return (SpellFactory(spell).new()
                for spell in self.morgue_parser.spells())

    def spells_above(self, level):
        print(f"Spells Above!: {level}")
        return [
            spell.overview() for spell in self.spells()
            if spell.level >= float(level)
        ]

    # ========================================================================================

    def morgue_file(self):
        if self.local_mode:
            morgue = open(self.morgue_filepath).read()
        else:
            morgue = self.s3_morgue_file()
            if morgue is None:
                morgue = self.fetch_online_morgue()

        return morgue

    def non_saved_morgue_file(self):
        if self.local_mode:
            return open(self.morgue_filepath).read()
        else:
            return self.fetch_online_morgue()

    def s3_morgue_file(self):
        try:
            client = boto3.client("s3")
            response = client.get_object(Bucket=BUCKET, Key=self.key)
            return response["Body"].read().decode()
        except Exception as e:
            print(f"Error fetching morguefile: {BUCKET} {self.key}")
            return None

    def fetch_online_morgue(self):
        response = requests.get(self.morgue_url)
        if response.status_code == 200:
            return response.text
        else:
            if not os.environ.get("TEST_MODE", False):
                print(
                    f"\033[031;1mCould not find the Character at {self.morgue_url}\033[0m"
                )

    # ========================================================================================

    def _find_morguefile(self):
        if self.local_mode and self.morgue_filepath is None:
            self.morgue_filepath = self._find_morgue_filepath()
        else:
            self.morgue_url = self._find_morgue_url()

    def _find_name(self):
        if self.name:
            pass

        if self.local_mode:
            morgue_path = self.morgue_filepath
        else:
            morgue_path = self.morgue_url
        self.name = morgue_path.split("/")[-1].replace(".txt", "")

    def _find_morgue_url(self):
        MORGUE_DOMAIN = "http://crawl.akrasiac.org/rawdata"
        return f"{MORGUE_DOMAIN}/{self.name}/{self.name}.txt"

    def _find_morgue_filepath(self):
        return f"{find_morgue_folder()}/{self.name}.txt"

    def spellcasting(self):
        return self.lookup_skill("Spellcasting").level

    def skills(self):
        return self.morgue_parser.skills()

    def lookup_skill(self, desired_skill):
        try:
            raw_skill = [
                skill for skill in self.skills() if desired_skill in skill
            ][0]
            return SkillFactory(raw_skill).new()
        except Exception as e:
            return Skill(skill_type=desired_skill, level=0, status=None)

    def intelligence(self):
        return self.morgue_parser.intelligence()