public void StartConstruction() { //Error checking if (MassivePgnFileStream == null) { throw new Exception("A massive pgn file stream was not provided to the constructor."); } if (HasBeenStarted == true) { throw new Exception("Cannot start construction as construction has already been started for this instance."); } //Set up StartedProcessingTime = DateTime.Now; HasBeenStarted = true; ContinueConstructing = true; ResultIsReady = false; MassivePgnFileSplitter mpfs = new MassivePgnFileSplitter(MassivePgnFileStream); List <MoveNode> RootNodes = new List <MoveNode>(); do { try { string ng = mpfs.GetNextGame(); //PgnFileParser pfp = PgnFileParser.CreateFromPgnContent(ng); PgnParserLite ppl = PgnParserLite.ParsePgn(ng); //Get current node MoveNode CurrentNode = null; foreach (MoveNode mn in RootNodes) { if (mn.Move == ppl.Moves[0]) { CurrentNode = mn; CurrentNode.OccuranceCount = CurrentNode.OccuranceCount + 1; } } if (CurrentNode == null) { CurrentNode = MoveNode.Create(); CurrentNode.Move = ppl.Moves[0]; CurrentNode.OccuranceCount = 1; RootNodes.Add(CurrentNode); } int t = 1; for (t = 1; t <= ppl.Moves.Length - 1; t++) { //See if we can find the next one bool FoundIt = false; foreach (MoveNode mn in CurrentNode.ChildNodes) { if (mn.Move == ppl.Moves[t]) { CurrentNode = mn; CurrentNode.OccuranceCount = CurrentNode.OccuranceCount + 1; FoundIt = true; } } //If we can't find it if (FoundIt == false) { MoveNode nmn = MoveNode.Create(); nmn.Move = ppl.Moves[t]; nmn.OccuranceCount = 1; CurrentNode.ChildNodes.Add(nmn); CurrentNode = nmn; } } //Update the progress GamesAdded = GamesAdded + 1; ProcessingTime = DateTime.Now - StartedProcessingTime; } catch { } } while (ContinueConstructing); //The below happen once "Continue Constructing" is false MoveNodeTree MNT = new MoveNodeTree(); MNT.MoveTree = RootNodes; ResultMoveNodeTree = MNT; ResultIsReady = true; }
public void PrintToCsv(Stream MassivePgnFileStream, Stream TargetCsvFileStream, int PrintCount, int PrintNumberOfMoves) { MassivePgnFileSplitter mpfs = new MassivePgnFileSplitter(MassivePgnFileStream); StreamWriter sw = new StreamWriter(TargetCsvFileStream); //Write the header List <string> Header = new List <string>(); Header.Add("Event"); Header.Add("Site"); Header.Add("Date"); Header.Add("Round"); Header.Add("White"); Header.Add("Black"); Header.Add("Result"); Header.Add("UtcDateTime"); Header.Add("WhiteElo"); Header.Add("BlackElo"); Header.Add("WhiteRatingDifference"); Header.Add("BlackRatingDifference"); Header.Add("ECO"); Header.Add("OpeningName"); Header.Add("TimeControl"); Header.Add("TimeIncrement"); Header.Add("Termination"); int t = 0; for (t = 1; t <= PrintNumberOfMoves; t++) { Header.Add("Move" + t.ToString()); } sw.WriteLine(StringListToCsvLine(Header)); //Get the logs int GameIndex = 0; for (GameIndex = 0; GameIndex <= PrintCount; GameIndex++) { string ThisGame = mpfs.GetNextGame(); try { List <string> ThisRow = new List <string>(); PgnParserLite ppl = PgnParserLite.ParsePgn(ThisGame); ThisRow.Add(ppl.Event); ThisRow.Add(ppl.Site); ThisRow.Add(ppl.Date.ToShortDateString()); ThisRow.Add(ppl.Round); ThisRow.Add(ppl.White); ThisRow.Add(ppl.Black); ThisRow.Add(ppl.Result); ThisRow.Add(ppl.UtcDateTime.ToString()); ThisRow.Add(ppl.WhiteElo.ToString()); ThisRow.Add(ppl.BlackElo.ToString()); ThisRow.Add(ppl.WhiteRatingDifference.ToString()); ThisRow.Add(ppl.BlackRatingDifference.ToString()); ThisRow.Add(ppl.ECO); ThisRow.Add(ppl.OpeningName); ThisRow.Add(ppl.TimeControl.TotalSeconds.ToString()); ThisRow.Add(ppl.TimeIncrement.TotalSeconds.ToString()); ThisRow.Add(ppl.Termination); //Write moves t = 0; for (t = 0; t <= PrintNumberOfMoves - 1; t++) { if (ppl.Moves.Length > t) { ThisRow.Add(ppl.Moves[t]); } else { ThisRow.Add(""); } } //Write this line sw.WriteLine(StringListToCsvLine(ThisRow)); } catch { } float percentdone = (float)GameIndex / (float)PrintCount; Console.WriteLine("Writing game " + GameIndex.ToString("#,##0") + " out of " + PrintCount.ToString("#,##0") + " (" + percentdone.ToString("#0.00%") + ")..."); } Console.WriteLine("Game writing complete!"); }