예제 #1
0
파일: Game.py 프로젝트: sroehl/baseball_sim
    def __init__(self, awayTeam, homeTeam, awayBatters, homeBatters, awayPitcher, homePitcher, date):
        con = MemoryDB.get_con()
        cur = con.cursor()

        cur.execute("select distinct(game) from batter where team=? and season=? and date=?",
                    (awayTeam, ENV.YEAR, date))
        row = cur.fetchone()
        if row is not None:
            awayGameNum = row[0]
        else:
            raise LookupError("Game num not found for away team {}".format(awayTeam))
        cur.execute("select distinct(game) from batter where team=? and season=? and date=?",
                    (homeTeam, ENV.YEAR, date))
        row = cur.fetchone()
        if row is not None:
            homeGameNum = row[0]
        else:
            raise LookupError("Game num not found for home team {}".format(homeTeam))
        print("game nums: {}|{}".format(awayGameNum, homeGameNum))

        cur.execute("select pf from parkfactor where team='" + awayTeam + "' and year=2012")
        awayPF = cur.fetchone()[0]
        cur.execute("select pf from parkfactor where team='" + homeTeam + "' and year=2012")
        homePF = cur.fetchone()[0]

        self.away = Team(awayTeam, awayGameNum, awayPF, awayBatters, awayPitcher)
        self.home = Team(homeTeam, homeGameNum, homePF, homeBatters, homePitcher)

        self.inning = Inning()
        self.bases = Bases()

        self.leagueAvg = self.get_league_avg(homeGameNum)
예제 #2
0
파일: Game.py 프로젝트: sroehl/baseball_sim
class Game:
    def __init__(self, awayTeam, homeTeam, awayBatters, homeBatters, awayPitcher, homePitcher, date):
        con = MemoryDB.get_con()
        cur = con.cursor()

        cur.execute("select distinct(game) from batter where team=? and season=? and date=?",
                    (awayTeam, ENV.YEAR, date))
        row = cur.fetchone()
        if row is not None:
            awayGameNum = row[0]
        else:
            raise LookupError("Game num not found for away team {}".format(awayTeam))
        cur.execute("select distinct(game) from batter where team=? and season=? and date=?",
                    (homeTeam, ENV.YEAR, date))
        row = cur.fetchone()
        if row is not None:
            homeGameNum = row[0]
        else:
            raise LookupError("Game num not found for home team {}".format(homeTeam))
        print("game nums: {}|{}".format(awayGameNum, homeGameNum))

        cur.execute("select pf from parkfactor where team='" + awayTeam + "' and year=2012")
        awayPF = cur.fetchone()[0]
        cur.execute("select pf from parkfactor where team='" + homeTeam + "' and year=2012")
        homePF = cur.fetchone()[0]

        self.away = Team(awayTeam, awayGameNum, awayPF, awayBatters, awayPitcher)
        self.home = Team(homeTeam, homeGameNum, homePF, homeBatters, homePitcher)

        self.inning = Inning()
        self.bases = Bases()

        self.leagueAvg = self.get_league_avg(homeGameNum)

    @staticmethod
    def get_league_avg(game_num, use_memory=True):
        con = MemoryDB.get_con(use_memory)
        cur = con.cursor()
        cur.execute(
            """select sum(hits),sum(doubles),sum(triples),sum(homeruns),sum(walks),sum(hit_by_pitch),
            sum(batters_faced) from pitcher where game < """ + str(game_num) + ";")
        stat = cur.fetchone()

        league_avg = []

        league_avg.append(float(stat[4]) / (stat[0] + stat[4]))  # Walks
        if (float(league_avg[0]) == 0):
            print("Walks league zero")
            exit()
        league_avg.append(float(stat[5]) / (stat[0] + stat[4]))  # HBP
        if (float(league_avg[1]) == 0):
            print("HBP league zero")
            exit()
        league_avg.append(float(float(stat[0] - stat[1] - stat[2] - stat[3]) / (stat[0] + stat[4])))  # Singles
        if (float(league_avg[2]) == 0):
            print("Single league zero")
            exit()
        league_avg.append(float((stat[1])) / (stat[0] + stat[4]))  # Doubles
        if (float(league_avg[3]) == 0):
            print("Doubles league zero")
            exit()
        league_avg.append(float((stat[2])) / (stat[0] + stat[4]))  # Triples
        if (float(league_avg[4]) == 0):
            print("Triples league zero")
            exit()
        league_avg.append(float((stat[3])) / (stat[0] + stat[4]))  # Homerun
        if (float(league_avg[5]) == 0):
            print("Homerun league zero")
            exit()
        league_avg.append(float(stat[0] + stat[4] + stat[5]) / stat[6])
        if (float(league_avg[6]) <= 0):
            print("OBP league zero")
            exit()
        return league_avg

    def simulateInning(self):
        self.bases.clearbases()
        if (PRINT_PLAY):
            if (self.inning.getHalf()):
                print("Simulating bottom of the ", self.inning.getInning(), " inning")
            else:
                print("Simulating top of the ", self.inning.getInning(), " inning")
        if (self.inning.getHalf()):
            batting = self.home
            pitching = self.away
        else:
            batting = self.away
            pitching = self.home
        while (self.inning.getOuts() < 3):
            if ( stratPlay(self.inning, self.bases, batting.getCurrentBatter()) != 5):
                result = atBat(batting.getCurrentBatter(), pitching.getCurrentPitcher(), self.leagueAvg,
                               batting.getPF())
            else:
                result = 5
            batting.getCurrentBatter().gameAtBat()
            if (result == 0):
                self.inning.addOut()
                if (self.inning.getOuts() < 3):
                    if (doublePlay(self.bases)):
                        self.inning.addOut()
                        if (PRINT_PLAY):
                            print(batting.getCurrentBatter().getName(), " grounded into double play")
                if (PRINT_PLAY):
                    print(batting.getCurrentBatter().getName(), " is out")
                if (PRINT_PLAY):
                    print(batting.getCurrentBatter().getName(), " is out")
            elif (result == 1):
                if (PRINT_PLAY):
                    print(batting.getCurrentBatter().getName(), " hit a single")
                batting.addScore(self.bases.basesSingle())
                batting.getCurrentBatter().gameSingle()
            elif (result == 2):
                if (PRINT_PLAY):
                    print(batting.getCurrentBatter().getName(), " hit a double")
                batting.addScore(self.bases.basesDouble())
                batting.getCurrentBatter().gameDouble()
            elif (result == 3):
                if (PRINT_PLAY):
                    print(batting.getCurrentBatter().getName(), " hit a triple")
                batting.addScore(self.bases.basesTriple())
                batting.getCurrentBatter().gameTriple()
            elif (result == 4):
                if (PRINT_PLAY):
                    print(batting.getCurrentBatter().getName(), " hit a homerun")
                batting.addScore(self.bases.basesHR())
                batting.getCurrentBatter().gameHR()
            elif (result == 5):
                if (PRINT_PLAY):
                    print(batting.getCurrentBatter().getName(), " walked")
                batting.addScore(self.bases.basesSingle())
                batting.getCurrentBatter().gameWalk()
            batting.nextBatter()


    def simulateGame(self):
        while (self.inning.nextInning(self.away.getScore(), self.home.getScore())):
            self.simulateInning()


    def printStats(self, team):
        if (team):
            team = self.home
        else:
            team = self.away
        for player in team.getPlayers():
            stats = player.getGameStats()
            print("Player: ", player.getName())
            print("At Bats: ", stats[0])
            print("SO: ", stats[1])
            print("Walks: ", stats[2])
            print("Hits: ", stats[3])
            print("1B: ", stats[4])
            print("2B: ", stats[5])
            print("3B: ", stats[6])
            print("HR: ", stats[7])

    def printScore(self):
        print("Away Score:", self.away.getScore())
        print("Home Score:", self.home.getScore())


    def reset(self):
        self.inning.reset()
        self.bases.clearbases()
        self.home.reset()
        self.away.reset()