Exemplo n.º 1
0
        private PreviousMatch BuildPreviousMatch(ElementHistory history, Fixture fixture)
        {
            var previousMatch = new PreviousMatch();

            if (history != null || fixture != null)
            {
                previousMatch.IsValid    = 1;
                previousMatch.Minutes    = history.Minutes;
                previousMatch.Points     = history.TotalPoints;
                previousMatch.Influence  = double.Parse(history.Influence);
                previousMatch.Creativity = double.Parse(history.Creativity);
                previousMatch.Threat     = double.Parse(history.Threat);

                previousMatch.AtHome     = history.WasHome ? 1 : 0;
                previousMatch.Difficulty = history.WasHome ? fixture.HomeTeamDifficulty : fixture.AwayTeamDifficulty;
            }
            else
            {
                previousMatch.IsValid    = 0;
                previousMatch.Minutes    = 0;
                previousMatch.Points     = 0;
                previousMatch.Influence  = 0;
                previousMatch.Creativity = 0;
                previousMatch.Threat     = 0;
                previousMatch.AtHome     = 0;
                previousMatch.Difficulty = 0;
            }

            return(previousMatch);
        }
Exemplo n.º 2
0
        private static string BuildGameweeksHeader(int currentRound)
        {
            var headerBuilder = new StringBuilder();

            for (int round = 1; round < currentRound; ++round)
            {
                headerBuilder.Append(PreviousMatch.GetHeaderRow($"gw-less-{round}"));
                if (round < (currentRound - 1))
                {
                    headerBuilder.Append(",");
                }
            }

            return(headerBuilder.ToString());
        }
Exemplo n.º 3
0
        public async Task Build(WebApiClient fplWebApiClient, StaticResponse staticResponse, string path)
        {
            IEnumerable <Fixture> fixtures = await fplWebApiClient.GetFixturesAsync();

            using (StreamWriter writer = new StreamWriter(File.Create(path)))
            {
                Event currentEvent         = staticResponse.Events.Single(e => e.IsCurrent);
                int   numPreviousGameweeks = currentEvent.Id - 1;

                string gameweeksHeader = BuildGameweeksHeader(currentEvent.Id);
                writer.WriteLine($"{PreviousSeason.GetHeaderRow("2017")},{PreviousSeason.GetHeaderRow("2018")},{PreviousSeason.GetHeaderRow("2019")},{gameweeksHeader},playerid,teamid,position,selectedby,transfersin,transfersout,diff,home,value,points");

                int progress = 0;
                //IEnumerable<Element> elementsToProcess = staticResponse.Elements.Take(50);
                IEnumerable <Element> elementsToProcess = staticResponse.Elements;
                int max = elementsToProcess.Count();
                foreach (Element element in elementsToProcess)
                {
                    Console.WriteLine($"Processing {++progress} of {max}");

                    ElementSummaryResponse elementResponse = await fplWebApiClient.GetElementSummaryAsync(element.Id);

                    if (elementResponse == null)
                    {
                        continue;
                    }

                    ElementHistoryPast history2017 = elementResponse.HistoryPast.SingleOrDefault(hp => hp.SeasonName == "2017/18");
                    ElementHistoryPast history2018 = elementResponse.HistoryPast.SingleOrDefault(hp => hp.SeasonName == "2018/19");
                    ElementHistoryPast history2019 = elementResponse.HistoryPast.SingleOrDefault(hp => hp.SeasonName == "2019/20");

                    PreviousSeason season2017 = BuildPreviousSeason(history2017);
                    PreviousSeason season2018 = BuildPreviousSeason(history2018);
                    PreviousSeason season2019 = BuildPreviousSeason(history2019);

                    // First pass to build all previous match data by gameweek
                    var previousMatchesByGameweek = new Dictionary <int, PreviousMatch>();
                    foreach (ElementHistory history in elementResponse.History)
                    {
                        // TODO: add support for multi-match weeks
                        if (previousMatchesByGameweek.ContainsKey(history.Round))
                        {
                            continue;
                        }

                        Fixture       fixture  = fixtures.Single(f => f.Id == history.FixtureId);
                        PreviousMatch gameweek = BuildPreviousMatch(history, fixture);

                        previousMatchesByGameweek.Add(history.Round, gameweek);
                    }

                    PreviousMatch gameweekNull = BuildPreviousMatch(null, null);

                    foreach (ElementHistory history in elementResponse.History)
                    {
                        TrainingDataRow row = new TrainingDataRow();
                        row.Season2017 = season2017;
                        row.Season2018 = season2018;
                        row.Season2019 = season2019;

                        row.PreviousGameweeks = new PreviousMatch[numPreviousGameweeks];
                        int previousRound = history.Round - 1;
                        for (int round = 0; round < numPreviousGameweeks; ++round)
                        {
                            if (previousMatchesByGameweek.ContainsKey(previousRound))
                            {
                                row.PreviousGameweeks[round] = previousMatchesByGameweek[previousRound];
                            }
                            else
                            {
                                row.PreviousGameweeks[round] = gameweekNull;
                            }

                            --previousRound;
                        }

                        Fixture fixture = fixtures.Single(f => f.Id == history.FixtureId);
                        row.AtHome = history.WasHome ? 1 : 0;
                        if (history.WasHome)
                        {
                            row.Difficulty = fixture.HomeTeamDifficulty;
                        }
                        else
                        {
                            row.Difficulty = fixture.AwayTeamDifficulty;
                        }
                        row.Value       = history.Value;
                        row.TotalPoints = history.TotalPoints;

                        string seasonsData = $"{row.Season2017},{row.Season2018},{row.Season2019}";
                        var    previousGameweeksBuilder = new StringBuilder();
                        for (int round = 0; round < numPreviousGameweeks; ++round)
                        {
                            previousGameweeksBuilder.Append(row.PreviousGameweeks[round]);
                            if (round < (numPreviousGameweeks - 1))
                            {
                                previousGameweeksBuilder.Append(",");
                            }
                        }
                        string gameweeksData = previousGameweeksBuilder.ToString();
                        string otherData     = $"{element.Id},{element.Team},{element.ElementType},{element.SelectedByPercent:F1},{history.TransfersIn:F1},{history.TransfersOut:F1},{row.Difficulty},{row.AtHome},{row.Value:F1},{row.TotalPoints:F1}";
                        writer.WriteLine($"{seasonsData},{gameweeksData},{otherData}");
                    }
                }
            }
        }