static int RunAddAndReturnExitCode(AddCardOptions opts) { // Validate the card type CardType type = CardType.Definition; try { type = (CardType)Enum.Parse(typeof(CardType), opts.Type, true); } catch { Console.WriteLine("Card type '{0}' is not valid.", opts.Type); return(1); } ChapterSectionPair pair = Utils.ParseChapterSection(opts.Section); var bookDesc = Utils.Load <BookDesc>(opts.Path.FullName + "\\book.json"); var cardSection = Utils.LoadSection(opts.Path.FullName, pair.chapter, pair.section); Card card = bookDesc.AddCard(type, pair.chapter, pair.section, opts.Text); if (card != null && cardSection != null) { cardSection.Cards.Add(card); string filename = string.Format("Section.{0}.{1}.json", cardSection.ChapterNumber, cardSection.SectionNumber); Utils.Save <CardSection>(opts.Path.FullName + "\\" + filename, cardSection, false); } else if (card == null) { Console.WriteLine("Failed to add card. Chapter {0} or Section {1} is invalid.", pair.chapter, pair.section); return(1); } return(Utils.Save <BookDesc>(opts.Path.FullName + "\\book.json", bookDesc, false)); }
public int Run(string path, IEnumerable <string> sections, bool isSerial, bool isSimulating, string types, int number, bool oddsOnly, bool evensOnly) { // Parse the requested sections foreach (var sectionStr in sections) { ChapterSectionPair pair = Utils.ParseChapterSection(sectionStr); m_chapterSections.Add(pair); } if (m_chapterSections.Count == 0) { Console.WriteLine("ERROR: Failed to parse chapters/sections for study session."); return(1); } // Parse the requested card types types = types.ToLower(); if (types.Contains("all")) { m_cardTypes.Add(CardType.Problem); m_cardTypes.Add(CardType.Definition); m_cardTypes.Add(CardType.Lemma); m_cardTypes.Add(CardType.Theorem); m_cardTypes.Add(CardType.Note); m_cardTypes.Add(CardType.Algorithm); } else { if (types.Contains("def")) { m_cardTypes.Add(CardType.Definition); } if (types.Contains("prob")) { m_cardTypes.Add(CardType.Problem); } if (types.Contains("lem")) { m_cardTypes.Add(CardType.Lemma); } if (types.Contains("note")) { m_cardTypes.Add(CardType.Note); } if (types.Contains("algo")) { m_cardTypes.Add(CardType.Algorithm); } if (types.Contains("theo")) { m_cardTypes.Add(CardType.Theorem); } } if (m_cardTypes.Count == 0) { Console.WriteLine("ERROR: Failed to parse any valid card types."); return(1); } // Determine which section files to open string[] files = Directory.GetFiles(path); HashSet <string> filesToOpen = new HashSet <string>(); foreach (var chapterSection in m_chapterSections) { if (chapterSection.section == -1) { foreach (var file in files) { if (file.Contains(string.Format("Section.{0}", chapterSection.chapter))) { filesToOpen.Add(file); } } } else { foreach (var file in files) { if (file.Contains(string.Format("Section.{0}.{1}", chapterSection.chapter, chapterSection.section))) { filesToOpen.Add(file); } } } } if (filesToOpen.Count == 0) { Console.WriteLine("ERROR: Failed to find any section files to open."); return(1); } // Open section files foreach (var file in filesToOpen) { var cardSection = Utils.Load <CardSection>(file); if (cardSection != null) { m_cardSections.Add(cardSection); } else { Console.WriteLine("WARNING: Failed to load section file {0}", file); } } if (m_cardSections.Count == 0) { Console.WriteLine("ERROR: Failed to load any section files."); return(1); } List <SessionStats> sessionStatsList = new List <SessionStats>(); // Load cards into the base list DateTime now = DateTime.Now; foreach (var cardSection in m_cardSections) { // Track statistics SessionStats sessionStats = new SessionStats(); sessionStatsList.Add(sessionStats); // Determine whether to advance the session counter TimeSpan elapsed = now - cardSection.LastReviewDate; if (elapsed.Days > 0) { cardSection.SessionNumber = (cardSection.SessionNumber + 1) % 10; } foreach (var card in cardSection.Cards) { if (oddsOnly && !evensOnly && IsEven(card.Number)) { continue; } if (evensOnly && !oddsOnly && IsOdd(card.Number)) { continue; } if (Utils.DeckMatchesSession(card.Deck, cardSection.SessionNumber) && HasCardType(card.CardType)) { ReviewCard revCard = new ReviewCard(); revCard.Card = card; revCard.CardSection = cardSection; revCard.Stats = sessionStats.CardStats(card.CardType); m_cardList.Add(revCard); } } } // Run the study session bool bQuit = false; int currentCard = 0; int currentCardAbs = 0; while (!bQuit) { if (number > 0 && currentCardAbs >= number) { bQuit = true; break; } // Randomize if necessary if (currentCard == 0 && !isSerial) { Random rng = new Random(); for (int i = m_cardList.Count - 1; i > -1; i--) { int j = rng.Next(i); var temp = m_cardList[i]; m_cardList[i] = m_cardList[j]; m_cardList[j] = temp; } } // Present card and handle user input if (number > 0) { Console.WriteLine("Item {0}/{1}:", currentCardAbs + 1, number); } else { Console.WriteLine("Item {0}", currentCardAbs + 1); } bQuit = PresentCard(m_cardList[currentCard], isSimulating); // Advance to the next card currentCard = (currentCard + 1) % m_cardList.Count; currentCardAbs += 1; } // Save the results int sectionIndex = 0; Console.WriteLine(); foreach (var cardSection in m_cardSections) { if (!isSimulating) { string filename = string.Format("Section.{0}.{1}.json", cardSection.ChapterNumber, cardSection.SectionNumber); Utils.Save <CardSection>(path + "\\" + filename, cardSection, false); } Console.WriteLine("Stats for {0}", cardSection.GetDesc()); var sessionStats = sessionStatsList[sectionIndex]; if (sessionStats.NumberCardsStudied == 0) { Console.WriteLine(" No cards studied.\n"); } else { sessionStats.PrintStats(); } ++sectionIndex; } return(0); }