| Total Complexity | 90 |
| Total Lines | 304 |
| Duplicated Lines | 4.61 % |
| Coverage | 33.53% |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Hero often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Main app class |
||
| 2 | 1 | class Hero < Hash |
|
|
|
|||
| 3 | 1 | attr_accessor :id, :name, :nivel, |
|
| 4 | :personaje, :jugador, :status, :muerto, :gender, |
||
| 5 | :repu, :cuerpo, :mente, :mov, :historia, :premio, |
||
| 6 | :familiar, :mounts, :descendencia, :pareja, :progenitores, |
||
| 7 | :hechizos, :shadows, :blood, :sand, :ice, :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 | 92690 | args.each do |k, v| |
|
| 14 | 1553162 | 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... |
|
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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 |
| 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 |
| 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 clase |
|
| 95 | clases.select { |_c, ps| ps.include?(personaje) }.keys.first |
||
| 96 | end |
||
| 97 | |||
| 98 | 1 | def hab_base |
|
| 99 | habilidad_base(clase) |
||
| 100 | end |
||
| 101 | |||
| 102 | 1 | def lista_status(view) |
|
| 103 | case view |
||
| 104 | when 'licenciados' then 'retirado' |
||
| 105 | when 'heroes' then 'activo' |
||
| 106 | when 'ausentes' then 'ausente' |
||
| 107 | when 'reservistas' then 'reserva' |
||
| 108 | when 'extranjeros' then 'extranjero' |
||
| 109 | end |
||
| 110 | end |
||
| 111 | |||
| 112 | 1 | def elementos |
|
| 113 | elementos = [] |
||
| 114 | elementos = magias.map(&:elemento).uniq if magias |
||
| 115 | elementos << 'sombras' if shadows |
||
| 116 | elementos << 'sangre' if blood |
||
| 117 | elementos << 'arena' if sand |
||
| 118 | elementos |
||
| 119 | end |
||
| 120 | |||
| 121 | 1 | def img_path |
|
| 122 | "'../images/personajes/#{genderize}.png'" |
||
| 123 | end |
||
| 124 | |||
| 125 | 1 | def big_path |
|
| 126 | 115 | "'../../images/portraits/#{name}.png'" |
|
| 127 | end |
||
| 128 | |||
| 129 | 1 | def reputacion |
|
| 130 | 115 | repu || 0 |
|
| 131 | end |
||
| 132 | |||
| 133 | 1 | def movimiento |
|
| 134 | mov |
||
| 135 | end |
||
| 136 | |||
| 137 | 1 | def raza |
|
| 138 | %w(clérigo ladrón bárbaro mago).include?(clase) ? 'humano' : clase |
||
| 139 | end |
||
| 140 | |||
| 141 | 1 | def female? |
|
| 142 | sex == 'female' |
||
| 143 | end |
||
| 144 | |||
| 145 | 1 | def male? |
|
| 146 | sex == 'male' |
||
| 147 | end |
||
| 148 | |||
| 149 | 1 | def anillos |
|
| 150 | (baratijas || []).select { |m| m.fits == 'anillo' } |
||
| 151 | end |
||
| 152 | |||
| 153 | 1 | def amuletos |
|
| 154 | (baratijas || []).select { |m| m.fits == 'amuleto' } |
||
| 155 | end |
||
| 156 | |||
| 157 | 1 | def ataque |
|
| 158 | weapons.first.categoria != 'distancia' ? weapons.first.ataque : 0 |
||
| 159 | end |
||
| 160 | |||
| 161 | 1 | def rango |
|
| 162 | weapons.first.categoria == 'distancia' ? weapons.first.ataque : 0 |
||
| 163 | end |
||
| 164 | |||
| 165 | 1 | def defensa |
|
| 166 | armour.defensa |
||
| 167 | end |
||
| 168 | |||
| 169 | 1 | def pet |
|
| 170 | Pet.new(familiar) if familiar |
||
| 171 | end |
||
| 172 | |||
| 173 | 1 | def gremio |
|
| 174 | prof = profesions.find { |p| p.id == profesion['id'] } |
||
| 175 | Profesion.new(profesion.merge(name: prof.name)) |
||
| 176 | end |
||
| 177 | |||
| 178 | 1 | def baratijas |
|
| 179 | miscelaneas.map { |m| Miscelanea.new(m) } if miscelaneas |
||
| 180 | end |
||
| 181 | |||
| 182 | 1 | def proteccions |
|
| 183 | protecciones.map { |p| Proteccion.new(p) } if protecciones |
||
| 184 | end |
||
| 185 | |||
| 186 | 1 | def trinkets |
|
| 187 | abalorios.map { |a| Abalorio.new(a) } if abalorios |
||
| 188 | end |
||
| 189 | |||
| 190 | 1 | def pergs |
|
| 191 | pergaminos.map { |p| Pergamino.new(p) } if pergaminos |
||
| 192 | end |
||
| 193 | |||
| 194 | 1 | def cacharros |
|
| 195 | piezas.map { |num| Pieza.new(id: num) } if piezas |
||
| 196 | end |
||
| 197 | |||
| 198 | 1 | def brebajes |
|
| 199 | pociones.map { |num| Pocion.new(id: num) } if pociones |
||
| 200 | end |
||
| 201 | |||
| 202 | 1 | def componentes |
|
| 203 | materiales.map { |num| Material.new(id: num) } if materiales |
||
| 204 | end |
||
| 205 | |||
| 206 | 1 | def transportes |
|
| 207 | mounts.map { |num| Montura.new(montura(num)) } if mounts |
||
| 208 | end |
||
| 209 | |||
| 210 | 1 | def masters |
|
| 211 | master.map { |num| Habilidad.new(maestrodearma(num)) } if master |
||
| 212 | end |
||
| 213 | |||
| 214 | 1 | def habilidades |
|
| 215 | if skills |
||
| 216 | skills.map do |num| |
||
| 217 | p = personaje.gsub('señor de las bestias', 'beastslord') |
||
| 218 | Habilidad.new(send(p, num)) |
||
| 219 | end |
||
| 220 | end |
||
| 221 | end |
||
| 222 | |||
| 223 | 1 | def magias |
|
| 224 | hechizos.map { |num| spell(num) } if hechizos |
||
| 225 | end |
||
| 226 | |||
| 227 | 1 | def blood_magic |
|
| 228 | blood.map { |num| sangre(num) } if blood |
||
| 229 | end |
||
| 230 | |||
| 231 | 1 | def shadow_magic |
|
| 232 | shadows.map { |num| sombra(num) } if shadows |
||
| 233 | end |
||
| 234 | |||
| 235 | 1 | def sand_magic |
|
| 236 | arenas.map { |num| arena(num) } if sand |
||
| 237 | end |
||
| 238 | |||
| 239 | 1 | def sin_recursos |
|
| 240 | tesoro.nil? |
||
| 241 | end |
||
| 242 | |||
| 243 | 1 | def empadronado |
|
| 244 | ciudad || 'Jadessvärd' |
||
| 245 | end |
||
| 246 | |||
| 247 | 1 | def estado |
|
| 248 | empadronado == 'Jadessvärd' ? (status || 'ausente') : 'extranjero' |
||
| 249 | end |
||
| 250 | |||
| 251 | # inventario |
||
| 252 | 1 | def capacidad |
|
| 253 | nivel / 3 + 3 |
||
| 254 | end |
||
| 255 | |||
| 256 | 1 | def patrimonio |
|
| 257 | oro |
||
| 258 | end |
||
| 259 | |||
| 260 | # TODO: Calculate profesion level and add rentas as param. |
||
| 261 | 1 | def rentas |
|
| 262 | profesion ? 1 : 0 |
||
| 263 | end |
||
| 264 | |||
| 265 | 1 | def padre |
|
| 266 | return nil unless progenitores |
||
| 267 | papa = progenitores.first # Allways in the same position |
||
| 268 | case papa |
||
| 269 | when Fixnum then return { 'type' => 'pj', 'char' => hero(papa) } |
||
| 270 | when String then return { 'type' => 'pnj', 'char' => papa } |
||
| 271 | else return "Fallo de padre => #{papa.class}" |
||
| 272 | end |
||
| 273 | end |
||
| 274 | |||
| 275 | 1 | def madre |
|
| 276 | return nil unless progenitores |
||
| 277 | return nil unless progenitores.count == 2 |
||
| 278 | mama = progenitores.last # Allways in the same position |
||
| 279 | # Check class type, for polyform. |
||
| 280 | case mama |
||
| 281 | when Fixnum then return { 'type' => 'pj', 'char' => hero(mama) } |
||
| 282 | when String then return { 'type' => 'pnj', 'char' => mama } |
||
| 283 | else return "Fallo de madre => #{mama.class}" |
||
| 284 | end |
||
| 285 | end |
||
| 286 | |||
| 287 | 1 | def descendientes |
|
| 288 | padres = heros.map(&:progenitores) # nil values means no children |
||
| 289 | # gets IDs of heroes which parents == self.ID |
||
| 290 | hijos = padres.each_index.select do |i| |
||
| 291 | padres[i].include?(id) unless padres[i].nil? |
||
| 292 | end |
||
| 293 | # Avoid empty arrays. |
||
| 294 | hijos.empty? ? nil : hijos |
||
| 295 | end |
||
| 296 | |||
| 297 | 1 | def genderize |
|
| 298 | # Word dictionary male vs female |
||
| 299 | # TODO: some words are missing |
||
| 300 | male = %w(elfo mago bárbaro clérigo ladrón rakshasa tiefling paladín sacerdote) |
||
| 301 | female = %w(elfa maga bárbara clériga ladrona rakshasi tieflina paladina sacerdotisa) |
||
| 302 | # Returns char class, regarding the gender (only for females) |
||
| 303 | gender == 'female' ? female[male.index(clase)] : clase |
||
| 304 | end |
||
| 305 | end |
||
| 306 |