Class: PFM::Shop

Inherits:
Object show all
Defined in:
scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb

Overview

Class describing the shop logic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_state = PFM.game_state) ⇒ Shop

Create a new shop handler

Parameters:

  • game_state (PFM::GameState) (defaults to: PFM.game_state)

    variable responsive of containing the whole game state for easier access



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_statePFM::GameState

Get the game state responsive of the whole game state

Returns:



12
13
14
# File 'scripts/01450 Systems/00203 Shop/00001 PFM/00700 Shop.rb', line 12

def game_state
  @game_state
end

#pokemon_shop_listHash

Hash containing the defined Pokemon shops

Returns:

  • (Hash)


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_listHash

Hash containing the defined shops

Returns:

  • (Hash)


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

Parameters:

  • symbol_of_new_shop (Symbol)

    the symbol to link to the new shop

  • list_of_item_id (Array<Integer>) (defaults to: [])

    the array containing the id of the items to sell

  • list_of_item_quantity (Array<Integer>) (defaults to: [])

    the array containing the quantity of the items to sell

  • shop_rewrite (Boolean) (defaults to: false)

    if the system must completely overwrite an already existing 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

Parameters:

  • sym_new_shop (Symbol)

    the symbol to link to the new shop

  • list_id_mon (Array<Integer>)

    the array containing the id of the Pokemon to sell

  • list_param_mon (Array)

    the array containing the infos of the Pokemon to sell

  • list_price (Array<Integer>)

    the array containing the prices of the Pokemon to sell

  • list_quantity_mon (Array<Integer>) (defaults to: [])

    the array containing the quantity of the Pokemon to sell

  • shop_rewrite (Boolean) (defaults to: false)

    if the system must completely overwrite an already existing 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

Parameters:

  • sym (Symbol)
  • list_id (Array<Integer>) (defaults to: nil)
  • list_param (Array) (defaults to: nil)
  • list_price (Array<Integer>) (defaults to: nil)
  • list_quantity (Array<Integer>) (defaults to: nil)

Returns:

  • (Boolean)

    return true if all params are legit



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)

Parameters:

  • symbol_of_shop (Symbol)

    the symbol of the existing shop

  • list_item_id_to_refill (Array<Integer>) (defaults to: [])

    the array of the items' id

  • list_quantity_to_refill (Array<Integer>) (defaults to: [])

    the array of the quantity to refill



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)

Parameters:

  • symbol_of_shop (Symbol)

    the symbol of the shop

  • list_id_mon (Array<Integer>) (defaults to: [])

    the array containing the id of the Pokemon to sell

  • list_param_mon (Array) (defaults to: [])

    the array containing the infos of the Pokemon to sell

  • list_price (Array<Integer>) (defaults to: [])

    the array containing the prices of the Pokemon to sell

  • list_quantity_mon (Array<Integer>) (defaults to: [])

    the array containing the quantity of the Pokemon to sell



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

Parameters:

  • sym_shop (Symbol)

    the symbol of the shop

  • id (Integer)

    the ID of the Pokemon to register

  • price (Integer)

    the price of the Pokemon

  • param (Hash)

    the hash of the Pokemon (might be a single Integer)

  • quantity (Integer)

    the quantity of the Pokemon to register

  • rewrite (Boolean) (defaults to: false)

    if an existing Pokemon should be rewritten or not



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)

Parameters:

  • symbol_of_shop (Symbol)

    the symbol of the existing shop

  • list_item_id_to_remove (Array<Integer>)

    the array of the items' id

  • list_quantity_to_remove (Array<Integer>)

    the array of the quantity to remove



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)

Parameters:

  • symbol_of_shop (Symbol)

    the symbol of the existing shop

  • remove_list_mon (Array<Integer>)

    the array of the Pokemon id

  • param_form (Array<Hash>) (defaults to: [])

    the form of the Pokemon to delete (only if there is more than one form of a Pokemon in the list)

  • list_quantity_to_remove (Array<Integer>) (defaults to: [])

    the array of the quantity to remove



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

Parameters:

Returns:

  • (Boolean)

    return true if all params are legit



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

Parameters:

  • symbol_of_shop (Symbol)

    the symbol of the shop to sort



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