Module: Dialogue + + + +
+-
+
- Defined in: +
- lib/sapling.rb +
Overview
Dialogue is the module for traversing an existing tree.
+ + +Defined Under Namespace
++ + + + + Classes: Speaker + + +
+ + + + + + + + + +diff --git a/.gitignore b/.gitignore index fadeed9..3a20835 100644 --- a/.gitignore +++ b/.gitignore @@ -58,5 +58,4 @@ Thumbs.db # doc/ is the generated documentation for Yard, which can easily be generated # locally with yardoc # .yardoc/ is yard metadata, not necessary to be uploaded to GH -doc/ .yardoc/ diff --git a/.yardopts b/.yardopts index 6ccae4b..bd3f342 100644 --- a/.yardopts +++ b/.yardopts @@ -1,9 +1,9 @@ --title "Sapling Documentation" - README.md -docs/config_file.md -docs/config_file_example.md -docs/editor.md +var/docs/config_file.md +var/docs/config_file_example.md +var/docs/editor.md CONTRIBUTING.md LICENSE lib/**.*.rb diff --git a/doc/Dialogue.html b/doc/Dialogue.html new file mode 100644 index 0000000..66a5468 --- /dev/null +++ b/doc/Dialogue.html @@ -0,0 +1,126 @@ + + +
+ + +Dialogue is the module for traversing an existing tree.
+ + ++ + + + + Classes: Speaker + + +
+ + + + + + + + + +Spealer holds the functionality for viewing and going through a dialogue +tree.
+ + +The file, which should be a dialogue tree YAML file.
+Conversation handles navigating the tree, until the option to end is +reached.
+A new instance of Speaker.
+Talk displays a branch, the options, and prompts for a response.
+Returns a new instance of Speaker
+ + +
+ + + +106 +107 +108+ |
+
+ # File 'lib/sapling.rb', line 106 + +def initialize + @file = "" +end+ |
+
The file, which should be a dialogue tree YAML file.
+ + +
+ + + +104 +105 +106+ |
+
+ # File 'lib/sapling.rb', line 104 + +def file + @file +end+ |
+
Conversation handles navigating the tree, until the option to end is +reached.
+ + +
+ + + +112 +113 +114 +115 +116 +117 +118 +119 +120 +121 +122 +123+ |
+
+ # File 'lib/sapling.rb', line 112 + +def conversation() + tree = Gardner.grow(@file) + + 10.times { print "*" } + next_branch = talk(tree[1]) + until next_branch == 0 do + next_branch = talk(tree[next_branch]) + end + + puts tree[0]["desc"] + exit +end+ |
+
Talk displays a branch, the options, and prompts for a response
+ + +
+ + + +129 +130 +131 +132 +133 +134 +135 +136 +137 +138 +139 +140 +141 +142 +143 +144 +145 +146 +147 +148 +149 +150 +151 +152 +153 +154 +155 +156 +157 +158 +159+ |
+
+ # File 'lib/sapling.rb', line 129 + +def talk(branch) + # If there are no options on this branch, we assume it's a terminal + # branch. Return 0, and end the program. + if branch["options"].empty? + puts "\n#{branch["desc"]}\n\n" + return 0 + end + + = branch["options"].keys.join(", ") + + puts "\n#{branch["desc"]}\n\n" + branch["options"].each_pair do |k,v| + puts "\t#{k}: #{v.keys[0]}" + end + + print "\n[#{}]> " + STDOUT.flush + response = STDIN.gets.chomp.to_i + + until branch["options"].keys.include?(response) + print "[## Invalid options. " + print "Valid options are #{}, or 0 to exit." + print "\n[#{}]> " + response = STDIN.gets.chomp.to_i + end + + puts "\n" + 10.times { print "*" } + puts "\n(Your choice: #{branch["options"][response].keys[0]})" + return branch["options"][response].values[0].to_i +end+ |
+
Gardner is the module for working with a dialogue tree file
+ + +The main method for Sapling.
+Parse the branch.
+Parse the options.
+Parse the trunk The trunk is like the introduction to the tree.
+Verify that a file is a dialogue tree file.
+The main method for Sapling. From here, the tree is grown.
+ + +
+ + + +65 +66 +67 +68 +69 +70 +71+ |
+
+ # File 'lib/sapling.rb', line 65 + +def self.grow(file) + tree = YAML.load_file(file[0]) + tree = Gardner.prune_trunk(tree) + branches = Gardner.prune_branches(tree) + + return branches +end+ |
+
Parse the branch
+ + +
+ + + +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23+ |
+
+ # File 'lib/sapling.rb', line 13 + +def self.prune_branches(tree) + branches = { 0 => { "desc" => "Thanks for using Sapling!" } } + tree.each do |b| + branches[b["branch"]["number"]] = { + "desc" => b["branch"]["text"], + "options" => prune_leaves(b["branch"]["leaf"]) } + end + + return branches + +end+ |
+
Parse the options
+ + +
+ + + +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42+ |
+
+ # File 'lib/sapling.rb', line 29 + +def self.prune_leaves(leaves) + x = 1 + = {} + + return if leaves.nil? + + leaves.each do |l| + [x] = { l["text"] => l["branch"] } + x += 1 + end + + return + +end+ |
+
Parse the trunk The trunk is like the introduction to the tree.
+ + +
+ + + +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59+ |
+
+ # File 'lib/sapling.rb', line 49 + +def self.prune_trunk(tree) + trunk = tree.shift + puts "Welcome to Sapling, a Dialogue Tree Utility.\n" + 40.times { print "-" } + puts "\n#{trunk["trunk"]}" + 40.times { print "-" } + puts "\n" + + return tree + +end+ |
+
Verify that a file is a dialogue tree file.
+ + +
+ + + +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 +91 +92 +93+ |
+
+ # File 'lib/sapling.rb', line 77 + +def self.verify_tree(file) + results = [] + begin + tree = YAML.load_file(file) + results << tree[0].keys.include?("trunk") + results << tree[1]["branch"].keys.include?("number") + results << tree[1]["branch"].keys.include?("text") + results << tree[1]["branch"].keys.include?("leaf") + rescue + puts "Sorry chummer, I don't think this is a tree." + puts "Verify your YAML file is formatted properly." + results << false + end + + results.include?(false) ? false : true + +end+ |
+
Planter is the module for creating or editing a tree.
+ + +Sapling is the main module for the program. From here, the rest of the +world starts building.
+ + ++ + + + + Classes: CLI + + +
+ + + + + + + + + +CLI is the class for option parsing, and the gateway to the program
+ + +Option parsing, and gateway to either reading and traversing a tree, or +editing/creating a tree.
+Option parsing, and gateway to either reading and traversing a tree, or +editing/creating a tree.
+ + +
+ + + +179 +180 +181 +182 +183 +184 +185 +186 +187 +188 +189 +190 +191 +192 +193 +194 +195 +196 +197 +198 +199 +200 +201 +202 +203 +204 +205 +206 +207 +208 +209 +210 +211 +212 +213 +214 +215 +216 +217 +218 +219+ |
+
+ # File 'lib/sapling.rb', line 179 + +def talk() + opt_parser = OptionParser.new do |opt| + opt. = "Usage: sapling -t FILE\n" \ + "Usage: sapling -e [FILE]" + + opt.on_tail("-h", "--help", "Show this menu") do + puts opt + exit + end + + opt.on("-t", "--talk", + "Begin traversing the provided dialogue tree") do + + if ARGV.empty? + puts opt_parser + exit + end + + unless Gardner.verify_tree(ARGV[0]) + puts "\n#{opt}\n" + exit + end + + speaker = Dialogue::Speaker.new + speaker.file = ARGV + speaker.conversation + end + + opt.on("-e", "--edit", + "Create or edit a dialogue tree") do + puts "We gonna make a tree!" + end + + end + opt_parser.parse!() + + if ARGV.empty? + puts opt_parser + exit + end +end+ |
+
+
+
+
|
+
t |