Esempio n. 1
0
def view(character_id=None, name=None):
    character = get_character(character_id or name)
    if character:
        if character.creation_phase != 'done':
            return redirect(character.creation_wizard_url)

        g.bundle['races'] = list_races()
        g.bundle['skills'] = list_skills()
        g.bundle['abilities'] = list_abilities()
        g.bundle['characters'] = [character]
        g.bundle['fetch_updates_url'] = character.fetch_updates_url
        g.bundle['stream_updates_url'] = character.stream_updates_url
        g.bundle['fetch_item_url'] = url_for('ItemType.get')

        require_scripts('chat', 'character', 'characters', 'binds', 'ItemType',
                        'updates', 'tabs', 'view_character')
        require_styles('character', 'tabs')

        response = make_response(
            render_template('view_character.html', character=character))
        response.set_cookie('character', str(character.id))
        return response
    else:
        flash("Character not found: " + (name or str(character_id)))
        return redirect('/')
Esempio n. 2
0
    def __init__(self, name='', max_hit_points=10, description='...', **kwargs):
        self.name = name
        self.max_hitpoints = max_hit_points
        self.hitpoints = max_hit_points
        self.description = description

        for ability in list_abilities():
            db.session.add(AbilityScore(ability, self, kwargs.get(ability.name, 10)))
Esempio n. 3
0
 def list_attribute_names():
     """
     Return a list of the names of all attributes that can be accessed directly.
     :rtype: list[str]
     """
     attributes = ['id', 'name', 'description', 'hitpoints', 'max_hitpoints', 'creation_phase', 'initiative', 'creation_wizard_url']
     for ability in list_abilities():
         attributes.append(ability.name)
     return attributes
Esempio n. 4
0
 def __serialize__(self):
     serialized = {
         'description': self.description,
         'hitpoints': self.hitpoints,
         'max_hitpoints': self.max_hitpoints,
         'creation_phase': self.creation_phase,
         'creation_wizard_url': self.creation_wizard_url,
     }
     for ability in list_abilities():
         serialized[ability.name] = self.get_ability_score(ability)
     return serialized
Esempio n. 5
0
 def __serialize__(self):
     serialized = {
         'description': self.description,
         'hitpoints': self.hitpoints,
         'max_hitpoints': self.max_hitpoints,
         'creation_phase': self.creation_phase,
         'creation_wizard_url': self.creation_wizard_url,
     }
     for ability in list_abilities():
         serialized[ability.name] = self.get_ability_score(ability)
     return serialized
Esempio n. 6
0
def make_character(name):
    character = Character(name, random.randint(8, 16))
    db.session.add(character)
    db.session.commit()
    character.hitpoints = random.randint(1, character.max_hitpoints)
    character.campaign = get_main_campaign()
    for ability in list_abilities():
        character.set_ability_score(ability, random.randint(8, 18))
    for skill in list_skills():
        character.set_skill_level(skill, random.randint(0, 6))
    db.session.commit()
Esempio n. 7
0
 def list_attribute_names():
     """
     Return a list of the names of all attributes that can be accessed directly.
     :rtype: list[str]
     """
     attributes = [
         'id', 'name', 'description', 'hitpoints', 'max_hitpoints',
         'creation_phase', 'initiative', 'creation_wizard_url'
     ]
     for ability in list_abilities():
         attributes.append(ability.name)
     return attributes
Esempio n. 8
0
    def __init__(self,
                 name='',
                 max_hit_points=10,
                 description='...',
                 **kwargs):
        self.name = name
        self.max_hitpoints = max_hit_points
        self.hitpoints = max_hit_points
        self.description = description

        for ability in list_abilities():
            db.session.add(
                AbilityScore(ability, self, kwargs.get(ability.name, 10)))
Esempio n. 9
0
def dump_all():
    """Dump all the database stuff to JSON files"""
    data = {
        'abilities': list_abilities(),
        'skills': list_skills(),
        'alignments': list_alignments(),
        'item_types': list_items(),
        'races': list_races(),
        'classes': list_classes(),
    }

    with open(DATA_PATH, mode='w') as f:
        encoded_data = jsonify(data)
        f.write(encoded_data.get_data())
    return encoded_data
Esempio n. 10
0
def dump_all():
    """Dump all the database stuff to JSON files"""
    data = {
        'abilities': list_abilities(),
        'skills': list_skills(),
        'alignments': list_alignments(),
        'item_types': list_items(),
        'races': list_races(),
        'classes': list_classes(),
    }

    with open(DATA_PATH, mode='w') as f:
        encoded_data = jsonify(data)
        f.write(encoded_data.get_data())
    return encoded_data
Esempio n. 11
0
    def __init__(self, name='', max_hit_points=10, backstory='...', personality='...',
                 race='Human', character_class='Fighter', **kwargs):
        self.name = name
        self.name = name
        self.max_hitpoints = max_hit_points
        self.hitpoints = max_hit_points
        self.backstory = backstory
        self.personality = personality
        self.race = get_race(race)
        self.character_class = get_class(character_class)

        self.abilities = AbilitiesComponent()
        self.skills = SkillsComponent()
        self.inventory = Inventory()
        self.messages = MessagesComponent()

        for ability in list_abilities():
            self.abilities.set_score(ability, kwargs.get(ability.name, DEFAULT_ABILITY_SCORE))

        for skill in list_skills():
            self.skills.set_level(skill, kwargs.get(skill.name, DEFAULT_SKILL_LEVEL))
Esempio n. 12
0
def view(character_id=None, name=None):
    character = get_character(character_id or name)
    if character:
        if character.creation_phase != 'done':
            return redirect(character.creation_wizard_url)

        g.bundle['races'] = list_races()
        g.bundle['skills'] = list_skills()
        g.bundle['abilities'] = list_abilities()
        g.bundle['characters'] = [character]
        g.bundle['fetch_updates_url'] = character.fetch_updates_url
        g.bundle['stream_updates_url'] = character.stream_updates_url
        g.bundle['fetch_item_url'] = url_for('ItemType.get')

        require_scripts('chat', 'character', 'characters', 'binds', 'ItemType', 'updates', 'tabs', 'view_character')
        require_styles('character', 'tabs')

        response = make_response(render_template('view_character.html', character=character))
        response.set_cookie('character', str(character.id))
        return response
    else:
        flash("Character not found: " + (name or str(character_id)))
        return redirect('/')
Esempio n. 13
0
def init_handlers():
    print "initializing character handlers"

    @handler('name')
    def update_name(character, value):
        old_name = character.name
        if old_name != value:
            character.name = str(value)
            db.session.commit()
            updates.add_character_update(character.id, 'name', character.name)
            updates.add_character_update(character.id, 'view_url',
                                         character.view_url)

    @handler('max_hitpoints')
    def update_max_hitpoints(character, value):
        value = int(value)
        character.hitpoints += value - character.max_hitpoints
        character.max_hitpoints = value
        db.session.commit()
        updates.add_character_update(character.id, 'max_hitpoints',
                                     character.max_hitpoints)

    @handler('hitpoints')
    def update_max_hitpoints(character, value):
        character.hitpoints = int(value)
        db.session.commit()
        updates.add_character_update(character.id, 'hitpoints',
                                     character.hitpoints)

    @handler('backstory')
    def update_backstory(character, value):
        value = value.replace('<br>', '\n')
        character.backstory = value
        db.session.commit()
        updates.add_character_update(character.id, 'backstory',
                                     character.backstory)

    @handler('personality')
    def update_personality(character, value):
        value = value.replace('<br>', '\n')
        character.personality = value
        db.session.commit()
        updates.add_character_update(character.id, 'personality',
                                     character.personality)

    @handler('creation_phase')
    def update_creation_phase(character, value):
        character.creation_phase = value
        db.session.commit()
        updates.add_character_update(character.id, 'creation_phase',
                                     character.creation_phase)

    @handler('race')
    def update_race(character, value):
        character.race = Race.query.get(int(value))
        db.session.commit()
        updates.add_character_update(character.id, 'race', character.race.name)

    @handler('alignment')
    def update_alignment(character, value):
        character.alignment = Alignment.query.get(int(value))
        db.session.commit()
        updates.add_character_update(character.id, 'alignment',
                                     character.alignment.name)

    @handler('class')
    def update_class(character, value):
        character.character_class = CharacterClass.query.get(int(value))
        db.session.commit()
        updates.add_character_update(character.id, 'class',
                                     character.character_class.name)

    @handler('give_item')
    def give_item(character, value):
        item_type = ItemType.query.get(value[u'item_type'])
        item = character.inventory.add_item(item_type, int(value[u'quantity']))
        db.session.commit()
        updates.add_inventory_update(character.id, item)

    # this is me getting a little too functional with python
    def make_attribute_handler(attribute_name):
        def attribute_handler(character, value):
            setattr(character, attribute_name, value)
            db.session.commit()
            updates.add_character_update(character.id, attribute_name,
                                         getattr(character, attribute_name))

        return attribute_handler

    for attribute in Character.list_attribute_names():
        handler(attribute)(make_attribute_handler(attribute))

    def make_skill_handler(skill_id):
        def skill_handler(character, value):
            skill = Skill.query.get(skill_id)
            character.set_skill_level(skill, value)
            db.session.commit()
            updates.add_character_update(character.id, skill.name,
                                         character.get_skill_level(skill))

        return skill_handler

    for skill in list_skills():
        handler(skill.name)(make_skill_handler(skill.id))

    def make_ability_handler(ability_id):
        def ability_handler(character, value):
            ability = Ability.query.get(ability_id)
            character.set_ability_score(ability, value)
            db.session.commit()
            updates.add_character_update(character.id, ability.name,
                                         character.get_ability_score(ability))

        return ability_handler

    for ability in list_abilities():
        handler(ability.name)(make_ability_handler(ability.id))
Esempio n. 14
0
def init_handlers():
    print "initializing character handlers"
    @handler('name')
    def update_name(character, value):
        old_name = character.name
        if old_name != value:
            character.name = str(value)
            db.session.commit()
            updates.add_character_update(character.id, 'name', character.name)
            updates.add_character_update(character.id, 'view_url', character.view_url)


    @handler('max_hitpoints')
    def update_max_hitpoints(character, value):
        value = int(value)
        character.hitpoints += value - character.max_hitpoints
        character.max_hitpoints = value
        db.session.commit()
        updates.add_character_update(character.id, 'max_hitpoints', character.max_hitpoints)


    @handler('hitpoints')
    def update_max_hitpoints(character, value):
        character.hitpoints = int(value)
        db.session.commit()
        updates.add_character_update(character.id, 'hitpoints', character.hitpoints)


    @handler('backstory')
    def update_backstory(character, value):
        value = value.replace('<br>', '\n')
        character.backstory = value
        db.session.commit()
        updates.add_character_update(character.id, 'backstory', character.backstory)


    @handler('personality')
    def update_personality(character, value):
        value = value.replace('<br>', '\n')
        character.personality = value
        db.session.commit()
        updates.add_character_update(character.id, 'personality', character.personality)


    @handler('creation_phase')
    def update_creation_phase(character, value):
        character.creation_phase = value
        db.session.commit()
        updates.add_character_update(character.id, 'creation_phase', character.creation_phase)


    @handler('race')
    def update_race(character, value):
        character.race = Race.query.get(int(value))
        db.session.commit()
        updates.add_character_update(character.id, 'race', character.race.name)

    @handler('alignment')
    def update_alignment(character, value):
        character.alignment = Alignment.query.get(int(value))
        db.session.commit()
        updates.add_character_update(character.id, 'alignment', character.alignment.name)

    @handler('class')
    def update_class(character, value):
        character.character_class = CharacterClass.query.get(int(value))
        db.session.commit()
        updates.add_character_update(character.id, 'class', character.character_class.name)

    @handler('give_item')
    def give_item(character, value):
        item_type = ItemType.query.get(value[u'item_type'])
        item = character.inventory.add_item(item_type, int(value[u'quantity']))
        db.session.commit()
        updates.add_inventory_update(character.id, item)

    # this is me getting a little too functional with python
    def make_attribute_handler(attribute_name):
        def attribute_handler(character, value):
            setattr(character, attribute_name, value)
            db.session.commit()
            updates.add_character_update(character.id, attribute_name, getattr(character, attribute_name))

        return attribute_handler

    for attribute in Character.list_attribute_names():
        handler(attribute)(make_attribute_handler(attribute))

    def make_skill_handler(skill_id):
        def skill_handler(character, value):
            skill = Skill.query.get(skill_id)
            character.set_skill_level(skill, value)
            db.session.commit()
            updates.add_character_update(character.id, skill.name, character.get_skill_level(skill))

        return skill_handler

    for skill in list_skills():
        handler(skill.name)(make_skill_handler(skill.id))

    def make_ability_handler(ability_id):
        def ability_handler(character, value):
            ability = Ability.query.get(ability_id)
            character.set_ability_score(ability, value)
            db.session.commit()
            updates.add_character_update(character.id, ability.name, character.get_ability_score(ability))

        return ability_handler

    for ability in list_abilities():
        handler(ability.name)(make_ability_handler(ability.id))