public string[] ToGrid(FixtureTeams teams) { int teamsCount = Math.Max(teams.Count, (int)this.Max(fg => fg.Teams.Count == 0 ? 0 : fg.Teams.Max(ft => ft.Key.Id()))); var lines = new string[teamsCount]; for (int col = 0; col < Count; col++) { var fg = this[col]; for (int row = 0; row < teamsCount; row++) { if (lines[row] == null) { lines[row] = ""; } if (row < teams.Count && fg.Teams.ContainsKey(teams[row])) { lines[row] += fg.Teams[teams[row]].ToChar(); } else { lines[row] += '.'; } } } return(lines); }
// A game will be a time or date/time, followed by a separator, followed // by a separated list of numbers, which are the teams in that game. e.g.: // 8:00 1 2 3 public void Parse(string s, FixtureTeams teams, char separator = '\t') { string[] lines = s.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string line in lines) { FixtureGame fg = new FixtureGame(); string[] fields = line.Split('\t'); fg.Time = DateTime.Parse(fields[0]); for (int i = 1; i < fields.Length; i++) { if (!string.IsNullOrEmpty(fields[i])) { FixtureTeam ft; if (int.TryParse(fields[i], out int teamnum)) { ft = teams.Find(x => x.Id() == teamnum); } else { ft = teams.Find(x => x.LeagueTeam != null && x.LeagueTeam.Name == fields[i]); } if (ft == null) { ft = new FixtureTeam { Name = "Team " + fields[i] } } ; if (!fg.Teams.ContainsKey(ft)) { if (fields.Length <= 5) // If there are five or less teams per game, { fg.Teams.Add(ft, (Colour)i); // assign colours to teams. } else { fg.Teams.Add(ft, Colour.None); } } } } Add(fg); } }
// This parses a grid. Each game will be a column; each team will be a row. Each character in the grid is a letter // representing that team's colour in that game, or is a non-colour character if that team does not play in that game. public string[] Parse(string[] lines, FixtureTeams teams, DateTime?firstGame, TimeSpan?duration) { int minLength = int.MaxValue; for (int row = 0; row < lines.Length && row < teams.Count; row++) { int pos = lines[row].LastIndexOf('\t'); while (pos > -1) { lines[row] = lines[row].Remove(pos, 1); pos = lines[row].LastIndexOf('\t'); } minLength = Math.Min(minLength, lines[row].Length); } if (minLength < int.MaxValue) { for (int col = 0; col < minLength; col++) { var game = new FixtureGame(); for (int row = 0; row < lines.Length && row < teams.Count; row++) { Colour colour = ColourExtensions.ToColour(lines[row][col]); if (colour != Colour.None) { game.Teams.Add(teams[row], colour); } else if (colour == Colour.None && char.IsLetter(lines[row][col])) { game.Teams.Add(teams[row], Colour.None); } } if (firstGame != null) { game.Time = (DateTime)firstGame; firstGame += duration ?? TimeSpan.Zero; } Add(game); } } if (firstGame != null) { Sort(); } return(lines); }
// Import past games from a league. public void Parse(League league, FixtureTeams teams) { foreach (Game lg in league.Games(false)) { FixtureGame fg = new FixtureGame(); fg.Time = lg.Time; foreach (GameTeam gt in lg.Teams) { FixtureTeam ft = teams.Find(x => x.LeagueTeam == league.LeagueTeam(gt)); if (ft != null && !fg.Teams.ContainsKey(ft)) { fg.Teams.Add(ft, gt.Colour); } } Add(fg); } }
public Fixture() { Teams = new FixtureTeams(); Games = new FixtureGames(); }