示例#1
0
 def dmg_mod(self):
     """
     >>> char = Character(load_yaml('characters', 'bardic_rogue.yaml'))
     >>> isinstance(char.dmg_mod(), int)
     True
     """
     ability_mods = FlattenedDict(load_yaml('rules', 'ability_scores.yaml'))
     strength = self.get('attributes/str', 0)
     return int(ability_mods.get('/str/%s/dmg' % strength, 0))
示例#2
0
 def saving_throws(self):
     """
     >>> char = Character(load_yaml('characters', 'bardic_rogue.yaml'))
     >>> isinstance(char.saving_throws, dict)
     True
     """
     key = self.get('class/parent', '')
     sts = FlattenedDict(load_yaml("rules", "saving_throws.yaml"))
     sts = FlattenedDict(sts.getsubtree(key))
     hitdice = self.get('combat/level-hitdice', 0)
     for key2 in sts.subkeys():
         if inrange(hitdice, key2):
             st = sts.getsubtree(key2)
             st['ppd'] = int(st['ppd']) + self.ppd_mod()
             return(st)
示例#3
0
 def level_up(self):
     """
     >>> char = Character(load_yaml('characters', 'bardic_rogue.yaml'))
     >>> level = char.get('combat/level-hitdice', 1)
     >>> hp = char.get('combat/hitpoints', 1)
     >>> max = char.get('combat/max_hp', 1)
     >>> debug(char.level_up())
     >>> char.get('combat/hitpoints', 1) > hp
     True
     >>> char.get('combat/max_hp', 1) > max
     True
     >>> char.get('combat/level-hitdice', 1) == level + 1
     True
     """
     level = int(self.get('combat/level-hitdice', 1))
     level += 1
     out = '%s has reached level %s !' % (self.displayname(), level)
     self.put('combat/level-hitdice', level)
     ability_scores = load_yaml('rules', 'ability_scores.yaml')
     con = self.get('attributes/con', 1)
     out += '<br>Character constitution: %s' % con
     con_bonus = int(FlattenedDict(ability_scores).get('/con/%s/hit' % con,0))
     out += '<br>Constitution Bonus: %s' % con_bonus
     xp_levels = FlattenedDict(load_yaml('rules', 'xp_levels.yaml'))
     pclass = self.get('class/parent', '')
     xp_levels = xp_levels.readall(pclass)
     hitdice = str(xp_levels.get('%s/%s/hit_dice' % (pclass, level), 1))
     debug("Read hitdice as ", hitdice)
     if '+' not in hitdice:
         hitdice = hitdice + '+0'
     hitdice, bonus = hitdice.split('+')
     dice = int(xp_levels.get('%s/dice' % pclass, 1))
     more_hp, roll = rolldice(numdice=int(hitdice), numsides=dice, modifier=con_bonus)
     out += '<br>%s' % roll
     more_hp += int(bonus)
     current_max = int(self.get('combat/max_hp', 1))
     new_max = current_max + more_hp
     out += '<br>Maximum hitpoints increased by %s. Maximum hitpoints now: %s' % (more_hp, new_max)
     self.put('combat/max_hp', new_max)
     new_hp = new_max
     out += '<br>Character hitpoints now %s' % new_hp
     self.put('combat/hitpoints', new_hp)
     self.__init__(self())
     return out
示例#4
0
 def learn_spell(self, spellitem):
     spells = self.get('inventory/spells', [])
     if isinstance(spells, str):
         try:
             spells = simpleobjdata.loads(spells)
         except:
             spells = []
     if not isinstance(spells, list):
         self.put('inventory/spells', [])
     spelltype = spellitem.get('spell_type', 'wizard spells')
     parentclass = self.get('class/parent', '')
     childclass = self.get('class/class', '')
     canlearn = load_yaml('rules', 'various.yaml')["spell progression"]
     found = False
     for key in canlearn:
         if key == parentclass or key == childclass:
             debug(key)
             found = True
             break
     if not found:
         return "%s cannot learn spells" % self.displayname()
     oneline = list(canlearn[key].keys())[0]
     if spelltype not in canlearn[key][oneline]:
         return "%s cannot learn %s, failed to learn spell %s" % (self.displayname(), spelltype, spellitem.displayname())
     intelect = str(self.get('attributes/int', 1))
     chance = FlattenedDict(load_yaml('rules', 'ability_scores.yaml'))
     chance = chance['/int/%s/spell_learn' % intelect]
     out = "<strong>%s has a %s chance to learn a new spell</strong>" % (self.displayname(), chance)
     chance = int(chance.replace('%', ''))
     roll = rolldice(numdice=1, numsides=100, modifier=0)
     out += '<br>%s' % roll[1]
     if roll[0] > chance:
         return '%s<br><strong>%s has failed to learn %s!</strong>' % (out, self.displayname(), spellitem.displayname())
     spellitem.identify()
     self()['core']['inventory']['spells'].append(spellitem())
     self.autosave()
     return "%s<br><strong>%s has learned %s</strong>" % (out, self.displayname(), spellitem.displayname())
示例#5
0
 def __init__(self, objdata):
     self.objdata = objdata
     self.objdata = FlattenedDict(self.objdata)
     if not self.get_hash():
         self.set_hash()
示例#6
0
class EzdmObject(object):
    objdata = FlattenedDict()

    def __init__(self, objdata):
        self.objdata = objdata
        self.objdata = FlattenedDict(self.objdata)
        if not self.get_hash():
            self.set_hash()

    @property
    def animations(self):
        return self.getsubtree('animations')

    def set_hash(self):
        """
        >>> o = EzdmObject({'test': 0})
        >>> hash = o.set_hash()
        >>> hash == o.get_hash()
        True
        """
        myhash = make_hash()
        self.put('hash', myhash)
        return myhash

    def get_hash(self):
        return self.get('hash', '')

    def __call__(self):
        """
        >>> o = EzdmObject({'test': 0})
        >>> o()['test'] == 0
        True
        """
        return self.objdata

    def __str__(self):
        return dump_yaml(dict(self()))

    def update(self, objdata):
        """
        >>> j1 = {'test': 0}
        >>> j2 = {'foo': 'bar'}
        >>> o = EzdmObject(j1)
        >>> o.update(j2)
        >>> o()['foo'] == 'bar'
        True

        """
        self.__init__(objdata)

    def get(self, key, default=None):
        """
        >>> o = EzdmObject({'a/b': 1, 'a/c': 2})
        >>> o.get('/a', '') 
        ''
        >>> o.get('/a/b', '')
        1
        >>> o.get('/f/g', 5)
        5
        """
        key = stripslashes(key)
        if not self() or not key in self():
            if default is not None:
                return default
        return self()[key]

    def getall(self, key):
        return readall(key,self())

    def getsubtree(self, key):
        return self.objdata.readsubtree(key)

    def put(self, key, value):
        """
        >>> o = EzdmObject({'a/b': 1, 'a/c': 2})
        >>> o.put('/f/g', 16)
        >>> o()['f/g']
        16
        """
        self.objdata[stripslashes(key)] = value

    def putsubtree(self, key, value):
        return self.objdata.writesubtree(key, value)

    def filename(self):
        return '%s.yaml' % self.get_hash()

    def save_to_file(self, directory):
        filename = save_yaml(directory, self.filename(), dict(self()), new=True)
        return filename

    def savetoslot(self, directory):
        savedir = gamedir[0]
        filename = os.path.join(savedir,directory,'%s.yaml' % self.get_hash())
        if not os.path.exists(os.path.dirname(filename)):
            os.makedirs(os.path.dirname(filename))
        open(filename, 'w').write(json.dumps(self(),indent=4))

    def save(self):
        #TODO - save to player save slot
        pass