diff --git a/TrackerLibrary/DataAccess/TextConnector.cs b/TrackerLibrary/DataAccess/TextConnector.cs index f0026a6..a2ecd26 100644 --- a/TrackerLibrary/DataAccess/TextConnector.cs +++ b/TrackerLibrary/DataAccess/TextConnector.cs @@ -13,6 +13,9 @@ namespace TrackerLibrary.DataAccess private const string PeopleFile = "PersonModels.csv"; private const string TeamFile = "TeamModels.csv"; private const string TournamentFile = "TournamentModels.csv"; + private const string MatchupFile = "MatchupModels.csv"; + private const string MatchupEntryFile = "MatchupEntryModels.csv"; + public PersonModel CreatePerson(PersonModel model) @@ -113,6 +116,9 @@ namespace TrackerLibrary.DataAccess currentId = tournaments.OrderByDescending(x => x.Id).First().Id + 1; } model.Id = currentId; + + model.SaveRoundsToFile( MatchupFile, MatchupEntryFile); + tournaments.Add(model); tournaments.SaveToTournamentFile(TournamentFile); diff --git a/TrackerLibrary/DataAccess/TextConnectorProcessor.cs b/TrackerLibrary/DataAccess/TextConnectorProcessor.cs index 8331fa6..e9d75b3 100644 --- a/TrackerLibrary/DataAccess/TextConnectorProcessor.cs +++ b/TrackerLibrary/DataAccess/TextConnectorProcessor.cs @@ -168,6 +168,151 @@ namespace TrackerLibrary.DataAccess.TextHelpers File.WriteAllLines(fileName.FullFilePath(), lines); } + public static void SaveRoundsToFile(this TournamentModel model,string matchupFile,string matchupEntryFile) + { + // Loop through each round + // Loop through each matchup + // Get the Id for the new matchup and save the record + // Loop through each Entry , get the id, and save it + foreach (List round in model.Rounds) + { + foreach (MatchupModel matchup in round) + { + // load all of the matchups + // Get the top id and add one + // Store the id + // Svae the matchup record + matchup.SaveMatchupToFile(matchupFile, matchupEntryFile); + + + } + } + } + + public static List ConvertToMatchupEntryModels(this List lines) + { + List output = new List(); + foreach (string line in lines) + { + string[] cols = line.Split(','); + MatchupEntryModel me = new MatchupEntryModel(); + me.Id = int.Parse(cols[0]); + 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; + } + + private static List ConvertStringToMatchupEntryModels(string input) + { + string[] ids = input.Split('|'); + List output = new List(); + List entries = GlobalConfig.MatchupEntryFile.FullFilePath().LoadFile().ConvertToMatchupEntryModels(); + + foreach (string id in ids) + { + output.Add(entries.Where(x => x.Id == int.Parse(id)).First()); + } + return output; + } + + private static TeamModel LookupTeamById(int id) + { + List teams = GlobalConfig.TeamFile.FullFilePath().LoadFile().ConvertToTeamModels(GlobalConfig.PeopleFile); + return teams.Where(x => x.Id == id).First(); + } + + private static MatchupModel LookupMatchupById(int id) + { + List matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile().ConvertToMatchupModels(); + return matchups.Where(x => x.Id == id).First(); + } + + public static List ConvertToMatchupModels(this List lines) + { + + // id=0,entries=1(pipe delimited by id), winner=2 , matchupRound = 3 + List output = new List(); + foreach (string line in lines) + { + string[] cols = line.Split(','); + MatchupModel p = new MatchupModel(); + p.Id = int.Parse(cols[0]); + p.Entries = ConvertStringToMatchupEntryModels(cols[1]); + p.Winner = LookupTeamById(int.Parse(cols[2])); + p.MatchupRound = int.Parse(cols[3]); + output.Add(p); + } + + return output; + } + + public static void SaveMatchupToFile(this MatchupModel matchup, string matchupFile, string matchupEntryFile) + { + List matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile().ConvertToMatchupModels(); + int currentId = 1; + if(matchups.Count > 0) + { + currentId = matchups.OrderByDescending(x => x.Id).First().Id + 1; + } + matchup.Id = currentId; + foreach (MatchupEntryModel entry in matchup.Entries) + { + entry.SaveEntryToFile(matchupEntryFile); + } + + List lines = new List(); + foreach (MatchupModel m in matchups) + { + string winner = ""; + if (m.Winner != null) + { + winner = m.Winner.Id.ToString(); ; + } + lines.Add($"{m.Id},{ConvertMatchupEntryListToString(m.Entries)},{winner},{m.MatchupRound}"); + } + + File.WriteAllLines(GlobalConfig.MatchupFile.FullFilePath(), lines); + } + + public static void SaveEntryToFile(this MatchupEntryModel entry, string matchupEntryFile) + { + List 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 lines = new List(); + foreach (MatchupEntryModel e in entries) + { + string parent = ""; + if(e.ParentMatchup != null) + { + parent = e.ParentMatchup.Id.ToString(); ; + } + lines.Add($"{e.Id},{e.TeamCompeting.Id},{e.Score},{parent}"); + } + + File.WriteAllLines(GlobalConfig.MatchupEntryFile.FullFilePath(), lines); + + } + public static void SaveToTournamentFile(this List models, string fileName) { @@ -218,6 +363,22 @@ namespace TrackerLibrary.DataAccess.TextHelpers return output; } + private static string ConvertMatchupEntryListToString(List entries) + { + string output = ""; + if (entries.Count == 0) + { + return ""; + } + foreach (MatchupEntryModel me in entries) + { + output += $"{me.Id}|"; + } + + output = output.Substring(0, output.Length - 1); + return output; + } + private static string ConvertPrizeListToString(List prizes) { string output = ""; diff --git a/TrackerLibrary/GlobalConfig.cs b/TrackerLibrary/GlobalConfig.cs index 9abd4fe..91f7fda 100644 --- a/TrackerLibrary/GlobalConfig.cs +++ b/TrackerLibrary/GlobalConfig.cs @@ -8,11 +8,19 @@ namespace TrackerLibrary { public static class GlobalConfig { + + public const string PrizesFile = "PrizeModels.csv"; + public const string PeopleFile = "PersonModels.csv"; + public const string TeamFile = "TeamModels.csv"; + public const string TournamentFile = "TournamentModels.csv"; + public const string MatchupFile = "MatchupModels.csv"; + public const string MatchupEntryFile = "MatchupEntryModels.csv"; + public static IDataConnection Connection { get; private set; } public static void InitializeConnections(DatabaseType db) { - switch (db) + switch (db) { case DatabaseType.Sql: // TODO - Set up the sql connector properly ! @@ -28,7 +36,7 @@ namespace TrackerLibrary break; } - + } public static string CnnString(string name)