Example #1
0
        // Match
        public static MatchResults ResultForCompetitor(this Match match, Competitor competitor)
        {
            var result = new MatchResults();
            
            if (match.Winner.CompetitorId != competitor.CompetitorId && match.Loser.CompetitorId != competitor.CompetitorId)
                result.Outcome = MatchOutcome.NotInvolved;
            else if (competitor.CompetitorId == match.Winner.CompetitorId)
            {
                result.Outcome = MatchOutcome.Win;
                result.EloChange = match.WinnerRatingDelta;
                result.Opponent = match.Loser;
            }
            else
            {
                result.Outcome = MatchOutcome.Lose;
                result.EloChange = match.LoserRatingDelta;
                result.Opponent = match.Winner;
            }

            if (match.Tied)
                result.Outcome = MatchOutcome.Tie;

            if (match.IsInvalid)
                result.Invalid = true;

            return (result);
        }
 // Match
 public static IQueryable<Match> InvolvesCompetitor(this IQueryable<Match> matches, Competitor competitor)
 {
     return
         (matches.Where(
             x =>
             x.Winner.CompetitorId == competitor.CompetitorId || x.Loser.CompetitorId == competitor.CompetitorId));
 }
        public ActionResult Create(Board board)
        {
            if (ModelState.IsValid)
            {
                var owner = new Competitor
                {
                    Name = User.Identity.Name,
                    Rating = board.StartingRating,
                    Profile = _repository.UserProfiles.FindProfile(User.Identity.Name)
                };

                _repository.Add(owner);
                _repository.CommitChanges();

                board.Created = DateTime.Now;
                board.Started = DateTime.Now;
                board.Owner = owner;
                board.Competitors.Add(owner);

                _repository.Add(board);
                _repository.CommitChanges();

                return RedirectToAction("Index");
            }

            return View(board);
        }
        public ActionResult Edit(int boardId, Competitor updatedCompetitor)
        {
            var board = _repository.GetBoardById(boardId);
            var competitor = _repository.GetCompetitorByName(boardId, updatedCompetitor.Name);
            var response = new JsonResponse<bool>();

            if (competitor == null)
            {
                response.Message = "Competitor not found";
                response.Error = true;
            }
            else if (!competitor.CanEdit(board, User.Identity.Name))
            {
                response.Message = "Invalid authority";
                response.Error = true;
            }
            else
            {
                // Just update status for now (owner only can change status)
                if(board.IsOwner(User.Identity.Name))
                    competitor.Status = updatedCompetitor.Status;

                _repository.CommitChanges();
                response.Result = true;
            }

            return (Json(response));
        }
Example #5
0
 public static int CalculateUnverifiedRank(this Competitor competitor, IList <Match> matches)
 {
     return(competitor.Rating +
            matches.Where(x => x.Loser.ProfileUserId == competitor.ProfileUserId && !x.IsResolved)
            .Sum(x => x.LoserRatingDelta) +
            matches.Where(x => x.Winner.ProfileUserId == competitor.ProfileUserId && !x.IsResolved)
            .Sum(x => x.WinnerRatingDelta));
 }
        public DiscussionViewModel(Board board, IPagedList<PostViewModel> posts, int focusPostId, Competitor viewer)
        {
            Board = board;
            FocusPostId = focusPostId;
            Viewer = viewer != null && viewer.Status == CompetitorStatus.Active
                         ? new CompetitorViewModel(viewer)
                         : new CompetitorViewModel();

            Posts = posts;
        }
Example #7
0
        public bool Invalidate(Competitor invalidator)
        {
            if (IsResolved)
                return false;

            Resolved = DateTime.Now;

            if (invalidator.CompetitorId == Winner.CompetitorId)
                Withdrawn = true;
            else
            {
                Rejected = true;
                Winner.RejectionsReceived++;
                Loser.RejectionsGiven++;
            }

            return true;
        }
        public CompetitorStats CalculateCompetitorStats(Competitor competitor, ICollection<Match> matches)
        {
            var statsByOpponent = new Dictionary<Competitor, PvpStats>();

            foreach (var match in matches.Select(match => match.ResultForCompetitor(competitor))
                                                                .Where(result => !result.Invalid)
                                                                .OrderBy(m => m.Opponent.Name))
            {
                PvpStats pvpStats;

                if (statsByOpponent.ContainsKey(match.Opponent))
                    pvpStats = statsByOpponent[match.Opponent];
                else
                {
                    pvpStats = new PvpStats { Opponent = match.Opponent };
                    statsByOpponent.Add(match.Opponent, pvpStats);
                }

                switch (match.Outcome)
                {
                    case MatchOutcome.Win:
                        pvpStats.Wins++;
                        break;
                    case MatchOutcome.Lose:
                        pvpStats.Loses++;
                        break;
                    case MatchOutcome.Tie:
                        pvpStats.Ties++;
                        break;
                    default:
                        continue;
                }

                pvpStats.EloNet += match.EloChange;

                statsByOpponent[match.Opponent] = pvpStats; // Update the dictionary
            }

            // Flatten dictionary and send it back
            var finalStats = new CompetitorStats();
            finalStats.Pvp.AddRange(statsByOpponent.Values.ToList());

            return (finalStats);
        }
Example #9
0
        public bool Invalidate(Competitor invalidator)
        {
            if (IsResolved)
            {
                return(false);
            }

            Resolved = DateTime.Now;

            if (invalidator.CompetitorId == Winner.CompetitorId)
            {
                Withdrawn = true;
            }
            else
            {
                Rejected = true;
                Winner.RejectionsReceived++;
                Loser.RejectionsGiven++;
            }

            return(true);
        }
Example #10
0
 public static bool Involves(this Match match, Competitor competitor)
 {
     return match.Winner.CompetitorId == competitor.CompetitorId ||
            match.Loser.CompetitorId == competitor.CompetitorId;
 }
 public CompetitorViewModel(Competitor competitor)
 {
     Name = competitor.Name;
 }
Example #12
0
 // Competitor
 public static bool CanEdit(this Competitor competitor, Board board, string name)
 {
     return(board.IsOwner(name) || competitor.Is(name));
 }
Example #13
0
 public static bool Is(this Competitor competitor, string name)
 {
     return(competitor.Profile.UserName.Equals(name, StringComparison.InvariantCultureIgnoreCase));
 }
Example #14
0
 public static bool DoesNotInvolve(this Match match, Competitor competitor)
 {
     return(!Involves(match, competitor));
 }
Example #15
0
 public static bool IsOwner(this Board board, Competitor competitor)
 {
     return(board.Owner.CompetitorId == competitor.CompetitorId);
 }
 public void Delete(Competitor competitor)
 {
     _competitors.Remove(competitor);
 }
Example #17
0
        public void ThrowsIfNotSubmitterOrLoserOrAdmin()
        {
            var repository = Repository.CreatePopulatedRepository();
            
            var service = new MatchService(repository, null);
            var match = repository.GetUnresolvedMatchesByBoardId(1).First();

            var profile = new UserProfile {UserName = "******", UserId = 999};
            var nonParticipant = new Competitor { CompetitorId = 999, Name = "Joe Shmoe", Rating = 1500, ProfileUserId = 999};
            
            repository.Add(profile);
            repository.Add(nonParticipant);

            repository.CommitChanges();
            Assert.Throws<ServiceException>(() => service.RejectMatch(match.Board.BoardId, match.MatchId, nonParticipant.Name));
        }
Example #18
0
 public static bool DoesNotInvolve(this Match match, Competitor competitor)
 {
     return !Involves(match, competitor);
 }
 public void Add(Competitor competitor)
 {
     _competitors.Add(competitor);
 }
 public CompetitorViewModel(Competitor competitor)
 {
     Name = competitor.Name;
     CompetitorId = competitor.CompetitorId;
 }
Example #21
0
 public static bool IsOwner(this Board board, Competitor competitor)
 {
     return (board.Owner.CompetitorId == competitor.CompetitorId);
 }
Example #22
0
 public static IQueryable <Match> InvolvesEither(this IQueryable <Match> matches, Competitor competitor1, Competitor competitor2)
 {
     return
         (matches.Where(
              x =>
              x.Winner.CompetitorId == competitor1.CompetitorId ||
              x.Loser.CompetitorId == competitor1.CompetitorId ||
              x.Winner.CompetitorId == competitor2.CompetitorId ||
              x.Loser.CompetitorId == competitor2.CompetitorId));
 }
 public void Add(Competitor competitor)
 {
     Db.Competitors.Add(competitor);
 }
Example #24
0
 public static bool Involves(this Match match, Competitor competitor)
 {
     return(match.Winner.CompetitorId == competitor.CompetitorId ||
            match.Loser.CompetitorId == competitor.CompetitorId);
 }
 public void Delete(Competitor competitor)
 {
     Db.Competitors.Remove(competitor);
 }
Example #26
0
        public static IRepository CreatePopulatedRepository()
        {
            var repository = new InMemoryRepository();

            var competitorOwnerProfile = new UserProfile
            {
                UserId = 0,
                UserName = "******"
            };

            var competitorProfile1 = new UserProfile
            {
                UserId = 1,
                UserName = "******"
            };

            var competitorProfile2 = new UserProfile
            {
                UserId = 2,
                UserName = "******"
            };

            var boardOwner = new Competitor
            {
                Name = competitorOwnerProfile.UserName,
                ProfileUserId = 0,
                Profile = competitorOwnerProfile,
                Rating = 1500,
            };

            var competitor1 = new Competitor
            {
                Name = competitorProfile1.UserName,
                ProfileUserId = 1,
                Profile = competitorProfile1,
                Rating = 1500,
            };

            var competitor2 = new Competitor
            {
                Name = competitorProfile2.UserName,
                ProfileUserId = 2,
                Profile = competitorProfile2,
                Rating = 1500,
            };

            var board = new Board
            {
                BoardId = 1,
                AutoVerification = 1,
                Created = DateTime.Now,
                Description = "Test Board",
                End = DateTime.Now.AddDays(30),
                Name = "Test Board",
                Owner = boardOwner,
                Started = DateTime.Now,
                StartingRating = 1500,
                Competitors = new[] { boardOwner, competitor1, competitor2 }
            };

            // Unresolved match
            var match1 = new Match
            {
                Board = board,
                Created = DateTime.Now,
                Loser = competitor2,
                Winner = competitor1,
                VerificationDeadline = DateTime.Now.AddHours(board.AutoVerification),
                MatchId = 1,
                LoserRatingDelta = -10,
                WinnerRatingDelta = 10
            };

            // Unresolved match
            var match2 = new Match
            {
                Board = board,
                Created = DateTime.Now,
                Loser = competitor1,
                Winner = competitor2,
                VerificationDeadline = DateTime.Now.AddHours(board.AutoVerification),
                MatchId = 2,
                LoserRatingDelta = -10,
                WinnerRatingDelta = 10
            };

            // Unresolved match
            var match3 = new Match
            {
                Board = board,
                Created = DateTime.Now,
                Loser = competitor2,
                Winner = competitor1,
                VerificationDeadline = DateTime.Now.AddHours(board.AutoVerification),
                MatchId = 3,
                LoserRatingDelta = -10,
                WinnerRatingDelta = 10
            };

            // Resolved Match [Verified]
            var match4 = new Match
            {
                Board = board,
                Created = DateTime.Now.AddDays(-7),
                Loser = competitor1,
                Winner = competitor2,
                VerificationDeadline = DateTime.Now.AddHours(board.AutoVerification),
                MatchId = 4,
                LoserRatingDelta = -10,
                WinnerRatingDelta = 10,
                Resolved = DateTime.Now.AddHours(board.AutoVerification),
                Verified = true
            };

            // Resolved Match [Rejected]
            var match5 = new Match
            {
                Board = board,
                Created = DateTime.Now.AddDays(-7),
                Loser = competitor2,
                Winner = competitor1,
                VerificationDeadline = DateTime.Now.AddHours(board.AutoVerification),
                MatchId = 4,
                LoserRatingDelta = -10,
                WinnerRatingDelta = 10,
                Resolved = DateTime.Now.AddHours(board.AutoVerification),
                Rejected = true
            };

            board.Matches = new[] { match1, match2, match3 };

            // Boards
            repository.Add(board);

            // User Profiles
            repository.Add(competitorOwnerProfile);
            repository.Add(competitorProfile1);
            repository.Add(competitorProfile2);

            // Competitors
            repository.Add(boardOwner);
            repository.Add(competitor1);
            repository.Add(competitor2);

            // Matches
            repository.Add(match1);
            repository.Add(match2);
            repository.Add(match3);
            repository.Add(match4);
            repository.Add(match5);

            return (repository);
        }