Exemplo n.º 1
0
def parse_character(url):
    req_data = requests.get(url)
    char_data = req_data.json()['data']
    actions = get_actions(char_data)
    for action_kwargs in actions:
        try:
            action_msg, created_action = Action.create_action(**action_kwargs)
        except Exception as e:
            print("Failed on:", action_kwargs['name'])
            print("Kwargs:", action_kwargs)
            raise e
        if action_msg != "Success":
            raise RuntimeError(f"Failed to create {action_kwargs['name']} "
                               f"because of: {action_msg}")
    action_names = [a['name'] for a in actions]
    msg = Combatant.create(name=char_data['name'],
                           hp=get_hp(char_data),
                           ac=get_ac(char_data),
                           proficiency=get_proficiency(char_data),
                           strength=get_stat_data('Strength', char_data),
                           dexterity=get_stat_data('Dexterity', char_data),
                           constitution=get_stat_data('Constitution',
                                                      char_data),
                           wisdom=get_stat_data('Wisdom', char_data),
                           intelligence=get_stat_data('Intelligence',
                                                      char_data),
                           charisma=get_stat_data('Charisma', char_data),
                           cr=get_level(char_data),
                           actions=",".join(action_names))
    return msg
def create_action(request):
    arg_dict = {}
    for arg in action_args:
        if arg in request.POST:
            arg_dict[arg] = request.POST.get(arg)
    action_type = request.POST.get('action_type')
    # Parse dice into useful string
    dice = parse_dice_str(request.POST.get('dice'))
    arg_dict['dice'] = dice
    msg, action = Action.create_action(action_type, **arg_dict)
    return JsonResponse({
        'msg': msg,
        'actions': [a.jsonify() for a in Action.objects.all()]
    }, safe=False)