Class: GamesAndRpgParadise::Mud::Container

Inherits:
MudObject
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/mud/container/container.rb

Overview

RpgParadise::Mud::Container

Constant Summary

Constants inherited from MudObject

MudObject::DEFAULT_NAME, MudObject::NAMESPACE

Instance Method Summary collapse

Methods inherited from MudObject

[], #add_prop, #add_to_inventory, #alias_action, #can_speak?, #define_action, #describe_the_mud_object, #description?, disable_debug, #empty?, enable_debug, #enable_speak, #find, #has_inventory?, #height?, #id?, #inspect, #internal_hash?, #inventory?, #is_armour?, #is_inventory?, #is_mud_object?, #is_weapon?, #is_wearable?, #length?, #method_missing, #name=, #name?, #obtain_name_from_filename, #random_inventory_element, #report_height, #report_weight, #run, #set_description, #set_height, #set_is_an_armour, #set_is_inventory, #set_is_wearable, #set_length, #set_name, #set_object_name, #set_value, #set_weight, shall_we_debug?, #show_inventory, #store, #store_where?, #value?, #wear, #wearables?, #weight?, #who_am_i?, #wields?

Constructor Details

#initialize(unique_id = 1) ⇒ Container

#

initialize

unique_id is for registering.

#


35
36
37
38
39
40
# File 'lib/games_and_rpg_paradise/mud/container/container.rb', line 35

def initialize(
    unique_id = 1
  )
  super(self)
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GamesAndRpgParadise::Mud::MudObject

Instance Method Details

#add(this_stuff) ⇒ Object

#

add

This method can be used to add items to the inventory.

#


77
78
79
80
81
82
83
84
85
# File 'lib/games_and_rpg_paradise/mud/container/container.rb', line 77

def add(this_stuff)
  raise 'May not add simple String-Objects to this inventory.' if this_stuff.is_a? String
  # raise 'Object is not of proper type.' unless this_stuff.is_mud_object? == true  
  if this_stuff.is_a? Array # traverse our array recursively.
    this_stuff.each {|_| inventory? << _ }
  else
    inventory? << this_stuff
  end
end

#find_object(for_this_type = 'troll') ⇒ Object Also known as: include?

#

find_object

This method will find all objects that match a given criteria, otherwise false will be returned.

#


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/games_and_rpg_paradise/mud/container/container.rb', line 114

def find_object(
    for_this_type = 'troll'
  )
  for_this_type = for_this_type.to_s
  array_result = [] # lists this object
  # FindNames simply finds all possible names; bl $RUBY_MUD/find*
  possible_names = FindNames.new.find_matches_to(for_this_type)

  inventory?.each do |object|
    # Handle all and everything as special case.
    if [ 'all', 'everything' ].include? for_this_type  
      array_result << object
    # Here, we want only one entry, handling only singular entries 
    elsif possible_names.include? object.object_name
      # Add only unless we already have an object inside
      array_result << object unless array_result.size >= 1
    # Handle plural like "trolls" so we return all trolls:
    # possible_names in this case is 'troll' and 'trolls'
    elsif possible_names.map {|entry|
      if object.object_name.include? entry
        array_result << object unless array_result.size >= 1
      end
    }
    else 
      e '>>DEBUG<< IN container.rb: is not included! ..'+for_this_type
      pp inventory?
      @result = false
      return @result # return false to indiate we have not found it.
    end
  end
  @result = array_result 
  return @result #.dup # is dup notwendig? mal auskommentieren
end

#remove(this_item) ⇒ Object

#

remove

Removes entries from our list. Only one at a given time.

#


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/games_and_rpg_paradise/mud/container/container.rb', line 92

def remove(this_item)
  result = find_object(this_item) # result can be array, or false
  if result
    if this_item.end_with? 's' # plural
      _ = result.first
    else
      _ = result.dup
    end
    # inventory?.delete(_)
    _.map { |r| inventory?.delete(r) }
    _
  else
    return false
  end
end

#resetObject

#

reset (reset tag)

#


45
46
47
48
49
50
51
52
# File 'lib/games_and_rpg_paradise/mud/container/container.rb', line 45

def reset
  # ====================================================================== #
  # === @result
  #
  # This contains the actual objects of our container.
  # ====================================================================== #
  @result = nil
end

#result?Boolean Also known as: result

#

result?

#

Returns:

  • (Boolean)


151
152
153
# File 'lib/games_and_rpg_paradise/mud/container/container.rb', line 151

def result?
  @result
end

#silently_display_inventory(display_the_real_weight_of_objects = true) ⇒ Object

#

silently_display_inventory

Use this method to not get the “you are carrying” bla header.

#


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/games_and_rpg_paradise/mud/container/container.rb', line 59

def silently_display_inventory(
    display_the_real_weight_of_objects = true
  )
  inventory?.each { |object|
    _ = '  - a '+object.object_name
    if display_the_real_weight_of_objects and object.respond_to? :weight?
      _ << ' (Weight: ' << object.weight?.to_s << ' kg)'
    end
    _ << N
    print _
  }
end