public ActionResult Create(TournamentModel tournamentModel)
        {
            try
            {
                if(!ModelState.IsValid)
                    return View(new TournamentEditorViewModel(EditorAction.Create, tournamentModel));

                var tournament = new Tournament();
                tournament.Name = tournamentModel.Name;
                switch (tournamentModel.TournamentType)
                {
                    case TournamentType.SingleElimination:
                        tournament.TournamentType = "single elimination";
                        break;
                    case TournamentType.DoubleElimination:
                        tournament.TournamentType = "double elimination";
                        break;
                    case TournamentType.League:
                        tournament.TournamentType = "round robin";
                        break;
                }
                tournament.Url = tournamentModel.Name.Replace(" ", "_").Trim();
                tournament.OpenSignup = false;
                if (tournamentModel.TournamentType == TournamentType.SingleElimination ||
                    tournamentModel.TournamentType == TournamentType.DoubleElimination)
                    tournament.HoldThirdPlaceMatch = true;
                tournament.RoundRobinPointsForMatchTie = 1;
                tournament.RoundRobinPointsForMatchWin = 3;

                _tournamentService.Create(tournament);

                foreach (var player in tournamentModel.Players)
                {
                    var participant = new Participant
                    {
                        TournamentId = tournament.Id,
                        Name = player.Name,
                        Misc = JsonConvert.SerializeObject(player)
                    };

                    _participantService.Create(participant);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View(new TournamentEditorViewModel(EditorAction.Create, tournamentModel));
            }
        }
        public TournamentEditorViewModel(EditorAction action, TournamentModel tournament)
        {
            Action = action;
            Tournament = tournament;

            TournamentTypes = new List<SelectListItem>();
            TournamentTypes.Add(new SelectListItem
            {
                Selected = tournament != null && tournament.TournamentType == TournamentType.League,
                Text = "Liga",
                Value = TournamentType.League.ToString()
            });

            TournamentTypes.Add(new SelectListItem
            {
                Selected = tournament != null && tournament.TournamentType == TournamentType.SingleElimination,
                Text = "Eliminación Simple",
                Value = TournamentType.SingleElimination.ToString()
            });

            TournamentTypes.Add(new SelectListItem
            {
                Selected = tournament != null && tournament.TournamentType == TournamentType.DoubleElimination,
                Text = "Eliminación Doble",
                Value = TournamentType.DoubleElimination.ToString()
            });

            IList<PlayerModel> players = new List<PlayerModel>();

            if (tournament != null && tournament.Players != null)
                players = tournament.Players;

            PlayersJson = JsonConvert.SerializeObject(players);

            if (tournament != null)
            {
                TournamentTypeForDisplay = tournament.TournamentType == TournamentType.DoubleElimination ? "Eliminación" : "Liga";
            }
        }
Esempio n. 3
0
        public static TournamentModel Create(Tournament tournament, IList<Participant> participants, 
			IList<Match> matches, string loggedParticipantUsername)
        {
            var tournamentModel = new TournamentModel
            {
                Id = tournament.Id,
                Name = tournament.Name,
                Description = tournament.DescriptionSource
            };

            switch(tournament.TournamentType)
            {
                case "single elimination":
                    tournamentModel.TournamentType = TournamentType.SingleElimination;
                    break;
                case "double elimination":
                    tournamentModel.TournamentType = TournamentType.DoubleElimination;
                    break;
                case "round robin":
                    tournamentModel.TournamentType = TournamentType.League;
                    break;
            }

            if (tournamentModel.TournamentType == TournamentType.League)
                return CreateLeageTournament(tournamentModel, tournament, participants, matches, loggedParticipantUsername);

            return tournamentModel;
        }
Esempio n. 4
0
        public static TournamentModel CreateLeageTournament(TournamentModel tournamentModel, Tournament tournament, IList<Participant> participants,
			IList<Match> matches, string loggedParticipantUsername)
        {
            foreach (var participant in participants)
            {
                var playerModel = PlayerModel.Create(participant);
                tournamentModel.Players.Add(playerModel);

                var playerMatches = matches.Where(m => (m.Player1Id == participant.Id || m.Player2Id == participant.Id) && m.IsComplete);

                var playerPosition = new PlayerTablePositionModel
                {
                    Player = playerModel,
                    Wins = playerMatches.Where(m => m.WinnerId == participant.Id).Count(),
                    Tie = playerMatches.Where(m => m.IsTie).Count(),
                    Lost = playerMatches.Where(m => !m.IsTie && m.WinnerId != participant.Id).Count(),
                    GoalsFavor = matches.Where(m => m.Player1Id == participant.Id).Select(m => m.GoalsPlayer1).Sum() +
                                    matches.Where(m => m.Player2Id == participant.Id).Select(m => m.GoalsPlayer2).Sum(),
                    GoalsAgainst = matches.Where(m => m.Player1Id == participant.Id).Select(m => m.GoalsPlayer2).Sum() +
                                    matches.Where(m => m.Player2Id == participant.Id).Select(m => m.GoalsPlayer1).Sum()

                };

                playerPosition.Score = playerPosition.Wins * (int)tournament.RoundRobinPointsForMatchWin + playerPosition.Tie * (int)tournament.RoundRobinPointsForMatchTie;

                tournamentModel.PlayerPositions.Add(playerPosition);
            }

            var matchGrouping = matches.GroupBy(m => m.Round.Value);
            tournamentModel.Matches = new Dictionary<int, IList<MatchModel>>();
            foreach (var group in matchGrouping)
            {
                IList<MatchModel> matchModels = new List<MatchModel>();
                foreach (var match in group.ToList())
                {
                    matchModels.Add(MatchModel.Create(match, participants));
                }
                tournamentModel.Matches.Add(group.Key, matchModels);
            }

            tournamentModel.Players = tournamentModel.Players.OrderBy(p => p.Name).ToList();

            tournamentModel.PlayerPositions = tournamentModel.PlayerPositions
                                                    .OrderByDescending(p => p.Score)
                                                    .ThenByDescending(p => p.GoalsFavor - p.GoalsAgainst)
                                                    .ThenByDescending(p => p.GoalsFavor)
                                                    .ToList();

            var loggedParticipant = tournamentModel.Players.Where(p => string.Equals(p.Name, loggedParticipantUsername, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

            if (loggedParticipant != null)
            {
                var notPlayedPlayerIds = matches.Where(m => !m.IsComplete && (m.Player1Id == loggedParticipant.Id || m.Player2Id == loggedParticipant.Id))
                                        .Select(m =>
                                        {
                                            if (m.Player1Id == loggedParticipant.Id)
                                                return m.Player2Id;
                                            else
                                                return m.Player1Id;
                                        });

                tournamentModel.CanSendResults = true;

                tournamentModel.NotPlayedPlayers = tournamentModel.Players.Where(p => notPlayedPlayerIds.Contains(p.Id)).ToList();

                tournamentModel.LoggedParticipant = loggedParticipant;
            }

            return tournamentModel;
        }