public void ProcessFlagSetting() { List <FlagValue> flags = new List <FlagValue> { new FlagValue("a", true), new FlagValue("b", 5), new FlagValue("c", 10), new FlagValue("d", false), new FlagValue("e", true), new FlagValue("f", 1), new FlagValue("g", true), }; // Test new flags Stitch stitchA = new Stitch { Flags = new List <string> { "a = false", // set a boolean "b = 2", // set a number "c + 1", // add to a number "d = 7", // convert a boolean to a number "e + 1", // add a number to a boolean, which is weird, but valid "f + true", // add a boolean to a number, invalid "g + true" // add a boolean to a boolean, invalid } }; StoryModel.ProcessFlagSetting(stitchA, flags); Assert.AreEqual(0, flags [0].value); Assert.AreEqual(2, flags [1].value); Assert.AreEqual(11, flags [2].value); Assert.AreEqual(7, flags [3].value); Assert.AreEqual(2, flags [4].value); Assert.AreEqual(1, flags [5].value); Assert.AreEqual(1, flags [6].value); // Test updating flags Stitch stitchB = new Stitch { Flags = new List <string> { "b = 3", "a = true", "h = 5" // new flag } }; StoryModel.ProcessFlagSetting(stitchB, flags); Assert.AreEqual(1, flags [0].value); // a = true Assert.AreEqual(3, flags [1].value); // b = 3 Assert.AreEqual(5, flags [7].value); // h = 5 }
/// <summary> /// Generates and returns a PlayChunk that begins with the given Stitch /// and continues through the first available block of options. /// </summary> public PlayChunk CreateChunkForStitch(Stitch stitch) { if (stitch == null) { return(null); } PlayChunk chunk = new PlayChunk(); // Reload all flags from the previous stitch AllFlagsCollected.Clear(); if (LastChunk != null) { AllFlagsCollected.AddRange(LastChunk.FlagsCollected); } var currentStitch = stitch; var compiledText = ""; // Loop through all linked stitches while (currentStitch != null) { visitedStitches.Add(currentStitch); if (currentStitch.PageNumber >= 1) { chunk.HasSectionHeading = true; } bool isStitchVisible = StoryModel.DoesArrayMeetConditions(currentStitch.IfConditions, currentStitch.NotIfConditions, AllFlagsCollected); // This stitch passes flag tests and should be included in this chunk if (isStitchVisible) { // Remove newlines in preparation for compiling run-on stitches into one paragraph compiledText += currentStitch.Text.Replace("\n", " ") + " "; // If no more processing is needed, apply text substitutions and store the paragraph bool isRunOn = Regex.IsMatch(currentStitch.Text, @"\[\.\.\.\]") || currentStitch.RunOn; if (!isRunOn || currentStitch.DivertStitch == null) { var styledText = ApplyRuleSubstitutions(compiledText, AllFlagsCollected); chunk.Paragraphs.Add(new Paragraph(styledText, currentStitch.Image, currentStitch.PageLabel)); compiledText = ""; } // Modify all flags with flag states from the current stitch if (currentStitch.Flags.Count > 0) { StoryModel.ProcessFlagSetting(currentStitch, AllFlagsCollected); } } // Add stitch to chunk chunk.Stitches.Add(new BlockContent <Stitch> (currentStitch, isStitchVisible)); currentStitch = currentStitch.DivertStitch; } WordCount = 0; foreach (var p in chunk.Paragraphs) { WordCount += WordCountOf(p.Text); } // Add options to chunk if (LastStitch.Options.Count > 0) { foreach (var option in LastStitch.Options) { var isVisible = StoryModel.DoesArrayMeetConditions(option.IfConditions, option.NotIfConditions, AllFlagsCollected); if (isVisible) { chunk.Options.Add(new BlockContent <Option> (option, isVisible)); } } } chunk.FlagsCollected.AddRange(AllFlagsCollected); allChunks.Add(chunk); return(chunk); }