Permalink
Browse files

repl loop now contained in clocker object so that you can get the gam…

…e's runtime on exit; unused (for now) timer class added
  • Loading branch information...
michaelchadwick committed Jul 6, 2015
1 parent f9813c9 commit 4dfaf443e4c3d4b1f309de4428c0ef35769b2e50
Showing with 81 additions and 14 deletions.
  1. +1 −0 gemwarrior.gemspec
  2. +1 −1 lib/gemwarrior/battle.rb
  3. +2 −2 lib/gemwarrior/evaluator.rb
  4. +50 −0 lib/gemwarrior/misc/timer.rb
  5. +27 −11 lib/gemwarrior/repl.rb
View
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'json', '~> 1.8.2'
spec.add_runtime_dependency 'colorize', '~> 0.7.7'
spec.add_runtime_dependency 'feep', '~> 0.1.3'
spec.add_runtime_dependency 'clocker', '~> 0.1.1'
#spec.add_runtime_dependency 'hr', '~> 0.0.2'
#spec.add_runtime_dependency 'wordnik', '~> 4.12'
View
@@ -307,7 +307,7 @@ def player_death
puts "You are dead, slain by the #{monster.name}!".colorize(:red)
puts 'Your adventure ends here. Try again next time!'.colorize(:red)
print_battle_line
exit(0)
return 'exit'
end
def player_escape?
@@ -388,13 +388,13 @@ def evaluate(input)
response = gets.chomp.downcase
if (response.eql?("y") || response.eql?("yes"))
puts QUIT_MESSAGE
exit(0)
return 'exit'
else
puts RESUME_MESSAGE
end
when 'quit!', 'exit!', 'qq', 'xx'
puts QUIT_MESSAGE
exit(0)
return 'exit'
else
return
end
@@ -0,0 +1,50 @@
# lib/gemwarrior/misc/timer.rb
# Timer
module Gemwarrior
class Timer
attr_accessor :duration_in_s, :timer_name, :background, :progress, :verbose
DEFAULTS = {
duration_in_s: 1,
timer_name: 'Timer',
background: false,
progress: false,
verbose: true
}
def initialize(options = {})
options = DEFAULTS.merge(options)
self.duration_in_s = options[:duration_in_s]
self.timer_name = options[:timer_name]
self.background = options[:background]
self.progress = options[:progress]
self.verbose = options[:verbose]
end
def start
if background
Thread.start { self.run }
else
self.run
end
end
def run
puts "#{timer_name} began at #{Time.now} for #{duration_in_s} seconds" if verbose
end_time = Time.now + duration_in_s
loop do
sleep 1
print '.' if progress
if Time.now >= end_time
print "\n"
puts "#{timer_name} ended at #{Time.now}" if verbose
return
end
end
end
end
end
View
@@ -3,7 +3,9 @@
require 'readline'
require 'os'
require 'clocker'
require_relative 'misc/timer'
require_relative 'misc/wordlist'
require_relative 'evaluator'
require_relative 'version'
@@ -25,18 +27,32 @@ def initialize(world, evaluator)
def start(initialCommand = nil)
setup_screen(initialCommand)
# main loop
loop do
prompt
begin
input = read_line
puts eval.evaluate(input)
rescue Interrupt
puts
puts QUIT_MESSAGE
exit(0)
end
clocker = Clocker.new
at_exit do
duration = clocker.stop
puts "Gem Warrior played for #{duration[:secs]} secs and #{duration[:ms]} ms"
end
clocker.clock {
# main loop
loop do
prompt
begin
input = read_line
result = eval.evaluate(input)
if result.eql?("exit")
exit
else
puts result
end
rescue Interrupt
puts
puts QUIT_MESSAGE
exit
end
end
}
end
private

0 comments on commit 4dfaf44

Please sign in to comment.