lib/room.rb in room-0.1.3 vs lib/room.rb in room-0.2.0

- old
+ new

@@ -1,17 +1,62 @@ require "thread" -def reload! filename=nil +$state = {} + +def reload! room_name = nil, filename = nil $commands = {} + + $room_name ||= room_name $last_filename ||= filename - old_count = Room.rooms.keys.length load $last_filename - Room.rooms.keys.length - old_count + load! + + $state[:here] ||= $starting_room end +def prefs_paths + dir_path = File.expand_path("~/.room") + file_path = File.join(dir_path, $room_name) + [dir_path, file_path] +end + +def no_save! + $no_save = true +end + +def save! obj = $state + return if $no_save + + dir_path, file_path = prefs_paths + begin + Dir::mkdir dir_path unless File.exist? dir_path + File.open(file_path, "wb") { |f| f.write Marshal.dump(obj || {}) } + @previous_save_error = false + rescue Exception => e + unless @previous_save_error + Printer.puts "[warning: couldn't save state: #{e}]" + @previous_save_error = true + end + end +end + +def load! + _, file_path = prefs_paths + return unless File.exist? file_path + begin + $state = Marshal.load(File.read(file_path)) + @previous_load_error = false + rescue Exception => e + unless @previous_load_error + Printer.puts "[warning: couldn't load state: #{e}]" + @previous_load_error = true + end + end +end + class String def |(o) if o.nil? self else @@ -58,11 +103,11 @@ def no_echo $secretive = true end def inventory - $inventory ||= [] + $state[:inventory] ||= [] end def have? item inventory.include? item end @@ -76,14 +121,10 @@ end def do action Printer.puts if action.strip == "" - elsif action == "reload!" - d = reload! - Printer.puts "A great wave of relief washes over you." - Printer.puts "The world seems larger by about #{d}." if d > 0 elsif (r = self.class.commands.detect { |c, m| c =~ action }) command, method = r args = command.match(action).to_a.drop(1) Printer.puts self.send(method, *args).to_s.rstrip else @@ -118,26 +159,50 @@ def commands $commands ||= {} $commands[key] ||= DEFAULT_COMMANDS.dup end - def rooms - $rooms ||= {} + def room key + $state[:rooms] ||= {} + $state[:rooms][key] = room_keys[key].new if $state[:rooms][key].nil? && room_keys[key] + $state[:rooms][key] end + def room_keys + $room_keys ||= {} + end + + def here + room $state[:here] + end + def go key, look = true - if rooms[key] - $here = rooms[key] - "\n" + $here.look if look + if room key + $state[:here] = key + "\n" + here.look if look else - $here.unknown_room key + here.unknown_room key end end def do action - if $here - $here.do action + if action == "debug!" + Printer.puts + Printer.puts $state.inspect + elsif action == "restart!" + $state = { :here => $starting_room } + Printer.puts + Printer.puts " ....." + Printer.puts "Your mind, a wild monkey." + Printer.puts " ....,.," + Room.do "look" + elsif action == "reload!" + reload! + Printer.puts + Printer.puts "A great wave of relief washes over you." + elsif here + here.do action else Printer.puts Printer.puts "Where am I?" end end @@ -151,11 +216,12 @@ def add_command regexp, method commands << [regexp, method] end def inherited subclass - rooms[subclass.key] = subclass.new - $here ||= rooms[subclass.key] + room_keys[subclass.key] = subclass + $starting_room ||= subclass.key + $state[:here] ||= $starting_room end def method_added name if public_instance_methods.include? name.to_s $last_command = name