def makeRoster(players, weights, budget, nteam): purchased = PlayerStorage() # Now try to maximize our team value while not rfilled(nteam): # Duplicate players and weights playerscopy = players.copy() weightscopy = copy.deepcopy(weights) filled = False while (not filled): try: maxw = max(weightscopy) except ValueError: print "Whoops, check weights length" idx = weightscopy.index(maxw) pos = playerscopy.pos[idx] #print "Searching for %s which has %d remaining slots." % (pos, nteam[pos]) # We found a valid player if nteam[pos] > 0: nteam[pos] = nteam[pos] - 1 filled = True else: weightscopy.pop(idx) playerscopy.removeplayer(idx) # We have found a player, append them to our purchased list # and remove from the non-copy players list # Note: the player idx came from the copylist (name, pos, points, cost) = playerscopy.getplayer(idx) purchased.addplayer(name, pos=pos, points=points, cost=cost) players.removeplayer(name) weights.pop(idx) budget -= cost return (purchased, budget)
def makeRoster(players,weights,budget,nteam): purchased = PlayerStorage() # Now try to maximize our team value while not rfilled(nteam): # Duplicate players and weights playerscopy = players.copy() weightscopy = copy.deepcopy(weights) filled = False while (not filled): try: maxw = max(weightscopy) except ValueError: print "Whoops, check weights length" idx = weightscopy.index(maxw) pos = playerscopy.pos[idx] #print "Searching for %s which has %d remaining slots." % (pos, nteam[pos]) # We found a valid player if nteam[pos] > 0: nteam[pos] = nteam[pos]-1 filled = True else: weightscopy.pop(idx) playerscopy.removeplayer(idx) # We have found a player, append them to our purchased list # and remove from the non-copy players list # Note: the player idx came from the copylist (name,pos,points,cost) = playerscopy.getplayer(idx) purchased.addplayer(name,pos=pos,points=points,cost=cost) players.removeplayer(name) weights.pop(idx) budget -= cost return (purchased,budget)
def mergePointsAndCost(points,costa,costb): merged = PlayerStorage() for name in points.names: (na,pa,pta,ca) = costa.getplayer(name) (nb,pb,ptb,cb) = costa.getplayer(name) (n,p,pt,c) = points.getplayer(name) merged.addplayer(name,pos=p,points=pt,cost=(ca+cb)/2) return merged
def mergePointsAndCost(points, costa, costb): merged = PlayerStorage() for name in points.names: (na, pa, pta, ca) = costa.getplayer(name) (nb, pb, ptb, cb) = costa.getplayer(name) (n, p, pt, c) = points.getplayer(name) merged.addplayer(name, pos=p, points=pt, cost=(ca + cb) / 2) return merged
def makePlayerList(): # Parse the EPSN cost data cost_espn = PlayerStorage() fnames = open('costESPN.dat') for line in fnames: lv = line.strip().split('\t') nv = lv[1].split() nv.pop() name = ' '.join(nv).strip(', ') mc = lv[4].strip() if mc != '--': cost = int(mc[1::]) else: cost = 0 cost_espn.addplayer(name,cost=cost) # Parse the fftoolbox.com cost data cost_ff = PlayerStorage() fnames = open('costFF.dat') for line in fnames: lv = line.strip().split('\t') name = lv[1] mc = lv[-1] cost = int(mc[1::]) cost_ff.addplayer(name,cost=cost) # Parse the scoring data from fftoday.com points = PlayerStorage() positions = ['QB','RB','WR','TE','K','DEF'] for pos in positions: fname = 'pts' + pos + '.dat' fnames = open(fname) for line in fnames: lv = line.strip().split('\t') nv = lv[0].split() nv.pop(0) name = ' '.join(nv).strip() # get rid of the last space pts = float(lv[-2]) points.addplayer(name,pos=pos,points=pts) return (points,cost_espn,cost_ff)
def __init__(self, name, teams, positions, values, keepers = {}): self.name = name self.values = values self.asc = 1 self.teams = [] for team in teams: self.teams.append(Team(team, positions)) self.positions = positions self.teamIndex = 0 self.positionsLeft = {} for pos in positions: self.positionsLeft[pos] = self.positions[pos] * len(self.teams) self.players = PlayerStorage() self.players.sort(self.getValues(), self.getNumPlayersToFill()) self.eliminateKeepers(keepers)
def makePlayerList(): # Parse the EPSN cost data cost_espn = PlayerStorage() fnames = open('costESPN.dat') for line in fnames: lv = line.strip().split('\t') nv = lv[1].split() nv.pop() name = ' '.join(nv).strip(', ') mc = lv[4].strip() if mc != '--': cost = int(mc[1::]) else: cost = 0 cost_espn.addplayer(name, cost=cost) # Parse the fftoolbox.com cost data cost_ff = PlayerStorage() fnames = open('costFF.dat') for line in fnames: lv = line.strip().split('\t') name = lv[1] mc = lv[-1] cost = int(mc[1::]) cost_ff.addplayer(name, cost=cost) # Parse the scoring data from fftoday.com points = PlayerStorage() positions = ['QB', 'RB', 'WR', 'TE', 'K', 'DEF'] for pos in positions: fname = 'pts' + pos + '.dat' fnames = open(fname) for line in fnames: lv = line.strip().split('\t') nv = lv[0].split() nv.pop(0) name = ' '.join(nv).strip() # get rid of the last space pts = float(lv[-2]) points.addplayer(name, pos=pos, points=pts) return (points, cost_espn, cost_ff)
class League: def __init__(self, name, teams, positions, values, keepers = {}): self.name = name self.values = values self.asc = 1 self.teams = [] for team in teams: self.teams.append(Team(team, positions)) self.positions = positions self.teamIndex = 0 self.positionsLeft = {} for pos in positions: self.positionsLeft[pos] = self.positions[pos] * len(self.teams) self.players = PlayerStorage() self.players.sort(self.getValues(), self.getNumPlayersToFill()) self.eliminateKeepers(keepers) def eliminateKeepers(self, keepers = {}): for team in keepers: players = keepers[team] team = self.getTeam(team) for player in players: self.draft(self.getPlayer(player), team) def getNumPlayersToFill(self): return self.positionsLeft def draft(self, player, team = None): self.players.eliminatePlayer(player) self.positionsLeft[player.getPosition()] -= 1 if team is None: team = self.getCurrentTeam() team.draft(player) self.updateIndex() self.players.sort(self.getValues(), self.getNumPlayersToFill()) def updateIndex(self): if self.asc == 1 and self.teamIndex == len(self.teams) - 1: self.asc = 0 elif self.asc == 1: self.teamIndex += 1 elif self.teamIndex == 0: self.asc = 1 else: self.teamIndex -= 1 def printTeam(self, name): team = None for t in self.teams: if t.getName() == name: team = t break return str(team) def getCurrentTeam(self): return self.teams[self.teamIndex] def getValues(self): return self.values def top(self, pos, num): return self.players.top(pos, num) def getPlayer(self, name): return self.players.getPlayer(name) def getTeam(self, teamName): for team in self.teams: if teamName == team.getName(): return team def getPossiblePlayers(self, name): return difflib.get_close_matches(name, [x.getName() for x in self.players.players])