Hero.pet()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
# Main app class
2
require_all 'model/personaje'
3
4
class Hero < Hash
5
  attr_accessor :id, :name, :nivel,
6
    :personaje, :jugador, :status, :muerto, :gender,
7
    :repu, :cuerpo, :mente, :mov, :historia, :premio,
8
    :familiar, :mounts, :descendencia, :pareja, :progenitores,
9
    :hechizos, :shadows, :blood, :sand, :ice, :skills, :master,
10
    :armas, :armadura, :protecciones, :miscelaneas, :abalorios,
11
    :profesion, :ciudad, :titulo, :camino, :hijos,
12
    :piezas, :pociones, :pergaminos, :materiales, :oro, :tesoro
13
14
  def initialize(args)
15
    args.each do |k, v|
16
      instance_variable_set("@#{k}", v) unless v.nil?
17
    end
18
  end
19
20
  # TODO: Conflictive method
21 View Code Duplication
  def resistencia(elemento) # I'm sorry for this...
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
    total = 0 # Initialize default returns 0
23
    regex = /vs #{Regexp.quote(elemento)}/ # looks for "+N vs #{elemento}"
24
    reg2x = /vs todas las resistencias/
25
26
    %w(proteccions baratijas armour).each do |i|
27
      next unless send(i) # ask for item-type
28
      send(i).each do |item|
29
        if item.enchanted?
30
          item.enchants.each do |e|
31
            texto =  enchant(e)[:descripcion] # takes description
32
            if m = (regex =~ texto) # if positive (TODO: tune up this)
33
              bono = texto[m.to_i - 2].to_i # add the bonificator
34
              # puts "#{elemento}, #{item.name},magia: #{texto}"
35
              total += bono
36
            end
37
            next unless m = (reg2x =~ texto) # if positive (TODO: tune up this)
38
            bono = texto[m.to_i - 2].to_i # add the bonificator
39
            # puts "+1 todas las resistencias"
40
            total += bono
41
          end
42
        end
43
        next unless item.engarzado?
44
        %w(gemas joyas runas).each do |engarce|
45
          next unless eng = item.send(engarce)
46
          eng.each do |id|
47
            texto = send(engarce[0..-2], id).fits[item.fits] # takes description
48
            if m = (regex =~ texto) # if positive (TODO: tune up this)
49
              # puts "#{elemento}, #{item.name},#{engarce} #{texto}"
50
              bono = texto[m.to_i - 2].to_i # add the bonificator
51
              total += bono
52
            end
53
            next unless m = (reg2x =~ texto) # if positive (TODO: tune up this)
54
            bono = texto[m.to_i - 2].to_i # add the bonificator
55
            # puts "+1 todas las resistencias"
56
            total += bono
57
          end
58
        end
59
      end
60
    end
61
    total
62
  end
63
64
  # Custom meta-methods created by each item:
65
  (fields[0] + fields[1] + fields[2]).each do |f|
66
   define_method(f) do
67
     ((proteccions || []) + (baratijas || [])).detect { |item| item.fits == f }
68
   end
69
  end
70
71
  # Default-ed meta-methods
72 View Code Duplication
  def armour
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
    case armadura.class.to_s
74
    when 'Fixnum' then Armadura.new(id: armadura)
75
    when 'Hash'   then Armadura.new(armadura)
76
    else Armadura.new(id: 0)
77
    end
78
  end
79
80 View Code Duplication
  def get_weapon(weapon) # Analyze data structure
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
81
    case weapon.class.to_s
82
    when 'Hash'   then Arma.new(weapon)     # Ad-hoc item
83
    when 'Fixnum' then Arma.new(id: weapon) # Item mundano
84
    else Arma.new(id: 0) # Nil items
85
    end
86
  end
87
88 View Code Duplication
  def weapons
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
89
    if armas.class.to_s == 'Array'
90
      armas.map { |w| get_weapon(w) }
91
    else # Single weapon // nil item.
92
      [get_weapon(armas)]
93
    end
94
  end
95
96
  def clase
97
    clases.select { |_c, ps| ps.include?(personaje) }.keys.first
98
  end
99
100
  def hab_base
101
    habilidad_base(clase)
102
  end
103
104 View Code Duplication
  def elementos
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
105
    elementos = []
106
    elementos = magias.map(&:elemento).uniq if magias
107
    elementos << 'sombras' if shadows
108
    elementos << 'sangre'  if blood
109
    elementos << 'arena'   if sand
110
    elementos
111
  end
112
113
  def img_path
114
    "/images/personajes/#{genderize}.png"
115
  end
116
117
  def big_path
118
    "/images/portraits/#{name}.png"
119
  end
120
121
  def reputacion
122
    repu || 0
123
  end
124
  
125
  def estado
126
    status ||= 'ausente'
127
  end
128
129
  def movimiento
130
    mov
131
  end
132
133
  def raza
134
    %w(clérigo ladrón bárbaro mago).include?(clase) ? 'humano' : clase
135
  end
136
137
  def female?
138
    sex == 'female'
139
  end
140
141
  def male?
142
    sex == 'male'
143
  end
144
145
  def anillos
146
    (baratijas || []).select { |m| m.fits == 'anillo'  }
147
  end
148
149
  def amuletos
150
    (baratijas || []).select { |m| m.fits == 'amuleto' }
151
  end
152
153 View Code Duplication
  def ataque
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
154
    weapons.first.categoria != 'distancia' ? weapons.first.ataque : 0
155
  end
156
157 View Code Duplication
  def rango
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
158
    weapons.first.categoria == 'distancia' ? weapons.first.ataque : 0
159
  end
160
161
  def defensa
162
    armour.defensa
163
  end
164
165
  def pet
166
    Pet.new(familiar) if familiar
167
  end
168
169 View Code Duplication
  def gremio
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
170
    prof = profesions.find { |p| p.id == profesion['id'] }
171
    Profesion.new(profesion.merge(name: prof.name))
172
  end
173
174
  def baratijas
175
    miscelaneas.map  { |m|  Miscelanea.new(m) } if miscelaneas
176
  end
177
178
  def proteccions
179
    protecciones.map { |p|  Proteccion.new(p) } if protecciones
180
  end
181
182
  def trinkets
183
    abalorios.map    { |a| Abalorio.new(a) } if abalorios
184
  end
185
186
  def pergs
187
    pergaminos.map   { |p| Pergamino.new(p) } if pergaminos
188
  end
189
190
  def cacharros
191
    piezas.map       { |num| Pieza.new(id: num) } if piezas
192
  end
193
194
  def brebajes
195
    pociones.map     { |num| Pocion.new(id: num) } if pociones
196
  end
197
198
  def componentes
199
    materiales.map   { |num| Material.new(id: num) } if materiales
200
  end
201
202
  def transportes
203
    mounts.map       { |num| Montura.new(montura(num)) } if mounts
204
  end
205
206
  def masters
207
    master.map       { |num| Habilidad.new(maestrodearma(num)) } if master
208
  end
209
210
  def habilidades
211
    if skills
212
      skills.map do |num|
213
        p = personaje.gsub('señor de las bestias', 'beastslord')
214
        p = p.gsub('señor del Kraken', 'krakenlord')
215
        Habilidad.new(send(p, num))
216
      end
217
    end
218
  end
219
220
  def magias
221
    hechizos.map { |num| spell(num) } if hechizos
222
  end
223
224
  def blood_magic
225
    blood.map    { |num| sangre(num) } if blood
226
  end
227
228
  def shadow_magic
229
    shadows.map  { |num| sombra(num) } if shadows
230
  end
231
232
  def sand_magic
233
    arenas.map  { |num| arena(num) } if sand
234
  end
235
236
  def sin_recursos
237
    tesoro.nil?
238
  end
239
240
  def empadronado
241
    ciudad || 'Jadessvärd'
242
  end
243
244
  def estado
245
    empadronado == 'Jadessvärd' ? (status || 'ausente') : 'extranjero'
246
  end
247
248
  # inventario
249
  def capacidad
250
    nivel / 3 + 3
251
  end
252
253
  def patrimonio
254
    oro
255
  end
256
257
  # TODO: Calculate profesion level and add rentas as param.
258
  def rentas
259
    profesion ? 1 : 0
260
  end
261
262
  def padre
263
    return nil unless progenitores
264
    papa = progenitores.first # Allways in the same position
265 View Code Duplication
    case papa
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
266
    when Fixnum then return { 'type' => 'pj',  'char' => hero(papa) }
267
    when String then return { 'type' => 'pnj', 'char' => papa }
268
    else return "Fallo de padre => #{papa.class}"
269
    end
270
  end
271
272
  def madre
273
    return nil unless progenitores
274
    return nil unless progenitores.count == 2
275
    mama = progenitores.last # Allways in the same position
276
    # Check class type, for polyform.
277 View Code Duplication
    case mama
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
278
    when Fixnum then return { 'type' => 'pj',  'char' => hero(mama) }
279
    when String then return { 'type' => 'pnj', 'char' => mama }
280
    else return "Fallo de madre => #{mama.class}"
281
    end
282
  end
283
284
  def descendientes
285
    padres = heros.map(&:progenitores) # nil values means no children
286
    # gets IDs of heroes which parents == self.ID
287
    hijos = padres.each_index.select do |i|
288
      padres[i].include?(id) unless padres[i].nil?
289
    end
290
    # Avoid empty arrays.
291
    hijos.empty? ? nil : hijos
292
  end
293
294
  def genderize
295
    # Word dictionary male vs female
296
    # TODO: some words are missing
297
    male   = %w(elfo mago bárbaro clérigo ladrón  rakshasa tiefling paladín  sacerdote enano gigante)
298
    female = %w(elfa maga bárbara clériga ladrona rakshasi tieflina paladina sacerdotisa enana giganta)
299
    # Returns char class, regarding the gender (only for females)
300
    gender == 'female' ? female[male.index(clase)] : clase
301
  end
302
end
303