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

MoreEventPokemon.genPokemonAndGive   F

Complexity

Conditions 17

Size

Total Lines 88
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 75
dl 0
loc 88
rs 1.8
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 MoreEventPokemon.genPokemonAndGive 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 { GameDataService } from './../../data/gameData.service';
2
3
/**
4
   Copyright 2018 June Hanabi
5
6
   Licensed under the Apache License, Version 2.0 (the "License");
7
   you may not use this file except in compliance with the License.
8
   You may obtain a copy of the License at
9
10
       http://www.apache.org/licenses/LICENSE-2.0
11
12
   Unless required by applicable law or agreed to in writing, software
13
   distributed under the License is distributed on an "AS IS" BASIS,
14
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
   See the License for the specific language governing permissions and
16
   limitations under the License.
17
 */
18
19
 // @ts-ignore
20
const _: any = window.require("lodash");
21
22
import { Component, OnInit } from '@angular/core';
23
import { SaveFileService } from "../../data/savefile.service";
24
import { PokemonParty } from 'src/app/data/savefile-expanded/fragments/PokemonParty';
25
import {MatSnackBar} from '@angular/material';
26
import { EventPokemon } from 'src/assets/data/eventPokemon';
27
import { Pokemon } from 'src/assets/data/pokemon';
28
import { Move } from 'src/assets/data/moves';
29
30
@Component({
31
    selector: 'more-event-pokemon',
32
    templateUrl: './more-event-pokemon.component.pug',
33
    styleUrls: ['./more-event-pokemon.component.scss'],
34
})
35
export class MoreEventPokemon implements OnInit {
36
37
    constructor(
38
        public fileService: SaveFileService,
39
        public gd: GameDataService,
40
        private snackBar: MatSnackBar
41
    ) {}
42
43
    ngOnInit() {}
44
45
    notify(message: string) {
46
        this.snackBar.open(message, '', {
47
            duration: 2 * 1000,
48
        });
49
    }
50
51
    givePokemon(pkmn: PokemonParty): void {
52
        pkmn.updateExp();
53
        pkmn.updateStats();
54
        pkmn.hp = pkmn.hpStat;
55
56
        const f = this.fileService.fileDataExpanded;
57
        const curBox = f.storage.curBox;
58
59
        if(f.player.pokemon.playerParty.length < 6) {
60
            f.player.pokemon.playerParty.push(pkmn);
61
            this.notify("Event Pokemon was sent to your party");
62
        }
63
        else if(f.storage.pokemonBoxes[curBox].length < 20) {
64
            PokemonParty.convertToPokemonBox(pkmn);
65
            f.storage.pokemonBoxes[curBox].push(pkmn);
66
            this.notify("Event Pokemon was sent to your current box");
67
        }
68
        else {
69
            this.notify("No space in party or your current box");
70
        }
71
    }
72
73
    // Random integer inclusive to min and max
74
    getRandomInt(min: number, max: number): number {
75
        min = Math.ceil(min);
76
        max = Math.floor(max);
77
        return Math.floor(Math.random() * (max - min + 1)) + min;
78
    }
79
80
    // Gets a blank Pokemon with random DV's and OTID
81
    get pkmn() {
82
        const pkmn = new PokemonParty();
83
        pkmn.otID = Number(this.getRandomInt(0, 65535)).toString(16).padStart(4, "0").toUpperCase();
84
        pkmn.dv.attack = this.getRandomInt(0, 15);
85
        pkmn.dv.defense = this.getRandomInt(0, 15);
86
        pkmn.dv.speed = this.getRandomInt(0, 15);
87
        pkmn.dv.special = this.getRandomInt(0, 15);
88
        return pkmn;
89
    }
90
91
    // Get all events
92
    get rawEvents(): EventPokemon[] {
93
        return this.fileService.gd.file("eventPokemon").data;
94
    }
95
96
    // Get all Japanese events
97
    get jpRawEvents(): EventPokemon[] {
98
        return _.filter(this.rawEvents, ['region', 'Japan']);
99
    }
100
101
    // Get all European events
102
    get euRawEvents(): EventPokemon[] {
103
        return _.filter(this.rawEvents, ['region', 'Europe']);
104
    }
105
106
    // Get all types
107
    get rawTypes(): {
108
        name: string,
109
        ind: number
110
    }[] {
111
        return this.fileService.gd.file("types").data;
112
    }
113
114
    // Get all moves
115
    get rawMoves(): {
116
        name: string,
117
        ind: number,
118
        glitch?: true
119
    }[] {
120
        return this.fileService.gd.file("moves").data;
121
    }
122
123
    getType(typ: string) : number | null {
124
        const allTypes = this.rawTypes;
125
126
        for(let i = 0; i < allTypes.length; i++) {
127
            const typeEntry = allTypes[i];
128
            if(typeEntry.name.toUpperCase() == typ.toUpperCase())
129
                return typeEntry.ind;
130
        }
131
132
        return null;
133
    }
134
135
    getMove(move: string) : Move | null {
136
        const allMoves = this.rawMoves;
137
138
        for(let i = 0; i < allMoves.length; i++) {
139
            const moveEntry = allMoves[i];
140
            if(moveEntry.name.toUpperCase() == move.toUpperCase())
141
                return moveEntry;
142
        }
143
144
        return null;
145
    }
146
147
    // List tracking
148
    entriesTracking(index: number) {
149
        return index;
150
    }
151
152
    findPokemonRecord(name: String): Pokemon | null {
153
        const allPokemon = this.fileService.gd.file("pokemon").data as Pokemon[];
154
155
        for(let i = 0; i < allPokemon.length; i++) {
156
            const pkmnEntry = allPokemon[i];
157
            if(pkmnEntry.name == name)
158
                return pkmnEntry;
159
        }
160
161
        return null;
162
    }
163
164
    convertCase(str: string) {
165
        let newCase: string = _.startCase(_.lowerCase(str));
166
167
        if(newCase == "Mrmime")
168
            newCase = "Mr.Mime";
169
        else if(newCase == "Nidoranf")
170
            newCase = "Nidoran<f>";
171
        else if(newCase == "Nidoranm")
172
            newCase = "Nidoran<m>";
173
174
        return newCase;
175
    }
176
177
    // Gives the Pokemon
178
    genPokemonAndGive(entry: EventPokemon) {
179
        // Get a blank Pokemon with random DV's and OT ID
180
        const pkmn = this.pkmn;
181
182
        // Get Pokemon Record
183
        const nameRecord = this.findPokemonRecord(entry.pokemon);
184
        if(nameRecord == null)
185
            return;
186
187
        // Assign Species Number
188
        pkmn.species = nameRecord.ind;
189
190
        if(nameRecord.catchRate !== undefined)
191
            pkmn.catchRate = nameRecord.catchRate;
192
193
        // Assign Types 1 & 2
194
        if(nameRecord.type1 === undefined || nameRecord.type2 === undefined)
195
            return;
196
197
        let typeX = this.getType(nameRecord.type1);
198
        if(typeX === null)
199
            return;
200
        
201
        pkmn.type1 = typeX;
202
203
        typeX = this.getType(nameRecord.type2);
204
        if(typeX === null)
205
            return;
206
207
        pkmn.type2 = typeX;
208
209
        if(pkmn.type1 == pkmn.type2)
210
            pkmn.type2 = 0xFF;
211
212
        // Assign OT Name
213
        let otName = entry.otName;
214
        if(Array.isArray(entry.otName))
215
            otName = otName[this.getRandomInt(0, otName.length - 1)];
216
217
        // Handled Above
218
        // @ts-ignore
219
        pkmn.otName = otName;
220
221
        // OT ID if present, random otherwise
222
        if(entry.otID !== undefined)
223
            pkmn.otID = entry.otID;
224
225
        if(entry.dv != undefined && entry.dv == "max") {
226
            pkmn.dv.attack = 15;
227
            pkmn.dv.defense = 15;
228
            pkmn.dv.special = 15;
229
            pkmn.dv.speed = 15;
230
        }
231
        else if(entry.dv != undefined && entry.dv.startsWith(":")) {
232
            const dvs = entry.dv.split(":");
233
            dvs.shift();
234
235
            pkmn.dv.attack = parseInt(dvs[0]);
236
            pkmn.dv.defense = parseInt(dvs[1]);
237
            pkmn.dv.speed = parseInt(dvs[2]);
238
            pkmn.dv.special = parseInt(dvs[3]);
239
        }
240
241
        // Assign Nickname
242
        pkmn.nickname = nameRecord.name.toUpperCase();
243
244
        if(pkmn.nickname == "NIDORAN<M>")
245
            pkmn.nickname = "NIDORAN<m>";
246
        else if(pkmn.nickname == "NIDORAN<F>")
247
            pkmn.nickname = "NIDORAN<f>";
248
249
        // Assign Level
250
        pkmn.level = (entry.level) ? entry.level : 5;
251
252
        for(let i = 0; i < entry.moves.length; i++) {
253
            const move = entry.moves[i];
254
            const moveData = this.getMove(move);
255
            if(moveData == null)
256
                return;
257
258
            pkmn.moves[i].moveID = moveData.ind;
259
            if(moveData.pp !== undefined)
260
                pkmn.moves[i].pp = moveData.pp;
261
        }
262
263
        this.givePokemon(pkmn);
264
    }
265
}
266