Completed
Push — master ( eaaae9...e25d73 )
by Borja
03:20
created

Renegado.proteccions()   A

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2.5

Importance

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