Esempio n. 1
0
        private void UpdateMatchFromViewModel(UpdateMatchViewModel viewModel, Match match, User player1, User player2)
        {
            if (match.IsComplete) {
            var results = match.Results.ToList();
            foreach (var result in results) {
              repository.Delete(result);
            }
            match.Results.Clear();
              }
              match.Winner = viewModel.Winner == player1.Id ? player1 : player2;
              match.IsComplete = true;
              match.IsForfeit = viewModel.IsForfeit;

              if (!match.IsForfeit) {
            match.DatePlayed = DateTime.Parse(viewModel.Date + " " + viewModel.Time);
            var matchResult = new MatchResult(player1,
              viewModel.Player1Innings,
              viewModel.Player1DefensiveShots,
              viewModel.Player1Wins);
            match.AddResult(matchResult);

            matchResult = new MatchResult(player2,
              viewModel.Player2Innings,
              viewModel.Player2DefensiveShots,
              viewModel.Player2Wins);
            match.AddResult(matchResult);
              }
              // set meet to complete if all matches are complete
              var meet = match.Meet;
              meet.IsComplete = !meet.Matches.Where(m => !m.IsComplete).Any();
              // must do this because EF is stupid
              var d = meet.Division;
        }
Esempio n. 2
0
        public ActionResult UpdateMatch(UpdateMatchViewModel viewModel)
        {
            var errorViewModel = ValidateModelState(viewModel);
              if (null != errorViewModel) {
            return Json(errorViewModel);
              }

              var match = repository.Get<Match>(viewModel.Id);
              if (null == match) {
            return HttpNotFound();
              }

              var player1 = repository.Get<User>(viewModel.Player1Id);
              var player2 = repository.Get<User>(viewModel.Player2Id);
              errorViewModel = VerifyUserCanUpdateMatch(match, player1, player2);
              if (null != errorViewModel) {
            return Json(errorViewModel);
              }

              UpdateMatchFromViewModel(viewModel, match, player1, player2);
              // must save here to get new match results in DB, for calculation below
              repository.SaveChanges();

              var gameType = match.Meet.Division.Season.GameType;
              player1.UpdateSkillLevel(gameType, repository);
              player2.UpdateSkillLevel(gameType, repository);
              repository.SaveChanges();

              return Json(new UpdateMatchResponseViewModel(true));
        }
Esempio n. 3
0
 private UpdateMatchResponseViewModel ValidateModelState(UpdateMatchViewModel viewModel)
 {
     if (!ModelState.IsValid) {
     return new UpdateMatchResponseViewModel(false, "Validation errors", ModelState);
       }
       else {
     // we must perform some manual validation as well
     // this should maybe be moved to the view model itself?
     if (!viewModel.IsForfeit) {
       // verify that a valid date & time were entered
       DateTime tempDate;
       if (!DateTime.TryParse(viewModel.Date, out tempDate)) {
     return new UpdateMatchResponseViewModel(false, "Enter a valid date");
       }
       if (!DateTime.TryParse(viewModel.Time, out tempDate)) {
     return new UpdateMatchResponseViewModel(false, "Enter a valid time");
       }
       // verify that neither player's defensive shots are > innings
       if (viewModel.Player1DefensiveShots > viewModel.Player1Innings ||
       viewModel.Player2DefensiveShots > viewModel.Player2Innings) {
     return new UpdateMatchResponseViewModel(false, "Defensive shots cannot be greater than innings");
       }
       // verify that the winner has >= 2 wins
       int winnerWins = 0;
       if (viewModel.Winner == viewModel.Player1Id) {
     winnerWins = viewModel.Player1Wins;
       }
       else {
     winnerWins = viewModel.Player2Wins;
       }
       if (winnerWins < 2) {
     return new UpdateMatchResponseViewModel(false, "Winner must have at least 2 wins");
       }
     }
       }
       return null;
 }
Esempio n. 4
0
        public override void EstablishContext()
        {
            var repository = new Mock<IRepository>();
              authService = AuthHelper.CreateMockAuthenticationService();
              controller = new UpdateMatchController(repository.Object, authService);

              loggedInUser = new User("admin", "pass", "first", "last", "email");
              loggedInUser.SetIdTo(3);
              loggedInUser.AddRole(new Role(Roles.Administrators));
              authService.MockPrincipal.MockIdentity.IsAuthenticated = true;
              authService.MockPrincipal.MockIdentity.Name = loggedInUser.Username;

              var season = new Season("test", GameType.EightBall);
              var division = new Division("test", DateTime.Parse("1/1/2011"), season);
              season.AddDivision(division);
              var team1 = new Team("team1", division);
              division.AddTeam(team1);
              player1 = new User("player1", "test", "player1", "test", "test");
              player1.SetIdTo(1);
              team1.AddPlayer(player1);
              var team2 = new Team("team2", division);
              division.AddTeam(team2);
              player2 = new User("player2", "test", "player2", "test", "test");
              player2.SetIdTo(2);
              team2.AddPlayer(player2);
              var meet = new Meet(team1, team2, 1);
              division.Meets.Add(meet);
              match = new Web.Models.Match(meet, new MatchPlayer(player1, team1), new MatchPlayer(player2, team2));
              match.SetIdTo(1);

              repository.Setup(r => r.Get<Web.Models.Match>(match.Id)).Returns(match);
              repository.Setup(r => r.Get<User>(player1.Id)).Returns(player1);
              repository.Setup(r => r.Get<User>(player2.Id)).Returns(player2);

              var player1Results = new List<MatchResult>();
              var player2Results = new List<MatchResult>();
              for (int i = 0; i < 4; i++) {
            var tempMatch = new Web.Models.Match(meet, new MatchPlayer(player1, team1), new MatchPlayer(player2, team2));
            meet.AddMatch(tempMatch);
            tempMatch.DatePlayed = DateTime.Parse("8/1/2010").AddDays(i);
            tempMatch.IsComplete = true;
            var matchResult = new MatchResult(player1, 30, 0, 3);
            player1Results.Add(matchResult);
            tempMatch.AddResult(matchResult);
            matchResult = new MatchResult(player2, 30, 0, 3);
            player2Results.Add(matchResult);
            tempMatch.AddResult(matchResult);
              }
              repository.InitAll(new List<User> { player1, player2, loggedInUser }.AsQueryable(),
            loggedInUser.Roles.AsQueryable(),
            new List<Season> { season }.AsQueryable());
              player1.UpdateSkillLevel(GameType.EightBall, repository.Object);
              player1SkillLevel = player1.SkillLevels.Where(sl => sl.GameType == GameType.EightBall).First().Value;
              player2.UpdateSkillLevel(GameType.EightBall, repository.Object);
              player2SkillLevel = player2.SkillLevels.Where(sl => sl.GameType == GameType.EightBall).First().Value;

              viewModel = new UpdateMatchViewModel() {
            Id = match.Id,
            IsForfeit = false,
            Player1Id = player1.Id,
            Player1Innings = 2,
            Player1DefensiveShots = 1,
            Player1Wins = 4,
            Player2Id = player2.Id,
            Player2Innings = 2,
            Player2DefensiveShots = 1,
            Player2Wins = 4,
            Winner = player1.Id,
            Date = "1/1/2011",
            Time = "06:00 PM"
              };
        }