Ejemplo n.º 1
0
        public ActionResult DisplayChallongeData(string challongeUrl)
        {
            var tournamentDataModel = new TournamentDataModel();
            var validUrl = _challongeApiUrlHelper.IsValidChallongeUrl(challongeUrl);           

            if (!validUrl)
            {
                tournamentDataModel.ErrorMessage = "Please enter a valid Challonge url.";
                return PartialView("_TournamentData", tournamentDataModel);
            }

            var matchesUrl = _challongeApiUrlHelper.ConvertUrlToMatchesUrl(challongeUrl);
            var participantsUrl = _challongeApiUrlHelper.ConvertUrlToParticipantsUrl(challongeUrl);
            var tournamentUrl = _challongeApiUrlHelper.ConvertToTournamentUrl(challongeUrl);

            tournamentDataModel.Matches = _asyncDataRetrivalHelper.Deserialize<List<RootChallongeMatch>>(matchesUrl);
            tournamentDataModel.Participants = _asyncDataRetrivalHelper.Deserialize<List<RootChallongeParticipant>>(participantsUrl);
            tournamentDataModel.Tournament = _asyncDataRetrivalHelper.Deserialize<RootChallongeTournament>(tournamentUrl);

            if (tournamentDataModel.Matches == null || tournamentDataModel.Participants == null || tournamentDataModel.Tournament == null)
                tournamentDataModel.ErrorMessage += "There was an error reaching Challonge";

            HttpContext.Session.Add(Settings.Default.SessionTournamentDataModel, tournamentDataModel);

            return PartialView("_TournamentData", tournamentDataModel);
        }
 public TournamentDataModel ReplaceChallongeIdsWithLocalIds(TournamentDataModel model, IEnumerable<string[]> matchedPlayers, IEnumerable<string[]> unmatchedPlayers)
 {
     var playerTags = Database.PlayerTags.ToList();
     model = ReplaceMatchedIds(model, playerTags, matchedPlayers);
     model = ReplaceUnmatchedIds(model, playerTags, unmatchedPlayers);
     return SwapChallongeIdsForRankingsId(model);
 }
 public TournamentRegistrationModel(TournamentDataModel tournamentDataModel)
 {
     MatchApiUrl = tournamentDataModel.MatchApiUrl;
     Matches = tournamentDataModel.Matches;
     Participants = tournamentDataModel.Participants;
     Tournament = tournamentDataModel.Tournament;
 }
        public MatchedPlayersModel Create(TournamentDataModel tournamentDataModel)
        {
            var model = _playerRegMatchHelper.MatchTagsToPlayers(tournamentDataModel.Participants);
            model.TournamentData = tournamentDataModel;
            model.MatchedPlayers = new MatchedPlayersListModel();

            model.UnmatchedPlayers.RegisteredPlayerTags = _tournamentRegHelper.GetNonparticipatingRegisteredPlayersAndTags(tournamentDataModel.Participants);
            model.MatchedPlayers.RegisteredPlayerTags = _tournamentRegHelper.GetParticipatingRegisteredPlayersAndTags(tournamentDataModel.Participants);

            return model;
        }
        private TournamentDataModel ReplaceUnmatchedIds(TournamentDataModel model, List<PlayerTag> playerTags, IEnumerable<string[]> unmatchedPlayers)
        {
            var playerTag = new PlayerTag();
            foreach (var unmatchedPlayer in unmatchedPlayers)
            {
                playerTag = ReplaceChallongeIdWithPlayerTag(playerTags, unmatchedPlayer[0]);

                model.Participants
                    .Single(x => x.participant.display_name == playerTag.PlayerGameTag)
                    .participant.tournament_id = playerTag.PlayerId;
            }
            
            return model;
        }
        private TournamentDataModel ReplaceMatchedIds(TournamentDataModel model, List<PlayerTag> playerTags, IEnumerable<string[]> matchedPlayers)
        {            
            var playerTag = new PlayerTag();
            foreach (var matchedPlayer in matchedPlayers)
            {                
                if (matchedPlayer.Length == 1)
                    playerTag = ReplaceChallongeIdWithPlayerTag(playerTags, matchedPlayer[0]);
                else if(matchedPlayer.Length == 2)
                    playerTag = ReplaceMatchedAssociatedId(playerTags, matchedPlayer);
                else
                    playerTag = ReplaceMatchedAssociatedById(playerTags, matchedPlayer[2]);

                model.Participants
                    .Single(x => x.participant.display_name == playerTag.PlayerGameTag)
                    .participant.tournament_id = playerTag.PlayerId;
            }

            return model;
        }
        private TournamentDataModel SwapChallongeIdsForRankingsId(TournamentDataModel model)
        {
            foreach(var particiapant in model.Participants)
            {
                foreach(var player1Matches in model.Matches.Where(x => x.match.player1_id == particiapant.participant.id))
                {
                    player1Matches.match.player1_id = particiapant.participant.tournament_id;
                }

                foreach (var player1Matches in model.Matches.Where(x => x.match.player2_id == particiapant.participant.id))
                {
                    player1Matches.match.player2_id = particiapant.participant.tournament_id;
                }

                foreach (var player1Matches in model.Matches.Where(x => x.match.winner_id == particiapant.participant.id))
                {
                    player1Matches.match.winner_id = particiapant.participant.tournament_id;
                }
            }

            return model;
        }
        public void SaveTournamentData(TournamentDataModel model, IEnumerable<string[]> matchedPlayers, IEnumerable<string[]> unmatchedPlayers, int tournamentId)
        {
            matchedPlayers = matchedPlayers ?? new List<string[]>();
            unmatchedPlayers = unmatchedPlayers ?? new List<string[]>();
            AddIdentifiersToRegisteredTags(matchedPlayers);
            AssociateUnmatchedPlayer(unmatchedPlayers);
            SaveUnmatchedPlayers(unmatchedPlayers, tournamentId);
            Database.SaveChanges();

            model = _convertChallongeHelper.ReplaceChallongeIdsWithLocalIds(model, matchedPlayers, unmatchedPlayers);

            ChangeMatchedTagIdForPlayerId(matchedPlayers);
            SaveAttendees(model, tournamentId);
            SaveMatches(model, tournamentId);

            Database.SaveChanges();
        }
        private void SaveAttendees(TournamentDataModel model, int tournamentId)
        {
            var attendeeIds = model.Participants.Select(x => x.participant.tournament_id);

            foreach(var id in attendeeIds)
            {
                Database.TournamentAttendees.Add(
                    new TournamentAttendee
                    {
                        TournamentId = tournamentId,
                        PlayerId = id
                    }
                );
            }
        }
 private void SaveMatches(TournamentDataModel model, int tournamentId)
 {
     Database.Matches.AddRange(model.Matches.ToDbMatch(tournamentId));
 }