Esempio n. 1
0
        private async Task ImportGoalscorers(MatchV matchV, HtmlDocument document, bool isTeam1)
        {
            var teamGuid       = isTeam1 ? matchV.Team1Guid : matchV.Team2Guid;
            var goalscorersDiv = document.DocumentNode.Descendants("div").FirstOrDefault(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "goalscorers");

            if (goalscorersDiv == null)
            {
                return;
            }

            var teamDiv = isTeam1
                ? goalscorersDiv.Descendants("div").FirstOrDefault(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "teamA")
                : goalscorersDiv.Descendants("div").FirstOrDefault(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "teamB");

            if (teamDiv != null)
            {
                foreach (var scorerSpan in teamDiv.Descendants("span"))
                {
                    var playerLink = scorerSpan.Descendants("a").FirstOrDefault(a => a.Attributes.Contains("href"));

                    if (playerLink == null || !playerLink.Attributes.Contains("href"))
                    {
                        continue;
                    }

                    var playerHref = playerLink.Attributes["href"].Value;
                    var lookupId   = playerHref.GetTextAfter("player.sd?player_id=");

                    var personKey = await ProcessPlayer(lookupId);

                    if (personKey == null)
                    {
                        continue;
                    }

                    var scoreTimes = scorerSpan.InnerText.GetTextBetween("(", ")").Trim().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (var scoreTimeText in scoreTimes)
                    {
                        if (scoreTimeText.IndexOf("s/o") >= 0)
                        {
                            continue;
                        }

                        var matchEventType = scoreTimeText.IndexOf("og") >= 0 ? MatchEventType.OwnGoal : MatchEventType.Scored;

                        var scoreTime = scoreTimeText.Replace("pen", "").Replace("og", "").Trim().ToNullableShort();

                        if (matchEventType != MatchEventType.OwnGoal)
                        {
                            await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, null, matchEventType, scoreTime, null);
                        }
                        else
                        {
                            await ProcessMatchEvent(matchV.PrimaryKey, isTeam1?matchV.Team2Guid : matchV.Team1Guid, (Guid)personKey, null, matchEventType, scoreTime, null);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private async Task ProcessMatchPlayers(HtmlDocument document, MatchV matchV)
        {
            var playersDiv = document.DocumentNode.Descendants("div").FirstOrDefault(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "matchPanel");

            if (playersDiv == null)
            {
                return;
            }

            var team1Div = playersDiv.Descendants("div").FirstOrDefault(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "soccerColumn teamA");

            if (team1Div != null)
            {
                await ImportPlayers(matchV, true, team1Div, MatchEventType.Started);
                await ImportPlayers(matchV, true, team1Div, MatchEventType.Substitute);
            }

            var team2Div = playersDiv.Descendants("div").FirstOrDefault(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "soccerColumn teamB");

            if (team2Div != null)
            {
                await ImportPlayers(matchV, false, team2Div, MatchEventType.Started);
                await ImportPlayers(matchV, false, team2Div, MatchEventType.Substitute);
            }

            await ImportGoalscorers(matchV, document, true);
            await ImportGoalscorers(matchV, document, false);

            Provider.SaveChanges();
        }
        // Sort of a cheaty pipeline here
        private void EventView_SendMatchesData(object sender, EventArgs <EventViewModel> e)
        {
            UpdateMatchTabEnabled();

            MatchV.SendData(sender, e);
            UpdateV.SendData(sender, e);
            TeamsV.SendInitData();
        }
        public async Task <ActionResult> EditPlayers(MatchPlayersEditorViewModel viewModel)
        {
            await SetModelsByPrimaryKey(viewModel);

            var teamGuid    = viewModel.TeamNumber == 1 ? viewModel.VersionEntity.Team1Guid : viewModel.VersionEntity.Team2Guid;
            var matchEvents = viewModel.VersionEntity.MatchEvents.Where(m => m.TeamPrimaryKey == teamGuid);

            if (viewModel.VersionEntity.ModifiedUserId == UserId)
            {
                if (viewModel.MatchPersonViewModels != null)
                {
                    foreach (var item in viewModel.MatchPersonViewModels)
                    {
                        if (item.MatchEventStartType == null)
                        {
                            var query = from m in matchEvents
                                        where m.PersonPrimaryKey == item.PersonGuid
                                        select m;

                            if (query.Any())
                            {
                                var primaryKey = query.First().PrimaryKey;
                                var header     = await DbProvider.GetMatchEventByPrimaryKey(primaryKey);

                                for (var i = query.Count() - 1; i >= 0; i--)
                                {
                                    DbProvider.Remove(query.ElementAt(i));
                                }

                                DbProvider.Remove(header);
                            }
                        }
                        else
                        {
                            var query = from m in matchEvents
                                        where m.PersonPrimaryKey == item.PersonGuid &&
                                        (m.MatchEventType == MatchEventType.Substitute || m.MatchEventType == MatchEventType.Started)
                                        select m;

                            if (query.Any())
                            {
                                query.First().MatchEventType = (MatchEventType)item.MatchEventStartType.ToMatchEventType();
                                query.First().PositionType   = item.PositionType;
                            }
                        }
                    }
                }

                if (viewModel.NewMatchPersonViewModels != null)
                {
                    foreach (var item in viewModel.NewMatchPersonViewModels.Where(m => m.MatchEventStartType != null))
                    {
                        DbProvider.Add(new MatchEvent()
                        {
                            PrimaryKey       = Guid.NewGuid(),
                            MatchVPrimaryKey = viewModel.PrimaryKey,
                            PersonPrimaryKey = item.PersonGuid,
                            PositionType     = item.PositionType,
                            TeamPrimaryKey   = teamGuid,
                            MatchEventType   = (MatchEventType)item.MatchEventStartType.ToMatchEventType(),
                            Minute           = null,
                            Extra            = null
                        });
                    }
                }

                DbProvider.SaveChanges();

                return(RedirectToAction("EditPlayers", new { hk = viewModel.ShortHeaderKey, pk = viewModel.ShortPrimaryKey, tm = viewModel.TeamNumber }));
            }
            else
            {
                var previousVersion = await DbProvider.GetMatch(viewModel.VersionEntity.PrimaryKey, viewModel.VersionEntity.HeaderKey);

                var newMatchV = MatchV.CreateNewVersion <MatchV>(previousVersion.OwnerUserId, UserId);
                newMatchV.HeaderKey   = previousVersion.HeaderKey;
                newMatchV.DateCreated = previousVersion.DateCreated;
                newMatchV.SetData(previousVersion);

                foreach (var matchEvent in previousVersion.MatchEvents)
                {
                    var newMatchEvent = new MatchEvent()
                    {
                        PrimaryKey       = Guid.NewGuid(),
                        MatchVPrimaryKey = newMatchV.PrimaryKey,
                        TeamPrimaryKey   = matchEvent.TeamPrimaryKey,
                        PersonPrimaryKey = matchEvent.PersonPrimaryKey,
                        PositionType     = matchEvent.PositionType,
                        MatchEventType   = matchEvent.MatchEventType,
                        Minute           = matchEvent.Minute,
                        Extra            = matchEvent.Extra
                    };

                    DbProvider.Add(newMatchEvent);
                }

                DbProvider.SaveChanges();

                return(RedirectToAction("EditPlayers", new { hk = newMatchV.HeaderKey.ToShortGuid(), pk = newMatchV.PrimaryKey.ToShortGuid(), tm = viewModel.TeamNumber }));
            }
        }
Esempio n. 5
0
        private async Task ImportPlayers(MatchV matchV, bool isTeam1, HtmlNode teamDiv, MatchEventType matchEventType)
        {
            var startersTbody = matchEventType == MatchEventType.Started
                ? teamDiv.Descendants("tbody").FirstOrDefault(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "firstTeam")
                : teamDiv.Descendants("tbody").FirstOrDefault(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "reserve");

            if (startersTbody != null)
            {
                foreach (var tr in startersTbody.Descendants("tr"))
                {
                    short?sentOffTime = null;
                    var   teamGuid    = isTeam1 ? matchV.Team1Guid : matchV.Team2Guid;

                    //Starters-Subs
                    var link = tr.Descendants("a").FirstOrDefault(t => t.Attributes.Contains("href"));

                    if (link == null)
                    {
                        break;
                    }

                    var lookupId = link.Attributes["href"].Value.GetTextAfter("player.sd?player_id=");

                    var positionString = tr.Descendants("td").ElementAt(isTeam1 ? 1 : 2).InnerText.GetTextBetween("(", ")");

                    var personKey = await ProcessPlayer(lookupId);

                    if (personKey == null)
                    {
                        continue;
                    }

                    await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, GetPositionType(positionString), matchEventType, null, null);

                    //Substitutions
                    var timeText = tr.Descendants("td").ElementAt(isTeam1 ? 0 : 3).InnerText.GetTextBetween("(", ")").Trim();

                    if (!string.IsNullOrWhiteSpace(timeText))
                    {
                        if (matchEventType == MatchEventType.Started)
                        {
                            // (s/o 90)
                            if (timeText.IndexOf("s/o") >= 0)
                            {
                                sentOffTime = timeText.GetTextAfter("s/o").Trim().ToNullableShort();
                            }
                            else
                            {
                                var substitutedTime = timeText.ToNullableShort();

                                if (substitutedTime != null)
                                {
                                    await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, null, MatchEventType.TakenOff, substitutedTime, null);
                                }
                            }
                        }

                        if (matchEventType == MatchEventType.Substitute)
                        {
                            // (85, s/o 85) brought on and sent off
                            // (77-89) brought on and substituted
                            if (timeText.IndexOf(",") >= 0)
                            {
                                var broughtOnTime = timeText.GetTextBefore(",").Trim().ToNullableShort();

                                if (broughtOnTime != null)
                                {
                                    await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, null, MatchEventType.BroughtOn, broughtOnTime, null);
                                }

                                var remainingText = timeText.GetTextAfter(",");

                                if (remainingText.IndexOf("s/o") >= 0)
                                {
                                    sentOffTime = remainingText.GetTextAfter("s/o").Trim().ToNullableShort();
                                }
                            }
                            else if (timeText.IndexOf("-") >= 0)
                            {
                                var broughtOnTime = timeText.GetTextBefore("-").Trim().ToNullableShort();

                                if (broughtOnTime != null)
                                {
                                    await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, null, MatchEventType.BroughtOn, broughtOnTime, null);
                                }

                                var remainingText = timeText.GetTextAfter("-").Trim();

                                var takenOffTime = remainingText.Trim().ToNullableShort();

                                if (takenOffTime != null)
                                {
                                    await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, null, MatchEventType.TakenOff, takenOffTime, null);
                                }
                            }
                            else
                            {
                                var broughtOnTime = timeText.Trim().ToNullableShort();

                                if (broughtOnTime != null)
                                {
                                    await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, null, MatchEventType.BroughtOn, broughtOnTime, null);
                                }
                            }
                        }
                    }

                    //Discipline
                    var image = tr.Descendants("td").ElementAt(isTeam1 ? 3 : 0).Descendants("img").FirstOrDefault();

                    if (image != null)
                    {
                        switch (image.Attributes["title"].Value)
                        {
                        case "Yellow card":
                            await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, null, MatchEventType.Booked, null, null);

                            break;

                        case "Red card":
                            await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, null, MatchEventType.SentOff, sentOffTime, null);

                            break;


                        case "Red and yellow cards":
                            await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, null, MatchEventType.Booked, null, null);
                            await ProcessMatchEvent(matchV.PrimaryKey, teamGuid, (Guid)personKey, null, MatchEventType.SentOff, sentOffTime, null);

                            break;

                        default:
                            throw new NotImplementedException();
                        }
                    }
                }
            }
        }
Esempio n. 6
0
        private async Task ImportMatch(Competition competition, Guid?team1Key, Guid?team2Key, string matchId, DateTime matchDate)
        {
            // http://www.soccerbase.com/matches/additional_information.sd?id_game=689457
            var uri      = new Uri(string.Format("http://www.soccerbase.com/matches/additional_information.sd?id_game={0}", matchId));
            var document = GetHtmlDocument(uri);

            //win - on aggregate
            var matchStatus = document.DocumentNode.Descendants("p").First(p => p.Attributes["class"].Value == "status").InnerText;
            var isCompleted = matchStatus == "result" || matchStatus.Contains("on aggregate") || matchStatus.Contains("on penalties") || matchStatus.Contains("AFTER EXTRA TIME") || matchStatus.Contains("win on away goals");

            if (!isCompleted)
            {
                return;
            }

            //Scores
            var span    = document.DocumentNode.Descendants("span").FirstOrDefault(s => s.Attributes.Contains("title") && s.Attributes["title"].Value.Contains("Half-time"));
            var team1Ht = span != null?span.Descendants("em").ElementAt(0).InnerText.ToNullableShort() : null;

            var team2Ht = span != null?span.Descendants("em").ElementAt(1).InnerText.ToNullableShort() : null;

            span = document.DocumentNode.Descendants("span").FirstOrDefault(s => s.Attributes.Contains("title") && s.Attributes["title"].Value.Contains("Full-time"));
            var team1Ft = span != null?span.Descendants("em").ElementAt(0).InnerText.ToNullableShort() : null;

            var team2Ft = span != null?span.Descendants("em").ElementAt(1).InnerText.ToNullableShort() : null;

            //Attendance
            var attendance = GetAttendance(matchId);

            //Does match exist in Soccerbase lookup?
            var matchKey = await LookupMatch(matchId);

            Match  match  = null;
            MatchV matchV = null;

            //Does campaign exist?
            var campaign = await ProcessCampaign(competition.PrimaryKey, matchDate);

            if (campaign == null)
            {
                return;
            }

            var venueGuid = await GetVenueGuid((Guid)team1Key, matchDate);

            if (matchKey == null)
            {
                var endOfMatchDay       = matchDate.ToEndOfDay();
                var beginningOfMatchDay = matchDate.Date;

                //Does match exist in Match tables
                //var matchQuery = from m in provider.MatchVs
                //                 where m.CampaignGuid == campaign.PrimaryKey
                //                 && m.Team1Guid == team1Key
                //                 && m.Team2Guid == team2Key
                //                 && m.IsActive
                //                 && !m.IsMarkedForDeletion
                //                 && m.MatchDate >= beginningOfMatchDay
                //                 && m.MatchDate <= endOfMatchDay
                //                 select m;

                var searchMatchV = await Provider.GetMatchByCampaignAndTeams(campaign.PrimaryKey, (Guid)team1Key, (Guid)team2Key, matchDate);

                if (searchMatchV != null)
                {
                    match    = searchMatchV.Match;
                    matchKey = match.PrimaryKey;
                    matchV   = match.GetApprovedVersion <MatchV>(matchDate);
                }
                else
                {
                    match                = Match.Create <Match>();
                    matchV               = MatchV.CreateNew <MatchV>(User.GetUserId());
                    matchV.HeaderKey     = match.PrimaryKey;
                    matchV.EffectiveFrom = Date.LowDate;
                    matchV.EffectiveTo   = Date.HighDate;

                    CreateLookupMatch(match.PrimaryKey, matchId);
                }
            }
            else
            {
                match  = (await Provider.GetMatch((Guid)matchKey, matchDate)).Match;
                matchV = match.GetApprovedVersion <MatchV>(matchDate);
            }

            if (matchV.MatchImportType == MatchImportType.AutomaticResultWithEvents || matchV.MatchImportType == MatchImportType.ManualResult)
            {
                return;
            }

            matchV.MatchDate       = matchDate.Date;
            matchV.MatchTimeTicks  = matchV.MatchTimeTicks > 0 ? matchV.MatchTimeTicks : (matchDate - matchDate.Date).Ticks;
            matchV.MatchImportType = MatchImportType.AutomaticResultWithEvents;
            matchV.VenueGuid       = venueGuid;
            matchV.Attendance      = attendance;

            if (competition.GetApprovedVersion <CompetitionV>(matchDate).CompetitionType == CompetitionType.League)
            {
                var campaignStage = campaign.CampaignStages.FirstOrDefault(f => f.IsDefault);

                if (campaignStage != null)
                {
                    matchV.CampaignStageKey = campaignStage.PrimaryKey;
                }
            }

            matchV.Team1Guid = (Guid)team1Key;
            matchV.Team1HT   = team1Ht;
            matchV.Team1FT   = team1Ft;
            matchV.Team2Guid = (Guid)team2Key;
            matchV.Team2HT   = team2Ht;
            matchV.Team2FT   = team2Ft;

            if (matchKey == null)
            {
                Provider.Add(match);
                Provider.Add(matchV);
            }

            Provider.SaveChanges();

            await ProcessMatchPlayers(document, matchV);
        }
 public void Remove(MatchV matchV)
 {
     MatchVRepository.Remove(matchV);
 }
 public void Add(MatchV matchV)
 {
     MatchVRepository.Add(matchV);
 }
Esempio n. 9
0
        protected async Task ProcessMatch(string lookupId, DateTime matchDate, CompetitionV competitionV, Guid?venueGuid, int?attendance, Guid team1Guid, short?team1Ht, short?team1Ft, Guid team2Guid, short?team2Ht, short?team2Ft, Guid campaignStageKey)
        {
            if (campaignStageKey == null)
            {
                return;
            }

            var lookupMatchSearch = await Provider.GetLookupMatch(ImportSite, lookupId);

            var startOfDay = matchDate.Date;
            var endOfDay   = matchDate.ToEndOfDay();

            var matchSearch = await Provider.GetMatchByTeams(team1Guid, team2Guid, matchDate);

            var campaign = await Provider.FindCampaignAsync(competitionV.HeaderKey, matchDate);

            if (campaign == null)
            {
                var startDate = Date.LowDate;
                var endDate   = Date.HighDate;

                if (!competitionV.GetCampaignDates(matchDate, ref startDate, ref endDate))
                {
                    return;
                }

                var newCampaign = Campaign.CreateNew(competitionV.HeaderKey, startDate, endDate);
                campaign = newCampaign;

                Provider.Add(newCampaign);
                Provider.SaveChanges();
            }

            if (lookupMatchSearch != null || matchSearch != null)
            {
                MatchV matchV = null;

                if (lookupMatchSearch != null)
                {
                    var lookupMatch = lookupMatchSearch;
                    matchV = (await Provider.GetMatch(lookupMatch.MatchGuid, DateTime.Now));
                }
                else if (matchSearch != null)
                {
                    matchV = matchSearch;
                }

                if (matchV == null || matchV.MatchImportType == MatchImportType.ManualResult)
                {
                    return;
                }

                matchV.MatchDate        = matchDate.Date;
                matchV.MatchTimeTicks   = (matchDate - matchDate.Date).Ticks;
                matchV.VenueGuid        = venueGuid;
                matchV.Attendance       = attendance;
                matchV.Team1Guid        = team1Guid;
                matchV.Team1HT          = team1Ht;
                matchV.Team1FT          = team1Ft;
                matchV.Team2Guid        = team2Guid;
                matchV.Team2HT          = team2Ht;
                matchV.Team2FT          = team2Ft;
                matchV.MatchImportType  = matchV.GetMatchImportType(true);
                matchV.CampaignStageKey = campaignStageKey;
            }
            else
            {
                if (matchDate < DateTime.Now && team1Ft == null && team2Ft == null)
                {
                    return;
                }

                var matchGuid = Guid.NewGuid();

                Provider.Add(new Match()
                {
                    PrimaryKey = matchGuid
                });

                var matchV = MatchV.CreateNew <MatchV>(User.GetUserId());
                matchV.HeaderKey        = matchGuid;
                matchV.MatchDate        = matchDate.Date;
                matchV.MatchTimeTicks   = (matchDate - matchDate.Date).Ticks;
                matchV.VenueGuid        = venueGuid;
                matchV.Attendance       = attendance;
                matchV.Team1Guid        = team1Guid;
                matchV.Team1HT          = team1Ht;
                matchV.Team1FT          = team1Ft;
                matchV.Team2Guid        = team2Guid;
                matchV.Team2HT          = team2Ht;
                matchV.Team2FT          = team2Ft;
                matchV.EffectiveFrom    = Date.LowDate;
                matchV.EffectiveTo      = Date.HighDate;
                matchV.MatchImportType  = matchV.GetMatchImportType(true);
                matchV.CampaignStageKey = campaignStageKey;

                Provider.Add(matchV);

                if (lookupId != string.Empty)
                {
                    Provider.Add(new LookupMatch()
                    {
                        PrimaryKey = Guid.NewGuid(),
                        ImportSite = ImportSite,
                        MatchGuid  = matchGuid,
                        LookupId   = lookupId
                    });
                }
            }

            Provider.SaveChanges();
        }
Esempio n. 10
0
        public static void MatchBreadcrumb(this IList<BreadcrumbViewModel> breadcrumbViewModels, MatchV matchV, DateTime viewDate)
        {
            breadcrumbViewModels.CompetitionBreadcrumb(matchV.CampaignStage.Campaign.Competition.GetApprovedVersion<CompetitionV>(viewDate), viewDate);

            breadcrumbViewModels.Add(AreaType.Mtc, matchV.HeaderKey, matchV.ToViewModel(viewDate).ToString(), string.Empty);
        }