Textfile save all tournament but captureRounds information
This commit is contained in:
@ -13,6 +13,9 @@ namespace TrackerLibrary.DataAccess
|
|||||||
private const string PeopleFile = "PersonModels.csv";
|
private const string PeopleFile = "PersonModels.csv";
|
||||||
private const string TeamFile = "TeamModels.csv";
|
private const string TeamFile = "TeamModels.csv";
|
||||||
private const string TournamentFile = "TournamentModels.csv";
|
private const string TournamentFile = "TournamentModels.csv";
|
||||||
|
private const string MatchupFile = "MatchupModels.csv";
|
||||||
|
private const string MatchupEntryFile = "MatchupEntryModels.csv";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public PersonModel CreatePerson(PersonModel model)
|
public PersonModel CreatePerson(PersonModel model)
|
||||||
@ -113,6 +116,9 @@ namespace TrackerLibrary.DataAccess
|
|||||||
currentId = tournaments.OrderByDescending(x => x.Id).First().Id + 1;
|
currentId = tournaments.OrderByDescending(x => x.Id).First().Id + 1;
|
||||||
}
|
}
|
||||||
model.Id = currentId;
|
model.Id = currentId;
|
||||||
|
|
||||||
|
model.SaveRoundsToFile( MatchupFile, MatchupEntryFile);
|
||||||
|
|
||||||
tournaments.Add(model);
|
tournaments.Add(model);
|
||||||
|
|
||||||
tournaments.SaveToTournamentFile(TournamentFile);
|
tournaments.SaveToTournamentFile(TournamentFile);
|
||||||
|
|||||||
@ -168,6 +168,151 @@ namespace TrackerLibrary.DataAccess.TextHelpers
|
|||||||
File.WriteAllLines(fileName.FullFilePath(), lines);
|
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<MatchupModel> 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<MatchupEntryModel> ConvertToMatchupEntryModels(this List<string> lines)
|
||||||
|
{
|
||||||
|
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]);
|
||||||
|
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<MatchupEntryModel> ConvertStringToMatchupEntryModels(string input)
|
||||||
|
{
|
||||||
|
string[] ids = input.Split('|');
|
||||||
|
List<MatchupEntryModel> output = new List<MatchupEntryModel>();
|
||||||
|
List<MatchupEntryModel> 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<TeamModel> teams = GlobalConfig.TeamFile.FullFilePath().LoadFile().ConvertToTeamModels(GlobalConfig.PeopleFile);
|
||||||
|
return teams.Where(x => x.Id == id).First();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MatchupModel LookupMatchupById(int id)
|
||||||
|
{
|
||||||
|
List<MatchupModel> matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile().ConvertToMatchupModels();
|
||||||
|
return matchups.Where(x => x.Id == id).First();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<MatchupModel> ConvertToMatchupModels(this List<string> lines)
|
||||||
|
{
|
||||||
|
|
||||||
|
// id=0,entries=1(pipe delimited by id), winner=2 , matchupRound = 3
|
||||||
|
List<MatchupModel> output = new List<MatchupModel>();
|
||||||
|
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<MatchupModel> 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<string> lines = new List<string>();
|
||||||
|
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<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(); ;
|
||||||
|
}
|
||||||
|
lines.Add($"{e.Id},{e.TeamCompeting.Id},{e.Score},{parent}");
|
||||||
|
}
|
||||||
|
|
||||||
|
File.WriteAllLines(GlobalConfig.MatchupEntryFile.FullFilePath(), lines);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static void SaveToTournamentFile(this List<TournamentModel> models, string fileName)
|
public static void SaveToTournamentFile(this List<TournamentModel> models, string fileName)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -218,6 +363,22 @@ namespace TrackerLibrary.DataAccess.TextHelpers
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string ConvertMatchupEntryListToString(List<MatchupEntryModel> 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<PrizeModel> prizes)
|
private static string ConvertPrizeListToString(List<PrizeModel> prizes)
|
||||||
{
|
{
|
||||||
string output = "";
|
string output = "";
|
||||||
|
|||||||
@ -8,6 +8,14 @@ namespace TrackerLibrary
|
|||||||
{
|
{
|
||||||
public static class GlobalConfig
|
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 IDataConnection Connection { get; private set; }
|
||||||
public static void InitializeConnections(DatabaseType db)
|
public static void InitializeConnections(DatabaseType db)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user