Пример #1
0
 def test_allin(self):
     self.command("!create")
     replies = self.command("!investi 200", post="testpost1")
     self.assertEqual(len(replies), 1)
     replies = self.command("!investitutto", post="testpost2")
     self.assertEqual(len(replies), 1)
     self.assertEqual(replies[0].body, message.modify_invest(800, 100, 0))
Пример #2
0
 def test_allin(self):
     self.command('!create')
     replies = self.command('!investi 200', post='testpost1')
     self.assertEqual(len(replies), 1)
     replies = self.command('!investitutto', post='testpost2')
     self.assertEqual(len(replies), 1)
     self.assertEqual(replies[0].body, message.modify_invest(800, 100, 0))
Пример #3
0
def invest(comment, author, text):
    post = reddit.submission(comment.submission)
    post_ID = post.id
    upvotes = post.ups
    investor = users[author]
    
    # If it can't extract the invest amount, abandon the operation
    try:
        investm = int(float(text.replace("!invest", "").replace(" ", "")))
    except ValueError as ve:
        return False

    if (investm < 100):
        send_not(comment, message.min_invest_org, False)
        return False
    
    is_enough = investor.enough(investm)
    
    if (is_enough):
        commentID = send_not(comment, message.modify_invest(investm, upvotes, investor.get_balance() - investm), False)
        inv = investor.invest(post_ID, upvotes, commentID, investm)
        
        awaiting.append(inv)
        save_data()
        return True
    else:
        send_not(comment, message.insuff_org, False)
        return True
Пример #4
0
    def investi(self, sess, comment, investor, amount, suffix):
        """
        This function invests
        """
        if config.CLOSED:
            return comment.reply_wrap(message.CLOSED_ORG)

        multiplier = CommentWorker.multipliers.get(suffix, 1)

        # Allows input such as '!invest 100%' and '!invest 50%'
        if multiplier == "%":
            amount = int(investor.balance * (int(amount) / 100))
        else:
            try:
                amount = int(amount.replace(",", ""))
                amount = amount * multiplier
            except ValueError:
                return

        # Sets the minimum investment to 1% of an investor's balance or 100 Mc
        minim = int(investor.balance / 100)
        if amount < minim or amount < 100:
            return comment.reply_wrap(message.modify_min_invest(minim))

        author = comment.author.name
        new_balance = investor.balance - amount

        if new_balance < 0:
            return comment.reply_wrap(message.modify_insuff(investor.balance))

        upvotes_now = int(comment.submission.ups)
        # apply 15 minute grace period
        if comment.created_utc - comment.submission.created_utc < 60 * 15:
            upvotes_now = min(upvotes_now,
                              int(math.pow(3, upvotes_now / 5) - 1))
        # 0 upvotes is too strong, so what we do is make around 1 minumum
        if upvotes_now < 1:
            upvotes_now = 1
        deltatime = min(
            int((comment.created_utc - comment.submission.created_utc) / 60),
            60)

        # Sending a confirmation
        response = comment.reply_wrap(
            message.modify_invest(amount, upvotes_now, new_balance))

        sess.add(
            Investment(
                post=comment.submission.id,
                upvotes=upvotes_now,
                deltatime=deltatime,
                comment=comment.id,
                name=author,
                amount=amount,
                response=response.id,
                done=False,
            ))

        investor.balance = new_balance
Пример #5
0
    def invest(self, sess, comment, investor, amount, suffix):
        """
        This function invests
        """
        if config.PREVENT_INSIDERS:
            if comment.submission.author.name == comment.author.name:
                return comment.reply_wrap(message.INSIDE_TRADING_ORG)
        multiplier = CommentWorker.multipliers.get(suffix, 1)

        # Allows input such as '!invest 100%' and '!invest 50%'
        if multiplier == '%':
            amount = int(investor.balance * (int(amount)/100))
        else:
            try:
                amount = int(amount.replace(',', ''))
                amount = amount * multiplier
            except ValueError:
                return

        # Sets the minimum investment to 1% of an investor's balance or 100 Mc
        minim = int(investor.balance / 100)
        if amount < minim or amount < 100:
            return comment.reply_wrap(message.modify_min_invest(minim))

        author = comment.author.name
        new_balance = investor.balance - amount

        if new_balance < 0:
            return comment.reply_wrap(message.modify_insuff(investor.balance))

        # Sending a confirmation
        response = comment.reply_wrap(message.modify_invest(
            amount,
            comment.submission.ups,
            new_balance
        ))

        # 0 upvotes is too OP, so what we do is make around 5 minumum
        upvotes_now = int(comment.submission.ups)
        if upvotes_now < 5:
            upvotes_now = 5

        sess.add(Investment(
            post=comment.submission,
            upvotes=upvotes_now,
            comment=comment,
            name=author,
            amount=amount,
            response=response,
            done=False,
        ))

        investor.balance = new_balance
Пример #6
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)
Пример #7
0
    def invest(self, sess, comment, investor, amount):
        # Post related vars
        if not investor:
            return

        if comment.submission.author.name == comment.author.name:
            comment.reply(message.inside_trading_org)
            return

        try:
            amount = int(amount)
        except ValueError:
            return

        if amount < 100:
            comment.reply_wrap(message.min_invest_org)
            return

        author = comment.author.name
        new_balance = investor.balance - amount

        if new_balance < 0:
            comment.reply_wrap(message.insuff_org)
            return

        # Sending a confirmation
        response = comment.reply_wrap(message.modify_invest(
            amount,
            comment.submission.ups,
            new_balance
        ))

        sess.add(Investment(
            post=comment.submission,
            upvotes=comment.submission.ups,
            comment=comment,
            name=author,
            amount=amount,
            response=response,
            done=False,
        ))

        sess.query(Investor).\
            filter(Investor.name == author).\
            update({
                Investor.balance: new_balance,
            }, synchronize_session=False)
Пример #8
0
    def invest(self, sess, comment, investor, amount):
        if not isinstance(comment, praw.models.Comment):
            return

        if config.prevent_insiders:
            if comment.submission.author.name == comment.author.name:
                comment.reply_wrap(message.inside_trading_org)
                return

        try:
            amount = int(amount)
        except ValueError:
            return

        if amount < 100:
            comment.reply_wrap(message.min_invest_org)
            return

        author = comment.author.name
        new_balance = investor.balance - amount

        if new_balance < 0:
            comment.reply_wrap(message.insuff_org)
            return

        # Sending a confirmation
        response = comment.reply_wrap(message.modify_invest(
            amount,
            comment.submission.ups,
            new_balance
        ))

        sess.add(Investment(
            post=comment.submission,
            upvotes=comment.submission.ups,
            comment=comment,
            name=author,
            amount=amount,
            response=response,
            done=False,
        ))

        investor.balance = new_balance
    def invest(self, sess, comment, investor, amount, suffix):
        """
        This function invests
        """
        if config.PREVENT_INSIDERS:
            if comment.submission.author.name == comment.author.name:
                comment.reply_wrap(message.INSIDE_TRADING_ORG)
                return

        try:
            amount = float(amount.replace(',', ''))
            amount = int(amount * CommentWorker.multipliers.get(suffix, 1))
        except ValueError:
            return

        if amount < 100:
            comment.reply_wrap(message.MIN_INVEST_ORG)
            return

        author = comment.author.name
        new_balance = investor.balance - amount

        if new_balance < 0:
            comment.reply_wrap(message.modify_insuff(investor.balance))
            return

        # Sending a confirmation
        response = comment.reply_wrap(
            message.modify_invest(amount, comment.submission.ups, new_balance))

        sess.add(
            Investment(
                post=comment.submission,
                upvotes=comment.submission.ups,
                comment=comment,
                name=author,
                amount=amount,
                response=response,
                done=False,
            ))

        investor.balance = new_balance
Пример #10
0
    def test_basic(self):
        self.command('!create')

        replies = self.command('!invest 100')
        self.assertEqual(len(replies), 1)
        self.assertEqual(replies[0], message.modify_invest(100, 100, 900))
Пример #11
0
    def test_invest_50_percent(self):
        self.command('!create')

        replies = self.command('!invest 50%')
        self.assertEqual(len(replies), 1)
        self.assertEqual(replies[0], message.modify_invest(500, 100, 500))
Пример #12
0
    def test_invest_50_percent(self):
        self.command("!create")

        replies = self.command("!invest 50%")
        self.assertEqual(len(replies), 1)
        self.assertEqual(replies[0].body, message.modify_invest(500, 100, 500))
Пример #13
0
 def test_basic(self):
     self.command("!create")
     replies = self.command("!investi 100")
     self.assertEqual(len(replies), 1)
     self.assertEqual(replies[0].body, message.modify_invest(100, 100, 900))
Пример #14
0
    def test_invest_100_percent(self):
        self.command('!create')

        replies = self.command('!investi 1k')
        self.assertEqual(len(replies), 1)
        self.assertEqual(replies[0].body, message.modify_invest(1000, 100, 0))