Beispiel #1
0
def handle_message(ch, method, properties, body):
    start_time = time.time()

    message = json.loads(body)

    # get the comment instance so we can reply to it
    comment = reddit.comment(message['comment_id'])

    comment_tokens = comment.body.strip().split(' ')
    if comment_tokens[0].lower() == '!deposit' and comment_tokens[1].isnumeric():
        player_username = comment.parent().author.name
        add_amount = int(comment_tokens[1])
        #print('Adding {} points to /u/{}\'s account...'.format(add_amount, player_username))

        player_dto = sg_repo.GET_PLAYER_BY_USERNAME(player_username)
        new_balance = player_dto['balance'] + add_amount

        #print('/u/{}\'s new balance is {}\n\n'.format(player_username, new_balance))
        sg_repo.UPDATE_PLAYER_BALANCE_BY_USERNAME(player_username, new_balance)

        SG_Utils.update_player_flair(player_username, new_balance, player_dto['flair_css_class'])

        reddit.redditor('eganwall').message('Deposit successful',
                                            'Successfully deposited {} for /u/{}'.format(add_amount, player_username))
        logger.log_info_message(message['message_id'], SG_Utils.LogUtilityConstants.deposit_success_event,
                                logger_name, '[new_balance={}]'.format(new_balance))

    ch.basic_ack(delivery_tag=method.delivery_tag)
    logger.log_info_message(message['message_id'], SG_Utils.LogUtilityConstants.handler_finished_event,
                            logger_name, 'Handler finished fulfilling request : [comment_id={}] [elapsed_seconds={:.3f}] [processing_time={:.3f}]'.format(
                                message['comment_id'],
                                SG_Utils.get_elapsed_secs(comment.created_utc, time.time()),
                                SG_Utils.get_elapsed_secs(start_time, time.time())))
Beispiel #2
0
def bot_loop():
    # get the Submission object for our dice roll thread
    submission = reddit.submission(id='6lppms')

    submission.comment_sort = 'new'
    submission.comments.replace_more(limit=0)
    for comment in list(submission.comments):
        # if we haven't processed this comment yet, make a new record for it and
        # process it
        if sg_repo.GET_COMMENT_BY_ID(comment.id) is None:
            sg_repo.INSERT_COMMENT_ID(comment.id)

            # if this player hasn't commented on the sub yet, make sure we
            # create a record of them, update their flair, and send them the
            # welcome PM
            if sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name) is None:
                sg_repo.INSERT_PLAYER(comment.author.name, starting_balance)
                SG_Utils.update_player_flair(comment.author.name, starting_balance, '')
                reddit.redditor(comment.author.name).message('Welcome!',
                                                             reply_messages.NEW_PLAYER_WELCOME_MESSAGE.format(
                                                                 comment.author.name),
                                                             from_subreddit='/r/SolutionGambling')

            # get the player from the DB so we can validate their wager
            # and update their balance
            player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)

            # now process the comment - first we convert it to lower
            post_body_lower = comment.body.lower()
            print("Processing post body: ".format(post_body_lower))

            wager_amount = parse_post_for_wager(post_body_lower, player['balance'])
            print("Wager amount from post: {}".format(wager_amount))

            if wager_amount == 0:
                print("Player has no balance")
                comment.reply(error_messages.COIN_FLIP_NO_BALANCE_ERROR_MSG.format(player['username']))
                continue

            if wager_amount == -1:
                print("Player triggered bot incorrectly")
                comment.reply(error_messages.COIN_FLIP_ERROR_MSG.format(player['username']))
                continue

            wager_result = play_coin_toss(wager_amount)
            new_player_balance = player['balance'] - wager_amount + wager_result['winnings']

            sg_repo.INSERT_WAGER(player['username'], wager_result['outcome'],
                                 wager_amount, wager_result['winnings'], new_player_balance, game_type)
            SG_Utils.update_player_after_wager(player['username'], new_player_balance, player['flair_css_class'])

            reply = format_wager_reply(player['username'], wager_amount, wager_result['toss_result'], wager_result['outcome'],
                                       wager_result['winnings'], new_player_balance)

            print("Reply formatted:\n{}".format(reply))
            comment.reply(reply)
        else:
            break
Beispiel #3
0
def handle_message(ch, method, properties, body):
    start_time = time.time()

    message = json.loads(body)

    # get the comment instance so we can reply to it
    comment = reddit.comment(message['comment_id'])

    # get the player from the DB so we can validate their wager
    # and update their balance
    player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)

    # create new player if this account hasn't played before
    if player is None:
        SG_Utils.add_new_player(comment.author.name, message['message_id'])
        player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)

    # now process the comment - first we convert it to lower
    post_body_lower = comment.body.lower()

    # parse the post for wagers
    remaining_balance = player['balance']
    player_wagers = list()
    for line in post_body_lower.splitlines():
        new_wager = parse_individual_wager(line, remaining_balance)
        player_wagers.append(new_wager)
        remaining_balance = new_wager['balance_after']

        logger.log_info_message(
            message['message_id'],
            SG_Utils.LogUtilityConstants.wager_validated_event, logger_name,
            '[wager_type={}] [wager_value={}] [wager_amount={}] [game_type={}]'
            .format(new_wager['wager_type'], new_wager['wager_value'],
                    new_wager['wager_amount'], game_type))

    # execute the ADVANCED ROULETTE SIMULATION
    roulette_result = spin_roulette()

    # analyze the results
    total_winnings = 0
    total_wagered = 0
    wager_results_string = """"""
    for index, wager in enumerate(player_wagers):
        outcome = determine_outcome(wager, roulette_result)

        if outcome == SG_Repository.WagerOutcome.WIN:
            wager_winnings = math.trunc(wager['wager_amount'] *
                                        payout_table[wager['wager_type']])
            wager_string = SG_Messages.ReplyMessages.ROULETTE_INDIVIDUAL_WAGER_TEMPLATE_MSG.format(
                index + 1, wager['wager_amount'], wager['wager_value'],
                outcome, wager_winnings)

            new_player_balance = player['balance'] - wager[
                'wager_amount'] + wager_winnings
            sg_repo.INSERT_WAGER(player['username'], outcome,
                                 wager['wager_amount'], wager_winnings,
                                 new_player_balance, game_type)
            total_wagered += wager['wager_amount']

            logger.log_info_message(
                message['message_id'],
                SG_Utils.LogUtilityConstants.wager_executed_event, logger_name,
                '[outcome={}] [new_balance={}]'.format(outcome,
                                                       new_player_balance))
        elif outcome == SG_Repository.WagerOutcome.LOSE:
            wager_winnings = 0
            wager_string = SG_Messages.ReplyMessages.ROULETTE_INDIVIDUAL_WAGER_TEMPLATE_MSG.format(
                index + 1, wager['wager_amount'], wager['wager_value'],
                outcome, wager_winnings)

            new_player_balance = player['balance'] - wager[
                'wager_amount'] + wager_winnings
            sg_repo.INSERT_WAGER(player['username'], outcome,
                                 wager['wager_amount'], wager_winnings,
                                 new_player_balance, game_type)
            total_wagered += wager['wager_amount']

            logger.log_info_message(
                message['message_id'],
                SG_Utils.LogUtilityConstants.wager_executed_event, logger_name,
                '[outcome={}] [new_balance={}]'.format(outcome,
                                                       new_player_balance))
        else:
            wager_winnings = 0
            wager_string = """Wager {}: """.format(
                index + 1) + wager['error_msg'] + """

 

"""
            logger.log_info_message(
                message['message_id'],
                SG_Utils.LogUtilityConstants.wager_rejected_event, logger_name,
                '[rejected_reason={}] [comment_id={}] [elapsed_seconds={:.3f}]'
                .format(
                    wager['error_msg'], message['comment_id'],
                    SG_Utils.get_elapsed_secs(comment.created_utc,
                                              time.time())))
        total_winnings += wager_winnings
        wager_results_string += wager_string

    # calculate the new balance after all of the bets are executed
    new_player_balance = player['balance'] - total_wagered + total_winnings

    # update the player's balance
    SG_Utils.update_player_after_wager(player['username'], new_player_balance,
                                       player['flair_css_class'],
                                       message['message_id'])
    updated_player = sg_repo.GET_PLAYER_BY_USERNAME(player['username'])

    # format and send the reply
    reply = SG_Messages.ReplyMessages.ROULETTE_REPLY_WRAPPER_TEMPLATE_MSG.format(
        updated_player['username'], roulette_result['value'],
        roulette_result['color'].upper(), wager_results_string, total_wagered,
        total_winnings, (total_winnings - total_wagered),
        updated_player['balance'])

    SG_Utils.post_comment_reply(comment, reply, message['message_id'])

    ch.basic_ack(delivery_tag=method.delivery_tag)
    logger.log_info_message(
        message['message_id'],
        SG_Utils.LogUtilityConstants.handler_finished_event, logger_name,
        'Handler finished fulfilling request : [comment_id={}] [elapsed_seconds={:.3f}] [processing_time={:.3f}]'
        .format(message['comment_id'],
                SG_Utils.get_elapsed_secs(comment.created_utc, time.time()),
                SG_Utils.get_elapsed_secs(start_time, time.time())))
Beispiel #4
0
# create our Reddit instance
c_id = config.get("General", "client_id")
c_secret = config.get("General", "client_secret")
user = config.get("General", "plain_username")
pw = config.get("General", "password")

reddit = praw.Reddit(
    client_id=c_id,
    client_secret=c_secret,
    username=user,
    password=pw,
    user_agent='Dealer bot v{} by /u/eganwall'.format(version))

# initialize our repository and logger
sg_repo = SG_Repository.Repository()
logger = SG_Utils.LogUtility()

# get our messaging classes
error_messages = SG_Messages.ErrorMessages
reply_messages = SG_Messages.ReplyMessages
constants = SG_Messages.MiscConstants


def handle_message(ch, method, properties, body):
    start_time = time.time()

    message = json.loads(body)

    # get the comment instance so we can reply to it
    comment = reddit.comment(message['comment_id'])
def handle_message(ch, method, properties, body):
    # get current time for elapsed time tracking
    start_time = time.time()

    message = json.loads(body)

    # get the comment instance so we can reply to it
    comment = reddit.comment(message['comment_id'])

    # get the player from the DB so we can validate their wager
    # and update their balance
    player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)

    # create new player if this account hasn't played before
    if player is None:
        SG_Utils.add_new_player(comment.author.name, message['message_id'])
        player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)

    # now process the comment
    wager_amount = parse_post_for_wager(message['comment_body'],
                                        player['balance'])
    logger.log_info_message(
        message['message_id'],
        SG_Utils.LogUtilityConstants.wager_validated_event, logger_name,
        '[wager_amount={}] [game_type={}]'.format(wager_amount, game_type))

    if wager_amount <= 0:
        #print("Wager amount not valid")
        SG_Utils.post_comment_reply(comment, error_messages.POKER_ERROR_MSG,
                                    message['message_id'])
        ch.basic_ack(delivery_tag=method.delivery_tag)
        logger.log_info_message(
            message['message_id'],
            SG_Utils.LogUtilityConstants.wager_rejected_event, logger_name,
            '[rejected_reason={}] [comment_id={}] [elapsed_seconds={:.3f}]'.
            format(SG_Utils.LogUtilityConstants.incorrect_format_reason,
                   message['comment_id'],
                   SG_Utils.get_elapsed_secs(comment.created_utc,
                                             time.time())))
        return

    if wager_amount > player['balance']:
        #print("Player wagered more than their balance")
        SG_Utils.post_comment_reply(
            comment, error_messages.INSUFFICIENT_BALANCE_ERROR_MSG,
            message['message_id'])
        ch.basic_ack(delivery_tag=method.delivery_tag)
        logger.log_info_message(
            message['message_id'],
            SG_Utils.LogUtilityConstants.wager_rejected_event, logger_name,
            '[rejected_reason={}] [comment_id={}] [elapsed_seconds={:.3f}]'.
            format(SG_Utils.LogUtilityConstants.insufficient_balance_reason,
                   message['comment_id'],
                   SG_Utils.get_elapsed_secs(comment.created_utc,
                                             time.time())))
        return

    if wager_amount > max_bet:
        #print("Player wagered more than this game's max bet")
        SG_Utils.post_comment_reply(
            comment, error_messages.OVER_MAX_BET_ERROR_MSG.format(max_bet),
            message['message_id'])
        ch.basic_ack(delivery_tag=method.delivery_tag)
        logger.log_info_message(
            message['message_id'],
            SG_Utils.LogUtilityConstants.wager_rejected_event, logger_name,
            '[rejected_reason={}] [comment_id={}] [elapsed_seconds={:.3f}]'.
            format(SG_Utils.LogUtilityConstants.over_max_bet_reason,
                   message['comment_id'],
                   SG_Utils.get_elapsed_secs(comment.created_utc,
                                             time.time())))
        return

    wager_result = play_poker(wager_amount, comment.id)
    new_player_balance = player['balance'] - wager_amount + wager_result[
        'winnings']

    sg_repo.INSERT_WAGER(player['username'], wager_result['outcome'],
                         wager_amount, wager_result['winnings'],
                         new_player_balance, game_type)
    SG_Utils.update_player_after_wager(player['username'], new_player_balance,
                                       player['flair_css_class'],
                                       message['message_id'])

    reply = format_wager_reply(player['username'], wager_amount,
                               wager_result['full_hand_string'],
                               wager_result['full_board_string'],
                               wager_result['hand_type'],
                               wager_result['outcome'],
                               wager_result['winnings'], new_player_balance)

    logger.log_info_message(
        message['message_id'],
        SG_Utils.LogUtilityConstants.wager_executed_event, logger_name,
        '[outcome={}] [new_balance={}]'.format(wager_result['outcome'],
                                               new_player_balance))

    SG_Utils.post_comment_reply(comment, reply, message['message_id'])

    ch.basic_ack(delivery_tag=method.delivery_tag)
    logger.log_info_message(
        message['message_id'],
        SG_Utils.LogUtilityConstants.handler_finished_event, logger_name,
        'Handler finished fulfilling request : [comment_id={}] [elapsed_seconds={:.3f}] [processing_time={:.3f}]'
        .format(message['comment_id'],
                SG_Utils.get_elapsed_secs(comment.created_utc, time.time()),
                SG_Utils.get_elapsed_secs(start_time, time.time())))
Beispiel #6
0
    submission = reddit.submission(id='6kiwhu')

    submission.comment_sort = 'new'
    submission.comments.replace_more(limit=0)
    for comment in list(submission.comments):
        # if we haven't processed this comment yet, make a new record for it and
        # process it
        if sg_repo.GET_COMMENT_BY_ID(comment.id) is None:
            sg_repo.INSERT_COMMENT_ID(comment.id)

            # if this player hasn't commented on the sub yet, make sure we
            # create a record of them, update their flair, and send them the
            # welcome PM
            if sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name) is None:
                sg_repo.INSERT_PLAYER(comment.author.name, starting_balance)
                SG_Utils.update_player_flair(comment.author.name,
                                             starting_balance, '')
                reddit.redditor(comment.author.name).message(
                    'Welcome!',
                    reply_messages.NEW_PLAYER_WELCOME_MESSAGE.format(
                        comment.author.name),
                    from_subreddit='/r/SolutionGambling')

            # get the player from the DB so we can validate their wager
            # and update their balance
            player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)

            # now process the comment - first we convert it to lower
            post_body_lower = comment.body.lower()
            print("Processing post body: ".format(post_body_lower))

            wager_amount = parse_post_for_wager(post_body_lower,
def handle_message(ch, method, properties, body):
    start_time = time.time()

    message = json.loads(body)

    # get the comment instance so we can reply to it
    comment = reddit.comment(message['comment_id'])

    # get the player from the DB so we can validate their purchase and update them
    player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)

    # create new player if this account hasn't played before
    if player is None:
        SG_Utils.add_new_player(comment.author.name, message['message_id'])
        player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)

    # now process the comment - first we convert it to lower
    post_body_lower = comment.body.lower()

    if (is_requesting_purchase(post_body_lower)):
        target_flair_level = player['flair_level'] + 1
        log_msg = "/u/{} is requesting upgrade to level {}".format(
            player['username'], target_flair_level)

        logger.log_info_message(message['message_id'],
                                SG_Utils.LogUtilityConstants.flair_req_event,
                                logger_name, log_msg)

        target_flair = flair_table[target_flair_level]

        if target_flair_level == len(flair_table):
            logger.log_info_message(
                message['message_id'],
                SG_Utils.LogUtilityConstants.flair_rejected_event, logger_name,
                '[rejected_reason={}] [comment_id={}] [elapsed_seconds={:.3f}]'
                .format(
                    SG_Utils.LogUtilityConstants.max_flair_level_reason,
                    message['comment_id'],
                    SG_Utils.get_elapsed_secs(comment.created_utc,
                                              time.time())))
            reply = error_messages.FLAIR_SHOP_ALREADY_MAX_LEVEL.format(
                player['username'])
        elif player['balance'] < target_flair['cost']:
            cost_str = "[flair_cost={}]".format(target_flair['cost'])

            logger.log_info_message(
                message['message_id'],
                SG_Utils.LogUtilityConstants.flair_rejected_event, logger_name,
                '[rejected_reason={}] [comment_id={}] [elapsed_seconds={:.3f}] {}'
                .format(
                    SG_Utils.LogUtilityConstants.insufficient_balance_reason,
                    message['comment_id'],
                    SG_Utils.get_elapsed_secs(comment.created_utc,
                                              time.time()), cost_str))

            reply = error_messages.FLAIR_SHOP_INSUFFICIENT_BALANCE_ERROR_MSG.format(
                player['username'], target_flair_level, target_flair['cost'],
                player['balance'])
        else:
            new_flair_css_class = target_flair['css_class']
            new_player_balance = player['balance'] - target_flair['cost']

            logger.log_info_message(
                message['message_id'],
                SG_Utils.LogUtilityConstants.flair_executed_event, logger_name,
                '[new_balance={}]'.format(new_player_balance))

            update_player_after_purchase(player['username'],
                                         new_player_balance,
                                         target_flair_level,
                                         new_flair_css_class)
            reply = reply_messages.FLAIR_SHOP_SUCCESS_MSG.format(
                player['username'], target_flair_level, target_flair['cost'],
                new_player_balance)

        SG_Utils.post_comment_reply(comment, reply, message['message_id'])

    ch.basic_ack(delivery_tag=method.delivery_tag)
    logger.log_info_message(
        message['message_id'],
        SG_Utils.LogUtilityConstants.handler_finished_event, logger_name,
        'Handler finished fulfilling request : [comment_id={}] [elapsed_seconds={:.3f}] [processing_time={:.3f}]'
        .format(message['comment_id'],
                SG_Utils.get_elapsed_secs(comment.created_utc, time.time()),
                SG_Utils.get_elapsed_secs(start_time, time.time())))
def update_player_after_purchase(username, new_balance, flair_level,
                                 flair_class):
    sg_repo.UPDATE_PLAYER_FLAIR_BY_USERNAME(username, flair_level, flair_class)
    SG_Utils.update_player_after_wager(username, new_balance, flair_class)
def handle_message(ch, method, properties, body):
    start_time = time.time()

    message = json.loads(body)

    # get the comment instance so we can reply to it
    comment = reddit.comment(message['comment_id'])

    # get the player from the DB so we can validate their wager
    # and update their balance
    player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)

    # create new player if this account hasn't played before
    if player is None:
        SG_Utils.add_new_player(comment.author.name, message['message_id'])
        player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)

    wager_amount = parse_post_for_wager(message['comment_body'], player['balance'])
    logger.log_info_message(message['message_id'], SG_Utils.LogUtilityConstants.wager_validated_event,
                            logger_name, '[wager_amount={}] [game_type={}]'.format(wager_amount, game_type))

    if wager_amount == 0:
        # print("Player has no balance")
        SG_Utils.post_comment_reply(comment, error_messages.AON_DICE_ROLL_NO_BALANCE_ERROR_MSG.format(player['username'], message['message_id']))
        ch.basic_ack(delivery_tag=method.delivery_tag)
        logger.log_info_message(message['message_id'], SG_Utils.LogUtilityConstants.wager_rejected_event,
                                logger_name, '[rejected_reason={}] [comment_id={}] [elapsed_seconds={:.3f}]'.format(
                                    SG_Utils.LogUtilityConstants.insufficient_balance_reason, message['comment_id'],
                                    SG_Utils.get_elapsed_secs(comment.created_utc, time.time())))
        return

    if wager_amount == -1:
        # print("Player triggered bot incorrectly")
        SG_Utils.post_comment_reply(comment, error_messages.AON_DICE_ROLL_ERROR_MSG.format(player['username'], message['message_id']))
        ch.basic_ack(delivery_tag=method.delivery_tag)
        logger.log_info_message(message['message_id'], SG_Utils.LogUtilityConstants.wager_rejected_event,
                                logger_name, '[rejected_reason={}] [comment_id={}] [elapsed_seconds={:.3f}]'.format(
                                    SG_Utils.LogUtilityConstants.incorrect_format_reason, message['comment_id'],
                                    SG_Utils.get_elapsed_secs(comment.created_utc, time.time())))
        return

    wager_result = play_dice(wager_amount)
    new_player_balance = player['balance'] - wager_amount + wager_result['winnings']

    sg_repo.INSERT_WAGER(player['username'], wager_result['outcome'],
                         wager_amount, wager_result['winnings'], new_player_balance, game_type)
    SG_Utils.update_player_after_wager(player['username'], new_player_balance, player['flair_css_class'], message['message_id'])

    reply = format_wager_reply(player['username'], wager_amount, wager_result['die_roll_result'],
                               wager_result['outcome'], wager_result['winnings'], new_player_balance)

    logger.log_info_message(message['message_id'], SG_Utils.LogUtilityConstants.wager_executed_event,
                            logger_name, '[outcome={}] [new_balance={}]'.format(wager_result['outcome'], new_player_balance))

    SG_Utils.post_comment_reply(comment, reply, message['message_id'])

    ch.basic_ack(delivery_tag=method.delivery_tag)
    logger.log_info_message(message['message_id'], SG_Utils.LogUtilityConstants.handler_finished_event,
                            logger_name, 'Handler finished fulfilling request : [comment_id={}] [elapsed_seconds={:.3f}] [processing_time={:.3f}]'.format(
                                message['comment_id'],
                                SG_Utils.get_elapsed_secs(comment.created_utc, time.time()),
                                SG_Utils.get_elapsed_secs(start_time, time.time())))