Ejemplo n.º 1
0
def _gex_inspect(bot, args, author_id, thread_id, thread_type):
    if not args:
        raise RuntimeError('Need to provide a card id.')
    deets = gex_inspect(args[0])
    print(deets)
    if deets['image'] and deets['image'] is not None:
        print('inspecting image', deets['image'])
        bot.sendRemoteImage(deets['image'],
                            thread_id=thread_id,
                            thread_type=thread_type)
    info = '*{}*\n'.format(bold(deets['id']))
    if deets['desc'] is not None:
        info += '{}\n\n'.format(italic(deets['desc']))
    masters = [
        gex_util.user_id_to_name(bot, master) for master in deets['masters']
    ]
    info += 'Masters:\n' + ',\n'.join(masters)
    if deets['id'] in CARD_TO_USER_ID:
        owner_quants = CARD_TO_USER_ID[deets['id']]
        owner_quants = sorted(owner_quants, key=lambda x: x[1], reverse=True)
        owners = [
            gex_util.user_id_to_name(bot, owner_quant[0])
            for owner_quant in owner_quants
        ]
        info += '\n\nOwners:\n' + ',\n'.join(owners)
    bot.sendMessage(info, thread_id=thread_id, thread_type=thread_type)
Ejemplo n.º 2
0
def _check_for_battle_finish(bot, battle_id):
    matching_battles = BATTLE_DB.search(BATTLE.id == battle_id)
    battle = matching_battles[0]
    if 'finished' in battle and battle['finished']:
        return
    winner, loser = None, None
    if battle['attacker']['power'] <= 0 or len(
            battle['attacker']['deck']) == 0:
        # attacker lost
        winner = battle['defender']['id']
        loser = battle['attacker']['id']
    elif battle['defender']['power'] <= 0 or len(
            battle['defender']['deck']) == 0:
        # defender lost
        winner = battle['attacker']['id']
        loser = battle['defender']['id']
    else:
        return
    winner_name = gex_util.user_id_to_name(bot, winner)
    loser_name = gex_util.user_id_to_name(bot, loser)
    thread_id = battle['thread_id']
    thread_type = ThreadType.GROUP if battle[
        'is_group_thread'] else ThreadType.USER
    out = '_{}_ won battle `{}` against _{}_'.format(winner_name, battle_id,
                                                     loser_name)
    bot.sendMessage(out, thread_id=thread_id, thread_type=thread_type)

    # Set battle as finished.
    battle['finished'] = True
    BATTLE_DB.update(battle, BATTLE.id == battle_id)

    # Assign cards for winning and losing.
    NOAH_ID = '100003244958231'
    MASTERS = [bot.uid, NOAH_ID]
    # TODO(iandioch): Make it possible to assume a card exists.
    try:
        gex.gex_create('battle_won', MASTERS, 'I won a gex battle!')
    except Exception as e:
        pass
    try:
        gex.gex_create('battle_lost', MASTERS, 'I lost a gex battle >:(')
    except Exception as e:
        pass
    print('Giving gex cards for winning and losing battle.')
    gex.gex_give(bot.uid, 'battle_won', winner)
    gex.gex_give(bot.uid, 'battle_lost', loser)
Ejemplo n.º 3
0
def battle_start(bot, args, author_id, thread_id, thread_type):
    attacker_id = author_id
    if len(args) == 0:
        bot.sendMessage('Please choose someone to battle xo',
                        thread_id=thread_id,
                        thread_type=thread_type)
        return
    defender_name = args[0]
    print(defender_name)
    defender_id = gex_util.user_name_to_id(bot, defender_name)
    gex_util.check_user_in_db(attacker_id)
    gex_util.check_user_in_db(defender_id)
    print(defender_id)
    battle_id = _generate_battle_id()
    data = {
        'attacker': {
            'id': attacker_id,
            'deck': _get_shuffled_deck(attacker_id),
            'ready': False,
            'power': STARTING_POWER_AMOUNT,
            'commands': [],
        },
        'defender': {
            'id': defender_id,
            'deck': _get_shuffled_deck(defender_id),
            'ready': False,
            'power': STARTING_POWER_AMOUNT,
            'commands': [],
        },
        'id': battle_id,
        'thread_id': thread_id,
        'is_group_thread': (thread_type == ThreadType.GROUP),
        'finished': False,
    }
    print(data)
    BATTLE_DB.insert(data)

    # Send battle ID in its own message, so it is easy to copy and paste on
    # mobile.
    attacker_name = gex_util.user_id_to_name(bot, attacker_id)
    defender_name = gex_util.user_id_to_name(bot, defender_id)
    message = 'Started battle! {} is attacking {}.\nBattle id: {}'.format(
        attacker_name, defender_name, battle_id)
    bot.sendMessage(message, thread_id=thread_id, thread_type=thread_type)
    bot.sendMessage(battle_id, thread_id=thread_id, thread_type=thread_type)
Ejemplo n.º 4
0
def _gex_decks(bot, args, author_id, thread_id, thread_type):
    users = gex_decks()
    format_str = '{:<16} {:>4} {:>5} {:>' + str(MAX_CARD_ID_LENGTH) + '}'
    out_strings = [format_str.format('Name', 'Uniq', 'Total', 'Most recent')]
    for user_id, unique, total, last_card in users:
        name = gex_util.user_id_to_name(bot, user_id)
        if last_card is None:
            last_card = ''
        name_line = format_str.format(name[:16], str(unique), str(total),
                                      last_card[:MAX_CARD_ID_LENGTH])
        out_strings.append(name_line)
    out = code_block('\n'.join(out_strings))
    bot.sendMessage(out, thread_id=thread_id, thread_type=thread_type)
Ejemplo n.º 5
0
def _gex_flex(bot, args, author_id, thread_id, thread_type):
    user_id = author_id
    if len(args):
        user_id = gex_util.user_name_to_id(bot, args[0])
    cards = gex_flex(user_id)
    total = sum(c[1] for c in cards)
    name = gex_util.user_id_to_name(bot, user_id)
    output = '{}\nID: _{}_\n'.format(bold(name), user_id)
    output += 'Total cards: _{}_ (_{}_ unique)\n'.format(total, len(cards))
    output += '\n' + bold('Cards') + ':\n'
    for card, num in cards:
        output += '{}: {}\n'.format(monospace(card[:MAX_CARD_ID_LENGTH]), num)
    bot.sendMessage(output, thread_id=thread_id, thread_type=thread_type)
Ejemplo n.º 6
0
def battle_list(bot, args, author_id, thread_id, thread_type):
    user_id = author_id
    if len(args) != 0:
        user_id = gex_util.user_name_to_id(bot, ' '.join(args))
    user_name = gex_util.user_id_to_name(bot, user_id)

    out = ''
    matching_battles = BATTLE_DB.search(BATTLE.defender.id == user_id)
    if len(matching_battles) > 0:
        out = 'Battles in which {} is defending:'.format(user_name)
        for battle in matching_battles:
            opponent_id = battle['attacker']['id']
            opponent_name = gex_util.user_id_to_name(bot, opponent_id)
            out += '\n' + \
                '`{}` (against _{}_)'.format(battle['id'], opponent_name)
    matching_battles = BATTLE_DB.search(BATTLE.attacker.id == user_id)
    if len(matching_battles) > 0:
        out += '\nBattles in which {} is attacking:'.format(user_name)
        for battle in matching_battles:
            opponent_id = battle['defender']['id']
            opponent_name = gex_util.user_id_to_name(bot, opponent_id)
            out += '\n' + \
                '`{}` (against _{}_)'.format(battle['id'], opponent_name)
    bot.sendMessage(out, thread_id=thread_id, thread_type=thread_type)
Ejemplo n.º 7
0
def _get_participant_info(bot, data):
    name = gex_util.user_id_to_name(bot, data['id'])
    deck = data['deck']
    ready = data['ready']
    power = data['power']
    return (name, deck, ready, power)