Class: PFM::Bag

Inherits:
Object show all
Defined in:
scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb

Overview

InGame Bag management

The global Bag object is stored in $bag and PFM.game_state.bag

Author:

  • Nuri Yuri

Constant Summary collapse

SHORTCUT_AMOUNT =

Number of shortcut

4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_state = PFM.game_state) ⇒ Bag

Create a new Bag

Parameters:

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

    variable responsive of containing the whole game state for easier access



30
31
32
33
34
35
36
37
38
39
40
41
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 30

def initialize(game_state = PFM.game_state)
  self.game_state = game_state
  # @type [Hash<Symbol => Integer>]
  @items = Hash.new(0)
  @orders = [[], [], [], [], [], [], []]
  @last_socket = 1
  @last_index = 0
  @shortcut = Array.new(SHORTCUT_AMOUNT, :__undef__)
  @locked = false
  @last_battle_item_db_symbol = :__undef__
  @alpha_sorted = false
end

Instance Attribute Details

#alpha_sortedBoolean

Tell if the bag is alpha sorted

Returns:

  • (Boolean)


21
22
23
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 21

def alpha_sorted
  @alpha_sorted
end

#game_statePFM::GameState

Get the game state responsive of the whole game state

Returns:



24
25
26
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 24

def game_state
  @game_state
end

#last_battle_item_db_symbolSymbol

Set the last battle item

Returns:

  • (Symbol)


18
19
20
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 18

def last_battle_item_db_symbol
  @last_battle_item_db_symbol
end

#last_indexInteger

Last index in the socket

Returns:



12
13
14
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 12

def last_index
  @last_index
end

#last_socketInteger

Last socket used in the bag

Returns:



9
10
11
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 9

def last_socket
  @last_socket
end

#lockedBoolean

If the bag is locked (and react as being empty)

Returns:

  • (Boolean)


15
16
17
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 15

def locked
  @locked
end

Instance Method Details

#add_item(db_symbol, nb = 1) Also known as: store_item

Add items in the bag and trigger the right quest objective

Parameters:

  • db_symbol (Symbol)

    db_symbol of the item

  • nb (Integer) (defaults to: 1)

    number of item to add



83
84
85
86
87
88
89
90
91
92
93
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 83

def add_item(db_symbol, nb = 1)
  return if @locked
  return remove_item(db_symbol, -nb) if nb < 0

  db_symbol = data_item(db_symbol).db_symbol if db_symbol.is_a?(Integer)
  return if db_symbol == :__undef__

  @items[db_symbol] += nb
  add_item_to_order(db_symbol)
  game_state.quests.add_item(data_item(db_symbol).id) unless game_state.bag != self
end

#contain_item?(db_symbol) ⇒ Boolean Also known as: has_item?

If the bag contain a specific item

Parameters:

  • db_symbol (Symbol)

    db_symbol of the item

Returns:

  • (Boolean)


59
60
61
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 59

def contain_item?(db_symbol)
  return item_quantity(db_symbol) > 0
end

#convert_to_dot26

Convert bag to .26 format



44
45
46
47
48
49
50
51
52
53
54
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 44

def convert_to_dot26
  return if @items.is_a?(Hash)

  items = Hash.new(0)
  items.merge!(
    @items.map.with_index { |quantity, id| [data_item(id).db_symbol, quantity] }.reject { |v| v.last == 0 }.to_h
  )
  items.delete(:__undef__)
  @items = items
  @orders.map! { |order| order.map { |id| data_item(id).db_symbol }.reject { |db_symbol| db_symbol == :__undef__ } }
end

#empty?Boolean

Tell if the bag is empty

Returns:

  • (Boolean)


66
67
68
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 66

def empty?
  return @items.empty?
end

#get_order(socket) ⇒ Array

Get the order of items in a socket

Parameters:

  • socket (Integer, Symbol)

    ID of the socket

Returns:

  • (Array)


117
118
119
120
121
122
123
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 117

def get_order(socket)
  return [] if @locked
  return @shortcut if socket == :favorites
  return process_battle_order(socket) if socket.is_a?(Symbol) # TODO

  return (@orders[socket] ||= [])
end

#item_quantity(db_symbol) ⇒ Integer

The quantity of an item in the bag

Parameters:

  • db_symbol (Symbol)

    db_symbol of the item

Returns:



73
74
75
76
77
78
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 73

def item_quantity(db_symbol)
  return 0 if @locked

  db_symbol = data_item(db_symbol).db_symbol if db_symbol.is_a?(Integer)
  return @items[db_symbol]
end

#last_battle_itemStudio::Item

Get the last battle item

Returns:



162
163
164
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 162

def last_battle_item
  data_item(@last_battle_item_db_symbol)
end

#remove_item(db_symbol, nb = 999) Also known as: drop_item

Remove items from the bag

Parameters:

  • db_symbol (Symbol)

    db_symbol of the item

  • nb (Integer) (defaults to: 999)

    number of item to remove



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 99

def remove_item(db_symbol, nb = 999)
  return if @locked
  return add_item(db_symbol, -nb) if nb < 0

  db_symbol = data_item(db_symbol).db_symbol if db_symbol.is_a?(Integer)
  return if db_symbol == :__undef__

  @items[db_symbol] -= nb
  if @items[db_symbol] <= 0
    @items.delete(db_symbol)
    remove_item_from_order(db_symbol)
  end
end

#reset_order(socket) ⇒ Array Also known as: sort_ids

Reset the order of items in a socket

Parameters:

  • socket (Integer)

    ID of the socket

Returns:

  • (Array)

    the new order



128
129
130
131
132
133
134
135
136
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 128

def reset_order(socket)
  arr = get_order(socket)
  arr.select! { |db_symbol| data_item(db_symbol).socket == socket && @items[db_symbol] > 0 } unless socket == :favorites
  unless each_data_item.select { |item| item.socket == socket }.all? { |item| item.position.zero? }
    arr.sort! { |a, b| data_item(a).position <=> data_item(b).position }
  end
  @alpha_sorted = false
  return arr
end

#shortcutsArray<Symbol> Also known as: get_shortcuts

Get the shortcuts

Returns:

  • (Array<Symbol>)


154
155
156
157
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 154

def shortcuts
  @shortcut ||= Array.new(SHORTCUT_AMOUNT, :__undef__)
  return @shortcut
end

#sort_alpha(socket, reverse = false)

Sort the item of a socket by their names

Parameters:

  • socket (Integer)

    ID of the socket

  • reverse (Boolean) (defaults to: false)

    if we want to sort reverse



142
143
144
145
146
147
148
149
150
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 142

def sort_alpha(socket, reverse = false)
  if reverse
    reset_order(socket).sort! { |a, b| data_item(b).name <=> data_item(a).name }
    @alpha_sorted = false
  else
    reset_order(socket).sort! { |a, b| data_item(a).name <=> data_item(b).name }
    @alpha_sorted = true
  end
end