Beispiel #1
0
    def getStartingPitcher(self, index, pitcherFile, gameNumber, year, team):
        if (self.getShowFunctions()):
            print("LineupCreator.getStartingPitcher")
        else:
            pass

        def getGame(gameNumber, index, year, team):
            if (self.getShowFunctions()):
                print("LineupCreator.getStartingPitcher.getGame")
            else:
                pass
            #add up the team's wins and losses from that year to determine how many games they played.
            yearFile = open(File.teamYearInfo(year))
            for line in yearFile:
                if team in line:
                    tempLine = line.split(',')
                    seasonGames = int(tempLine[3]) + int(
                        tempLine[4]
                    )  #this is how many games they played that year.
                    break
            #if the gameNumber is higher than the number of games that team played that year, subtract the number of games from that season until the number is <= games played that year.
            while (gameNumber > seasonGames):
                gameNumber -= seasonGames
            return (gameNumber)

        if not (team == "CTM"):
            thisGame = str(getGame(gameNumber, index, year, team))
            file = open(pitcherFile)
            for line in file:
                if (thisGame in line):
                    tempLine = line
                    break
            file.close()
            startingPitcher = tempLine.split(',')[1].split(';')[0]
        else:
            spList = []
            for i in range(len(self.__addedPlayers[index])):
                positionList = []
                playerTeams = Importer.findPlayerTeam(
                    self.__addedPlayers[index][i],
                    self.__additionalPlayers_YearList[index][i].get(), False,
                    self.getShowFunctions())
                for team in playerTeams:
                    positionList += Importer.getPositionIncrementally(
                        self.__addedPlayers[index][i],
                        File.teamFielding(
                            team,
                            self.__additionalPlayers_YearList[index][i].get()),
                        1, "American League", self.getShowFunctions())
                    if (Utility.removeDuplicates(
                            positionList, self.getShowFunctions())[0] == "SP"):
                        spList.append(self.__addedPlayers[index][i])
                    else:
                        continue
            #gameNumber -= 1 #the first game should be zero so it starts with the first starting pticher in the list
            seasonGames = len(spList)
            while (gameNumber > seasonGames):
                gameNumber -= seasonGames
            startingPitcher = spList[gameNumber - 1]
        return (startingPitcher)
Beispiel #2
0
 def ensurePositionUniqueness(self, index, attempt, position, place,
                              players, battingFile, positionFile, league,
                              batters, positions, year):
     if (self.getShowFunctions()):
         print("LineupCreator.ensurePositionUniqueness")
     else:
         pass
     #print (batters)
     #print (positions)
     if ((position in positions) or (position in {"none", "RP"})
             or (self.__homeYear >= 1973 and league == "American League"
                 and position == "SP")):
         #print ("is " + batters[place]  + " in")
         #print (self.__addedPlayers[index])
         if not (batters[place] in self.__addedPlayers[index]
                 ):  #if this is not an added player to the team
             #print ("no\n")
             position = Importer.getPositionIncrementally(
                 batters[place], self.getFieldingFile(), attempt, league,
                 self.getShowFunctions())
         else:  #if this is an added player to the team
             #print ("yes\n")
             for t in range(
                     len(self.__addedPlayers[index])
             ):  #loop through the added players list for this team
                 if (self.__addedPlayers[index][t] == batters[place]
                     ):  #if this player is the player in question
                     fieldingFile = File.teamFielding(
                         Importer.findPlayerTeam(
                             batters[place],
                             self.__additionalPlayers_YearList[index]
                             [t].get(), False, self.getShowFunctions())[0],
                         self.__additionalPlayers_YearList[index][t].get()
                     )  #get the first team returned for purposes of finding their position
                     break
                 else:  #if this player is not the player in question
                     continue  #increment
             position = Importer.getPositionIncrementally(
                 batters[place], fieldingFile, attempt, league,
                 self.getShowFunctions())
         if (position == "none"):
             newBatter = self.getPlayerIncrementally(
                 players[place], batters[place])
             if (newBatter != "restart"):
                 batters = self.ensurePlayerUniqueness(
                     place, newBatter, batters, players)
                 if (batters != "restart"):
                     attempt = 1
                     #print ("is " + batters[place]  + " in")
                     #print (self.__addedPlayers[index])
                     if not (batters[place] in self.__addedPlayers[index]
                             ):  #if this is not an added player to the team
                         #print ("no\n")
                         newPlayerPosition = Importer.getPositionIncrementally(
                             batters[place], self.getFieldingFile(),
                             attempt, league, self.getShowFunctions())
                     else:  #if this is an added player to the team
                         #print ("yes\n")
                         for t in range(
                                 len(self.__addedPlayers[index])
                         ):  #loop through the added players list for this team
                             if (
                                     self.__addedPlayers[index][t] ==
                                     batters[place]
                             ):  #if this player is the player in question
                                 fieldingFile = File.teamFielding(
                                     Importer.findPlayerTeam(
                                         batters[place],
                                         self.__additionalPlayers_YearList[
                                             index][t].get(), False,
                                         self.getShowFunctions())[0],
                                     self.__additionalPlayers_YearList[
                                         index][t].get()
                                 )  #get the first team returned for purposes of finding their position
                                 break
                             else:  #if this player is not the player in question
                                 continue  #increment
                         newPlayerPosition = Importer.getPositionIncrementally(
                             batters[place], fieldingFile, attempt, league,
                             self.getShowFunctions())
                     return (self.ensurePositionUniqueness(
                         index, 2, newPlayerPosition, place, players,
                         battingFile, positionFile, league, batters,
                         positions, year))
                 else:
                     return ("restart", "restart")
             else:
                 return ("restart", "restart")
         else:
             return (self.ensurePositionUniqueness(
                 index, attempt + 1, position, place, players, battingFile,
                 positionFile, league, batters, positions, year))
     else:
         if (place == len(positions)):
             positions.append(position)
         else:
             positions[place] = position
     return (batters, positions)
Beispiel #3
0
 def controller(self, index, team, year, gameNumber, league, players,
                starts, startingPitcher):
     if (self.getShowFunctions()):
         print("LineupCreator.controller")
     else:
         pass
     place = 0
     batters = []
     positions = []
     while place <= 8:
         batter = self.getPlayerRandomly(players[place], starts[place])
         tempBatters = self.ensurePlayerUniqueness(place, batter, batters,
                                                   players)
         if (tempBatters != "restart"):
             batters = tempBatters
         else:
             #print ("restart")
             return (self.controller(index, team, year, gameNumber, league,
                                     players, starts, startingPitcher))
         if (
                 place == 8 and "SP" not in positions
                 and (self.__homeYear < 1973 or league == "National League")
         ):  #if it has come to the last spot in the order of a lineup under National League rules and the pitcher has not yet been add.
             position = "SP"  #the next player must be a pitcher
         else:
             #print ("is " + batters[place]  + " in")
             #print (self.__addedPlayers[index])
             if not (batters[place] in self.__addedPlayers[index]
                     ):  #if this is not an added player to the team
                 #print ("no\n")
                 position = Importer.getPositionIncrementally(
                     batters[place], self.getFieldingFile(), 1, league,
                     self.getShowFunctions())  #get their primary position
             else:  #if this is an added player to the team
                 #print ("yes\n")
                 for t in range(
                         len(self.__addedPlayers[index])
                 ):  #loop through the added players list for this team
                     if (self.__addedPlayers[index][t] == batters[place]
                         ):  #if this player is the player in question
                         fieldingFile = File.teamFielding(
                             Importer.findPlayerTeam(
                                 batters[place],
                                 self.__additionalPlayers_YearList[index]
                                 [t].get(), False,
                                 self.getShowFunctions())[0],
                             self.__additionalPlayers_YearList[index]
                             [t].get()
                         )  #get the first team returned for purposes of finding their position
                         break
                     else:  #if this player is not the player in question
                         continue  #increment
                 position = Importer.getPositionIncrementally(
                     batters[place], fieldingFile, 1, league,
                     self.getShowFunctions())
         batters, positions = self.ensurePositionUniqueness(
             index, 2, position, place, players, self.getBattingFile(),
             self.getFieldingFile(), league, batters, positions, year)
         if not (batters == "restart" and positions == "restart"):
             place += 1
         else:
             #print ("restart")
             return (self.controller(index, team, year, gameNumber, league,
                                     players, starts, startingPitcher))
     if (self.__homeYear < 1973 or league == "National League"):
         batters = self.addStartingPitcherToLineup(batters, positions,
                                                   startingPitcher)
     else:
         pass
     output = []
     output.append(batters)
     output.append(positions)
     return (output)