Total Complexity | 62 |
Total Lines | 239 |
Duplicated Lines | 5.86 % |
Coverage | 32.27% |
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 Renegado 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 Renegado < Hash |
|
|
|||
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... |
|
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 hab_base |
|
95 | habilidad_base(clase) |
||
96 | end |
||
97 | |||
98 | 1 | def elementos |
|
99 | elementos = [] |
||
100 | elementos = magias.map(&:elemento).uniq if magias |
||
101 | elementos << 'sombras' if shadows |
||
102 | elementos << 'sangre' if blood |
||
103 | elementos |
||
104 | end |
||
105 | |||
106 | 1 | def img_path |
|
107 | "'../images/revenge/personajes/#{genderize}.png'" |
||
108 | end |
||
109 | |||
110 | 1 | def big_path |
|
111 | "'../../images/revenge/portraits/#{name}.png'" |
||
112 | end |
||
113 | |||
114 | 1 | def reputacion |
|
115 | repu || 0 |
||
116 | end |
||
117 | |||
118 | 1 | def movimiento |
|
119 | mov |
||
120 | end |
||
121 | |||
122 | 1 | def female? |
|
123 | sex == 'female' |
||
124 | end |
||
125 | |||
126 | 1 | def male? |
|
127 | sex == 'male' |
||
128 | end |
||
129 | |||
130 | 1 | def anillos |
|
131 | (baratijas || []).select { |m| m.fits == 'anillo' } |
||
132 | end |
||
133 | |||
134 | 1 | def amuletos |
|
135 | (baratijas || []).select { |m| m.fits == 'amuleto' } |
||
136 | end |
||
137 | |||
138 | 1 | def ataque |
|
139 | weapons.first.categoria != 'distancia' ? weapons.first.ataque : 0 |
||
140 | end |
||
141 | |||
142 | 1 | def rango |
|
143 | weapons.first.categoria == 'distancia' ? weapons.first.ataque : 0 |
||
144 | end |
||
145 | |||
146 | 1 | def defensa |
|
147 | armour.defensa |
||
148 | end |
||
149 | |||
150 | 1 | def pet |
|
151 | Pet.new(familiar) if familiar |
||
152 | end |
||
153 | |||
154 | 1 | def gremio |
|
155 | prof = profesions.find { |p| p.id == profesion['id'] } |
||
156 | Profesion.new(profesion.merge(name: prof.name)) |
||
157 | end |
||
158 | |||
159 | 1 | def baratijas |
|
160 | miscelaneas.map { |m| Miscelanea.new(m) } if miscelaneas |
||
161 | end |
||
162 | |||
163 | 1 | def proteccions |
|
164 | protecciones.map { |p| Proteccion.new(p) } if protecciones |
||
165 | end |
||
166 | |||
167 | 1 | def trinkets |
|
168 | abalorios.map { |a| Abalorio.new(a) } if abalorios |
||
169 | end |
||
170 | |||
171 | 1 | def pergs |
|
172 | pergaminos.map { |p| Pergamino.new(p) } if pergaminos |
||
173 | end |
||
174 | |||
175 | 1 | def cacharros |
|
176 | piezas.map { |num| Pieza.new(id: num) } if piezas |
||
177 | end |
||
178 | |||
179 | 1 | def brebajes |
|
180 | pociones.map { |num| Pocion.new(id: num) } if pociones |
||
181 | end |
||
182 | |||
183 | 1 | def componentes |
|
184 | materiales.map { |num| Material.new(id: num) } if materiales |
||
185 | end |
||
186 | |||
187 | 1 | def transportes |
|
188 | mounts.map { |num| Montura.new(montura(num)) } if mounts |
||
189 | end |
||
190 | |||
191 | 1 | def masters |
|
192 | master.map { |num| Habilidad.new(maestrodearma(num)) } if master |
||
193 | end |
||
194 | |||
195 | 1 | def habilidades |
|
196 | if skills |
||
197 | skills.map do |num| |
||
198 | p = personaje |
||
199 | Habilidad.new(send(p, num)) |
||
200 | end |
||
201 | end |
||
202 | end |
||
203 | |||
204 | 1 | def magias |
|
205 | hechizos.map { |num| spell(num) } if hechizos |
||
206 | end |
||
207 | |||
208 | 1 | def blood_magic |
|
209 | blood.map { |num| sangre(num) } if blood |
||
210 | end |
||
211 | |||
212 | 1 | def shadow_magic |
|
213 | shadows.map { |num| sombra(num) } if shadows |
||
214 | end |
||
215 | |||
216 | 1 | def empadronado |
|
217 | ciudad || 'Revenge' |
||
218 | end |
||
219 | |||
220 | 1 | def estado |
|
221 | 'renegado' |
||
222 | end |
||
223 | |||
224 | # inventario |
||
225 | 1 | def capacidad |
|
226 | nivel / 3 + 3 |
||
227 | end |
||
228 | |||
229 | 1 | def genderize |
|
230 | # Word dictionary male vs female |
||
231 | # TODO: some words are missing |
||
232 | male = %w(nomuerto orco goblin drow ungor) |
||
233 | female = %w(nomuerta orca goblina elfa sátira) |
||
234 | puts name |
||
235 | puts 'raza: ' + raza |
||
236 | puts 'clase: ' + clase |
||
237 | # Returns raza, regarding the gender (only for females) |
||
238 | gender == 'female' ? female[male.index(raza)] : raza |
||
239 | end |
||
240 | end |
||
241 |