Class: PFM::Shop
- Defined in:
- scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb
Overview
Class describing the shop logic
Instance Attribute Summary collapse
-
#game_state ⇒ PFM::GameState
Get the game state responsive of the whole game state.
-
#pokemon_shop_list ⇒ Hash
Hash containing the defined Pokemon shops.
-
#shop_list ⇒ Hash
Hash containing the defined shops.
Instance Method Summary collapse
-
#create_new_limited_shop(symbol_of_new_shop, list_of_item_id = [], list_of_item_quantity = [], shop_rewrite: false)
Create a new limited Shop.
-
#create_new_pokemon_shop(sym_new_shop, list_id_mon, list_price, list_param_mon, list_quantity_mon = [], shop_rewrite: false)
Create a new Pokemon Shop.
-
#initialize(game_state = PFM.game_state) ⇒ Shop
constructor
Create a new shop handler.
-
#mon_shop_param_legit?(sym, list_id: nil, list_param: nil, list_price: nil, list_quantity: nil) ⇒ Boolean
Check the legitimity of the parameters.
-
#refill_limited_shop(symbol_of_shop, list_item_id_to_refill = [], list_quantity_to_refill = [])
Refill an already existing shop with items (Create the shop if it does not exist).
-
#refill_pokemon_shop(symbol_of_shop, list_id_mon = [], list_price = [], list_param_mon = [], list_quantity_mon = [], pkm_rewrite: false)
Refill an already existing Pokemon Shop (create it if it does not exist).
-
#register_new_pokemon_in_shop(sym_shop, id, price, param, quantity, rewrite: false)
Register the Pokemon into the Array under certain conditions.
-
#remove_from_limited_shop(symbol_of_shop, list_item_id_to_remove, list_quantity_to_remove)
Remove items from an already existing shop (return if do not exist).
-
#remove_from_pokemon_shop(symbol_of_shop, remove_list_mon, param_form = [], list_quantity_to_remove = [])
Remove Pokemon from an already existing shop (return if do not exist).
-
#shop_param_legit?(symbol, arr1, arr2) ⇒ Boolean
Check the legitimity of the parameters.
-
#sort_pokemon_shop(symbol_of_shop)
Sort the Pokemon Shop list.
Constructor Details
#initialize(game_state = PFM.game_state) ⇒ Shop
Create a new shop handler
16 17 18 19 20 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 16 def initialize(game_state = PFM.game_state) @shop_list = {} @pokemon_shop_list = {} @game_state = game_state end |
Instance Attribute Details
#game_state ⇒ PFM::GameState
Get the game state responsive of the whole game state
12 13 14 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 12 def game_state @game_state end |
#pokemon_shop_list ⇒ Hash
Hash containing the defined Pokemon shops
9 10 11 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 9 def pokemon_shop_list @pokemon_shop_list end |
#shop_list ⇒ Hash
Hash containing the defined shops
6 7 8 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 6 def shop_list @shop_list end |
Instance Method Details
#create_new_limited_shop(symbol_of_new_shop, list_of_item_id = [], list_of_item_quantity = [], shop_rewrite: false)
Create a new limited Shop
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 27 def create_new_limited_shop(symbol_of_new_shop, list_of_item_id = [], list_of_item_quantity = [], shop_rewrite: false) return unless shop_param_legit?(symbol_of_new_shop, list_of_item_id, list_of_item_quantity) if @shop_list.key?(symbol_of_new_shop) && shop_rewrite @shop_list.delete(symbol_of_new_shop) elsif @shop_list.key?(symbol_of_new_shop) && shop_rewrite == false return refill_limited_shop(symbol_of_new_shop, list_of_item_id, list_of_item_quantity) end @shop_list[symbol_of_new_shop] = {} list_of_item_id.each_with_index do |id, index| if data_item(id).is_limited @shop_list[symbol_of_new_shop][id] = (list_of_item_quantity[index] != nil ? list_of_item_quantity[index] : 1) else @shop_list[symbol_of_new_shop][id] = 1 end end end |
#create_new_pokemon_shop(sym_new_shop, list_id_mon, list_price, list_param_mon, list_quantity_mon = [], shop_rewrite: false)
Create a new Pokemon Shop
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 102 def create_new_pokemon_shop(sym_new_shop, list_id_mon, list_price, list_param_mon, list_quantity_mon = [], shop_rewrite: false) return unless mon_shop_param_legit?(sym_new_shop, list_id: list_id_mon, list_param: list_param_mon, list_price: list_price, list_quantity: list_quantity_mon) if @pokemon_shop_list.key?(sym_new_shop) && shop_rewrite @pokemon_shop_list.delete(sym_new_shop) elsif @pokemon_shop_list.key?(sym_new_shop) && shop_rewrite == false return refill_pokemon_shop(sym_new_shop, list_id_mon, list_param_mon, list_price, list_quantity_mon) end @pokemon_shop_list[sym_new_shop] = [] list_id_mon.each_with_index do |id, index| register_new_pokemon_in_shop(sym_new_shop, id, list_price[index], list_param_mon[index], list_quantity_mon[index]) end sort_pokemon_shop(sym_new_shop) end |
#mon_shop_param_legit?(sym, list_id: nil, list_param: nil, list_price: nil, list_quantity: nil) ⇒ Boolean
Check the legitimity of the parameters
211 212 213 214 215 216 217 218 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 211 def mon_shop_param_legit?(sym, list_id: nil, list_param: nil, list_price: nil, list_quantity: nil) validate_param(:mon_shop_param_legit?, :sym, sym => Symbol) validate_param(:mon_shop_param_legit?, :list_id, list_id => { Array => Integer }) if list_id validate_param(:mon_shop_param_legit?, :list_param, list_param => Array) if list_param validate_param(:mon_shop_param_legit?, :list_price, list_price => { Array => Integer }) if list_price validate_param(:mon_shop_param_legit?, :list_quantity, list_quantity => { Array => Integer }) if list_quantity return true end |
#refill_limited_shop(symbol_of_shop, list_item_id_to_refill = [], list_quantity_to_refill = [])
Refill an already existing shop with items (Create the shop if it does not exist)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 50 def refill_limited_shop(symbol_of_shop, list_item_id_to_refill = [], list_quantity_to_refill = []) return unless shop_param_legit?(symbol_of_shop, list_item_id_to_refill, list_quantity_to_refill) if @shop_list.key?(symbol_of_shop) list_item_id_to_refill.each_with_index do |id, index| @shop_list[symbol_of_shop][id] = 0 unless @shop_list[symbol_of_shop].key?(id) if data_item(id).is_limited @shop_list[symbol_of_shop][id] += (list_quantity_to_refill[index] != nil ? list_quantity_to_refill[index] : 1) else @shop_list[symbol_of_shop][id] = 1 end end else # We create a shop if one do not already exist create_new_limited_shop(symbol_of_shop, list_item_id_to_refill, list_quantity_to_refill) end end |
#refill_pokemon_shop(symbol_of_shop, list_id_mon = [], list_price = [], list_param_mon = [], list_quantity_mon = [], pkm_rewrite: false)
Refill an already existing Pokemon Shop (create it if it does not exist)
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 130 def refill_pokemon_shop(symbol_of_shop, list_id_mon = [], list_price = [], list_param_mon = [], list_quantity_mon = [], pkm_rewrite: false) return unless mon_shop_param_legit?(symbol_of_shop, list_id: list_id_mon, list_param: list_param_mon, list_price: list_price, list_quantity: list_quantity_mon) if @pokemon_shop_list.key?(symbol_of_shop) list_id_mon.each_with_index do |id, index| register_new_pokemon_in_shop(symbol_of_shop, id, list_price[index], list_param_mon[index], list_quantity_mon[index], rewrite: pkm_rewrite) end sort_pokemon_shop(symbol_of_shop) else # We create a shop if one do not already exist create_new_pokemon_shop(symbol_of_shop, list_id_mon, list_price, list_param_mon, list_quantity_mon) end end |
#register_new_pokemon_in_shop(sym_shop, id, price, param, quantity, rewrite: false)
Register the Pokemon into the Array under certain conditions
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 180 def register_new_pokemon_in_shop(sym_shop, id, price, param, quantity, rewrite: false) param = { level: param } if param.is_a?(Integer) # <= param is always hash from here @Rey index_condition = proc { |hash| hash[:id] == id && hash[:form].to_i == param[:form].to_i } if (result = @pokemon_shop_list[sym_shop].index(&index_condition)) && rewrite @pokemon_shop_list[sym_shop].delete_at(result) elsif (result = @pokemon_shop_list[sym_shop].index(&index_condition)) return @pokemon_shop_list[sym_shop][result][:quantity] += quantity || 1 end hash_pkm = param.dup hash_pkm[:id] = id hash_pkm[:price] = price hash_pkm[:quantity] = quantity || 1 @pokemon_shop_list[sym_shop] << hash_pkm end |
#remove_from_limited_shop(symbol_of_shop, list_item_id_to_remove, list_quantity_to_remove)
Remove items from an already existing shop (return if do not exist)
71 72 73 74 75 76 77 78 79 80 81 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 71 def remove_from_limited_shop(symbol_of_shop, list_item_id_to_remove, list_quantity_to_remove) return unless shop_param_legit?(symbol_of_shop, list_item_id_to_remove, list_quantity_to_remove) return unless @shop_list.key?(symbol_of_shop) list_item_id_to_remove.each_with_index do |id, index| next unless @shop_list[symbol_of_shop].key?(id) @shop_list[symbol_of_shop][id] -= (list_quantity_to_remove[index].nil? ? 999 : list_quantity_to_remove[index]) @shop_list[symbol_of_shop].delete(id) if @shop_list[symbol_of_shop][id] <= 0 end end |
#remove_from_pokemon_shop(symbol_of_shop, remove_list_mon, param_form = [], list_quantity_to_remove = [])
Remove Pokemon from an already existing shop (return if do not exist)
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 153 def remove_from_pokemon_shop(symbol_of_shop, remove_list_mon, param_form = [], list_quantity_to_remove = []) return unless mon_shop_param_legit?(symbol_of_shop, list_id: remove_list_mon, list_param: param_form, list_quantity: list_quantity_to_remove) return unless @pokemon_shop_list.key?(symbol_of_shop) pkm_list = @pokemon_shop_list[symbol_of_shop] remove_list_mon.each_with_index do |id, index| form = param_form[index].is_a?(Hash) ? param_form[index][:form].to_i : 0 result = pkm_list.find_index { |hash| hash[:id] == id && hash[:form].to_i == form } next unless result pkm_list[result][:quantity] -= (list_quantity_to_remove[index].nil? ? 999 : list_quantity_to_remove[index]) pkm_list.delete_at(result) if pkm_list[result][:quantity] <= 0 end @pokemon_shop_list[symbol_of_shop] = pkm_list sort_pokemon_shop(symbol_of_shop) end |
#shop_param_legit?(symbol, arr1, arr2) ⇒ Boolean
Check the legitimity of the parameters
88 89 90 91 92 93 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 88 def shop_param_legit?(symbol, arr1, arr2) validate_param(:shop_param_legit?, :symbol, symbol => Symbol) validate_param(:shop_param_legit?, :arr1, arr1 => { Array => Integer }) validate_param(:shop_param_legit?, :arr2, arr2 => { Array => Integer }) return true end |
#sort_pokemon_shop(symbol_of_shop)
Sort the Pokemon Shop list
200 201 202 |
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 200 def sort_pokemon_shop(symbol_of_shop) @pokemon_shop_list[symbol_of_shop].sort_by! { |hash| [hash[:id], hash[:form].to_i] } end |