예제 #1
0
def check_investments():

    print("Starting checking investments...")
    while True:
        time.sleep(60)
        done_ids = database.investment_find_done()
        for id_number in done_ids:
            # I know that everything can be compacted in a tuple
            # This way it is more understandable
            name = database.investment_get_name(id_number)
            postID = database.investment_get_post(id_number)
            upvotes_old = database.investment_get_upvotes(id_number)
            amount = database.investment_get_amount(id_number)
            responseID = database.investment_get_response(id_number)
            response = reddit.comment(id=responseID)

            # If comment is deleted, skip it
            try:
                commentID = database.investment_get_comment(id_number)
                comment = reddit.comment(id=commentID)
            except:
                response.edit(message.deleted_comment_org)
                continue

            post = reddit.submission(postID)
            upvotes_now = post.ups

            # Updating the investor's balance
            factor = calculate(upvotes_now, upvotes_old)
            balance = database.investor_get_balance(name)
            new_balance = int(balance + (amount * factor))
            database.investor_update_balance(name, new_balance)
            change = new_balance - balance

            # Updating the investor's variables
            active = database.investor_get_active(name)
            active -= 1
            database.investor_update_active(name, active)

            completed = database.investor_get_completed(name)
            completed += 1
            database.investor_update_completed(name, completed)

            # Marking the investment as done
            database.investment_update_done(id_number)

            # Editing the comment as a confirmation
            text = response.body
            if (factor > 1):
                response.edit(message.modify_invest_return(text, change))
                database.investment_update_success(id_number)
            else:
                lost_memes = int(amount - (amount * factor))
                response.edit(message.modify_invest_lose(text, lost_memes))

            print(f"Investment returned! {change}")
예제 #2
0
def invest(comment, author):

    # Post related vars
    post = reddit.submission(comment.submission)
    post_author = post.author.name.lower()

    # Insider trading is not allowed!
    if (author == post.author):
        send_reply(comment, message.inside_trading_org)
        return False

    postID = post.id
    commentID = comment.id
    upvotes = post.ups

    # UNIX timestamp
    unix = time.time() 

    # The invest amount, if fails, return False
    text = comment.body.lower()
    invest_string = text.replace("!invest", "").replace(" ", "")
    try:
        invest_amount = int(float(invest_string))
    except ValueError:
        return False
    
    if (invest_amount < 100):
        send_reply(comment, message.min_invest_org)
        return False
    
    # Balance operations
    balance = database.investor_get_balance(author)
    active = database.investor_get_active(author)
    new_balance = balance - invest_amount

    if (new_balance < 0):
        send_reply(comment, message.insuff_org)
        return False

    active += 1

    # Sending a confirmation
    response = send_reply(comment, message.modify_invest(invest_amount, upvotes, new_balance))
    # If comment is not present, exit
    if (not response):
        return False
    # Filling the database
    database.investment_insert(post, upvotes, comment, author, invest_amount, unix, response)
    database.investor_update_balance(author, new_balance)
    database.investor_update_active(author, active)
예제 #3
0
def broke(comment, author):

    balance_amount = database.investor_get_balance(author)
    active_number = database.investor_get_active(author)

    if (balance_amount < 100):
        if (active_number < 1):
            # Indeed, broke
            database.investor_update_balance(author, 100)
            database.investor_update_active(author, 0)
            broke_times = database.investor_get_broke(author)
            broke_times += 1
            database.investor_update_broke(author, broke_times)
            # Sure, you can do it like
            # database.investor_get_broke(author, database.investor_get_broke(author) + 1)
            # But it is way to messy, we are for the code understandability
            
            send_reply(comment, message.modify_broke(broke_times))
        else:
            # Still has investments
            send_reply(comment, message.modify_broke_active(active_number))
    else:
        # Still can invest
        send_reply(comment, message.modify_broke_money(balance_amount))
예제 #4
0
def activity(comment, author):

    active = database.investor_get_active(author)
    send_reply(comment, message.modify_active(active))