/// <summary> /// Creates a new instance of story from an html file. /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static Story LoadStoryFromFile(string filePath) { HtmlDocument doc = new HtmlDocument(); doc.Load(filePath); // This is the stroy element in the html file. HtmlNode rootNode = new List <HtmlNode>(doc.DocumentNode.Descendants("tw-storydata"))[0]; string storyName = rootNode.GetAttributeValue("name", ""); int startNodePId = rootNode.GetAttributeValue <int>("startnode", 1); // This is the story that is returned. Story story = new BasicStoryFormat(storyName, startNodePId); // Finds all passage data elements in the story. List <HtmlNode> nodes = new List <HtmlNode>(rootNode.Descendants("tw-passagedata")); // Adds each passage to the story. foreach (HtmlNode n in nodes) { int pId = n.GetAttributeValue <int>("pid", 1); string name = n.GetAttributeValue("name", pId.ToString()); string text = n.InnerText; Passage p = new Passage(story, name, pId, text); story.AddPassage(p); } return(story); }
protected override void DisplayPassage(Passage passage) { // This is cursed base.DisplayPassage(passage); Regex regex = new Regex(@"\[\[(.*?)]]"); MatchCollection matches = regex.Matches(passage.RawText); string s = regex.Replace(passage.RawText, linkReplacement); List <string> passages = new List <string>(); int i = 0; foreach (Match m in matches) { i++; string value = m.Groups[1].Value; string[] seperateValues = value.Split('|'); string target = seperateValues[seperateValues.Length - 1]; int index = s.IndexOf(linkReplacement); s = s.Remove(index, linkReplacement.Length); s = s.Insert(index, i.ToString() + ": " + seperateValues[0]); passages.Add(target); } Console.WriteLine("\n" + s); string input = Console.ReadLine(); int numb; if (int.TryParse(input, out numb) && passages.Count >= numb && numb > 0) { GoToPassage(passages[numb - 1]); } }
/// <summary> /// Adds a passage to the collection of all passages. /// </summary> /// <param name="passage"></param> /// <returns></returns> public Passage AddPassage(Passage passage) { passages.Add(passage.Name, passage); return(passage); }
/// <summary> /// Sets the current passage to the given passage. /// </summary> /// <param name="passage"></param> /// <returns></returns> protected Passage GoToPassage(Passage passage) { CurrentPassage = passage; DisplayPassage(); return(passage); }
/// <summary> /// Displays the given passage. The implementation of this is left mostly up to children of the story class. /// </summary> /// <param name="passage"></param> protected virtual void DisplayPassage(Passage passage) { }