コード例 #1
0
 def build_world(self):
     TicketManager.create_default_ticket_amounts()
     self.create_site_sports()
     self.create_sport_and_rosters()
     self.create_simple_player_stats_list()
     self.create_pool_and_draftgroup()
     self.create_contest()
コード例 #2
0
    def handle(self, *args, **options):
        """
        generate a test game with questions

        :param args:
        :param options:
        :return:
        """

        values = []
        for x in options['values']:
            values.append(x)

        max_entries = float(values[0])
        # ticket_amount   = float(values[1])
        # num_payouts     = int(values[2])
        # entries         = int(values[3])
        # try:
        #     name = ' '.join( values[4:] )
        # except:
        #     name = ''
        #
        # self.stdout.write('%s %s %s name[%s]' % (str(buyin),
        #                         str(ticket_amount), str(num_payouts), str(name)))

        # creator = TicketPrizeStructureCreator(buyin, ticket_amount, num_payouts, entries, name=name)
        # creator.save()

        # print out possible prize structures for Flat Ticket Prize structures
        TicketManager.create_default_ticket_amounts()
        FlatTicketPrizeStructureCreator.print_all(max_entries)
コード例 #3
0
    def __refund_entry(self, entry):
        """
        refund a single entry.

        THIS DOES NOT remove the entry from a contest!

        :param entry:
        :return:
        """

        buyin = entry.contest_pool.prize_structure.buyin
        bm = BuyinManager(entry.user)
        transaction = None

        # Create a cash or ticket deposit as a refund,
        # based on what the user used to get into the contest
        if bm.entry_did_use_ticket(entry):
            tm = TicketManager(entry.user)
            tm.deposit(buyin)
            transaction = tm.transaction
            refund = self.__create_refund(transaction, entry)
        else:
            ct = CashTransaction(entry.user)
            ct.deposit(buyin)
            transaction = ct.transaction
            refund = self.__create_refund(transaction, entry)

        # Create refund transaction from escrow
        escrow_ct = CashTransaction(self.get_escrow_user())
        escrow_ct.withdraw(buyin, trans=transaction)
        return refund
コード例 #4
0
    def setUp(self):
        super().setUp()
        self.user1 = self.get_basic_user("test1")
        self.user2 = self.get_basic_user("test2")
        self.user3 = self.get_basic_user("test3")
        self.build_world()
        self.user1_ct = CashTransaction(self.user1)
        self.user1_ct.deposit(100)

        self.user2_ct = CashTransaction(self.user2)
        self.user2_ct.deposit(50)

        ta = TicketAmount.objects.get(amount=10.00)
        self.user3_tm = TicketManager(self.user3)
        self.user3_tm.deposit(10)

        self.escrow_user = self.user2_ct.get_escrow_user()

        self.escrow_ct = CashTransaction(self.escrow_user)

        bm = BuyinManager(self.user1)
        bm.buyin(self.contest_pool)

        bm = BuyinManager(self.user2)
        bm.buyin(self.contest_pool)

        bm = BuyinManager(self.user3)
        bm.buyin(self.contest_pool)
        Entry.objects.filter(contest_pool=self.contest_pool).update(
            contest=self.contest)
コード例 #5
0
 def build_world(self):
     TicketManager.create_default_ticket_amounts()
     self.world = BuildWorldForTesting()
     self.world.build_world()
     self.world.contest.entries = 3
     self.contest = self.world.contest
     draftgroup = self.world.draftgroup
     self.contest_pool, created = ContestPoolCreator(
         'nfl', self.contest.prize_structure, draftgroup.start,
         (draftgroup.end - draftgroup.start).seconds * 60,
         draftgroup).get_or_create()
コード例 #6
0
    def setUp(self):
        super().setUp()
        # creates very standard ticket amounts like 1,2,5, 10, 20, 50
        TicketManager.create_default_ticket_amounts(verbose=False)

        self.first = 34.0
        self.second = 10.0
        self.third = 10.0
        #
        # create a simple Rank and Prize Structure
        cps = CashPrizeStructureCreator(name='test1')
        cps.add(1, self.first)
        cps.add(2, self.second)
        cps.add(3, self.third)
        cps.set_buyin(10)
        cps.save()
        self.prize_structure = cps.prize_structure
        self.prize_structure.generator.prize_pool = 54.0  # minus rake
        self.prize_structure.save()
        self.ranks = cps.ranks

        self.world = BuildWorldForTesting()
        self.world.build_world()
        self.draftgroup = self.world.draftgroup

        self.contest = self.world.contest
        self.contest.status = Contest.SCHEDULED
        self.contest.prize_structure = self.prize_structure
        self.contest.draft_group = self.draftgroup
        self.contest.entries = 6
        self.contest.save()

        self.contest_pool, created = ContestPoolCreator(
            'nfl', self.prize_structure, self.draftgroup.start,
            (self.draftgroup.end - self.draftgroup.start).seconds * 60,
            self.draftgroup).get_or_create()
        self.contest_pool.entries = 6
        self.contest_pool.save()
        self.scorer_class = TestSalaryScoreSystem
コード例 #7
0
 def create(sport='nfl'):
     TicketManager.create_default_ticket_amounts()
     ticket_amount = TicketAmount.objects.all()[0]
     # ticket_amount.amount
     ticket_prize_creator = TicketPrizeStructureCreator(
         ticket_value=5.00,
         number_of_prizes=1,
         name='contest-dummy-prize-structure')
     prize_structure = ticket_prize_creator.save()
     # prize_structure.name
     now = timezone.now()
     start = DfsDateTimeUtil.create(now.date(), time(23, 0))
     end = DfsDateTimeUtil.create(now.date() + timedelta(days=1),
                                  time(0, 0))
     creator = ContestCreator(name=Dummy.DEFAULT_NAME,
                              sport=sport,
                              prize_structure=prize_structure,
                              start=start,
                              end=end)
     contest = creator.create()
     logger.info('created new dummy contest with pk: %s' % contest.pk)
     return contest
コード例 #8
0
    def test_simple_ticket_buyin(self):
        tm = TicketManager(self.user)
        try:
            tm.get_ticket_amount(self.buyin)

        except Exception:
            ta = TicketAmount()
            ta.amount = self.buyin
            ta.save()
        tm.deposit(amount=self.buyin)
        bm = BuyinManager(self.user)
        bm.buyin(self.contest_pool)
        tm.ticket.refresh_from_db()
        self.assertEqual((tm.ticket.consume_transaction is not None), True)
コード例 #9
0
    def setUp(self):
        super().setUp()
        # ensure the default ticket
        TicketManager.create_default_ticket_amounts(verbose=False)
        # add funds to user
        self.user = self.get_basic_user()
        ct = CashTransaction(self.user)
        ct.deposit(100)

        # salary_generator = Dummy.generate_salaries()
        # self.salary_pool = salary_generator.pool
        # start
        #
        #
        self.verbose = True  # set to False to disable print statements

        #
        # The sport we are going to build fake stats for.
        # Lets use nfl, but it doesnt matter what sport we use
        self.sport = 'nfl'

        #
        # Ensure there are Games by using the Dummy to generate fake stats.
        # The ScheduleManager requires that Game objects exist
        # because when it creates scheduled Contest objects
        # it is required to create a draft group.
        self.dummy = Dummy(sport=self.sport)
        self.generator = self.dummy.generate()
        self.salary_pool = self.generator.pool
        self.site_sport = self.dummy.site_sport  # stash the site_sport for easy use

        self.site_sport_manager = SiteSportManager()
        self.game_model = self.site_sport_manager.get_game_class(
            self.site_sport)  # ie: sports.nfl.models.Game
        self.games = self.game_model.objects.all()  # there should be handful now, for today
        if self.games.count() <= 0:
            raise Exception(
                'buyin.tests.BuyinTest - we meant to create games.... but none were created!')
        # end

        # create a simple prize pool
        self.first = 100.0
        self.second = 50.0
        self.third = 25.0
        self.buyin = 10
        cps = CashPrizeStructureCreator(name='test')
        cps.add(1, self.first)
        cps.add(2, self.second)
        cps.add(3, self.third)
        cps.set_buyin(self.buyin)
        cps.save()
        cps.prize_structure.save()

        self.prize_structure = cps.prize_structure
        self.ranks = cps.ranks

        #
        # create the Contest
        # now = timezone.now()
        # start = DfsDateTimeUtil.create(now.date(), time(23,0))
        # end = DfsDateTimeUtil.create(now.date() + timedelta(days=1), time(0,0))
        start = self.games[0].start + timedelta(minutes=5)
        end = self.games[self.games.count() - 1].start  # set 'end' to start of last game
        cc = ContestCreator("test_contest", self.sport, self.prize_structure, start, end)
        self.draft_group2 = DraftGroup()
        self.draft_group2.salary_pool = self.salary_pool
        self.draft_group2.start = start
        self.draft_group2.end = end
        self.draft_group2.save()

        self.contest_pool, created = ContestPoolCreator(
            self.sport,
            self.prize_structure,
            start,
            (end - start).seconds * 60,
            self.draft_group2
        ).get_or_create()
        self.contest = cc.create()
        self.contest.status = Contest.RESERVABLE
        self.contest.save()

        self.draft_group = DraftGroup()
        self.draft_group.salary_pool = self.salary_pool
        self.draft_group.start = start
        self.draft_group.end = end
        self.draft_group.save()
コード例 #10
0
    def buyin(self, contest_pool, lineup=None):
        """
        Creates the buyin for the user based on the ContestPool and lineup. Lineup can
        be null or not passed to allow for reserving contest spots.

        This should be only run in a task

        :param contest_pool: the ContestPool to register in
        :param lineup: assumed the lineup is validated on creation

        :raises :class:`contest.exceptions.ContestCouldNotEnterException`: When the
        there is a race condition and the contest cannot be entered after max_retries
        :raises :class:`contest.exceptions.ContestIsNotAcceptingLineupsException`: When
            contest_pool does not have a draftgroup, because it is not accepting teams yet.
        :raises :class:`contest.exceptions.ContestLineupMismatchedDraftGroupsException`:
            When the lineup was picked from a draftgroup that does not match the contest_pool.
        :raises :class:`contest.exceptions.ContestIsInProgressOrClosedException`: When
            The contest_pool has started, or its past the start time
        :raises :class:`contest.exceptions.LineupDoesNotMatchUser`: When the lineup
            is not owned by the user.
        :raises :class:`contest.exceptions.ContestMaxEntriesReachedException`: When the
            max entries is reached by the lineup.
        """

        # validate the contest and the lineup are allowed to be created
        self.lineup_contest(contest_pool, lineup)

        # Create either the ticket or cash transaction
        # Try to pay with a ticket first, if that doesn't work because the user doesn't have any
        # tickets, then try to pay with their cash balance.
        tm = TicketManager(self.user)
        # Get the transaction type - `ContestBuyin`
        # category = TransactionType.objects.get(pk=TransactionTypeConstants.ContestBuyin)

        try:
            tm.consume(amount=contest_pool.prize_structure.buyin)
            # keep a reference of the transaction.
            transaction = tm.transaction

        except (UserDoesNotHaveTicketException, ticket.models.TicketAmount.DoesNotExist):
            # Paying via Ticket failed, Create a cash transaciton with the type of 'ContestBuyin'.
            ct = CashTransaction(user=self.user)
            # Make the transaction a withdrawal.
            ct.withdraw(amount=contest_pool.prize_structure.buyin)
            # keep a reference of the transaction for user later.
            transaction = ct.transaction

        #
        # Transfer money into escrow
        escrow_ct = CashTransaction(self.get_escrow_user())
        escrow_ct.deposit(contest_pool.prize_structure.buyin, trans=transaction)

        #
        # Create the Entry
        entry = Entry()
        entry.contest_pool = contest_pool
        # entry.contest = contest # the contest will be set later when the ContestPool starts
        entry.contest = None
        entry.lineup = lineup
        entry.user = self.user
        entry.save()

        #
        # Create the Buyin model
        buyin = Buyin()
        buyin.contest_pool = contest_pool
        buyin.transaction = transaction
        # buyin.contest = contest # the contest will be set later when the ContestPool starts (?)
        buyin.contest = None
        buyin.entry = entry
        buyin.save()

        #
        # Increment the contest_entry variable
        contest_pool.current_entries = F('current_entries') + 1
        contest_pool.save()
        contest_pool.refresh_from_db()

        msg = "User[" + self.user.username + "] bought into the contest_pool #" \
              + str(contest_pool.pk) + " with entry #" + str(entry.pk)
        Logger.log(ErrorCodes.INFO, "ContestPool Buyin", msg)

        #
        # pusher contest updates because entries were changed
        ContestPoolPush(ContestPoolSerializer(contest_pool).data).send()
        return entry
コード例 #11
0
class RefundTest(AbstractTest, RefundBuildWorldMixin):
    def setUp(self):
        super().setUp()
        self.user1 = self.get_basic_user("test1")
        self.user2 = self.get_basic_user("test2")
        self.user3 = self.get_basic_user("test3")
        self.build_world()
        self.user1_ct = CashTransaction(self.user1)
        self.user1_ct.deposit(100)

        self.user2_ct = CashTransaction(self.user2)
        self.user2_ct.deposit(50)

        ta = TicketAmount.objects.get(amount=10.00)
        self.user3_tm = TicketManager(self.user3)
        self.user3_tm.deposit(10)

        self.escrow_user = self.user2_ct.get_escrow_user()

        self.escrow_ct = CashTransaction(self.escrow_user)

        bm = BuyinManager(self.user1)
        bm.buyin(self.contest_pool)

        bm = BuyinManager(self.user2)
        bm.buyin(self.contest_pool)

        bm = BuyinManager(self.user3)
        bm.buyin(self.contest_pool)
        Entry.objects.filter(contest_pool=self.contest_pool).update(
            contest=self.contest)

    def test_refund(self):

        self.assertEqual(self.user1_ct.get_balance_amount(), 90)
        self.assertEqual(self.user2_ct.get_balance_amount(), 40)
        self.assertEqual(self.escrow_ct.get_balance_amount(), 30)
        self.assertEqual(self.user3_tm.get_available_tickets().count(), 0)

        refund_manager = RefundManager()
        refund_manager.refund(self.contest, force=True)

        self.assertEqual(self.user1_ct.get_balance_amount(), 100)
        self.assertEqual(self.user2_ct.get_balance_amount(), 50)
        self.assertEqual(self.escrow_ct.get_balance_amount(), 0)
        self.assertEqual(self.user3_tm.get_available_tickets().count(), 1)

    # in progress should be refundable in cases
    # where its not a GPP and it did not fill
    # def test_contest_is_in_progress(self):
    #     self.contest.status = self.contest.INPROGRESS
    #     self.contest.save()
    #     self.should_raise_contest_is_in_progress_or_closed_exception()

    def test_contest_is_cancelled(self):
        self.contest.status = self.contest.CANCELLED
        self.contest.save()
        self.should_raise_contest_is_cancelled_or_closed()

    def test_contest_is_closed(self):
        self.contest.status = self.contest.CLOSED
        self.contest.save()
        self.should_raise_contest_is_cancelled_or_closed()

    # completed is an "in progress" status, which has
    # no more live inprogress games
    # def test_contest_is_completed(self):
    #     self.contest.status = self.contest.COMPLETED
    #     self.contest.save()
    #     self.should_raise_contest_is_in_progress_or_closed_exception()

    def should_raise_contest_is_cancelled_or_closed(self):

        self.assertEqual(self.user1_ct.get_balance_amount(), 90)
        self.assertEqual(self.user2_ct.get_balance_amount(), 40)
        self.assertEqual(self.escrow_ct.get_balance_amount(), 30)
        self.assertEqual(self.user3_tm.get_available_tickets().count(), 0)

        refund_manager = RefundManager()
        self.assertRaises(
            ContestCanNotBeRefunded,
            lambda: refund_manager.refund(self.contest, force=True))

        self.assertEqual(self.user1_ct.get_balance_amount(), 90)
        self.assertEqual(self.user2_ct.get_balance_amount(), 40)
        self.assertEqual(self.escrow_ct.get_balance_amount(), 30)
        self.assertEqual(self.user3_tm.get_available_tickets().count(), 0)