Exemplo n.º 1
0
def story(config, e2e_mode):
    # Set paths
    heroes_path = os.path.join(CURRENT_PATH, '..', HEROES_FOLDER_NAME)

    # Generate cards
    cards = read_csv(os.path.join(CURRENT_PATH, '..', CARDS_CSV_FILE_NAME))
    create_file_structure(path=CARDS_FOLDER_PATH, cards=cards)

    # Set vars
    current_level = 1
    has_finish_story_mode = False

    if e2e_mode:
        hero = select_all_files(f'{CURRENT_PATH}/../../tests/e2e/heroes')[0]
    else:
        heroes = select_all_files(heroes_path)
        hero = heroes[0]

    click.echo('Welcome to "Battle for Castile" story mode!')

    while current_level <= MAX_NUM_LEVELS and not has_finish_story_mode:
        bosses_path = os.path.join(CURRENT_PATH, '..', BOSSES_FOLDER_NAME)
        enemy = select_random_boss_by_level(bosses_path,
                                            level=current_level,
                                            e2e_mode=e2e_mode)

        click.echo(
            click.style(
                '============================================================',
                fg='cyan',
                bold=True))
        click.echo(
            click.style(f'Level {current_level}: {enemy["meta"]["name"]}',
                        fg='cyan',
                        bold=True))
        click.echo(
            click.style(
                '============================================================',
                fg='cyan',
                bold=True))

        did_hero_win = play_game(hero, enemy, e2e_mode)

        if did_hero_win:
            click.echo('Congrats! You won this level! :)')
            if current_level == MAX_NUM_LEVELS or (
                    current_level == MAX_NUM_LEVELS_E2E and e2e_mode is True):
                click.echo('You finished the story mode!')
                has_finish_story_mode = True
        else:
            click.echo('You lost... Please try again!')
            break

        current_level += 1
Exemplo n.º 2
0
def select_random_boss_by_level(path: str,
                                level: int = 1,
                                e2e_mode: bool = False) -> dict:
    selected_boss = None

    if e2e_mode:
        return select_all_files(f'{CURRENT_PATH}/../../tests/e2e/bosses')[level
                                                                          - 1]
    else:
        bosses = select_all_files(path)

    for boss in bosses:
        boss_level = boss['stats']['level']
        if boss_level == level:
            selected_boss = boss

    return selected_boss
def select_card_by_name(path: str, name: str) -> dict:
    selected_card = {}
    cards = select_all_files(path)

    for card in cards:
        if card['meta']['name'] == name:
            selected_card = card
            break
    return selected_card
Exemplo n.º 4
0
def test_if_boss_power_is_only_selected_if_cost_allows_it():
    cards_path = os.path.join(CURRENT_PATH, 'fixtures', 'cards')
    bosses_path = os.path.join(CURRENT_PATH, 'fixtures', 'bosses')
    boss = select_all_files(bosses_path)[0]

    cost_available = 0
    invoked_cards, power = select_random_boss_power(cards_path, boss,
                                                    cost_available)
    assert invoked_cards == []
    assert power == {}
Exemplo n.º 5
0
def select_random_cards_from_set(path: str,
                                 max_num_cards: int,
                                 e2e_mode: bool = False) -> List:
    selected_cards = []

    if e2e_mode:
        return select_all_files(f'{CURRENT_PATH}/../../tests/e2e/cards')
    else:
        cards = select_all_files(path)

    # If the set has fewer cards than the required
    if len(cards) < max_num_cards:
        raise Exception('Card Set doesnt contain enough cards')

    while len(selected_cards) < max_num_cards:
        index = random.randrange(0, len(cards))
        card = cards[index]
        if card not in selected_cards:
            selected_cards.append(card)

    return selected_cards
Exemplo n.º 6
0
def core_set():
    click.echo('These are the cards available on the Core Set:')

    creatures_core_set_path = os.path.join(CURRENT_PATH, '..',
                                           CARDS_FOLDER_NAME,
                                           CORE_SET_FOLDER_NAME, 'creatures')
    kingdom_core_set_path = os.path.join(CURRENT_PATH, '..', CARDS_FOLDER_NAME,
                                         CORE_SET_FOLDER_NAME, 'kingdom')
    outlaws_core_set_path = os.path.join(CURRENT_PATH, '..', CARDS_FOLDER_NAME,
                                         CORE_SET_FOLDER_NAME, 'outlaws')
    click.echo('Creatures:')
    imported_cards = select_all_files(creatures_core_set_path)
    for card in imported_cards:
        click.echo(display_card(card))

    click.echo('Kingdom:')
    imported_cards = select_all_files(kingdom_core_set_path)
    for card in imported_cards:
        click.echo(display_card(card))

    click.echo('Outlaws:')
    imported_cards = select_all_files(outlaws_core_set_path)
    for card in imported_cards:
        click.echo(display_card(card))
Exemplo n.º 7
0
def test_if_enemy_power_is_successfully_randomly_selected():
    cards_path = os.path.join(CURRENT_PATH, 'fixtures', 'cards')
    bosses_path = os.path.join(CURRENT_PATH, 'fixtures', 'bosses')
    boss = select_all_files(bosses_path)[0]

    cost_available = 9
    invoked_cards, power = select_random_boss_power(cards_path, boss,
                                                    cost_available)

    assert invoked_cards is not []
    assert len(invoked_cards) > 0
    assert invoked_cards[0]['meta']['name'] != ''
    assert invoked_cards[0]['stats']['cost'] <= cost_available

    assert power != {}
    assert power['name'] != ''
Exemplo n.º 8
0
def test_if_all_heroes_can_be_selected():
    path = os.path.join(CURRENT_PATH, 'fixtures', 'heroes')
    heroes = select_all_files(path)
    assert len(heroes) == 1
    assert heroes[0]['meta']['name'] != ''
Exemplo n.º 9
0
def match(config, e2e_mode):
    if not config.token:
        click.echo('You seem to be logged out. Please log in first')
        exit(1)

    # Set paths
    heroes_path = os.path.join(CURRENT_PATH, '..', HEROES_FOLDER_NAME)

    # Generate cards
    cards = read_csv(os.path.join(CURRENT_PATH, '..', CARDS_CSV_FILE_NAME))
    create_file_structure(path=CARDS_FOLDER_PATH, cards=cards)

    # Set vars
    match = None

    r = get_user(config.token)
    if r.status_code != 200:
        click.echo('A problem has occurred. Please try again later.')
        exit(1)

    username = r.json()['username']
    click.echo(f'Hello {username}!')

    # Set random hero for now (This should change later on)
    if e2e_mode:
        hero = select_all_files(f'{CURRENT_PATH}/../../tests/e2e/heroes')[0]
    else:
        heroes = select_all_files(heroes_path)
        hero = heroes[0]

    # TODO: Create unittest
    try:
        match = find_or_create_match(username, hero)

    except MatchCouldNotBeCreatedException:
        click.echo('The Match could not be created. Please try again later.')
        exit(1)

    except MatchNotFoundException:
        click.echo(
            '[IMPORTANT] There was a problem while fetching the Match. Please try again later.'
        )
        exit(1)

    try:
        # TODO: Create unittest
        match = wait_until_another_player_joins(match)

    except MatchNotFoundException:
        click.echo(
            '[IMPORTANT] There was a problem while fetching the Match. Please try again later.'
        )
        finish_match(match['id'])
        exit(1)

    except MatchTimeoutException:
        click.echo(
            '[IMPORTANT] We couldn\'t find a player. Please try again later.')
        finish_match(match['id'])
        exit(1)

    try:
        # TODO: Create unittest
        start_match(match['id'])
    except MatchCouldNotBeStartedException:
        click.echo(
            '[IMPORTANT] The Match could not be started. Please try again later.'
        )
        finish_match(match['id'])
        exit(1)

    enemy_info, should_start = (match['first_user'], True) if \
                                match['first_user']['username'] != username else (
                                match['second_user'], False)
    try:
        play_multiplayer_game(hero=hero,
                              enemy=json.loads(enemy_info['character']),
                              should_start=should_start,
                              match_id=match['id'],
                              hero_username=username,
                              enemy_username=enemy_info['username'],
                              e2e_mode=e2e_mode)

    except MatchNotFoundException:
        click.echo(
            '\n[IMPORTANT] A technical issue has occurred while fetching the Match. Unfortunately, you lost the game.'
        )
        finish_match(match['id'])
        exit(0)

    except TurnCouldNotBeSentException:
        click.echo(
            '\n[IMPORTANT] A technical issue has occurred while sending your Turn. Unfortunately, you lost the game.'
        )
        finish_match(match['id'])
        exit(0)

    except EnemyPlayerHasWonException:
        click.echo('\n[IMPORTANT] The enemy has won. Better luck next time!')
        click.echo(
            '\n[NOTICE] When the total value is the same for both players, the player who started the match wins'
        )
        exit(0)

    except EnemyPlayerConcededException:
        click.echo('\n[IMPORTANT] The enemy has conceded. You win!')
        finish_match(match['id'], username)
        exit(0)

    except HeroPlayerHasWonException:
        click.echo('\n[IMPORTANT] You win this game. Congrats! :)')
        finish_match(match['id'], username)
        exit(0)

    except KeyboardInterrupt:
        click.echo(
            '\n[IMPORTANT] It looks like you have conceded by pressing Control + C. Best luck next time!'
        )
        finish_match(match['id'])
        exit(0)
def test_if_all_cards_from_set_can_be_selected():
    path = os.path.join(CURRENT_PATH, 'fixtures', 'cards')
    cards = select_all_files(path)
    assert len(cards) == 2
    assert cards[0]['meta']['name'] != ''
def test_if_random_cards_from_set_get_e2e_cards_when_e2e_mode_is_provided():
    path = os.path.join(CURRENT_PATH, 'fixtures', 'cards')
    cards = select_random_cards_from_set(path, 1, e2e_mode=True)
    for card in cards:
        assert card in select_all_files(
            f'{CURRENT_PATH}/../../tests/e2e/cards')