Example #1
0
    def _get(self, args):
        draft_id = args[0]
        id = args[1] if len(args) > 1 else None

        q = self.db.query(Player)
        if id is not None:
            player = q.filter(Player.id == int(id)).first()
            team = self.db.query(Team).filter(and_(Team.is_owner == True,
                                                   Team.draft_id == draft_id)).first()

            available_players = self.db.query(Player).join(Player.core).filter(and_(PlayerCore.rank != None,
                                                                                    PlayerCore.target_price != None,
                                                                                    PlayerCore.points > 0,
                                                                                    Player.draft_id == draft_id,
                                                                                    Player.team_id == None,
                                                                                    Player.id != player.id)).order_by(PlayerCore.rank).all()

            min_price = 1
            max_price = min(player.core.target_price + 21, team.money)
            manager = Manager()
            max_starters_points = manager.dict()
            max_bench_points = manager.dict()
            pool = Pool(processes=8)
            starters, bench = get_starters_and_bench(self.db, team.id)
            max_starters_points[0] = optimizer.optimize_roster(starters, available_players, team.money - (constants.BENCH_SIZE - len(bench)))[1]
            for m in range(min_price, 10):
                pool.apply_async(wrap_optimizer, args=(starters, available_players, team.money - m - (constants.BENCH_SIZE - len(bench)) + 1, max_bench_points, m))

            full_starters = True
            for s in starters:
                if s is None:
                    full_starters = False
            if not full_starters:
                starters_clone = list(starters)
                bench_clone = list(bench)
                place_player(player, starters_clone, bench_clone)
                for m in range(min_price, max_price):
                    pool.apply_async(wrap_optimizer, args=(starters_clone, available_players, team.money - m - (constants.BENCH_SIZE - len(bench_clone)), max_starters_points, m))

            pool.close()
            pool.join()

            ret = player.to_dict(['core'])
            ret['max_starters_points'] = dict(max_starters_points)
            ret['max_bench_points'] = dict(max_bench_points)

            return ret
        else:
            players = q.join(PlayerCore).filter(and_(Player.draft_id == int(draft_id),
                                                     PlayerCore.rank != None,
                                                     PlayerCore.target_price != None)).all()
            return {'players': [p.to_dict(['core']) for p in players]}
Example #2
0
    def _get(self, args):
        draft_id = args[0]

        owner_team = self.db.query(Team).filter(and_(Team.is_owner == True,
                                                     Team.draft_id == int(draft_id))).first()

        team_id = owner_team.id

        starters, bench = get_starters_and_bench(self.db, team_id)

        available_players = self.db.query(Player).join(Player.core).filter(and_(PlayerCore.rank != None,
                                                                                PlayerCore.target_price != None,
                                                                                PlayerCore.points > 0,
                                                                                Player.draft_id == draft_id,
                                                                                Player.team_id == None)).order_by(PlayerCore.rank).all()

        optimal_roster, points = optimizer.optimize_roster(starters, available_players, owner_team.money - (constants.BENCH_SIZE - len(bench)))

        for player in optimal_roster:
            place_player(player, starters, bench)

        money_spent = 0
        available_bench = list(available_players)
        for p in starters:
            if p in available_bench:
                money_spent += max(1, math.floor(p.core.target_price + (p.core.target_price * constants.PRICE_OFFSET)))
                available_bench.remove(p)

        optimal_bench = optimizer.optimize_bench(bench, available_players, owner_team.money - money_spent)[0]
        for player in optimal_bench:
            place_player(player, starters, bench)

        starters = [p.to_dict(['core']) for p in starters if p is not None]
        bench = [p.to_dict(['core']) for p in bench]

        return {'roster': {'starters': starters, 'bench': bench, 'max_points': points}}
Example #3
0
def wrap_optimizer(starters, available_players, money, max_points, key):
    max_points[key] = optimizer.optimize_roster(starters, available_players, money)[1]