Ejemplo n.º 1
0
def gex_remove(removing_user, card_id, receiving_user):
    global CARD_TO_USER_ID
    print('removing', removing_user, card_id, receiving_user)
    matching_cards = CARD_DB.search(CARD.id == card_id)
    if not len(matching_cards):
        raise RuntimeError('No card exists with the given id.')
    masters = matching_cards[0]['masters']
    if removing_user not in masters and removing_user != receiving_user:
        raise RuntimeError(
            'Only a card master or the user themselves can remove a card.')
    gex_util.check_user_in_db(receiving_user)
    user = gex_util.get_user(receiving_user)
    if card_id not in user['cards'] or user['cards'][card_id] <= 0:
        raise RuntimeError('This user does not own the given card!')
    deleted = False
    user['cards'][card_id] -= 1
    if user['cards'][card_id] == 0:
        del user['cards'][card_id]
        deleted = True
    USER_DB.update({'cards': user['cards']}, USER.id == receiving_user)

    # Update CARD_TO_USER_ID
    tups = CARD_TO_USER_ID[card_id]
    index = [i for i in range(len(tups)) if tups[i][0] == receiving_user][0]
    if deleted:
        del tups[index]
    else:
        tups[index] = (receiving_user, user['cards'][card_id])
    CARD_TO_USER_ID[card_id] = tups
Ejemplo n.º 2
0
def gex_create(card_id, card_masters, card_desc=None):
    gex_util.check_user_in_db(card_masters[0])
    time_millis = gex_util.get_time_millis()
    user_data = gex_util.get_user(card_masters[0])
    old_time = 0
    if 'last_create_time' in user_data and user_data[
            'last_create_time'] is not None:
        old_time = user_data['last_create_time']
    if any(user_id in GOD_IDS for user_id in card_masters):
        # for testing and automated card creations, bots don't need to timeout
        old_time = 0
    if time_millis - old_time < 1000 * 60 * 60:  # millis in an hour
        raise RuntimeError('Can only create a card every hour.')
    if not card_masters or not len(card_masters):
        raise RuntimeError('No master provided for this card.')
    matching_cards = CARD_DB.search(CARD.id == card_id)
    if len(matching_cards):
        raise RuntimeError('A card already exists with this id.')
    if len(card_id) > MAX_CARD_ID_LENGTH:
        raise RuntimeError('A card id may be no longer than {} chars.'.format(
            MAX_CARD_ID_LENGTH))
    CARD_DB.insert({
        'id': card_id,
        'masters': card_masters,
        'desc': card_desc,
        'image': None,
    })
    USER_DB.update({'last_create_time': time_millis},
                   USER.id == card_masters[0])
    CARD_TO_USER_ID[card_id] = []
Ejemplo n.º 3
0
def gex_give(giving_user, card_id, receiving_user):
    global CARD_TO_USER_ID
    print('giving', giving_user, card_id, receiving_user)
    gex_util.check_for_card(giving_user, card_id)
    gex_util.check_user_in_db(receiving_user)
    data = gex_util.get_user(receiving_user)
    cards = data['cards']
    if card_id in cards:
        cards[card_id] += 1
    else:
        cards[card_id] = 1
    USER_DB.update({
        'cards': cards,
        'last_card': card_id
    }, USER.id == receiving_user)

    # Update CARD_TO_USER_ID
    tups = []
    if card_id in CARD_TO_USER_ID:
        tups = CARD_TO_USER_ID[card_id]
    indexes = [i for i in range(len(tups)) if tups[i][0] == receiving_user]
    if len(indexes):
        tups[indexes[0]] = (receiving_user, cards[card_id])
    else:
        tups.append((receiving_user, cards[card_id]))
    CARD_TO_USER_ID[card_id] = tups
Ejemplo n.º 4
0
def _get_shuffled_deck(user_id):
    user_data = gex_util.get_user(user_id)
    print(user_data)
    cards = []
    for card in user_data['cards']:
        cards.extend([card] * (user_data['cards'][card]))
    shuffle(cards)
    return cards
Ejemplo n.º 5
0
def gex_flex(user_id):
    gex_util.check_user_in_db(user_id)
    data = gex_util.get_user(user_id)
    cards = data['cards']
    card_keys = list(cards)
    card_keys.sort()
    card_keys.sort(key=lambda x: cards[x], reverse=True)
    card_tuples = []
    for c in card_keys:
        card_tuples.append((c, cards[c]))
    return card_tuples