Module: Mathangman::Filer
- Included in:
- Game
- Defined in:
- lib/mathangman/filer.rb
Instance Attribute Summary collapse
-
#files ⇒ Object
Returns the value of attribute files.
-
#folder ⇒ Object
Returns the value of attribute folder.
-
#restart ⇒ Object
Returns the value of attribute restart.
Instance Method Summary collapse
- #archives(link) ⇒ Object
- #check_repeated(name) ⇒ Object
- #create_dir ⇒ Object
- #del_dir ⇒ Object
- #folder_not_exist? ⇒ Boolean
- #get_folder_items ⇒ Object
- #get_time ⇒ Object
- #load_file ⇒ Object
- #load_game ⇒ Object
- #restore_state(info) ⇒ Object
- #save_game ⇒ Object
- #save_state(loc) ⇒ Object
Instance Attribute Details
#files ⇒ Object
Returns the value of attribute files.
9 10 11 |
# File 'lib/mathangman/filer.rb', line 9 def files @files end |
#folder ⇒ Object
Returns the value of attribute folder.
9 10 11 |
# File 'lib/mathangman/filer.rb', line 9 def folder @folder end |
#restart ⇒ Object
Returns the value of attribute restart.
9 10 11 |
# File 'lib/mathangman/filer.rb', line 9 def restart @restart end |
Instance Method Details
#archives(link) ⇒ Object
85 86 87 |
# File 'lib/mathangman/filer.rb', line 85 def archives(link) "#{link}/#{@folder}/#{@restart}" end |
#check_repeated(name) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/mathangman/filer.rb', line 18 def check_repeated(name) if @restart.nil? create_dir loc = "saved_games/#{@folder}/#{name}" else File.delete "saved_games/#{@folder}/#{@restart}" loc = "saved_games/#{@folder}/#{name}" end end |
#create_dir ⇒ Object
32 33 34 35 36 |
# File 'lib/mathangman/filer.rb', line 32 def create_dir @folder = @name.downcase Dir.mkdir("saved_games") unless Dir.exist?("saved_games") Dir.mkdir("saved_games/#{@folder}") unless Dir.exist?("saved_games/#{@folder}") end |
#del_dir ⇒ Object
38 39 40 |
# File 'lib/mathangman/filer.rb', line 38 def del_dir Dir.rmdir("saved_games/#{@folder}") unless !Dir["saved_games/#{@folder}/*"].empty? end |
#folder_not_exist? ⇒ Boolean
55 56 57 |
# File 'lib/mathangman/filer.rb', line 55 def folder_not_exist? true unless Dir.exist?("saved_games/#{@folder}") end |
#get_folder_items ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mathangman/filer.rb', line 59 def get_folder_items files_hash = {} i = 1 Dir.entries("saved_games/#{@folder}").sort.each do | file | if file.include? ".txt" files_hash[i] = file i += 1 end end files_hash.each { | key, value | puts "#{key}) #{value}" } end |
#get_time ⇒ Object
28 29 30 |
# File 'lib/mathangman/filer.rb', line 28 def get_time Time.now.to_i end |
#load_file ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mathangman/filer.rb', line 71 def load_file @restart = @files[gets.chomp.to_i] unless @restart.nil? game_file = File.readlines archives "saved_games" game_data = [] game_file.each { | item | game_data << item } restore_state(game_data) guesses(self) else puts @display.msg "Incorrect entry. Please check and retype." load_file end end |
#load_game ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mathangman/filer.rb', line 42 def load_game puts @display.get_name @folder = gets.chomp.downcase if !folder_not_exist? @files = get_folder_items puts @display.msg "Your most recent game is at the BOTTOM\nEnter the number corresponding to the file you want to load" load_file else puts @display.msg "No archive with this username." end end |
#restore_state(info) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/mathangman/filer.rb', line 99 def restore_state(info) @diff = info[0].to_s puts @word = info[1].to_s @secret_word = info[2].to_s @wrongs_num = info[3].to_i @len = info[4].to_i @disp_word = @word if !@word.nil? @disp_word = "-" * @len if @word.nil? end |
#save_game ⇒ Object
11 12 13 14 15 16 |
# File 'lib/mathangman/filer.rb', line 11 def save_game moment = get_time save_name = moment.to_s + '.txt' loc = check_repeated save_name save_state loc end |
#save_state(loc) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/mathangman/filer.rb', line 89 def save_state(loc) File.open(loc, "w") do | line | line.puts @diff line.puts @word line.puts @secret_word line.puts @wrongs_num.to_s line.puts @len end end |