mirror of
https://github.com/infinitefusion/infinitefusion-e18.git
synced 2025-12-06 06:01:46 +00:00
Extracted common data class methods into mixin modules
This commit is contained in:
75
Data/Scripts/011_Data/014_Data.rb
Normal file
75
Data/Scripts/011_Data/014_Data.rb
Normal file
@@ -0,0 +1,75 @@
|
||||
class Data
|
||||
|
||||
# A mixin module for data classes which provides common class methods (called
|
||||
# by Data::Thing.method) that provide access to data held within. Assumes the
|
||||
# data class's data is stored in a class constant hash called DATA.
|
||||
module ClassMethods
|
||||
# @param other [Symbol, self, Integer]
|
||||
# @return [Boolean] whether the given other is defined as a self
|
||||
def exists?(other)
|
||||
return false if other.nil?
|
||||
validate other => [Symbol, self.class, Integer]
|
||||
other = other.id if other.is_a?(self.class)
|
||||
return !self::DATA[other].nil?
|
||||
end
|
||||
|
||||
# @param other [Symbol, self, Integer]
|
||||
# @return [self]
|
||||
def get(other)
|
||||
validate other => [Symbol, self.class, Integer]
|
||||
return other if other.is_a?(self.class)
|
||||
# if other.is_a?(Integer)
|
||||
# p "Please switch to symbols, thanks."
|
||||
# end
|
||||
raise "Unknown ID #{other}." unless self::DATA.has_key?(other)
|
||||
return self::DATA[other]
|
||||
end
|
||||
|
||||
def try_get(other)
|
||||
return nil if other.nil?
|
||||
validate other => [Symbol, self.class, Integer]
|
||||
return other if other.is_a?(self.class)
|
||||
# if other.is_a?(Integer)
|
||||
# p "Please switch to symbols, thanks."
|
||||
# end
|
||||
return (self::DATA.has_key?(other)) ? self::DATA[other] : nil
|
||||
end
|
||||
|
||||
def each
|
||||
keys = self::DATA.keys
|
||||
keys.sort! { |a, b| a.to_s <=> b.to_s }
|
||||
keys.each do |key|
|
||||
yield self::DATA[key] if key.is_a?(Symbol)
|
||||
end
|
||||
end
|
||||
|
||||
def load
|
||||
const_set(:DATA, load_data("Data/#{self::DATA_FILENAME}"))
|
||||
end
|
||||
|
||||
def save
|
||||
save_data(self::DATA, "Data/#{self::DATA_FILENAME}")
|
||||
end
|
||||
end
|
||||
|
||||
# A mixin module for data classes which provides common instance methods
|
||||
# (called by thing.method) that analyse the data of a particular thing which
|
||||
# the instance represents.
|
||||
module InstanceMethods
|
||||
# @param other [Symbol, self.class, Integer]
|
||||
# @return [Boolean] whether other represents the same thing as this thing
|
||||
def ==(other)
|
||||
return false if other.nil?
|
||||
validate other => [Symbol, self.class, Integer]
|
||||
if other.is_a?(Symbol)
|
||||
return @id == other
|
||||
elsif other.is_a?(self.class)
|
||||
return @id == other.id
|
||||
elsif other.is_a?(Integer)
|
||||
return @id_number == other
|
||||
end
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,92 +0,0 @@
|
||||
class Data
|
||||
|
||||
class Ability
|
||||
attr_reader :id
|
||||
attr_reader :id_number
|
||||
attr_reader :real_name
|
||||
attr_reader :real_description
|
||||
|
||||
DATA = {}
|
||||
|
||||
def initialize(hash)
|
||||
validate hash => Hash, hash[:id] => Symbol
|
||||
@id = hash[:id]
|
||||
@id_number = hash[:id_number] || -1
|
||||
@real_name = hash[:name] || "Unnamed"
|
||||
@real_description = hash[:description] || "???"
|
||||
end
|
||||
|
||||
# @return [String] the translated name of this ability
|
||||
def name
|
||||
return pbGetMessage(MessageTypes::Abilities, @id_number)
|
||||
end
|
||||
|
||||
# @return [String] the translated description of this ability
|
||||
def description
|
||||
return pbGetMessage(MessageTypes::AbilityDescs, @id_number)
|
||||
end
|
||||
|
||||
# @param other [Symbol, Ability, Integer]
|
||||
# @return [Boolean] whether other is the same as this ability
|
||||
def ==(other)
|
||||
return false if other.nil?
|
||||
validate other => [Symbol, Ability, Integer]
|
||||
if other.is_a?(Symbol)
|
||||
return @id == other
|
||||
elsif other.is_a?(Ability)
|
||||
return @id == other.id
|
||||
elsif other.is_a?(Integer)
|
||||
return @id_number == other
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
# @param ability_id [Symbol, Ability, Integer]
|
||||
# @return [Boolean] whether the given ability_id is defined as an Ability
|
||||
def self.exists?(ability_id)
|
||||
return false if ability_id.nil?
|
||||
validate ability_id => [Symbol, Ability, Integer]
|
||||
ability_id = ability_id.id if ability_id.is_a?(Ability)
|
||||
return !DATA[ability_id].nil?
|
||||
end
|
||||
|
||||
# @param ability_id [Symbol, Ability, Integer]
|
||||
# @return [Ability]
|
||||
def self.get(ability_id)
|
||||
validate ability_id => [Symbol, Ability, Integer]
|
||||
return ability_id if ability_id.is_a?(Ability)
|
||||
# if ability_id.is_a?(Integer)
|
||||
# p "Please switch to symbols, thanks."
|
||||
# end
|
||||
raise "Unknown ability ID #{ability_id}." unless DATA.has_key?(ability_id)
|
||||
return DATA[ability_id]
|
||||
end
|
||||
|
||||
def self.try_get(ability_id)
|
||||
return nil if ability_id.nil?
|
||||
validate ability_id => [Symbol, Ability, Integer]
|
||||
return ability_id if ability_id.is_a?(Ability)
|
||||
# if ability_id.is_a?(Integer)
|
||||
# p "Please switch to symbols, thanks."
|
||||
# end
|
||||
return (DATA.has_key?(ability_id)) ? DATA[ability_id] : nil
|
||||
end
|
||||
|
||||
def self.each
|
||||
keys = DATA.keys
|
||||
keys.sort! { |a, b| a.to_s <=> b.to_s }
|
||||
keys.each do |key|
|
||||
yield DATA[key] if key.is_a?(Symbol)
|
||||
end
|
||||
end
|
||||
|
||||
def self.load
|
||||
const_set(:DATA, load_data("Data/abilities.dat"))
|
||||
end
|
||||
|
||||
def self.save
|
||||
save_data(DATA, "Data/abilities.dat")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -14,6 +14,10 @@ class Data
|
||||
attr_reader :move
|
||||
|
||||
DATA = {}
|
||||
DATA_FILENAME = "items.dat"
|
||||
|
||||
extend ClassMethods
|
||||
include InstanceMethods
|
||||
|
||||
def initialize(hash)
|
||||
validate hash => Hash, hash[:id] => Symbol
|
||||
@@ -44,68 +48,6 @@ class Data
|
||||
def description
|
||||
return pbGetMessage(MessageTypes::ItemDescriptions, @id_number)
|
||||
end
|
||||
|
||||
# @param other [Symbol, Item, Integer]
|
||||
# @return [Boolean] whether other is the same as this item
|
||||
def ==(other)
|
||||
return false if other.nil?
|
||||
validate other => [Symbol, Item, Integer]
|
||||
if other.is_a?(Symbol)
|
||||
return @id == other
|
||||
elsif other.is_a?(Item)
|
||||
return @id == other.id
|
||||
elsif other.is_a?(Integer)
|
||||
return @id_number == other
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
# @param item_id [Symbol, Item, Integer]
|
||||
# @return [Boolean] whether the given item_id is defined as an Item
|
||||
def self.exists?(item_id)
|
||||
return false if item_id.nil?
|
||||
validate item_id => [Symbol, Item, Integer]
|
||||
item_id = item_id.id if item_id.is_a?(Item)
|
||||
return !DATA[item_id].nil?
|
||||
end
|
||||
|
||||
# @param item_id [Symbol, Item, Integer]
|
||||
# @return [Item]
|
||||
def self.get(item_id)
|
||||
validate item_id => [Symbol, Item, Integer]
|
||||
return item_id if item_id.is_a?(Item)
|
||||
# if item_id.is_a?(Integer)
|
||||
# p "Please switch to symbols, thanks."
|
||||
# end
|
||||
raise "Unknown item ID #{item_id}." unless DATA.has_key?(item_id)
|
||||
return DATA[item_id]
|
||||
end
|
||||
|
||||
def self.try_get(item_id)
|
||||
return nil if item_id.nil?
|
||||
validate item_id => [Symbol, Item, Integer]
|
||||
return item_id if item_id.is_a?(Item)
|
||||
# if item_id.is_a?(Integer)
|
||||
# p "Please switch to symbols, thanks."
|
||||
# end
|
||||
return (DATA.has_key?(item_id)) ? DATA[item_id] : nil
|
||||
end
|
||||
|
||||
def self.each
|
||||
keys = DATA.keys
|
||||
keys.sort! { |a, b| a.to_s <=> b.to_s }
|
||||
keys.each do |key|
|
||||
yield DATA[key] if key.is_a?(Symbol)
|
||||
end
|
||||
end
|
||||
|
||||
def self.load
|
||||
const_set(:DATA, load_data("Data/items.dat"))
|
||||
end
|
||||
|
||||
def self.save
|
||||
save_data(DATA, "Data/items.dat")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
30
Data/Scripts/011_Data/016_Ability_Data.rb
Normal file
30
Data/Scripts/011_Data/016_Ability_Data.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
class Ability
|
||||
attr_reader :id
|
||||
attr_reader :id_number
|
||||
attr_reader :real_name
|
||||
attr_reader :real_description
|
||||
|
||||
DATA = {}
|
||||
DATA_FILENAME = "abilities.dat"
|
||||
|
||||
extend ClassMethods
|
||||
include InstanceMethods
|
||||
|
||||
def initialize(hash)
|
||||
validate hash => Hash, hash[:id] => Symbol
|
||||
@id = hash[:id]
|
||||
@id_number = hash[:id_number] || -1
|
||||
@real_name = hash[:name] || "Unnamed"
|
||||
@real_description = hash[:description] || "???"
|
||||
end
|
||||
|
||||
# @return [String] the translated name of this ability
|
||||
def name
|
||||
return pbGetMessage(MessageTypes::Abilities, @id_number)
|
||||
end
|
||||
|
||||
# @return [String] the translated description of this ability
|
||||
def description
|
||||
return pbGetMessage(MessageTypes::AbilityDescs, @id_number)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user