dialogue.rb: Rubocop fixes and refactor changes
- Fix several Rubocop problems - Update module for gardner refactor - Update all tree references to now reference Gardner::Plot object
This commit is contained in:
parent
6e16b250a5
commit
e85a882851
1 changed files with 39 additions and 37 deletions
|
@ -2,16 +2,15 @@ require_relative './gardner'
|
|||
|
||||
# Dialogue is the module for traversing an existing tree.
|
||||
module Dialogue
|
||||
|
||||
# Format and display the trunk
|
||||
#
|
||||
# @param trunk [Hash] The trunk hash
|
||||
# @param debug [Boolean] The status of showing debug information
|
||||
def self.display_trunk(trunk, debug=false)
|
||||
40.times { print "-" }
|
||||
def self.display_trunk(trunk, debug = false)
|
||||
40.times { print '-' }
|
||||
puts "\n[ Trunk ]\n" if debug
|
||||
puts "\n#{trunk["trunk"]}"
|
||||
40.times { print "-" }
|
||||
puts "\n#{trunk['trunk']}"
|
||||
40.times { print '-' }
|
||||
puts "\n"
|
||||
end
|
||||
|
||||
|
@ -20,11 +19,11 @@ module Dialogue
|
|||
# @param branch [Hash] A branch data set
|
||||
# @param branch_no [Integer] The branch number
|
||||
# @param debug [Boolean] Status of showing debug information
|
||||
def self.display_branch(branch, branch_no, debug=false)
|
||||
def self.display_branch(branch, branch_no, debug = false)
|
||||
puts "\n[ Branch: #{branch_no} ]" if debug
|
||||
puts "\n#{branch["desc"]}\n\n"
|
||||
puts "\n#{branch['desc']}\n\n"
|
||||
|
||||
branch["options"].each_pair do |k,v|
|
||||
branch['options'].each_pair do |k, v|
|
||||
puts "\t#{k}: #{v.keys[0]}"
|
||||
puts "\t\t[ Goes to branch #{v.values[0]} ]\n" if debug
|
||||
end
|
||||
|
@ -32,30 +31,27 @@ module Dialogue
|
|||
|
||||
# Speaker holds the functionality for going through a dialogue tree.
|
||||
class Speaker
|
||||
# The file, which should be a dialogue tree YAML file.
|
||||
attr_accessor :file
|
||||
# The tree, an instance of Gardner::Plot
|
||||
attr_reader :tree
|
||||
# Status of verbose/debug mode. True = on; false = off.
|
||||
attr_accessor :debug
|
||||
attr_reader :debug
|
||||
|
||||
def initialize(file="", debug=false)
|
||||
@file = file
|
||||
def initialize(tree, debug = false)
|
||||
@tree = tree
|
||||
@debug = debug
|
||||
end
|
||||
|
||||
# Conversation handles navigating the tree, until the option to end is
|
||||
# reached.
|
||||
def conversation()
|
||||
tree = Gardner.prune_trunk(@file)
|
||||
|
||||
Dialogue.display_trunk(tree[0], false)
|
||||
branches = Gardner.prune_branches(tree[1])
|
||||
def conversation
|
||||
Dialogue.display_trunk(@tree.trunk, @debug)
|
||||
|
||||
next_branch = 1
|
||||
until next_branch == 0 do
|
||||
next_branch = talk(branches[next_branch], next_branch)
|
||||
until next_branch.zero?
|
||||
next_branch = talk(@tree.branches[next_branch], next_branch)
|
||||
end
|
||||
|
||||
puts "\n#{branches[0]["desc"]}"
|
||||
puts "\n#{@tree.branches[0]['desc']}"
|
||||
exit
|
||||
end
|
||||
|
||||
|
@ -65,25 +61,18 @@ module Dialogue
|
|||
# @param branch_no [Integer] The branch number
|
||||
# @return [Integer] The number of the next 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?
|
||||
puts "\n#{branch["desc"]}\n\n"
|
||||
return 0
|
||||
end
|
||||
return 0 if terminal?(branch)
|
||||
|
||||
Dialogue.display_branch(branch, branch_no, @debug)
|
||||
|
||||
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
|
||||
unless response.zero?
|
||||
puts "(Your choice: #{branch['options'][response].keys[0]})"
|
||||
response = branch['options'][response].values[0].to_i
|
||||
end
|
||||
|
||||
return response
|
||||
response
|
||||
end
|
||||
|
||||
# Get a response for the displayed branch
|
||||
|
@ -91,20 +80,33 @@ module Dialogue
|
|||
# @param branch [Hash] A branch data set
|
||||
# @return [Integer] the next branch
|
||||
def get_response(branch)
|
||||
valid_options = branch["options"].keys.join(", ")
|
||||
valid_options = branch['options'].keys.join(', ')
|
||||
|
||||
print "\n[#{valid_options}]> "
|
||||
STDOUT.flush
|
||||
response = STDIN.gets.chomp.to_i
|
||||
|
||||
until branch["options"].keys.include?(response) or response == 0
|
||||
print "[## Invalid options. "
|
||||
until branch['options'].keys.include?(response) || response.zero?
|
||||
print '[## Invalid options. '
|
||||
print "Valid options are #{valid_options}, or 0 to exit."
|
||||
print "\n[#{valid_options}]> "
|
||||
response = STDIN.gets.chomp.to_i
|
||||
end
|
||||
|
||||
return response
|
||||
response
|
||||
end
|
||||
|
||||
# Check if a branch is terminal
|
||||
#
|
||||
# @param branch [Hash] A branch data set
|
||||
# @return [Boolean] true if the branch is terminal, false otherwise
|
||||
def terminal?(branch)
|
||||
if branch['options'].empty?
|
||||
puts "\n#{branch['desc']}\n\n"
|
||||
return true
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue