private PreviousSeason BuildPreviousSeason(ElementHistoryPast history) { var previousSeason = new PreviousSeason(); if (history != null) { previousSeason.IsValid = 1; previousSeason.Minutes = history.Minutes; previousSeason.Points = history.TotalPoints; previousSeason.Influence = double.Parse(history.Influence); previousSeason.Creativity = double.Parse(history.Creativity); previousSeason.Threat = double.Parse(history.Threat); } else { previousSeason.IsValid = 0; previousSeason.Minutes = 0; previousSeason.Points = 0; previousSeason.Influence = 0; previousSeason.Creativity = 0; previousSeason.Threat = 0; } return(previousSeason); }
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}"); } } } }