public void Collate(Batter newSelf) { if (_id != newSelf._id) { return; } //not same person plateAppearances += newSelf.plateAppearances; atBats += newSelf.atBats; rbis += newSelf.rbis; hits += newSelf.hits; homeRuns += newSelf.homeRuns; walks += newSelf.walks; doubles += newSelf.doubles; triples += newSelf.triples; outsTaken[OutTypes.CaughtStealing] += newSelf.outsTaken[OutTypes.CaughtStealing]; outsTaken[OutTypes.DoublePlay] += newSelf.outsTaken[OutTypes.DoublePlay]; outsTaken[OutTypes.FieldersChoice] += newSelf.outsTaken[OutTypes.FieldersChoice]; outsTaken[OutTypes.Flyout] += newSelf.outsTaken[OutTypes.Flyout]; outsTaken[OutTypes.Groundout] += newSelf.outsTaken[OutTypes.Groundout]; outsTaken[OutTypes.Sacrifice] += newSelf.outsTaken[OutTypes.Sacrifice]; outsTaken[OutTypes.Strikeout] += newSelf.outsTaken[OutTypes.Strikeout]; //foreach(KeyValuePair<OutTypes, int> outCount in newSelf.outsTaken) //{ // if (!outsTaken.ContainsKey(outCount.Key)) { outsTaken.Add(outCount.Key, 0); } // outsTaken[outCount.Key] += outCount.Value; //} }
//Constructor with GameEvent. Includes naming teams. public SaveGame(GameEvent gameEvent, string updateAwayTeamNickname, string updateHomeTeamNickname) { _id = gameEvent.gameId; awayTeamNickname = updateAwayTeamNickname; homeTeamNickname = updateHomeTeamNickname; awayScore = (int)gameEvent.awayScore; homeScore = (int)gameEvent.homeScore; awayPitchers = new Dictionary <string, Pitcher>(); homePitchers = new Dictionary <string, Pitcher>(); awayBatters = new Dictionary <string, Batter>(); homeBatters = new Dictionary <string, Batter>(); baseRunners = new List <string>(); if (gameEvent.topOfInning) { awayBatters.Add(gameEvent.batterId, new Batter(gameEvent.batterId)); homePitchers.Add(gameEvent.pitcherId, new Pitcher(gameEvent.pitcherId)); } else { awayPitchers.Add(gameEvent.pitcherId, new Pitcher(gameEvent.pitcherId)); homeBatters.Add(gameEvent.batterId, new Batter(gameEvent.batterId)); } lastPitcher = new Pitcher(); lastBatter = new Batter(); inningsList = new List <Inning>(); basesOccupied = new List <int>(); }
//Constructor with GameEvent. Initializes score. public SaveGame(GameEvent gameEvent) { awayScore = (int)gameEvent.awayScore; homeScore = (int)gameEvent.homeScore; awayPitchers = new List <Pitcher>(); homePitchers = new List <Pitcher>(); awayBatters = new List <Batter>(); homeBatters = new List <Batter>(); if (gameEvent.topOfInning) { awayBatters.Add(new Batter(gameEvent.batterId)); homePitchers.Add(new Pitcher(gameEvent.pitcherId)); } else { awayPitchers.Add(new Pitcher(gameEvent.pitcherId)); homeBatters.Add(new Batter(gameEvent.batterId)); } lastPitcher = new Pitcher(); lastBatter = new Batter(); inningsList = new List <Inning>(); basesOccupied = new List <int>(); }
//Constructor with entire game update public SaveGame(Game game) { _id = game._id; awayTeamNickname = game.awayTeamNickname; homeTeamNickname = game.homeTeamNickname; awayTeamID = game.awayTeam; homeTeamID = game.homeTeam; awayScore = game.awayScore; homeScore = game.homeScore; season = game.season; day = game.day; inningsList = new List <Inning>(); awayPitchers = new Dictionary <string, Pitcher>(); homePitchers = new Dictionary <string, Pitcher>(); awayBatters = new Dictionary <string, Batter>(); homeBatters = new Dictionary <string, Batter>(); basesOccupied = new List <int>(); baseRunners = new List <string>(); if (game.awayBatter != "" && game.homeBatter != "") { if (game.topOfInning && game.awayBatter != "" && game.homeBatter != "") { lastBatter = new Batter(game.awayBatter); lastBatter.name = game.awayBatterName; awayBatters.Add(lastBatter._id, lastBatter); lastPitcher = new Pitcher(game.homePitcher); lastPitcher.name = game.homePitcherName; homePitchers.Add(lastPitcher._id, lastPitcher); } else { lastBatter = new Batter(game.homeBatter); lastBatter.name = game.homeBatterName; homeBatters.Add(lastBatter._id, lastBatter); lastPitcher = new Pitcher(game.awayPitcher); lastPitcher.name = game.awayPitcherName; awayPitchers.Add(lastPitcher._id, lastPitcher); } } awayBatting = true; }
//public void DisplaySaveGame() //{ // string output = string.Concat("") // MessageBox.Show(); //} //Update a savegame with a full new game state public void UpdateSaveGame(Game newState) { String[] hitStrings = { "Single", "Double", "Triple", "home run", "grand slam" }; if (thisUpdate == newState.lastUpdate) { return; } //check for unnecessary updates thisUpdate = newState.lastUpdate; if (thisUpdate.Contains("Bottom of") || thisUpdate.Contains("Top of")) //nothing interesting happens here except putting players on the wrong team { return; } //bookkeeping on day, in case it was init'd with GameEvent if (season == 0) { season = newState.season; } if (day == 0) { day = newState.day; } if (lastTopOfInning == newState.topOfInning && lastTopOfInning) { awayBatting = true; } else if (lastTopOfInning == newState.topOfInning && !lastTopOfInning) { awayBatting = false; } //check for turnover if (lastTopOfInning != newState.topOfInning) { foreach (int runnerNum in basesOccupied) { if (runnerNum == 1 || runnerNum == 2) { if (awayBatting) { awayRISP += 1; } else { homeRISP += 1; } } } turnover = true; } else { turnover = false; } Inning thisInning = null; //find inning we need, init new one if required try { foreach (Inning inning in inningsList) { if (inning.number == (newState.inning + 1)) { thisInning = inning; } } if (thisInning == null) { thisInning = new Inning(); thisInning.number = (newState.inning + 1); inningsList.Add(thisInning); } } catch { thisInning = new Inning(newState.inning + 1); inningsList.Add(thisInning); } //find batter and pitcher in respective team lists Batter batter = null; Pitcher pitcher = null; if (awayBatting) { bool foundAB = awayBatters.TryGetValue(lastBatter._id, out batter); if (!foundAB && lastBatter._id != "") { batter = new Batter(lastBatter._id); batter.name = lastBatter.name; awayBatters[batter._id] = batter; } bool foundHP = homePitchers.TryGetValue(lastPitcher._id, out pitcher); if (!foundHP && lastPitcher._id != "") { pitcher = new Pitcher(lastPitcher._id); pitcher.name = lastPitcher.name; homePitchers[pitcher._id] = pitcher; } lastBatter = new Batter(newState.awayBatter); lastBatter.name = newState.awayBatterName; lastPitcher = new Pitcher(newState.homePitcher); lastPitcher.name = newState.homePitcherName; } else { bool foundHB = homeBatters.TryGetValue(lastBatter._id, out batter); if (!foundHB && lastBatter._id != "") { batter = new Batter(lastBatter._id); batter.name = lastBatter.name; homeBatters[batter._id] = batter; } bool foundHP = awayPitchers.TryGetValue(lastPitcher._id, out pitcher); if (!foundHP && lastPitcher._id != "") { pitcher = new Pitcher(lastPitcher._id); pitcher.name = lastPitcher.name; awayPitchers[pitcher._id] = pitcher; } lastBatter = new Batter(newState.homeBatter); lastBatter.name = newState.homeBatterName; lastPitcher = new Pitcher(newState.awayPitcher); lastPitcher.name = newState.awayPitcherName; } if (thisUpdate.Contains("batting for")) { return; } //not a play of record, needed to get this far to set lastBatter else if (batter == null || pitcher == null) { return; } //last play didn't have a batter or pitcher or both //get any runs scored on play int scoredHome = newState.homeScore - homeScore; int scoredAway = newState.awayScore - awayScore; int scored = scoredAway + scoredHome; //and update runs homeScore = newState.homeScore; awayScore = newState.awayScore; thisInning.AddRun(scoredAway, true); thisInning.AddRun(scoredHome, false); pitcher.runs += scored; //they deserve it //check for hits foreach (string hitString in hitStrings) { if (thisUpdate.Contains(hitString)) { batter.AddHit(scored); if (newState.halfInningOuts == 2) { if (lastTopOfInning) { awayHitsOn2Out += 1; awayRunsOn2Out += scored; } else { homeHitsOn2Out += 1; homeRunsOn2Out += scored; } } pitcher.pitchCount += 1; pitcher.hits += 1; if (hitString == "Double") { batter.doubles += 1; } else if (hitString == "Triple") { batter.triples += 1; } else if (hitString == "home run" || hitString == "grand slam") { batter.homeRuns += 1; pitcher.homeRuns += 1; } if (lastTopOfInning) { awayHits += 1; } else { homeHits += 1; } } } //check for out types bool gotOut = false; if (thisUpdate.Contains("sacrifice")) { batter.AddOut(OutTypes.Sacrifice); batter.rbis += scored; gotOut = true; } else if (thisUpdate.Contains("fielder's choice")) { batter.AddOut(OutTypes.FieldersChoice); gotOut = true; } else if (thisUpdate.Contains("strikes out") || thisUpdate.Contains("struck out")) { batter.AddOut(OutTypes.Strikeout); gotOut = true; pitcher.strikeouts += 1; } else if (thisUpdate.Contains("ground out")) { batter.AddOut(OutTypes.Groundout); gotOut = true; } else if (thisUpdate.Contains("flyout")) { batter.AddOut(OutTypes.Flyout); gotOut = true; } else if (thisUpdate.Contains("double play")) { batter.AddOut(OutTypes.DoublePlay); gotOut = true; pitcher.outsRecorded += 1; } //add the out to pitcher stats if (gotOut) { pitcher.AddOut(); } //check for walks if (thisUpdate.Contains("draws a walk")) { batter.walks += 1; batter.plateAppearances += 1; pitcher.walks += 1; pitcher.pitchCount += 1; } //pitch with no interesting result if (thisUpdate.Contains("Ball.") || thisUpdate.Contains("Strike,") || thisUpdate.Contains("Foul Ball.")) { pitcher.pitchCount += 1; } baseRunners = newState.baseRunners; //steal attempt if (thisUpdate.Contains("caught stealing")) { if (awayBatting) { awayCaughtStealing += 1; } else { homeCaughtStealing += 1; } } else if (thisUpdate.Contains("steals")) { if (awayBatting) { awaySteals += 1; } else { homeSteals += 1; } } lastTopOfInning = newState.topOfInning; basesOccupied = newState.basesOccupied; }
private int CollectStats(Team team, int seasonNumber, Dictionary <string, Batter> teamBatters, Dictionary <string, Pitcher> teamPitchers, Dictionary <string, float> teamStats) { int count = 0; bool careAboutSeason = true; bool postSeason = false; if (seasonNumber == -1) { careAboutSeason = false; } //pass -1 to use all data else if (seasonNumber % 10000 != 0) { postSeason = true; seasonNumber = (seasonNumber - (seasonNumber % 10000)) / 10000; } else { seasonNumber = seasonNumber / 10000; } foreach (SaveGame game in loadedFile) { bool useGame = false; if (!careAboutSeason) { useGame = true; } else if (game.season == seasonNumber && game.day < 99 && !postSeason) { useGame = true; } else if (game.season == seasonNumber && game.day >= 99 && postSeason) { useGame = true; } if (useGame) { if (game.awayTeamNickname == team.nickname) { foreach (KeyValuePair <string, Batter> batter in game.awayBatters) { Batter thisInstance = batter.Value; bool found = teamBatters.ContainsKey(thisInstance._id); if (!found) { teamBatters[thisInstance._id] = batter.Value; } teamBatters[thisInstance._id].Collate(thisInstance); } foreach (KeyValuePair <string, Pitcher> pitcher in game.awayPitchers) { Pitcher thisInstance = pitcher.Value; bool found = teamPitchers.ContainsKey(thisInstance._id); if (!found) { teamPitchers[thisInstance._id] = pitcher.Value; } teamPitchers[thisInstance._id].Collate(thisInstance); teamPitchers[thisInstance._id].games += 1; if (game.awayScore > game.homeScore) { teamPitchers[thisInstance._id].wins += 1; } else { teamPitchers[thisInstance._id].losses += 1; } } teamStats["Hits"] += game.awayHits; teamStats["Runs"] += game.awayScore; teamStats["RISP"] += game.awayRISP; teamStats["Runs On Two Outs"] += game.awayRunsOn2Out; teamStats["Hits On Two Outs"] += game.awayHitsOn2Out; teamStats["Caught Stealing"] += game.awayCaughtStealing; teamStats["Stolen Bases"] += game.awaySteals; count += 1; } else if (game.homeTeamNickname == team.nickname) { foreach (KeyValuePair <string, Batter> batter in game.homeBatters) { Batter thisInstance = batter.Value; bool found = teamBatters.ContainsKey(thisInstance._id); if (!found) { teamBatters[thisInstance._id] = batter.Value; } teamBatters[thisInstance._id].Collate(thisInstance); } foreach (KeyValuePair <string, Pitcher> pitcher in game.homePitchers) { Pitcher thisInstance = pitcher.Value; bool found = teamPitchers.ContainsKey(thisInstance._id); if (!found) { teamPitchers[thisInstance._id] = pitcher.Value; } teamPitchers[thisInstance._id].Collate(thisInstance); teamPitchers[thisInstance._id].games += 1; if (game.awayScore < game.homeScore) { teamPitchers[thisInstance._id].wins += 1; } else { teamPitchers[thisInstance._id].losses += 1; } } teamStats["Hits"] += game.homeHits; teamStats["Runs"] += game.homeScore; teamStats["RISP"] += game.homeRISP; teamStats["Runs On Two Outs"] += game.homeRunsOn2Out; teamStats["Hits On Two Outs"] += game.homeHitsOn2Out; teamStats["Caught Stealing"] += game.homeCaughtStealing; teamStats["Stolen Bases"] += game.homeSteals; count += 1; } } } return(count); }