Textfile save all tournament but captureRounds information

This commit is contained in:
2020-04-10 00:15:27 +02:00
parent a3096138f9
commit dee234408d
3 changed files with 177 additions and 2 deletions

View File

@ -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<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)
{
@ -218,6 +363,22 @@ namespace TrackerLibrary.DataAccess.TextHelpers
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)
{
string output = "";