Conditions | 15 |
Total Lines | 77 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like TextService.convertEngToHTML 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 | import { TextSearch } from './text/TextSearch'; |
||
151 | |||
152 | // Converts an english format string to code represented as how it would be |
||
153 | // in-game |
||
154 | public convertEngToHTML(msg: string, maxChars: number, rival: string = "BLUE", player: string = "RED") { |
||
155 | // Convert string to char codes |
||
156 | let charCodes = Array.from(this.convertToCode(msg, maxChars, false)); |
||
157 | |||
158 | // Pre-pass |
||
159 | for (let i = 0; i < charCodes.length; i++) { |
||
160 | const char = charCodes[i]; |
||
161 | // <pkmn> |
||
162 | if (char === 0x4A) { |
||
163 | charCodes.splice(i, 1, 0xE1, 0xE2); |
||
164 | } |
||
165 | // <player> |
||
166 | else if (char === 0x52) { |
||
167 | const playerName = Array.from(this.convertToCode(player, 7, false)); |
||
168 | charCodes.splice(i, 1, ...playerName); |
||
169 | } |
||
170 | // <rival> |
||
171 | else if (char === 0x53) { |
||
172 | const rivalName = Array.from(this.convertToCode(rival, 7, false)); |
||
173 | charCodes.splice(i, 1, ...rivalName); |
||
174 | } |
||
175 | // POK<e> |
||
176 | else if (char === 0x54) { |
||
177 | const str = Array.from(this.convertToCode("POK<e>", 10, false)); |
||
178 | charCodes.splice(i, 1, ...str); |
||
179 | } |
||
180 | // <......> |
||
181 | else if (char === 0x56) { |
||
182 | const str = Array.from(this.convertToCode("<...><...>", 10, false)); |
||
183 | charCodes.splice(i, 1, ...str); |
||
184 | } |
||
185 | // <targ> |
||
186 | else if (char === 0x59) { |
||
187 | const str = Array.from(this.convertToCode("CHARIZARD", 100, false)); |
||
188 | charCodes.splice(i, 1, ...str); |
||
189 | } |
||
190 | // <user> |
||
191 | else if (char === 0x5A) { |
||
192 | const str = Array.from(this.convertToCode("Enemy BLASTOISE", 100, false)); |
||
193 | charCodes.splice(i, 1, ...str); |
||
194 | } |
||
195 | // <pc> |
||
196 | else if (char === 0x5B) { |
||
197 | const str = Array.from(this.convertToCode("PC", 100, false)); |
||
198 | charCodes.splice(i, 1, ...str); |
||
199 | } |
||
200 | // <tm> |
||
201 | else if (char === 0x5C) { |
||
202 | const str = Array.from(this.convertToCode("TM", 100, false)); |
||
203 | charCodes.splice(i, 1, ...str); |
||
204 | } |
||
205 | // <trainer> |
||
206 | else if (char === 0x5D) { |
||
207 | const str = Array.from(this.convertToCode("TRAINER", 100, false)); |
||
208 | charCodes.splice(i, 1, ...str); |
||
209 | } |
||
210 | // <rocket> |
||
211 | else if (char === 0x5E) { |
||
212 | const str = Array.from(this.convertToCode("ROCKET", 100, false)); |
||
213 | charCodes.splice(i, 1, ...str); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | const fontStr = []; |
||
218 | for (let i = 0; i < charCodes.length; i++) { |
||
219 | const char = charCodes[i]; |
||
220 | |||
221 | if (this.indToEng[char].useTilemap) |
||
222 | fontStr.push(`<div class="pr pr-pic pr-${char.toString(16).toUpperCase().padStart(2, "0")}"></div>`); |
||
223 | else |
||
224 | fontStr.push(`<div class="pr pr-${char.toString(16).toUpperCase().padStart(2, "0")}"></div>`); |
||
225 | } |
||
226 | |||
227 | return fontStr.join(''); |
||
228 | } |
||
243 |