From 7b5ff32dd40a1af273bbb7c7cdcc43bf39ecbf31 Mon Sep 17 00:00:00 2001 From: Yannis Nikolakis Date: Fri, 16 Sep 2016 02:27:32 +0300 Subject: [PATCH 1/7] Reset function Reset function for optional reseting of any player directory --- lib/ruby_warrior/runner.rb | 40 +++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/ruby_warrior/runner.rb b/lib/ruby_warrior/runner.rb index d2cb6c68..190f097c 100644 --- a/lib/ruby_warrior/runner.rb +++ b/lib/ruby_warrior/runner.rb @@ -1,4 +1,5 @@ require 'optparse' +require 'fileutils' module RubyWarrior class Runner @@ -16,7 +17,43 @@ def run parse_options @game.start end - + + def reset + if ! File.directory?('rubywarrior/') + # If the directory doesn't exist, we can't reset the progress. + print "No progress to reset.\n" + else + directories = Dir["rubywarrior/*"] + if directories.length>0 + puts inputProfile = UI.choose('directory', directories) + if inputProfile != nil + if File.directory?('%{IN}'% {IN:inputProfile}) + # Prompt the user to avoid accidental an reset. + print "Are you sure you want to reset your progress? [yn]\n" + input = gets.chomp + if input == 'y' + FileUtils.rm_rf('%{IN}'% {IN:inputProfile} ) + print "Progress reset.\n" + elsif input == 'n' + print "Progress not reset.\n" + else + print "Not a valid option (y or n). Please try again.\n" + reset + end + else + print "No such profile.\n" + end + else + print "No profile name provided.\n" + end + else + print "No profile to reset." + end + + end + exit + end + private def parse_options @@ -24,6 +61,7 @@ def parse_options options.banner = "Usage: rubywarrior [options]" options.on('-d', '--directory DIR', "Run under given directory") { |dir| Config.path_prefix = dir } options.on('-l', '--level LEVEL', "Practice level on epic") { |level| Config.practice_level = level.to_i } + options.on('-r', '--reset', "Reset all Progress ") { reset } options.on('-s', '--skip', "Skip user input") { Config.skip_input = true } options.on('-t', '--time SECONDS', "Delay each turn by seconds") { |seconds| Config.delay = seconds.to_f } options.on('-h', '--help', "Show this message") { puts(options); exit } From 870d6ad153d839ef710a58f6b4c5143883551890 Mon Sep 17 00:00:00 2001 From: Yannis Nikolakis Date: Fri, 16 Sep 2016 02:39:32 +0300 Subject: [PATCH 2/7] Reset function space fix --- lib/ruby_warrior/runner.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/ruby_warrior/runner.rb b/lib/ruby_warrior/runner.rb index 190f097c..0ff64e77 100644 --- a/lib/ruby_warrior/runner.rb +++ b/lib/ruby_warrior/runner.rb @@ -24,10 +24,10 @@ def reset print "No progress to reset.\n" else directories = Dir["rubywarrior/*"] - if directories.length>0 + if directories.length>0 puts inputProfile = UI.choose('directory', directories) if inputProfile != nil - if File.directory?('%{IN}'% {IN:inputProfile}) + if File.directory?('%{IN}'% {IN:inputProfile}) # Prompt the user to avoid accidental an reset. print "Are you sure you want to reset your progress? [yn]\n" input = gets.chomp @@ -41,15 +41,14 @@ def reset reset end else - print "No such profile.\n" - end - else - print "No profile name provided.\n" + print "No such profile.\n" + end + else + print "No profile name provided.\n" end else print "No profile to reset." end - end exit end From dce97ab5a9244ffbc5b1419e3a1d9fe395a44b34 Mon Sep 17 00:00:00 2001 From: Yannis Nikolakis Date: Fri, 16 Sep 2016 02:41:22 +0300 Subject: [PATCH 3/7] Fixing spaces --- lib/ruby_warrior/runner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_warrior/runner.rb b/lib/ruby_warrior/runner.rb index 0ff64e77..ccb211e0 100644 --- a/lib/ruby_warrior/runner.rb +++ b/lib/ruby_warrior/runner.rb @@ -27,7 +27,7 @@ def reset if directories.length>0 puts inputProfile = UI.choose('directory', directories) if inputProfile != nil - if File.directory?('%{IN}'% {IN:inputProfile}) + if File.directory?('%{IN}'% {IN:inputProfile}) # Prompt the user to avoid accidental an reset. print "Are you sure you want to reset your progress? [yn]\n" input = gets.chomp From 64d7f28aee465048730d1e466f6f3b62518af636 Mon Sep 17 00:00:00 2001 From: Yannis Nikolakis Date: Fri, 16 Sep 2016 02:42:34 +0300 Subject: [PATCH 4/7] Fixing spaces --- lib/ruby_warrior/runner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_warrior/runner.rb b/lib/ruby_warrior/runner.rb index ccb211e0..f4b780b9 100644 --- a/lib/ruby_warrior/runner.rb +++ b/lib/ruby_warrior/runner.rb @@ -42,7 +42,7 @@ def reset end else print "No such profile.\n" - end + end else print "No profile name provided.\n" end From 7e361980611feeb713d1df37a9a3b03cf6890eea Mon Sep 17 00:00:00 2001 From: Yannis Nikolakis Date: Fri, 16 Sep 2016 03:26:49 +0300 Subject: [PATCH 5/7] Compairing the template with the player's file --- lib/ruby_warrior/game.rb | 50 ++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/lib/ruby_warrior/game.rb b/lib/ruby_warrior/game.rb index 59f00c47..34bf7bbf 100644 --- a/lib/ruby_warrior/game.rb +++ b/lib/ruby_warrior/game.rb @@ -69,26 +69,31 @@ def play_current_level continue = true current_level.load_player UI.puts "Starting Level #{current_level.number}" - current_level.play - if current_level.passed? - if next_level.exists? - UI.puts "Success! You have found the stairs." + if !compared_files? + current_level.play + if current_level.passed? + if next_level.exists? + UI.puts "Success! You have found the stairs." + else + UI.puts "CONGRATULATIONS! You have climbed to the top of the tower and rescued the fair maiden Ruby." + continue = false + end + current_level.tally_points + if profile.epic? + UI.puts final_report if final_report && !continue + else + request_next_level + end else - UI.puts "CONGRATULATIONS! You have climbed to the top of the tower and rescued the fair maiden Ruby." continue = false + UI.puts "Sorry, you failed level #{current_level.number}. Change your script and try again." + if !Config.skip_input? && current_level.clue && UI.ask("Would you like to read the additional clues for this level?") + UI.puts current_level.clue.hard_wrap + end end - current_level.tally_points - if profile.epic? - UI.puts final_report if final_report && !continue - else - request_next_level - end - else - continue = false - UI.puts "Sorry, you failed level #{current_level.number}. Change your script and try again." - if !Config.skip_input? && current_level.clue && UI.ask("Would you like to read the additional clues for this level?") - UI.puts current_level.clue.hard_wrap - end + else + UI.puts "You haven't changed the initial rubywarrior/#{profile.directory_name}/player.rb file!" + continue=false end continue end @@ -160,6 +165,17 @@ def tower_paths end + #templates + + def templates_path + File.expand_path("../../../templates", __FILE__) + end + + def compared_files? + FileUtils.compare_file(current_level.player_path+'/player.rb',templates_path+'/player.rb') + end + + # levels def current_level From a753d67a456677cc4120d43c87ce8262042351dd Mon Sep 17 00:00:00 2001 From: Yannis Nikolakis Date: Fri, 16 Sep 2016 03:30:06 +0300 Subject: [PATCH 6/7] Fixing spaces --- lib/ruby_warrior/game.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ruby_warrior/game.rb b/lib/ruby_warrior/game.rb index 34bf7bbf..6c719f65 100644 --- a/lib/ruby_warrior/game.rb +++ b/lib/ruby_warrior/game.rb @@ -92,8 +92,8 @@ def play_current_level end end else - UI.puts "You haven't changed the initial rubywarrior/#{profile.directory_name}/player.rb file!" - continue=false + UI.puts "You haven't changed the initial rubywarrior/#{profile.directory_name}/player.rb file!" + continue=false end continue end From f5e31e96889c6383e2a07ccdee2dc8e4edda1f27 Mon Sep 17 00:00:00 2001 From: Yannis Nikolakis Date: Fri, 16 Sep 2016 03:56:35 +0300 Subject: [PATCH 7/7] Checking for wrong numerical input --- lib/ruby_warrior/ui.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/ruby_warrior/ui.rb b/lib/ruby_warrior/ui.rb index a616d54c..e97aea15 100644 --- a/lib/ruby_warrior/ui.rb +++ b/lib/ruby_warrior/ui.rb @@ -33,15 +33,22 @@ def choose(item, options) if options.length == 1 response = options.first else - options.each_with_index do |option, i| - if option.kind_of? Array - puts "[#{i+1}] #{option.last}" + loop do + options.each_with_index do |option, i| + if option.kind_of? Array + puts "[#{i+1}] #{option.last}" + else + puts "[#{i+1}] #{option}" + end + end + choice = request("Choose #{item} by typing the number: ") + if choice.to_i>options.length + print "You haven't provided a correct input. Try again!\n\n" else - puts "[#{i+1}] #{option}" + response = options[choice.to_i-1] end + break if choice.to_i<=options.length end - choice = request("Choose #{item} by typing the number: ") - response = options[choice.to_i-1] end if response.kind_of? Array response.first