public static void SaveEntryToFile(this MatchUpEntryModel entry, string matchupEntryFile) { List <MatchUpEntryModel> entries = GlobalConfig.MatchupEntryFile.FullFilePath().LoadFile().ConvertToMatchupEntryModels(); int currentId = 1; if (entries.Count > 0) { currentId = entries.OrderByDescending(x => x.Id).First().Id + 1; } entry.Id = currentId; entries.Add(entry); List <string> lines = new List <string>(); foreach (MatchUpEntryModel e in entries) { string parent = ""; if (e.ParentMatchup != null) { parent = e.ParentMatchup.Id.ToString(); } string teamCompeting = ""; if (e.TeamCompeting != null) { teamCompeting = e.TeamCompeting.Id.ToString(); } lines.Add($"{ e.Id },{ teamCompeting },{ e.Score },{ parent }"); } File.WriteAllLines(GlobalConfig.MatchupEntryFile.FullFilePath(), lines); }
public static List <MatchUpEntryModel> ConvertToMatchupEntryModels(this List <string> lines) { // id = 0, TeamCompeting = 1, Score = 2, ParentMatchup = 3 List <MatchUpEntryModel> output = new List <MatchUpEntryModel>(); foreach (string line in lines) { string[] cols = line.Split(','); MatchUpEntryModel me = new MatchUpEntryModel(); me.Id = int.Parse(cols[0]); if (cols[1].Length == 0) { me.TeamCompeting = null; } else { me.TeamCompeting = LookupTeamById(int.Parse(cols[1])); } me.Score = double.Parse(cols[2]); int parentId = 0; if (int.TryParse(cols[3], out parentId)) { me.ParentMatchup = LookupMatchupById(parentId); } else { me.ParentMatchup = null; } output.Add(me); } return(output); }
public static void UpdateEntryToFile(this MatchUpEntryModel entry) { List <MatchUpEntryModel> entries = GlobalConfig.MatchUpEntryFile.FullFilePath().LoadFile().ConvertToMatchUpEntryModels(); MatchUpEntryModel oldEntry = new MatchUpEntryModel(); foreach (MatchUpEntryModel e in entries) { if (e.Id == entry.Id) { oldEntry = e; } } entries.Remove(oldEntry); entries.Add(entry); List <string> lines = new List <string>(); foreach (MatchUpEntryModel e in entries) { string parent = ""; if (e.ParentMatchup != null) { parent = e.ParentMatchup.Id.ToString(); } string teamCompeting = ""; if (e.TeamCompeting != null) { teamCompeting = e.TeamCompeting.Id.ToString(); } lines.Add($"{e.Id},{e.TeamCompeting.Id},{e.Score},{e.ParentMatchup.Id}"); File.WriteAllLines(GlobalConfig.MatchUpEntryFile.FullFilePath(), lines); } }
/// <summary> /// Replace the old entry with the new one /// </summary> /// <param name="model"></param> public static void UpdateEntryToFile(this MatchUpEntryModel model) { List <MatchUpEntryModel> models = GlobalConfig.MatchupEntryFile.FullFilePath().LoadFile().ConvertToMatchupEntries(); MatchUpEntryModel oldEntry = new MatchUpEntryModel(); foreach (MatchUpEntryModel entry in models) { if (entry.id == model.id) { oldEntry = entry; } } models.Remove(oldEntry); models.Add(model); List <string> lines = new List <string>(); foreach (MatchUpEntryModel entry in models) { string parent = ""; string teamCompeting = ""; if (entry.ParentMatch != null) { parent = entry.ParentMatch.id.ToString(); } if (entry.TeamCompeting != null) { teamCompeting = entry.TeamCompeting.id.ToString(); } lines.Add($"{entry.id},{teamCompeting},{entry.Score},{parent}"); } File.WriteAllLines(GlobalConfig.MatchupEntryFile.FullFilePath(), lines); }
/// <summary> /// Save matchup entry of the tournament to file /// </summary> /// <param name="model"></param> public static void SaveEntryToFile(this MatchUpEntryModel model) { List <MatchUpEntryModel> models = GlobalConfig.MatchupEntryFile.FullFilePath().LoadFile().ConvertToMatchupEntries(); int curId = 1; if (models.Count > 0) { curId = models.OrderByDescending(x => x.id).First().id + 1; } model.id = curId; models.Add(model); List <string> lines = new List <string>(); foreach (MatchUpEntryModel entry in models) { string parent = ""; string teamCompeting = ""; if (entry.ParentMatch != null) { parent = entry.ParentMatch.id.ToString(); } if (entry.TeamCompeting != null) { teamCompeting = entry.TeamCompeting.id.ToString(); } lines.Add($"{entry.id},{teamCompeting},{entry.Score},{parent}"); } File.WriteAllLines(GlobalConfig.MatchupEntryFile.FullFilePath(), lines); }
/// <summary> /// Convert the data from the file to MatchupEntryModel models /// </summary> /// <param name="lines"></param> /// <returns>List of MatchupEntryModel</returns> public static List <MatchUpEntryModel> ConvertToMatchupEntries(this List <string> lines) { List <MatchUpEntryModel> output = new List <MatchUpEntryModel>(); foreach (string line in lines) { MatchUpEntryModel model = new MatchUpEntryModel(); string[] columns = line.Split(','); model.id = int.Parse(columns[0]); if (columns[1].Length > 0) { model.TeamCompeting = GetTeamById(columns[1]); } else { model.TeamCompeting = null; } model.Score = double.Parse(columns[2]); int parentId = 0; if (int.TryParse(columns[3], out parentId)) { model.ParentMatch = GetMatchupById(parentId.ToString()); } else { model.ParentMatch = null; } output.Add(model); } return(output); }