public void HomeAndAwayWins_TwoHomeWinsOneAwayWin_ReturnsTwoHomeWinsAndOneAwayWin()
        {
            var teamLeagueA = new TeamLeague()
            {
                Id = 1
            };
            var teamLeagueB = new TeamLeague()
            {
                Id = 2
            };
            var teamLeagueC = new TeamLeague()
            {
                Id = 3
            };
            var fixtures = new List <Fixture>();

            fixtures.Add(GetFixture(teamLeagueA, teamLeagueB, true));  // A home win
            fixtures.Add(GetFixture(teamLeagueA, teamLeagueB, true));  // A home win
            fixtures.Add(GetFixture(teamLeagueB, teamLeagueA, false)); // A away win
            fixtures.Add(GetFixture(teamLeagueB, teamLeagueA, true));  // B win
            fixtures.Add(GetFixture(teamLeagueB, teamLeagueC, true));

            Assert.That(teamLeagueA.GetHomeWins(fixtures), Is.EqualTo(2));
            Assert.That(teamLeagueA.GetAwayWins(fixtures), Is.EqualTo(1));
            Assert.That(teamLeagueA.GetTotalWins(fixtures), Is.EqualTo(3));
        }
        public TeamLeague UpdateTeamLeagueStats(int teamLeagueId)
        {
            TeamLeague newStats = this.competitionService.GetTeamLeague(teamLeagueId);

            newStats = ResetStats(newStats);

            List <Fixture> teamFixtureList = fixtureService.GetPlayedFixturesForTeamInReverseDateOrder(teamLeagueId);

            //Console.WriteLine("fixtures found: " + fixtureList.Count);

            // TODO Refactor this. Probably should be in save fixture method
            foreach (Fixture f in teamFixtureList)
            {
                if (f.IsHomeForfeit())
                {
                    f.HomeTeamScore = StandingsCalculations.ForfeitLossScore;
                    f.AwayTeamScore = StandingsCalculations.ForfeitWinScore;
                }
                else if (f.IsAwayForfeit())
                {
                    f.HomeTeamScore = StandingsCalculations.ForfeitWinScore;
                    f.AwayTeamScore = StandingsCalculations.ForfeitLossScore;
                }
            }

            var nonCupFixtures = teamFixtureList.Where(f => !f.IsCupFixture).ToList();

            newStats.PointsLeague           = newStats.GetLeaguePointsFromWins(nonCupFixtures);
            newStats.PointsLeague          += newStats.GetLeaguePointsFromLosses(nonCupFixtures);
            newStats.PointsScoredFor        = newStats.GetPointsScoredFor(nonCupFixtures);
            newStats.PointsScoredAgainst    = newStats.GetPointsScoredAgainst(nonCupFixtures);
            newStats.PointsScoredDifference = newStats.PointsScoredFor - newStats.PointsScoredAgainst;
            newStats.GamesPlayed            = newStats.GetGamesPlayed(nonCupFixtures);
            newStats.GamesWonHome           = newStats.GetHomeWins(nonCupFixtures);
            newStats.GamesWonAway           = newStats.GetAwayWins(nonCupFixtures);
            newStats.GamesLostHome          = newStats.GetHomeLosses(nonCupFixtures);
            newStats.GamesLostAway          = newStats.GetAwayLosses(nonCupFixtures);
            newStats.GamesWonTotal          = newStats.GetTotalWins(nonCupFixtures);
            newStats.GamesLostTotal         = newStats.GetTotalLosses(nonCupFixtures);
            newStats.GamesForfeited         = newStats.GetForfeitedGames(nonCupFixtures);

            // Deduct any penalty points (penalties defaults to 0)
            newStats.PointsLeague -= newStats.PointsPenalty;

            // Pts difference
            //newStats.PointsScoredDifference = newStats.PointsScoredFor - newStats.PointsScoredAgainst;
            if (newStats.GamesPlayed > 0)
            {
                newStats.GamesPct = (decimal)newStats.GamesWonTotal / (decimal)newStats.GamesPlayed;
                newStats.PointsScoredPerGameAvg           = (decimal)newStats.PointsScoredFor / (decimal)newStats.GamesPlayed;
                newStats.PointsAgainstPerGameAvg          = (decimal)newStats.PointsScoredAgainst / (decimal)newStats.GamesPlayed;
                newStats.PointsScoredPerGameAvgDifference = newStats.PointsScoredPerGameAvg - newStats.PointsAgainstPerGameAvg;
            }

            // Calc streak
            bool?lastResultWin = null;
            bool streakWin     = false;
            int  streakNumber  = 0;

            foreach (Fixture f in teamFixtureList)
            {
                // Home
                if (f.HomeTeamLeague.Id == teamLeagueId)
                {
                    if (f.HomeTeamScore > f.AwayTeamScore) // Home win
                    {
                        streakWin = true;
                    }
                    else
                    {
                        streakWin = false;
                    }
                }
                // Away
                else if (f.AwayTeamLeague.Id == teamLeagueId)
                {
                    if (f.AwayTeamScore > f.HomeTeamScore) // Away win
                    {
                        streakWin = true;
                    }
                    else
                    {
                        streakWin = false;
                    }
                }

                //Console.WriteLine(streakWin);
                // Keep track of the last result
                if (lastResultWin == null)
                {
                    lastResultWin = streakWin;
                }

                //Console.WriteLine("streak: " + streakWin);
                //Console.WriteLine("lastResultWin: " + lastResultWin);

                // If the present result was not the same as the last
                // result exit the loop
                if (streakWin != lastResultWin)
                {
                    break;
                }

                streakNumber++;
            }

            if (lastResultWin != null && streakNumber > 0)
            {
                newStats.Streak = ((bool)lastResultWin == true ? "W" : "L") + streakNumber;
            }
            else
            {
                newStats.Streak = "";
            }


            matchResultRepository.UpdateTeamLeague(newStats);

            return(newStats);
        }
 public static int GetLeaguePointsFromWins(this TeamLeague teamLeague, List <Fixture> teamFixtures)
 {
     return(teamLeague.GetTotalWins(teamFixtures) * LeaguePointsWin);
 }