Skip to content

Commit

Permalink
✨ add filtered by special method
Browse files Browse the repository at this point in the history
  • Loading branch information
YutaGoto committed Oct 27, 2024
1 parent 2f82436 commit 4bc7959
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/ika3/weapons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ def filter_by_sub(sub_name)
weapons.map { |weapon| W.new(weapon) }
end

def filter_by_special(special_name)
raise "unknown special weapon: #{special_name}" unless special_weapons.values.include?(special_name)

weapons = config.values.filter { |weapon| weapon[:special] == special_name }
weapons.map { |weapon| W.new(weapon) }
end

def reload_config!
@cache = {}
@config = nil
@sub_weapons = nil
config
config_sub_weapons
config_special_weapons
end

private
Expand Down Expand Up @@ -75,6 +83,16 @@ def sub_weapons
@sub_weapon_hash
end

def special_weapons
@special_weapon_hash ||= {}
if @special_weapon_hash.empty?
config_special_weapons.each do |key, value|
@special_weapon_hash[key] = value[:name]
end
end
@special_weapon_hash
end

def config
@config ||= load_yaml_file("#{File.dirname(__FILE__)}/../../config/weapons.yml").deep_symbolize_keys
@config
Expand All @@ -85,6 +103,11 @@ def config_sub_weapons
@sub_weapons
end

def config_special_weapons
@special_weapons ||= load_yaml_file("#{File.dirname(__FILE__)}/../../config/special_weapons.yml").deep_symbolize_keys
@special_weapons
end

def valid?(weapon_key)
names.include?(weapon_key)
end
Expand Down
5 changes: 5 additions & 0 deletions sig/ika3/weapon.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ module Ika3
self.@weapon_hash: Hash[untyped, untyped]
self.@sub_weapon_hash: Hash[untyped, untyped]
self.@sub_weapons: untyped
self.@special_weapon_hash: Hash[untyped, untyped]
self.@special_weapons: untyped

def self.find: (Symbol weapon_key) -> W
def self.find_by_name: (String weapon_name) -> W
def self.filter_by_sub: (String sub_name) -> Array[W]
def self.filter_by_special: (String special_name) -> Array[W]
def self.reload_config!: -> nil
def self.names: -> Array[Symbol]
def self.weapons: -> Hash[untyped, untyped]
def self.sub_weapons: -> Hash[untyped, untyped]
def self.special_weapons: -> Hash[untyped, untyped]
def self.config: -> untyped
def self.config_sub_weapons: -> untyped
def self.config_special_weapons: -> untyped
def self.valid?: (Symbol weapon_key) -> bool
def self.load_yaml_file: (String file) -> untyped

Expand Down
6 changes: 6 additions & 0 deletions spec/ika3/weapons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
end
end

describe '#filter_by_special' do
it 'get weapon info list from special weapon' do
expect(described_class.filter_by_special(special_name).map(&:name)).to include weapon_name
end
end

describe '#name' do
it 'get weapon name' do
expect(described_class.find(key_name).name).to eq weapon_name
Expand Down

0 comments on commit 4bc7959

Please sign in to comment.