public PlayerFixtureDto(PlayerFixture pf)
 {
     this.FixtureId     = pf.Fixture.Id;
     this.PlayerId      = pf.Player.Id;
     this.Forename      = pf.Player.Forename;
     this.Surname       = pf.Player.Surname;
     this.Fouls         = pf.Fouls;
     this.Points        = pf.PointsScored;
     this.IsMvp         = pf.IsMvp.YesNoToBool();
 }
        public void CanCreatePlayerFixture()
        {
            PlayerFixture playerFixture = new PlayerFixture(teamLeagueHome, fixture, player, 15, 3);

            Assert.IsNotNull(playerFixture);
            Assert.That(playerFixture.TeamLeague, Is.EqualTo(teamLeagueHome));
            Assert.That(playerFixture.Fixture, Is.EqualTo(fixture));
            Assert.That(playerFixture.Player, Is.EqualTo(player));
            Assert.That(playerFixture.PointsScored, Is.EqualTo(15));
            Assert.That(playerFixture.Fouls, Is.EqualTo(3));
            Assert.That(playerFixture.IsMvp, Is.EqualTo("N"));
        }
        public void Constructor_Success()
        {
            PlayerFixture pf = new PlayerFixture()
            {
                Id = 2,
                Player = new Player() { Id = 10, Forename = "A", Surname = "B" },
                PointsScored = 3,
                Fouls = 5,
                IsMvp = "Y"
            };
            PlayerFixtureStats stats = new PlayerFixtureStats(pf, true);

            Assert.That(stats.HasPlayed, Is.True);
        }
        /// <param name="playerFixture"></param>
        /// <param name="fixture">Required because EF won't load all the required information to look up the opposing team in a list of PlayerFixtures</param>
        public PlayerSeasonFixtureStatsDto(PlayerFixture playerFixture, Fixture fixture)
        {
            this.FixtureDate = playerFixture.Fixture.FixtureDate;
            this.CurrentTeam = playerFixture.TeamLeague.TeamName;
            // Sucky code alert. Because of the WCF/EF lazy loading problems only entities one level deep
            // from PlayerFixture are loaded. I.e. Fixture is loaded but Fixture.HomeTeamLeague isn't.
            // However PlayerFixture.TeamLeague will always be populated which means either
            // PlayerFixture.Fixture.HomeTeamLeague or PlayerFixture.Fixture.AwayTeamLeague is populated,
            // I'm not entirely sure why. So the opposing team will be null
            if(fixture.HomeTeamLeague.Team != null) // Very odd. This seems to work because a Player's team as already been loaded
                OpponentName = fixture.AwayTeamLeague.TeamName;
            else
               OpponentName = fixture.HomeTeamLeague.TeamName;

            this.IsMvp = playerFixture.IsMvp.YesNoToBool();
            this.Fouls = playerFixture.Fouls.ToString();
            this.Points = playerFixture.PointsScored.ToString();
        }
        public void MapToModel_Success()
        {
            PlayerFixture pf = new PlayerFixture()
            {
                Id = 2,
                Player = new Player() {Id = 10, Forename = "A", Surname = "B"},
                PointsScored = 3,
                Fouls = 5,
                IsMvp = "Y"
            };

            playerFixtureStats.MapToModel(pf);

            Assert.That(playerFixtureStats.PlayerFixtureId, Is.EqualTo(pf.Id));
            Assert.That(playerFixtureStats.PlayerId, Is.EqualTo(pf.Player.Id));
            Assert.That(playerFixtureStats.Name, Is.EqualTo(pf.Player.ToString()));
            Assert.That(playerFixtureStats.PointsScored, Is.EqualTo(pf.PointsScored));
            Assert.That(playerFixtureStats.Fouls, Is.EqualTo(pf.Fouls));
            Assert.That(playerFixtureStats.IsMvp, Is.True);
        }
        public void MapToPlayerFixture_Success()
        {
            playerFixtureStats.PointsScored = 10;
            playerFixtureStats.Fouls = 4;
            playerFixtureStats.IsMvp = false;

            PlayerFixture pf = new PlayerFixture()
            {
                Id = 2,
                Player = new Player() { Id = 10, Forename = "A", Surname = "B" },
                PointsScored = 3,
                Fouls = 5,
                IsMvp = "Y"
            };

               pf = playerFixtureStats.MapToPlayerFixture(pf);

               Assert.That(pf.Id,                Is.EqualTo(2));
               Assert.That(pf.Player.Id,         Is.EqualTo(10));
               Assert.That(pf.Player.ToString(), Is.EqualTo("A B"));
               Assert.That(pf.PointsScored,      Is.EqualTo(playerFixtureStats.PointsScored));
               Assert.That(pf.Fouls,             Is.EqualTo(playerFixtureStats.Fouls));
               Assert.That(pf.                   IsMvp, Is.EqualTo("N"));
        }
        //protected Player SavePlayer(Player player)
        //{
        //    playerRepository.SaveOrUpdate(player);
        //    FlushSessionAndEvict(player);
        //    return player;
        //}
        private PlayerFixture AddPlayerFixture(TeamLeague teamLeague, Fixture fixture, Player player, int points, int fouls, bool isMvp)
        {
            PlayerFixture playerFixture = new PlayerFixture(teamLeague, fixture, player, points, fouls);

            if (isMvp)
                playerFixture.IsMvp = "Y";
            else
                playerFixture.IsMvp = "N";

            //statsRepository.SaveOrUpdatePlayerFixture(playerFixture);
            //FlushSessionAndEvict(playerFixture);
            return playerFixture;
        }
        public PlayerSeasonStats UpdatePlayerSeasonStats(PlayerFixture playerFixture, Season season)
        {
            PlayerSeasonStats playerSeasonStats;
            int totalPoints = 0;
            int totalFouls = 0;
            int mvpAwards = 0;

            // Get all PlayerFixture records for specified season
            List<PlayerFixture> playerFixturesForSeason = this.statsReportingService.GetPlayerFixtureStatsForSeason(playerFixture.Player.Id, season.Id).ToList();

            // Total stats
            foreach (PlayerFixture pf in playerFixturesForSeason)
            {
                totalPoints += pf.PointsScored;
                totalFouls += pf.Fouls;
                if (pf.IsMvp == "Y")
                    mvpAwards++;
            }

            // Find existing record
            playerSeasonStats = this.statsReportingService.GetPlayerSeasonStats(playerFixture.Player.Id, season.Id);

            // If doesn't exist, create new
            if (playerSeasonStats == null)
                playerSeasonStats = new PlayerSeasonStats(playerFixture.Player, season, totalPoints, totalFouls, playerFixturesForSeason.Count, mvpAwards);
            else
            {
                // Update values
                playerSeasonStats.UpdateStats(totalPoints, totalFouls, playerFixturesForSeason.Count, mvpAwards);
            }

            // Save
            matchResultRepository.SavePlayerSeasonStats(playerSeasonStats);

            return playerSeasonStats;
        }
        public PlayerCupStats UpdatePlayerCupStats(PlayerFixture playerFixture, Cup cup, Season season)
        {
            PlayerCupStats playerCupStats;

            // Get all PlayerFixture records for specified season
            List<PlayerFixture> playerFixturesForCup = statsReportingService.GetPlayerFixtureStatsForCupAndSeason(playerFixture.Player.Id, cup.Id, season.Id).ToList();

            // Total stats
            int totalPoints = playerFixturesForCup.Sum(pf => pf.PointsScored);
            int totalFouls = playerFixturesForCup.Sum(pf => pf.Fouls);
            int mvpAwards = playerFixturesForCup.Count(pf => pf.IsMvp == "Y");

            // Find existing record
            playerCupStats = statsReportingService.GetPlayerCupStats(playerFixture.Player.Id, cup.Id, season.Id);

            // If doesn't exist, create new
            if (playerCupStats == null)
                playerCupStats = new PlayerCupStats(playerFixture.Player, cup, season, totalPoints, totalFouls, playerFixturesForCup.Count, mvpAwards);
            else
            {
                // Update values
                playerCupStats.UpdateStats(totalPoints, totalFouls, playerFixturesForCup.Count, mvpAwards);
            }

            // Save
            matchResultRepository.SavePlayerCupStats(playerCupStats);

            return playerCupStats;
        }
        // TODO This method really needs refactoring as it's difficult to unit test
        public void SaveMatchStats(bool hasPlayerStats, List<PlayerFixtureStats> playerStats, TeamLeague teamLeague, Fixture fixture)
        {
            // Deal appropriately with each player
            // Existing or non existing players that played - update or insert
            // Existing players that did not play - delete
            // Non existing players that did not play - ignore
            List<PlayerFixture> playerStatsToUpdate = new List<PlayerFixture>();
            PlayerFixture actionablePlayerFixture = null;
            foreach (var ps in playerStats)
            {
                if (ps.PlayerFixtureId > 0)
                    actionablePlayerFixture = this.statsReportingService.GetPlayerFixture(ps.PlayerFixtureId);
                else
                {
                    // Not need to add scores or fouls yet. That will be done by MapToPlayerFixture
                    // Not sure if the new Player setting Id thing will work
                    actionablePlayerFixture = new PlayerFixture(teamLeague, fixture, playerService.Get(ps.PlayerId), 0, 0);
                }

                if (ps.HasPlayed)
                {
                    matchResultRepository.InsertOrUpdatePlayerFixture(ps.MapToPlayerFixture(actionablePlayerFixture));
                    playerStatsToUpdate.Add(actionablePlayerFixture);
                }
                else if (!ps.HasPlayed && ps.PlayerFixtureId > 0)
                {
                    matchResultRepository.DeletePlayerFixture(actionablePlayerFixture);
                    // Awful hack alert. Because the PlayerFixture has been deleted the PlayerFixture.Player object will
                    // be null. But, we still need it, so the only solution I can currently think of is to get it again
                    playerStatsToUpdate.Add(new PlayerFixture(teamLeague, fixture, playerService.Get(ps.PlayerId), 0, 0));
                }
            }

            Commit();

            // Are all commits necessary?

            // Update player league stats
            UpdatePlayerLeagueStats(playerStatsToUpdate, teamLeague.League);
            Commit();

            // Update player season stats
            Season currentSeason = competitionService.GetCurrentSeason();
            UpdatePlayerSeasonStats(playerStatsToUpdate, currentSeason);
            Commit();

            if(fixture.IsCupFixture)
            {
                UpdatePlayerCupStats(playerStatsToUpdate, fixture.Cup, currentSeason);
                Commit();
            }

            // Update player career stats
            UpdatePlayerCareerStats(playerStatsToUpdate);
            Commit();

            // Update team stats
            UpdateTeamLeagueStats(teamLeague.Id);
            Commit();
        }
 public ActionResult Edit(PlayerFixture @playerFixture)
 {
     if (ModelState.IsValid) {
                 playerFixtureService.Update(@playerFixture);
                 playerFixtureService.Commit();
                 SuccessMessage(FormMessages.SaveSuccess);
                 return RedirectToAction("Index");
         }
         return View(@playerFixture);
 }
        public void UpdatePlayerSeasonStats_PlayersInListAndNoExistingRecord_Success()
        {
            PlayerFixture playerFixture = new PlayerFixture() { Player = new Player() { Id = 10 } };
            Season season = new Season() { Id = 5 };

            this.mockStatsReportingService.GetPlayerFixtureStatsForSeason(playerFixture.Player.Id, season.Id).Returns(PlayerFixturesWithStats);
            this.mockStatsReportingService.GetPlayerSeasonStats(playerFixture.Player.Id, season.Id).Returns((PlayerSeasonStats)null);

            var returnValue = matchResultService.UpdatePlayerSeasonStats(playerFixture, season);

            Assert.That(returnValue,               Is.Not.Null);
            Assert.That(returnValue.GamesPlayed,   Is.EqualTo(PlayerFixturesWithStatsGamesPlayed));
            Assert.That(returnValue.TotalPoints,   Is.EqualTo(PlayerFixturesWithStatsTotalPoints));
            Assert.That(returnValue.TotalFouls,    Is.EqualTo(PlayerFixturesWithStatsTotalFouls));
            Assert.That(returnValue.MvpAwards,     Is.EqualTo(PlayerFixturesWithStatsMvpAwards));
            Assert.That(returnValue.PointsPerGame, Is.EqualTo(PlayerFixturesWithStatsPointsPerGame));
            Assert.That(returnValue.FoulsPerGame,  Is.EqualTo(PlayerFixturesWithStatsFoulsPerGame));

            this.mockMatchResultRepository.Received().SavePlayerSeasonStats(returnValue);
        }
 public void InsertOrUpdatePlayerFixture(PlayerFixture playerFixture)
 {
     playerFixtureRepository.InsertOrUpdate(playerFixture);
 }
 public void DeletePlayerFixture(PlayerFixture playerFixture)
 {
     playerFixtureRepository.Delete(playerFixture);
 }
        public static PlayerFixture CreatePlayerFixture(int id)
        {
            Player player = CreatePlayer();

            Fixture fixture = CreateFixture(id);
            TeamLeague tl = CreateTeamLeague();

            PlayerFixture playerFixture = new PlayerFixture(tl, fixture, player, 10, 2);
            EntityIdSetter.SetIdOf(playerFixture, id);

            return playerFixture;
        }