From e27cd43e7c3221eb0744b1ec584aeabca33be143 Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sun, 7 May 2017 23:46:19 -0400 Subject: [PATCH] dialogue.rb: Refactor - Abstract functionality from talk into individual functions: - display_trunk: Displays the trunk of the branch. - display_branch: Displays the branch and option list - get_response: Displays the conversation prompt, and gets the response - Of note, this allows the editor to make use of talk functionality --- lib/sapling/dialogue.rb | 72 ++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/lib/sapling/dialogue.rb b/lib/sapling/dialogue.rb index bbae8b9..35acec9 100644 --- a/lib/sapling/dialogue.rb +++ b/lib/sapling/dialogue.rb @@ -2,13 +2,11 @@ require_relative './gardner' # Dialogue is the module for traversing an existing tree. module Dialogue - - # Spealer holds the functionality for viewing and going through a dialogue + # Speaker holds the functionality for viewing and going through a dialogue # tree. class Speaker # The file, which should be a dialogue tree YAML file. attr_accessor :file - # Status of verbose/debug mode. True = on; false = off. attr_accessor :debug @@ -20,26 +18,26 @@ module Dialogue # Conversation handles navigating the tree, until the option to end is # reached. def conversation() - tree = Gardner.grow(@file) + tree = Gardner.prune_trunk(@file) - 10.times { print "*" } - puts "\n[ Branch: 1 ]" if @debug - next_branch = talk(tree[1]) + display_trunk(tree[0]) + branches = Gardner.prune_branches(tree[1]) + + next_branch = 1 until next_branch == 0 do - puts "\n[ Branch: #{next_branch} ]" if @debug - next_branch = talk(tree[next_branch]) + next_branch = talk(branches[next_branch], next_branch) end - puts "\n#{tree[0]["desc"]}" + puts "\n#{branches[0]["desc"]}" exit end - # Talk displays a branch, the options, and prompts for a response + # Talk displays a branch, the options, and prompts for a response. # # @param branch [Hash] A branch data set + # @param branch_no [Integer] The branch number # @return [Integer] The number of the next branch - def talk(branch) - + def talk(branch, branch_no) # 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? @@ -47,13 +45,50 @@ module Dialogue return 0 end - valid_options = branch["options"].keys.join(", ") + display_branch(branch, branch_no) + response = get_response(branch) + + unless response == 0 + puts "\n" + 10.times { print "*" } + puts "\n(Your choice: #{branch["options"][response].keys[0]})" + response = branch["options"][response].values[0].to_i + end + + return response + end + + # Format and display the trunk + # + # @param trunk [Hash] The trunk hash + def display_trunk(trunk) + 40.times { print "-" } + puts "\n#{trunk["trunk"]}" + 40.times { print "-" } + puts "\n" + end + + # Format and display a branch + # + # @param branch [Hash] A branch data set + # @param branch_no [Integer] The branch number + def display_branch(branch, branch_no) + puts "\n[ Branch: #{branch_no} ]" if @debug puts "\n#{branch["desc"]}\n\n" + branch["options"].each_pair do |k,v| puts "\t#{k}: #{v.keys[0]}" puts "\t\t [ Goes to branch #{v.values[0]} ]" if @debug end + end + + # Get a response for the displayed branch + # + # @param branch [Hash] A branch data set + # @return [Integer] the next branch + def get_response(branch) + valid_options = branch["options"].keys.join(", ") print "\n[#{valid_options}]> " STDOUT.flush @@ -66,14 +101,7 @@ module Dialogue response = STDIN.gets.chomp.to_i end - - puts "\n" - 10.times { print "*" } - return 0 if response == 0 - puts "\n(Your choice: #{branch["options"][response].keys[0]})" - return branch["options"][response].values[0].to_i + return response end - end - end