Пример #1
0
def get_me_request():

    new_user = verify_pending_deposits_for_user(current_user)

    add_log(current_user.id, None, 'me', {})

    return json.dumps(new_user.serializable(fields={'balance': True}))
Пример #2
0
def play_slot_request():
    payload = request.get_json()

    bet_multiplier = payload.get('bet_multiplier')

    bet_price = bet_multiplier * base_bet

    success = add_user_balance(current_user, -bet_price)

    if not success:
        abort(400, 'Not enough balance')

    if not bet_multiplier:
        abort(400, 'Please provide bet_multiplier')

    if bet_multiplier > 10 or bet_multiplier < 1:
        abort(400, 'Please provide a bet_multiplier between 1 and 10')

    board, prize, winning_lines = get_random_board()

    multiplied_prize = prize * bet_multiplier

    if multiplied_prize > 0:
        add_user_balance(current_user, multiplied_prize)

    add_log(current_user.id, 1, 'play', {
        'bet': bet_price,
        'prize': multiplied_prize
    })

    return json.dumps({
        'board': board,
        'prize': multiplied_prize,
        'winning_lines': winning_lines
    })
Пример #3
0
def add_user_credentials_request():
    try:
        add_credentials_request = UserAddCredentialsRequest().load(
            request.get_json())
    except ValidationError as ve:
        return abort(400, json.dumps(ve.messages))

    code, result = add_username_and_password(current_user,
                                             add_credentials_request)

    if code != 200:
        abort(code, result)

    user: User = result.get('user')
    auth_token: str = result.get('auth_token')
    refresh_token: str = result.get('refresh_token')

    response = Response(
        json.dumps(user.serializable(fields={'balance': True})), 200)

    response.headers[
        'Access-Control-Expose-Headers'] = 'X-Auth-Token, X-Refresh-Token'
    response.headers['X-Auth-Token'] = auth_token
    response.headers['X-Refresh-Token'] = refresh_token

    add_log(user.id, None, 'addcredentials', {})

    return response
Пример #4
0
def user_login_request():
    try:
        login_request = UserLoginRequest().load(request.get_json())
    except ValidationError as ve:
        return abort(400, json.dumps(ve.messages))

    code, result = login(login_request)

    if code != 200:
        abort(code, result)

    user: User = result.get('user')
    auth_token: str = result.get('auth_token')
    refresh_token: str = result.get('refresh_token')

    response = Response(
        json.dumps(user.serializable(fields={'balance': True})), 200)

    response.headers[
        'Access-Control-Expose-Headers'] = 'X-Auth-Token, X-Refresh-Token'
    response.headers['X-Auth-Token'] = auth_token
    response.headers['X-Refresh-Token'] = refresh_token

    add_log(user.id, None, 'login', {})

    return response
Пример #5
0
def play_poker_hand_request():
    payload = request.get_json()

    hold_indexes = payload.get('hold_indexes')
    identifier = payload.get('identifier')

    if hold_indexes is None:
        abort(400, 'Please provide hold_indexes')

    if identifier is None:
        abort(400, 'Please provide identifier')

    poker_hand = get_pocker_hand(identifier, current_user)

    if poker_hand is None:
        abort(400, 'Invalid identifier')

    deduped_hold_indexes = []
    for index in hold_indexes:
        if not (0 <= index <= 4):
            abort(400, 'Please provide hold_indexes between 0 and 4')
        elif index not in deduped_hold_indexes:
            deduped_hold_indexes.append(index)

    result_cards, matched_indexes, prize_info = play_poker_hand(
        poker_hand, deduped_hold_indexes)

    settled = settle_poker_hand(poker_hand)

    if not settled:
        # Hand was already played in the meantime
        abort(400, 'Invalid identifier')

    bet = poker_hand.multiplier * base_bet
    prize = 0

    if prize_info is not None:
        prize_multiplier = prize_info.get('multiplier')
        prize = bet * prize_multiplier

    add_user_balance(current_user, prize)

    add_log(
        current_user.id, 3, 'play', {
            'bet': bet,
            'identifier': identifier,
            'result_cards': result_cards,
            'prize': prize
        })

    return json.dumps({
        'identifier': identifier,
        'result_cards': result_cards,
        'prize': prize,
        'matched_indexes': matched_indexes,
        'prize_info': prize_info
    })
Пример #6
0
def get_pocker_hand_request():

    payload = request.get_json()

    bet_multiplier = payload.get('bet_multiplier')

    if not bet_multiplier:
        abort(400, 'Please provide bet_multiplier')

    if bet_multiplier > 10 or bet_multiplier < 1:
        abort(400, 'Please provide a bet_multiplier between 1 and 10')

    bet_price = bet_multiplier * base_bet

    success = add_user_balance(current_user, -bet_price)

    if not success:
        abort(400, 'Not enough balance')

    identifier, front_cards, matched_indexes, prize_info, recovered, multiplier = new_poker_hand(
        current_user, bet_multiplier)

    if identifier is None:
        abort(400, 'Nope.')

    if recovered:
        # Crediting back the user since we recovered an already paid for hand
        add_user_balance(current_user, bet_price)

    prize = 0
    if prize_info is not None:
        prize_multiplier = prize_info.get('multiplier')
        prize = bet_price * prize_multiplier

    response = {
        'identifier': identifier,
        'front_cards': front_cards,
        'matched_indexes': matched_indexes,
        'prize_info': prize_info,
        'prize': prize
    }

    if not recovered:
        add_log(
            current_user.id, 3, 'play_get', {
                'identifier': identifier,
                'front_cards': front_cards,
                'multiplier': bet_multiplier,
                'bet': bet_price,
                'prize': prize
            })
    else:
        response['balance'] = get_user(current_user.id).balance
        response['multiplier'] = multiplier

    return json.dumps(response)
Пример #7
0
def play_roulette_request():

    payload = request.get_json()

    bets = payload.get('bets')

    if not isinstance(bets, list):
        abort(400, 'Malformed request (#1)')

    if len(bets) == 0:
        abort(400, 'Please provide some bets')

    total_bet_amount = 0

    # Check
    for bet in bets:
        symbols = bet.get('symbols')
        if symbols is None or not isinstance(symbols, list) or len(symbols) not in VALID_COUNTS:
            abort(400, 'Unexpected symbols')

        amount = bet.get('amount')
        if not isinstance(amount, int) or amount <= 0:
            abort(400, 'Invalid bet amount')

        bet['multiplier'] = REGULAR_SYMBOL_COUNT / len(symbols)

        total_bet_amount += amount

    if not (MIN_BET_AMOUNT <= total_bet_amount <= MAX_BET_AMOUNT):
        abort(400, 'Invalid total bet amount')

    success = add_user_balance(current_user, -total_bet_amount)

    if not success:
        abort(400, 'Not enough balance')

    result = play_roulette(bets)

    prize = result.get('prize')
    if prize > 0:
        add_user_balance(current_user, prize)

    add_log(current_user.id, 2, 'play', {
        'prize': prize,
        'bet': total_bet_amount,
        'result': result.get('result')
    })

    return json.dumps(result)
Пример #8
0
def tip_target(target, amount):

    try:
        target_components = target.split('/')

        if target_components[0] == 'lncity':

            success = add_user_balance(current_user, -amount)

            if not success:
                return 412, 'Not enough balance'

            add_notification(current_user, None, 'tip_out', {
                'target_entity': 'lncity',
                'amount': amount
            }, seen=True)

            nel0_user = get_user_by_username('nel0')

            if nel0_user is not None:
                add_notification(nel0_user, current_user, 'tip_in', {
                    'source_entity': 'user',
                    'source_id': current_user.id,
                    'target_entity': 'lncity',
                    'amount': amount
                })

        elif target_components[0] == 'blogpost':
            blogpost_id = int(target_components[1])
            blogpost: Blogpost = get_blog_post(blogpost_id)
            if blogpost is None:
                return 404, 'Target not found'

            success = add_user_balance(current_user, -amount)

            if not success:
                return 412, 'Not enough balance'

            add_user_balance(blogpost.user, amount)

            blogpost.donation_amount += amount
            blogpost.donation_count += 1
            blogpost.save()

            add_notification(current_user, blogpost.user, 'tip_out', {
                'target_entity': 'blogpost',
                'target_id': blogpost_id,
                'amount': amount
            }, seen=True)

            add_notification(blogpost.user, current_user, 'tip_in', {
                'source_entity': 'user',
                'source_id': current_user.id,
                'target_entity': 'blogpost',
                'target_id': blogpost_id,
                'amount': amount
            })

        elif target_components[0] == 'blogpostcomment':

            blogpostcomment_id = int(target_components[1])
            blogpostcomment: Blogpostcomment = get_blog_post_comment(blogpostcomment_id)
            if blogpostcomment is None:
                return 404, 'Target not found'

            success = add_user_balance(current_user, -amount)

            if not success:
                return 412, 'Not enough balance'

            add_user_balance(blogpostcomment.user, amount)

            blogpostcomment.donation_amount += amount
            blogpostcomment.donation_count += 1
            blogpostcomment.save()

            add_notification(current_user, blogpostcomment.user, 'tip_out', {
                'target_entity': 'blogpostcomment',
                'target_id': blogpostcomment_id,
                'amount': amount
            }, seen=True)

            add_notification(blogpostcomment.user, current_user, 'tip_in', {
                'source_entity': 'user',
                'source_id': current_user.id,
                'target_entity': 'blogpostcomment',
                'target_id': blogpostcomment_id,
                'amount': amount
            })

        elif target_components[0] == 'user':

            try:
                user_id = int(target_components[1])
                user = get_user(user_id)
            except Exception as e:
                username = str(target_components[1])
                logging.info(username)
                user = get_user_by_username(username)

            if user is None:
                return 404, 'Target not found'

            success = add_user_balance(current_user, -amount)

            if not success:
                return 412, 'Not enough balance'

            add_user_balance(user, amount)

            add_notification(current_user, user, 'transfer_out', {
                'target_entity': 'user',
                'target_id': user.id,
                'amount': amount
            }, seen=True)

            add_notification(user, current_user, 'transfer_in', {
                'source_entity': 'user',
                'source_id': current_user.id,
                'target_entity': 'user',
                'target_id': user.id,
                'amount': amount
            })

        else:
            return 400, 'Unrecognizable target'
    except Exception as e:
        logging.exception(e)
        return 400, 'Malformed target'

    add_log(current_user.id, None, 'tip', {'target': target, 'amount': amount})

    return 200, 'Ok'