Пример #1
0
def round():
    msg = request.form['text']
    commands = msg.split(' ')
    team_id = request.form['team_id']
    team = db.session.query(Team).filter(Team.team_id == team_id).one_or_none()
    team = Team(team_id) if team is None else team
    team_contents = team.get_contents()
    round = team_contents.get('round', 0)
    if commands[0] == 'start':
        round = 1
    elif commands[0] == 'end':
        round = 0
    elif len(commands[0]) == 0:
        round += 1
    else:
        round = get_int(commands[0], 1)
    team_contents['round'] = round
    team.set_contents(team_contents)
    db.session.add(team)
    db.session.commit()
    if commands[0] == 'end':
        characters = db.session.query(Character).all()
        for character in characters:
            contents = character.get_contents()
            if 'last_init' in contents:
                del (contents['last_init'])
                character.set_contents(contents)
                db.session.add(character)
        if 'tmp_inits' in team_contents:
            del (team_contents['tmp_inits'])
            team.set_contents(team_contents)
            db.session.add(team)
        db.session.commit()
        return make_response('*Rounds cleared!*')

    attachments = [{'title': f'Round {round}', 'color': '#f48704'}]
    attachments += stat(True)
    raw_data = {'response_type': 'in_channel', 'attachments': attachments}
    encoded = json.dumps(raw_data)
    resp = Response(encoded, content_type='application/json')
    return resp
Пример #2
0
def init():
    print(request.form)
    msg = request.form['text']

    username = request.form['user_name']
    user_id = request.form['user_id']
    team_id = request.form['team_id']
    commands = msg.split(' ')

    character = get_character(username)
    contents = character.get_contents()
    contents['user_id'] = user_id
    if 'user_id' not in contents:
        name = character.name
    else:
        name = f'<@{contents["user_id"]}|{character.name}>'

    if len(commands[0]) == 0 or commands[0] == 'roll' or commands[0][0] in (
            '+', '-'):
        if len(commands[0]) > 0 and commands[0][0] in ('+', '-'):
            init_mod = get_int(commands[0], 0)
        else:
            init_mod = int(contents.get('init_mod', 0))
        title = '{} rolls 1d20 {}'.format(name, modifier(init_mod))
        roll = random.randint(1, 20)
        last_init = roll + init_mod
        fields = [{
            'title': 'Rolls',
            'value': f'{roll}',
            'short': True
        }, {
            'title': 'Result',
            'value': f'{last_init}',
            'short': True
        }]
        contents['last_init'] = last_init
        set_and_commit(character, contents)
        color = get_color(roll / 20)
        return make_response(fields, title, color=color)

    if len(commands) > 1 and commands[0] == 'mod':
        init_mod = get_int(commands[1], 0)
        contents['init_mod'] = init_mod
        set_and_commit(character, contents)
        fields = [{'value': signed(init_mod)}]
        return make_response(fields, f'{name}\'s Initiative modifier')

    if commands[0] == 'set':
        if len(commands) == 2:
            last_init = get_int(commands[1], 0)
            contents['last_init'] = last_init
            set_and_commit(character, contents)
            fields = [{'value': str(last_init)}]
        elif len(commands) > 2:

            team = db.session.query(Team).filter(
                Team.team_id == team_id).one_or_none()
            team = Team(team_id) if team is None else team
            team_contents = team.get_contents()

            tmp_inits = team_contents.get('tmp_inits', {})

            last_init = get_int(commands[1], 0)
            target = ' '.join(commands[2:])
            if target.startswith('@'):
                target = target[1:]

            name = character.name
            name = f'{target}({name})'
            tmp_inits[name] = last_init

            team_contents['tmp_inits'] = tmp_inits
            team.set_contents(team_contents)
            db.session.add(team)
            db.session.commit()

            fields = [{'value': str(last_init)}]

        return make_response(fields, f'{name}\'s Initiative value')

    if commands[0] in ('party', 'all'):
        characters = db.session.query(Character).all()

        team = db.session.query(Team).filter(
            Team.team_id == team_id).one_or_none()
        team = Team(team_id) if team is None else team
        team_contents = team.get_contents()

        tmp_inits = team_contents.get('tmp_inits', {})
        log.info('tmp_inits: {}'.format(str(tmp_inits)))

        char_inits = []
        for character in characters:
            contents = character.get_contents()
            if 'last_init' in contents:
                last_init = int(contents['last_init'])
                if 'user_id' not in contents:
                    name = character.name
                else:
                    name = f'<@{contents["user_id"]}|{character.name}>'
                char_inits.append((name, last_init))
        for tmp_char in tmp_inits:
            char_inits.append((tmp_char, tmp_inits[tmp_char]))

        sorted_inits = sorted(char_inits, key=lambda tup: tup[1], reverse=True)

        merged_inits = [f'{name} ({init})' for name, init in sorted_inits]
        formatted_inits = ' > '.join(merged_inits)

        fields = [{'value': formatted_inits}]
        return make_response(fields, 'Round initiatives')
    if commands[0] in ('CLEAR', 'REMOVE'):
        if 'last_init' in contents:
            del (contents['last_init'])
            character.set_contents(contents)
            db.session.add(character)
        db.session.commit()
        return make_response('Your initiative is removed')

    if commands[0] in ('ALLCLEAR', 'ALLREMOVE', 'CLEARALL', 'REMOVEALL'):
        characters = db.session.query(Character).all()
        for character in characters:
            contents = character.get_contents()
            if 'last_init' in contents:
                del (contents['last_init'])
                character.set_contents(contents)
                db.session.add(character)
        db.session.commit()
        return make_response('All initiatives are removed')

    return process_unknown(username)