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.
|
# Dialogue is the module for traversing an existing tree.
|
||||||
module Dialogue
|
module Dialogue
|
||||||
|
|
||||||
# Format and display the trunk
|
# Format and display the trunk
|
||||||
#
|
#
|
||||||
# @param trunk [Hash] The trunk hash
|
# @param trunk [Hash] The trunk hash
|
||||||
# @param debug [Boolean] The status of showing debug information
|
# @param debug [Boolean] The status of showing debug information
|
||||||
def self.display_trunk(trunk, debug=false)
|
def self.display_trunk(trunk, debug = false)
|
||||||
40.times { print "-" }
|
40.times { print '-' }
|
||||||
puts "\n[ Trunk ]\n" if debug
|
puts "\n[ Trunk ]\n" if debug
|
||||||
puts "\n#{trunk["trunk"]}"
|
puts "\n#{trunk['trunk']}"
|
||||||
40.times { print "-" }
|
40.times { print '-' }
|
||||||
puts "\n"
|
puts "\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -20,11 +19,11 @@ module Dialogue
|
||||||
# @param branch [Hash] A branch data set
|
# @param branch [Hash] A branch data set
|
||||||
# @param branch_no [Integer] The branch number
|
# @param branch_no [Integer] The branch number
|
||||||
# @param debug [Boolean] Status of showing debug information
|
# @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: #{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#{k}: #{v.keys[0]}"
|
||||||
puts "\t\t[ Goes to branch #{v.values[0]} ]\n" if debug
|
puts "\t\t[ Goes to branch #{v.values[0]} ]\n" if debug
|
||||||
end
|
end
|
||||||
|
@ -32,30 +31,27 @@ module Dialogue
|
||||||
|
|
||||||
# Speaker holds the functionality for going through a dialogue tree.
|
# Speaker holds the functionality for going through a dialogue tree.
|
||||||
class Speaker
|
class Speaker
|
||||||
# The file, which should be a dialogue tree YAML file.
|
# The tree, an instance of Gardner::Plot
|
||||||
attr_accessor :file
|
attr_reader :tree
|
||||||
# Status of verbose/debug mode. True = on; false = off.
|
# Status of verbose/debug mode. True = on; false = off.
|
||||||
attr_accessor :debug
|
attr_reader :debug
|
||||||
|
|
||||||
def initialize(file="", debug=false)
|
def initialize(tree, debug = false)
|
||||||
@file = file
|
@tree = tree
|
||||||
@debug = debug
|
@debug = debug
|
||||||
end
|
end
|
||||||
|
|
||||||
# Conversation handles navigating the tree, until the option to end is
|
# Conversation handles navigating the tree, until the option to end is
|
||||||
# reached.
|
# reached.
|
||||||
def conversation()
|
def conversation
|
||||||
tree = Gardner.prune_trunk(@file)
|
Dialogue.display_trunk(@tree.trunk, @debug)
|
||||||
|
|
||||||
Dialogue.display_trunk(tree[0], false)
|
|
||||||
branches = Gardner.prune_branches(tree[1])
|
|
||||||
|
|
||||||
next_branch = 1
|
next_branch = 1
|
||||||
until next_branch == 0 do
|
until next_branch.zero?
|
||||||
next_branch = talk(branches[next_branch], next_branch)
|
next_branch = talk(@tree.branches[next_branch], next_branch)
|
||||||
end
|
end
|
||||||
|
|
||||||
puts "\n#{branches[0]["desc"]}"
|
puts "\n#{@tree.branches[0]['desc']}"
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -65,25 +61,18 @@ module Dialogue
|
||||||
# @param branch_no [Integer] The branch number
|
# @param branch_no [Integer] The branch number
|
||||||
# @return [Integer] The number of the next branch
|
# @return [Integer] The number of the next branch
|
||||||
def talk(branch, branch_no)
|
def talk(branch, branch_no)
|
||||||
# If there are no options on this branch, we assume it's a terminal
|
return 0 if terminal?(branch)
|
||||||
# branch. Return 0, and end the program.
|
|
||||||
if branch["options"].empty?
|
|
||||||
puts "\n#{branch["desc"]}\n\n"
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
|
|
||||||
Dialogue.display_branch(branch, branch_no, @debug)
|
Dialogue.display_branch(branch, branch_no, @debug)
|
||||||
|
|
||||||
response = get_response(branch)
|
response = get_response(branch)
|
||||||
|
|
||||||
unless response == 0
|
unless response.zero?
|
||||||
puts "\n"
|
puts "(Your choice: #{branch['options'][response].keys[0]})"
|
||||||
10.times { print "*" }
|
response = branch['options'][response].values[0].to_i
|
||||||
puts "\n(Your choice: #{branch["options"][response].keys[0]})"
|
|
||||||
response = branch["options"][response].values[0].to_i
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return response
|
response
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get a response for the displayed branch
|
# Get a response for the displayed branch
|
||||||
|
@ -91,20 +80,33 @@ module Dialogue
|
||||||
# @param branch [Hash] A branch data set
|
# @param branch [Hash] A branch data set
|
||||||
# @return [Integer] the next branch
|
# @return [Integer] the next branch
|
||||||
def get_response(branch)
|
def get_response(branch)
|
||||||
valid_options = branch["options"].keys.join(", ")
|
valid_options = branch['options'].keys.join(', ')
|
||||||
|
|
||||||
print "\n[#{valid_options}]> "
|
print "\n[#{valid_options}]> "
|
||||||
STDOUT.flush
|
STDOUT.flush
|
||||||
response = STDIN.gets.chomp.to_i
|
response = STDIN.gets.chomp.to_i
|
||||||
|
|
||||||
until branch["options"].keys.include?(response) or response == 0
|
until branch['options'].keys.include?(response) || response.zero?
|
||||||
print "[## Invalid options. "
|
print '[## Invalid options. '
|
||||||
print "Valid options are #{valid_options}, or 0 to exit."
|
print "Valid options are #{valid_options}, or 0 to exit."
|
||||||
print "\n[#{valid_options}]> "
|
print "\n[#{valid_options}]> "
|
||||||
response = STDIN.gets.chomp.to_i
|
response = STDIN.gets.chomp.to_i
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue