class Patterns::MonsterPrototypes

MonsterPrototypes -> Object

This class allows for the copying of any monster class to quickly create a new one. All monsters are loaded into memory immediately and can be copied later on demand by name. This uses the builder and prototype patterns.

Attributes

monster_names[RW]

The list of all the monster names in the monster_types foler

prototypes[RW]

The Hash of all the prototypes in the monster_types folder

Public Class Methods

new() click to toggle source

Initializes the MonsterPrototypes class

Reads all of the json files, strips their names from the directory path, and then creates new monsters for each name found.

# File app/models/type_object/monster_prototypes.rb, line 18
def initialize
  @monster_names = []
  @prototypes = {}
  Dir["#{MONSTER_JSON_PATH}/**/*.json"].each { |f| @monster_names << f.gsub('.json', '').gsub(MONSTER_JSON_PATH + '/', '') }

  @monster_names.each do |name|
    @prototypes[name] = Monster.new(name)
  end
end

Public Instance Methods

clone_type(type_name) click to toggle source

Performs a marshalling of the Monster stored in the Prototype array, which is returned as a new Monster to the caller as a clone of the original

  • type_name - The type of the monster to clone

Examples

=> monsters = MonsterPrototypes.new
=> dragon = monsters.clone_type('dragon')
# File app/models/type_object/monster_prototypes.rb, line 36
def clone_type(type_name)
  if @prototypes[type_name] != nil
    serialized_object = Marshal.dump(@prototypes[type_name])
    # Just in case someone was able to write some json onto disk and inject something.
    # This won't actually help anything because the type name is still being read from the same source, but it serves as a security example.
    if %w(exec system eval throw puts exit fork syscall %x send `).include? serialized_object
      Monster.new(type_name)
    else
      Marshal.load(serialized_object)
    end
  end
end