Passed
Push — depfu/update/npm/lodash-4.17.1... ( 152c97 )
by
unknown
04:58
created

PokemonDBService.process2Pokemon   F

Complexity

Conditions 15

Size

Total Lines 63
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 50
dl 0
loc 63
rs 2.9998
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like PokemonDBService.process2Pokemon 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 { Injectable } from "@angular/core";
2
import { GameDataService } from "./gameData.service";
3
4
import { Item } from "../../assets/data/items";
5
import { Move } from "../../assets/data/moves";
6
import { Pokemon } from "../../assets/data/pokemon";
7
import { Type } from "../../assets/data/types";
8
9
/**
10
 * Files
11
 *
12
 * items
13
 * moves
14
 * pokemon
15
 * types
16
 */
17
18
// @ts-ignore
19
const _ = window.require("lodash");
20
21
@Injectable({
22
    providedIn: 'root'
23
})
24
export class PokemonDBService {
25
    constructor(
26
        public gd: GameDataService
27
    ) {
28
        // @ts-ignore
29
        window.pokemonDB = this;
30
31
        // Get Raw Data
32
        this.rawItems = this.gd.file("items").data;
33
        this.rawMoves = this.gd.file("moves").data;
34
        this.rawPokemon = this.gd.file("pokemon").data;
35
        this.rawTypes = this.gd.file("types").data;
36
37
        // Convert to index data by name in snake_case form
38
        this.process1(this.rawItems, this.items, "name");
39
        this.process1(this.rawMoves, this.moves, "name");
40
        this.process1(this.rawPokemon, this.pokemon, "name");
41
        this.process1(this.rawTypes, this.types, "name");
42
43
        // Add in aliases
44
        this.process1Alts(this.rawItems, this.items, "ind", "name");
45
        this.process1Alts(this.rawMoves, this.moves, "ind", "name");
46
        this.process1Alts(this.rawPokemon, this.pokemon, "ind", "name");
47
        this.process1Alts(this.rawTypes, this.types, "ind", "name");
48
49
        // Circular Link everything
50
        // Yes I'm aware of the dangers and pitfalls of circular linking
51
        // I'm choosing it anyways for now
52
        this.process2Items();
53
        this.process2Moves();
54
        this.process2Pokemon();
55
    }
56
57
    process1(from: any, to: any, key: any) {
58
        for(let i = 0; i < from.length; i++) {
59
            const entry = from[i];
60
            to[_.snakeCase(entry[key])] = _.cloneDeep(entry);
61
        }
62
    }
63
64
    process1Alts(from: any, to: any, key: any, origKey: any) {
65
        for(let i = 0; i < from.length; i++) {
66
            const entry = from[i];
67
            to[entry[key]] = to[_.snakeCase(entry[origKey])];
68
        }
69
    }
70
71
    process2Items() {
72
        const self = this;
73
74
        _.forOwn(this.items, function(value: any) {
75
            if(value.tm !== undefined && value._tm === undefined) {
76
                value._tm = value.tm;
77
                value.tm = _.find(self.moves, ['tm', value.tm]);
78
            }
79
            if(value.hm !== undefined && value._hm === undefined) {
80
                value._hm = value.hm;
81
                value.hm = _.find(self.moves, ['hm', value.hm]);
82
            }
83
        });
84
    }
85
86
    process2Moves() {
87
        const self = this;
88
89
        _.forOwn(this.moves, function(value: any) {
90
            if(value.type !== undefined && value._type === undefined) {
91
                value._type = value.type;
92
                value.type = _.find(self.types, ['name', _.startCase(_.lowerCase(value.type))]);
93
            }
94
            if(value.tm !== undefined && value._tm === undefined) {
95
                value._tm = value.tm;
96
                value.tm = _.find(self.items, ['_tm', value.tm]);
97
            }
98
            if(value.hm !== undefined && value._hm === undefined) {
99
                value._hm = value.hm;
100
                value.hm = _.find(self.items, ['_hm', value.hm]);
101
            }
102
        });
103
    }
104
105
    process2Pokemon() {
106
        const self = this;
107
108
        _.forOwn(this.pokemon, function(value: any) {
109
            if(value.moves !== undefined) {
110
                for(let i = 0; i < value.moves.length; i++) {
111
                    const move = value.moves[i];
112
                    if(move._move !== undefined)
113
                        continue;
114
115
                    move._move = move.move;
116
                    move.move = _.find(self.moves, ['name', _.startCase(_.lowerCase(move.move))]);
117
                }
118
            }
119
120
            if(value.initial !== undefined && value._initial === undefined) {
121
                value._initial = _.cloneDeep(value.initial);
122
                for(let i = 0; i < value.initial.length; i++) {
123
                    const initial = value.initial[i];
124
                    value.initial[i] = _.find(self.moves, ['name', _.startCase(_.lowerCase(initial))]);
125
                }
126
            }
127
128
            if(value.tmHm !== undefined && value._tmHm === undefined) {
129
                value._tmHm = _.cloneDeep(value.tmHm);
130
                for(let i = 0; i < value.tmHm.length; i++) {
131
                    const tmHmEntry = value.tmHm[i];
132
                    value.tmHm[i] = _.find(self.moves, ['_tm', tmHmEntry]);
133
                }
134
            }
135
136
            if(value.type1 !== undefined && value._type1 === undefined) {
137
                value._type1 = value.type1;
138
                value.type1 = _.find(self.types, ['name', _.startCase(_.lowerCase(value.type1))]);
139
            }
140
141
            if(value.type2 !== undefined && value._type2 === undefined) {
142
                value._type2 = value.type2;
143
                value.type2 = _.find(self.types, ['name', _.startCase(_.lowerCase(value.type2))]);
144
            }
145
146
            if(value.evolution !== undefined) {
147
                const process = (entry: any) => {
148
                    if(entry.item !== undefined && entry._item === undefined) {
149
                        entry._item = entry.item;
150
                        entry.item = _.find(self.items, ['name', _.startCase(_.lowerCase(entry.item))]);
151
                    }
152
153
                    if(entry._toName !== undefined)
154
                        return;
155
156
                    entry._toName = entry.toName;
157
                    entry.toName = _.find(self.pokemon, ['name', _.startCase(_.lowerCase(entry.toName))]);
158
                }
159
160
                if(Array.isArray(value.evolution)) {
161
                    for(let i = 0; i < value.evolution.length; i++) {
162
                        process(value.evolution[i]);
163
                    }
164
                }
165
                else
166
                    process(value.evolution);
167
            }
168
        });
169
    }
170
171
    public rawItems: Item[];
172
    public rawMoves: Move[];
173
    public rawPokemon: Pokemon[];
174
    public rawTypes: Type[];
175
176
    public items: {
177
        [key: string]: any
178
    } = {};
179
180
    public moves: {
181
        [key: string]: any
182
    } = {};
183
184
    public pokemon: {
185
        [key: string]: any
186
    } = {};
187
188
    public types: {
189
        [key: string]: any
190
    } = {};
191
}
192