//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 void UpdateWatchedGame(List <SaveGame> games, Game gameUpdate) { SaveGame thisGame = null; SaveGame newGame = new SaveGame(); foreach (SaveGame game in games) { if (game._id == gameUpdate._id) { thisGame = game; } } if (thisGame == null) //if game not found, initialize with same ID { newGame._id = gameUpdate._id; newGame.inningsList = new List <Inning>(); newGame.inningsList.Add(new Inning()); newGame.inningsList[0].number = gameUpdate.inning + 1; newGame.topOfInning = true; newGame.homeTeamNickname = gameUpdate.homeTeamNickname; newGame.awayTeamNickname = gameUpdate.awayTeamNickname; games.Add(newGame); thisGame = newGame; } if (thisGame.lastUpdate == gameUpdate.lastUpdate) { return; } //Leave, nothing to do Inning thisInning; thisGame.inningsList.Sort(); thisInning = thisGame.inningsList.Last(); if (thisGame.topOfInning != gameUpdate.topOfInning) { if (gameUpdate.topOfInning) { thisInning = new Inning(); thisInning.number = gameUpdate.inning + 1; thisGame.inningsList.Add(thisInning); } } //check for hits, add to game total foreach (string hitPhrase in hitPhrases) { if (gameUpdate.lastUpdate.Contains(hitPhrase)) { if (gameUpdate.topOfInning) { thisGame.awayHits += 1; } else { thisGame.homeHits += 1; } } } //check for runs, add to game and inning total int homeScoreDiff = gameUpdate.homeScore - thisGame.homeScore; int awayScoreDiff = gameUpdate.awayScore - thisGame.awayScore; if (homeScoreDiff > 0) { thisGame.homeScore = gameUpdate.homeScore; thisInning.homeScore += homeScoreDiff; } else if (awayScoreDiff > 0) { thisGame.awayScore = gameUpdate.awayScore; thisInning.awayScore += awayScoreDiff; } thisGame.topOfInning = gameUpdate.topOfInning; thisGame.lastUpdate = gameUpdate.lastUpdate; }