Esempio n. 1
0
 def initialize_page(self, fields, pages, external_data):
     t = time.time()
     self.encounter_id = f'encounter-{t}'
     hd_plus_minus_pattern = re.compile(r'^\d+[+-]\d+$')
     monster_team = external_data['Monster Team']
     enemies = []
     for enemy in monster_team:
         if enemy['TableName'] == 'Characters':
             enemy['XP Value'] = SystemSettings.get_xp_value(enemy)
             enemies.append(enemy)
         else:
             hd = enemy['HD']
             if hd.isdigit():
                 roll_string = f'{hd}d8'
                 hp = Dice.rollString(roll_string)
             elif hd_plus_minus_pattern.match(hd):
                 hd_split = re.split(r'([+-])', hd)
                 roll_string = f'{hd_split[0]}d8{hd_split[1]}{hd_split[2]}'
                 hp = max(Dice.rollString(roll_string), 1)
             elif hd.endswith('hp'):
                 roll_string = f'''{hd.replace('hp', '').strip()}'''
                 hp = max(Dice.rollString(roll_string), 1)
             else:
                 # hp = hd
                 hd = Dice.rollString(hd)
                 hp = Dice.rollString(f'{hd}d8')
             enemy['HP'] = hp
             enemy['XP Value'] = SystemSettings.get_xp_value(enemy)
             enemies.append(enemy)
     serialized_enemies = json.dumps(enemies)
     DbQuery.insertRow(
         'Encounters',
         (self.encounter_id, '', serialized_enemies, self.campaign_id))
     DbQuery.commit()
Esempio n. 2
0
 def remove_client(fields):
     current_client = fields['Client List Current']
     DbQuery.deleteRow('WebApp', 'remote_addr',
                       current_client['remote_addr'])
     DbQuery.commit()
     web_app = DbQuery.getTable('WebApp')
     return {'Client List': [(i['remote_addr'], i) for i in web_app]}
Esempio n. 3
0
 def change_character(fields):
     current_character = fields['Choose Character Data']
     character_id = current_character['unique_id']
     current_client = fields['Client List Current']
     if current_client is None:
         return {}
     remote_addr = current_client['remote_addr']
     DbQuery.updateRow('WebApp', 'remote_addr', remote_addr,
                       (remote_addr, character_id))
     DbQuery.commit()
     return {'Client List': DbQuery.getTable('WebApp')}
Esempio n. 4
0
        def save_character_hp(char_id, new_hp):
            meta_indexed = get_meta_indexed(pcs_indexed[char_id])
            cur_hp_meta_row = meta_indexed.get('Current HP', None)

            if cur_hp_meta_row is None:
                cur_hp_insert_row = (char_id, 'Current HP', None, new_hp, None)
                DbQuery.insertRow('Characters_meta', cur_hp_insert_row)
                DbQuery.commit()
            else:
                DbQuery.update_cols('Characters_meta', 'character_id', char_id,
                                    ('Data', ), (new_hp, ), 'Type',
                                    'Current HP')
                DbQuery.commit()
Esempio n. 5
0
def get_save_background(environ):
    raw_post = environ.get('wsgi.input', '')
    post = raw_post.read(int(environ.get('CONTENT_LENGTH', 0)))
    post_dict = parse_qs(post.decode(), True)
    character_id = post_dict.get('character_id', None)
    background = post_dict.get('background', None)
    if character_id and background:
        character_id = character_id[0]
        background = background[0]
        characters = DbQuery.getTable('Characters')
        for c in characters:
            if c['unique_id'] == character_id:
                DbQuery.update_cols('Characters', 'unique_id', character_id, ['Background'], (background, ))
                DbQuery.commit()
                return get_existing_character(environ, character_id)
    return '<h1>Problem saving background!</h1>'
Esempio n. 6
0
        def adjust_enemy_hp(dialog_return, fields, enemy_index):
            new_hp = dialog_return + fields[f'Current HP {enemy_index}']
            en = enemies[enemy_index]
            if en['TableName'] == 'Monsters':
                enemies[enemy_index]['HP'] = new_hp
                serialized_enemies = json.dumps(enemies)
                DbQuery.update_cols('Encounters', 'unique_id', encounter_id,
                                    ('enemy_team', ), (serialized_enemies, ))
                DbQuery.commit()
            else:
                char_id = en['unique_id']
                save_character_hp(char_id, new_hp)

            return {
                f'Current HP {enemy_index}': new_hp,
            }
Esempio n. 7
0
def get_choose_character(environ):
    if environ.get('REQUEST_METHOD', '') != 'POST':
        return ''
    raw_post = environ.get('wsgi.input', '')
    post = raw_post.read(int(environ.get('CONTENT_LENGTH', 0)))
    post_dict = parse_qs(post.decode(), True)
    character_id = post_dict.get('character_id', None)
    if character_id:
        character_id = character_id[0]
        pcs = environ['Extern'].get('PCs', [])
        for c in pcs:
            if c['unique_id'] == character_id:
                DbQuery.insertRow('WebApp', (environ['REMOTE_ADDR'], character_id))
                DbQuery.commit()
                return get_character_html(c)

    return ''
Esempio n. 8
0
        def adjust_enemy_xp(dialog_return, fields, enemy_index):
            new_xp = dialog_return
            enemy = enemies[enemy_index]
            enemy['XP Value'] = new_xp
            serialized_enemies = json.dumps(enemies)
            DbQuery.update_cols('Encounters', 'unique_id', encounter_id,
                                ('enemy_team', ), (serialized_enemies, ))
            DbQuery.commit()

            new_total = \
                sum(fields[f] for f in fields if f.startswith('Current XP') and f != f'Current XP {enemy_index}')\
                + new_xp

            return {
                f'Current XP {enemy_index}': new_xp,
                'Total Label': f'<b>Total XP: </b>{new_total}',
            }
Esempio n. 9
0
File: Wizard.py Progetto: lowhrtz/gm
def wizard_accept( fields, pages ):
    character_dict = pages['ReviewPage'].make_character_dict()

    data_list = [
        character_dict['unique_id'],
        character_dict['Name'],
        character_dict['Level'],
        character_dict['XP'],
        character_dict['Gender'],
        character_dict['Alignment'],
        character_dict['Classes'],
        character_dict['Race'],
        character_dict['HP'],
        character_dict['Age'],
        character_dict['Height'],
        character_dict['Weight'],
        character_dict['Portrait'],
        character_dict['Portrait_Image_Type'],
        character_dict['STR'],
        character_dict['INT'],
        character_dict['WIS'],
        character_dict['DEX'],
        character_dict['CON'],
        character_dict['CHA'],
        ]

    DbQuery.begin()
    row_id = DbQuery.insertRow( 'Characters', data_list )

    for meta_row in character_dict['Characters_meta']:
        data_list = [
            meta_row['character_id'],
            meta_row['Type'],
            meta_row['Entry_ID'],
            meta_row['Data'],
            meta_row['Notes'],
        ]
        DbQuery.insertRow( 'Characters_meta', data_list )

    DbQuery.commit()

    return row_id
Esempio n. 10
0
def wizard_accept(fields, pages):
    character_dict = pages['ReviewPage'].make_character_dict()

    data_list = [
        character_dict['unique_id'],
        character_dict['Name'],
        character_dict['Level'],
        character_dict['XP'],
        character_dict['Gender'],
        character_dict['Alignment'],
        character_dict['Classes'],
        character_dict['Race'],
        character_dict['HP'],
        character_dict['Age'],
        character_dict['Height'],
        character_dict['Weight'],
        character_dict['Portrait'],
        character_dict['Portrait_Image_Type'],
        character_dict['STR'],
        character_dict['INT'],
        character_dict['WIS'],
        character_dict['DEX'],
        character_dict['CON'],
        character_dict['CHA'],
    ]

    DbQuery.begin()
    row_id = DbQuery.insertRow('Characters', data_list)

    for meta_row in character_dict['Characters_meta']:
        data_list = [
            meta_row['character_id'],
            meta_row['Type'],
            meta_row['Entry_ID'],
            meta_row['Data'],
            meta_row['Notes'],
        ]
        DbQuery.insertRow('Characters_meta', data_list)

    DbQuery.commit()

    return row_id
Esempio n. 11
0
def cc_submit(environ):
    if environ.get('REQUEST_METHOD', '') != 'POST':
        return ''
    raw_post = environ.get('wsgi.input', '')
    post = raw_post.read(int(environ.get('CONTENT_LENGTH', 0)))
    post_dict = parse_qs(post.decode(), True)

    name = post_dict.get('name', [''])[0]
    gender = post_dict.get('gender', [''])[0]
    race_id = post_dict.get('race', [''])[0]
    class_name = post_dict.get('classes', [''])[0]
    alignment = post_dict.get('alignment', [''])[0]
    portrait = post_dict.get('portrait', [''])[0]
    full_class = SystemSettings.get_full_class(class_name)
    race = {r['unique_id']: r for r in DbQuery.getTable('Races')}[race_id]
    unique_id = f"{name.lower().replace(' ', '_')}-{full_class['unique_id']}-{time.time()}"

    if 'classes' in full_class:
        level = '/'.join('1' for _ in full_class['classes'])
        classes = '/'.join(cl['unique_id'] for cl in full_class['classes'])
        xp = '/'.join('0' for _ in full_class['classes'])
    else:
        level = '1'
        classes = full_class['unique_id']
        xp = '0'

    attr_dict = SystemSettings.adjust_attributes(
        SystemSettings.roll_attributes(None, race, full_class),
        race)
    hp = SystemSettings.roll_hp(attr_dict, 1, full_class)
    age = SystemSettings.roll_age(race, full_class)
    height, weight = SystemSettings.roll_height_weight(race, gender)

    proficiency_choices = SystemSettings.get_proficiency_choices(full_class, race)
    if 'classes' in full_class:
        slots = max([cl['Initial_Weapon_Proficiencies'] for cl in full_class['classes']])
    else:
        slots = full_class['Initial_Weapon_Proficiencies']
    proficiencies = Dice.get_random_items(proficiency_choices, slots, r=False)
    equipment = SystemSettings.starting_items_basic(full_class, race)
    equipment.extend([i['unique_id'] for i in Dice.get_random_items(proficiencies)])

    character_dict = {
        'unique_id': unique_id,
        'Name': name,
        'Level': level,
        'XP': xp,
        'Gender': gender,
        'Alignment': alignment,
        'Classes': classes,
        'Race': race_id,
        'HP': hp,
        'Age': age,
        'Height': f'{height[0]}ft {height[1]}in',
        'Weight': f'{weight} lbs',
        'Background': '',
        'Portrait': portrait,
        'Portrait_Image_Type': 'jpg',
        'STR': attr_dict['STR'],
        'INT': attr_dict['INT'],
        'WIS': attr_dict['WIS'],
        'DEX': attr_dict['DEX'],
        'CON': attr_dict['CON'],
        'CHA': attr_dict['CHA'],
        'Characters_meta': [],
    }

    def make_meta_row(data_list):
        meta_row = {
            'character_id': data_list[0],
            'Type': data_list[1],
            'Entry_ID': data_list[2],
            'Data': data_list[3],
            'Notes': data_list[4]
        }
        return meta_row

    for e in equipment:
        equip_data = [
            unique_id,
            'Equipment',
            e,
            '',
            '',
        ]
        character_dict['Characters_meta'].append(make_meta_row(equip_data))

    gp_data = [
        unique_id,
        'Treasure',
        'gp',
        SystemSettings.get_initial_wealth(full_class),
        '',
    ]
    character_dict['Characters_meta'].append(make_meta_row(gp_data))

    for p in proficiencies:
        p_data = [
            unique_id,
            'Proficiency',
            p['unique_id'],
            'P',
            '',
        ]
        character_dict['Characters_meta'].append(make_meta_row(p_data))

    if 'classes' in full_class:
        class_list = full_class['classes']
    else:
        class_list = [full_class]
    spellbook = []
    daily_1 = []
    daily_2 = []
    for i, c in enumerate(class_list):
        if SystemSettings.has_spells_at_level(1, c):
            spells = [s for s in DbQuery.getTable('Spells') if s['Type'] == c['unique_id'] and s['Level'] == 1]
            if c['Category'] == 'wizard':
                slots = 4
                if c['unique_id'] == 'magic_user':
                    slots = 3
                    for j, s in enumerate(spells):
                        if s['spell_id'] == 'read_magic':
                            spellbook.append(spells.pop(j))
                            break
                spellbook.extend(Dice.get_random_items(spells, slots, False))
                spells = spellbook
            if i == 0:
                slots = SystemSettings.get_xp_table_row(1, c)['Level_1_Spells']
                daily_1.extend(Dice.get_random_items(spells, slots))
            else:
                slots = SystemSettings.get_xp_table_row(1, c)['Level_1_Spells']
                daily_2.extend(Dice.get_random_items(spells, slots))
    for s in spellbook:
        s_data = [
            unique_id,
            'Spellbook',
            s['spell_id'],
            '',
            '',
        ]
        character_dict['Characters_meta'].append(make_meta_row(s_data))
    for d in daily_1:
        d_data = [
            unique_id,
            'DailySpells',
            d['spell_id'],
            '',
            '',
        ]
        character_dict['Characters_meta'].append(make_meta_row(d_data))
    for d in daily_2:
        d_data = [
            unique_id,
            'DailySpells2',
            d['spell_id'],
            '',
            '',
        ]
        character_dict['Characters_meta'].append(make_meta_row(d_data))

    SystemSettings.add_character(character_dict)
    DbQuery.insertRow('WebApp', [environ['REMOTE_ADDR'], unique_id])
    DbQuery.commit()

    return WebApp.get_character_html(character_dict)
Esempio n. 12
0
    def accept(self, fields, pages, external_data):
        encounters = DbQuery.getTable('Encounters')
        encounters_indexed = {e['unique_id']: e for e in encounters}
        characters = DbQuery.getTable('Characters')
        characters_indexed = {c['unique_id']: c for c in characters}
        character_classes = DbQuery.getTable('Classes')
        c_classes_indexed = {c['unique_id']: c for c in character_classes}

        party_treasure = [
            t for t in self.campaign['Campaigns_meta']
            if t['Type'] == 'Party Treasure'
        ]
        everything_else = [
            e for e in self.campaign['Campaigns_meta']
            if e['Type'] != 'Party Treasure'
        ]
        # new_treasure = deepcopy(pages['Wrap Up'].treasure)
        new_treasure = {
            'cp': fields['CP'],
            'sp': fields['SP'],
            'ep': fields['EP'],
            'gp': fields['GP'],
            'pp': fields['PP'],
            'items': fields['Items'],
        }
        new_treasure_xp_value = Treasure.get_xp_value(new_treasure)
        for pt in party_treasure:
            t = pt['Data']
            t = json.loads(t)
            for k, v in t.items():
                # print(k, v)
                new_treasure[k] += v
        new_treasure_entry = {
            'Type': 'Party Treasure',
            'Entry_ID': None,
            'Data': json.dumps(new_treasure),
            'Notes': None
        }

        everything = everything_else + [
            new_treasure_entry,
        ]
        DbQuery.begin()
        DbQuery.deleteRow('Campaigns_meta', 'campaign_id',
                          self.campaign['unique_id'])
        for i in everything:
            row = self.campaign['unique_id'], i['Type'], i['Entry_ID'], i[
                'Data'], i['Notes']
            DbQuery.insertRow('Campaigns_meta', row)
        # DbQuery.commit()
        self.campaign['Campaigns_meta'] = everything

        encounter_id = pages['Encounter Tracker'].encounter_id
        enemies_serialized = encounters_indexed[encounter_id]['enemy_team']
        enemies = json.loads(enemies_serialized)
        xp_total = sum(e['XP Value'] for e in enemies)
        if fields['Include Treasure XP']:
            xp_total += new_treasure_xp_value
        pc_team = external_data['PC Team']
        xp_eligible = []
        for pc in pc_team:
            pc_id = pc['unique_id']
            pc = characters_indexed[pc_id]
            current_hp = get_character_hp(pc)
            if current_hp > 0:
                xp_eligible.append(pc)
        num_of_eligible = len(xp_eligible)
        xp_per = xp_total // num_of_eligible
        # print(xp_total, num_of_eligible, xp_per)
        from decimal import Decimal
        for pc in xp_eligible:
            pc_id = pc['unique_id']
            xp_list = str(pc['XP']).split('/')
            add_xp = xp_per // len(xp_list)
            pc_class_list = pc['Classes'].split('/')
            if len(pc_class_list) == 1:
                cl = c_classes_indexed[pc_class_list[0]]
                xp_bonus_string = cl['Experience_Bonus']
                xp_bonus = SystemSettings.parse_xp_bonus(xp_bonus_string, pc)
                if xp_bonus:
                    add_xp = int(add_xp *
                                 (1 + Decimal(xp_bonus) / Decimal(100)))
            new_xp_list = [int(xp) + add_xp for xp in xp_list]
            new_xp = '/'.join(str(xp) for xp in new_xp_list)
            DbQuery.update_cols('Characters', 'unique_id', pc_id, ('XP', ),
                                (new_xp, ))
        DbQuery.commit()