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

CardPokemonComponent.doFullReset   D

Complexity

Conditions 12

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 18
dl 0
loc 21
rs 4.8
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like CardPokemonComponent.doFullReset 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 { OnInit, Output, EventEmitter } from '@angular/core';
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
import { Component, Input } from '@angular/core';
20
import { PokemonParty } from '../../data/savefile-expanded/fragments/PokemonParty';
21
import {GameDataService} from '../../data/gameData.service';
22
import {PokemonDBService} from '../../data/pokemonDB.service';
23
import { MatSliderChange } from '@angular/material';
24
import { SaveFileService } from '../../data/savefile.service';
25
import {MatSnackBar} from '@angular/material';
26
27
// @ts-ignore
28
const _ = window.require("lodash");
29
30
@Component({
31
    selector: 'card-pokemon',
32
    templateUrl: './card-pokemon.component.pug',
33
    styleUrls: ['./card-pokemon.component.scss'],
34
})
35
export class CardPokemonComponent implements OnInit {
36
37
    constructor(
38
        public gd: GameDataService,
39
        public pdb: PokemonDBService,
40
        public file: SaveFileService,
41
        private snackBar: MatSnackBar
42
    ) {
43
44
    }
45
46
    ngOnInit() {}
47
48
    notify(message: string) {
49
        this.snackBar.open(message, '', {
50
            duration: 2 * 1000,
51
        });
52
    }
53
54
    public onSpeciesChange() {
55
        this.updateData();
56
57
        const species = this.pdb.pokemon[this.entry.species];
58
        if(species == undefined || species.name == undefined)
59
            return;
60
61
        if(species.type1 !== undefined)
62
            this.entry.type1 = species.type1.ind;
63
64
        if(species.type2 !== undefined)
65
            this.entry.type2 = species.type2.ind;
66
67
        if(this.entry.type1 == this.entry.type2)
68
            this.entry.type2 = 0xFF;
69
70
        if(species.catchRate !== undefined)
71
            this.entry.catchRate = species.catchRate;
72
73
        const nickname = this.entry.nickname;
74
        let changeNick = false;
75
76
        if(nickname != "") {
77
            for(let i = 0; i <= this.pdb.rawPokemon.length; i++) {
78
                const pkmnEntry = this.pdb.pokemon[i];
79
                if(pkmnEntry == undefined || pkmnEntry.name == undefined)
80
                    continue;
81
    
82
                let pkmnEntryName = pkmnEntry.name.toUpperCase();
83
                if(pkmnEntryName == "NIDORAN<F>")
84
                    pkmnEntryName = "NIDORAN<f>"
85
                else if(pkmnEntryName == "NIDORAN<M>")
86
                    pkmnEntryName = "NIDORAN<m>"
87
    
88
                if(nickname == pkmnEntryName) {
89
                    changeNick = true;
90
                    break;
91
                }
92
            }
93
        }
94
        else
95
            changeNick = true;
96
        
97
        if(!changeNick)
98
            return;
99
100
        this.entry.nickname = species.name.toUpperCase();
101
        if(this.entry.nickname == "NIDORAN<F>")
102
            this.entry.nickname = "NIDORAN<f>"
103
        else if(this.entry.nickname == "NIDORAN<M>")
104
            this.entry.nickname = "NIDORAN<m>"
105
    }
106
107
    public updateData() {
108
        this.entry.updateExp();
109
110
        if(this.entry.updateStats)
111
            this.entry.updateStats();
112
113
        if(this.entry.hp > (this.entry.maxHP || this.entry.hpStat))
114
            this.entry.hp = (this.entry.maxHP || this.entry.hpStat);
115
    }
116
117
    public updateHP(event: MatSliderChange) {
118
        this.entry.hp = event.value;
119
    }
120
121
    public setScreen(name: string) {
122
        this.screen = name;
123
    }
124
125
    public maxPP(moveId: number, move: any) {
126
        const moveEntry = this.pdb.moves[moveId];
127
        if(moveEntry == undefined)
128
            return "??";
129
130
        const pp = moveEntry.pp;
131
        if(pp == undefined)
132
            return "??";
133
134
        const ppUp = move.ppUp;
135
        const ppUpPercent = (ppUp * 0.2) + 1;
136
137
        return Math.floor(pp * ppUpPercent);
138
    }
139
140
    onPpUpChange(move: any) {
141
        const maxPP = this.maxPP(move.moveID, move);
142
        if(maxPP == "??")
143
            return;
144
145
        if(move.pp > maxPP)
146
            move.pp = maxPP;
147
    }
148
149
    onMoveChange(newVal: number, move: any) {
150
        const moveEntry = this.pdb.moves[newVal];
151
        if(moveEntry == undefined)
152
            return;
153
154
        if(moveEntry.glitch == true)
155
            return;
156
157
        const maxPP = this.maxPP(newVal, move);
158
        if(maxPP == "??")
159
            return;
160
161
        if(move.pp > maxPP)
162
            move.pp = maxPP;
163
    }
164
165
    get isMaxLevel() {
166
        return this.entry.level >= 100;
167
    }
168
169
    doMaxLevel() {
170
        this.entry.level = 100;
171
        this.updateData();
172
    }
173
174
    get isHealed() {
175
        const hpHealed = (this.entry.hp == (this.entry.maxHP || this.entry.hpStat));
176
        const statusHealed = this.entry.status == 0;
177
        let ppFull = true;
178
        for(let i = 0; i < this.entry.moves.length; i++) {
179
            const maxPP = this.maxPP(this.entry.moves[i].moveID, this.entry.moves[i]);
180
            if(maxPP != "??") {
181
                if(this.entry.moves[i].pp != maxPP)
182
                    ppFull = false;
183
            }
184
        }
185
186
        return hpHealed && statusHealed && ppFull;
187
    }
188
189
    doHeal() {
190
        this.entry.hp = this.entry.maxHP || this.entry.hpStat;
191
        this.entry.status = 0;
192
        for(let i = 0; i < this.entry.moves.length; i++) {
193
            const maxPP = this.maxPP(this.entry.moves[i].moveID, this.entry.moves[i]);
194
            if(maxPP != "??")
195
                this.entry.moves[i].pp = maxPP;
196
        }
197
    }
198
199
    get isMaxPPUps() {
200
        let ppUpsFull = true;
201
        for(let i = 0; i < this.entry.moves.length; i++) {
202
            const maxPP = this.maxPP(this.entry.moves[i].moveID, this.entry.moves[i]);
203
            if(maxPP != "??") {
204
                if(this.entry.moves[i].ppUp < 3)
205
                    ppUpsFull = false;
206
            }
207
        }
208
209
        return ppUpsFull;
210
    }
211
212
    maxPPUps() {
213
        for(let i = 0; i < this.entry.moves.length; i++) {
214
            const maxPP = this.maxPP(this.entry.moves[i].moveID, this.entry.moves[i]);
215
            if(maxPP != "??")
216
                this.entry.moves[i].ppUp = 3;
217
        }
218
    }
219
220
    get isMaxEVs() {
221
        return this.entry.hpExp == 0xFFFF &&
222
            this.entry.attackExp == 0xFFFF &&
223
            this.entry.defenseExp == 0xFFFF &&
224
            this.entry.speedExp == 0xFFFF &&
225
            this.entry.specialExp == 0xFFFF;
226
    }
227
228
    doMaxEVs() {
229
        this.entry.hpExp = 0xFFFF;
230
        this.entry.attackExp = 0xFFFF;
231
        this.entry.defenseExp = 0xFFFF;
232
        this.entry.speedExp = 0xFFFF;
233
        this.entry.specialExp = 0xFFFF;
234
        this.updateData();
235
    }
236
237
    get isMinEVs() {
238
        return this.entry.hpExp == 0 &&
239
            this.entry.attackExp == 0 &&
240
            this.entry.defenseExp == 0 &&
241
            this.entry.speedExp == 0 &&
242
            this.entry.specialExp == 0;
243
    }
244
245
    doResetEVs() {
246
        this.entry.hpExp = 0;
247
        this.entry.attackExp = 0;
248
        this.entry.defenseExp = 0;
249
        this.entry.speedExp = 0;
250
        this.entry.specialExp = 0;
251
        this.updateData();
252
    }
253
254
    get isMaxDVs() {
255
        return this.entry.dv.attack == 15 &&
256
            this.entry.dv.defense == 15 &&
257
            this.entry.dv.speed == 15 &&
258
            this.entry.dv.special == 15;
259
    }
260
261
    doMaxDVs() {
262
        this.entry.dv.attack = 15;
263
        this.entry.dv.defense = 15;
264
        this.entry.dv.speed = 15;
265
        this.entry.dv.special = 15;
266
        this.updateData();
267
    }
268
269
    doReRollDVs() {
270
        this.entry.dv.attack = _.random(0, 15, false);
271
        this.entry.dv.defense = _.random(0, 15, false);
272
        this.entry.dv.speed = _.random(0, 15, false);
273
        this.entry.dv.special = _.random(0, 15, false);
274
        this.updateData();
275
    }
276
277
    get isFullyMaxed() {
278
        return this.isMaxLevel && 
279
            this.isHealed &&
280
            this.isMaxPPUps &&
281
            this.isMaxEVs &&
282
            this.isMaxDVs;
283
    }
284
285
    doFullyMaxed() {
286
        this.doMaxLevel();
287
        this.maxPPUps();
288
        this.doMaxEVs();
289
        this.doMaxDVs();
290
291
        this.doHeal();
292
    }
293
294
    get isFullyReset() {
295
296
        const monData = this.pdb.pokemon[this.entry.species];
297
        if(monData == undefined)
298
            return null;
299
300
        let movesReset = true;
301
        
302
        for(let i = 0; i < 4; i++) {
303
            if(monData.initial == undefined)
304
                continue;
305
306
            movesReset = this.entry.moves[i].moveID == (monData.initial[i]) ? monData.initial[i].ind : 0;
307
            if(!movesReset)
308
                break;
309
310
            movesReset = this.entry.moves[i].pp == 0;
311
            if(!movesReset)
312
                break;
313
314
            movesReset = this.entry.moves[i].ppUp == 0;
315
            if(!movesReset)
316
                break;
317
        }
318
319
        return this.entry.level == 5 && movesReset && this.isMinEVs && this.isHealed;
320
    }
321
322
    doFullReset() {
323
        this.entry.level = 5;
324
325
        const monData = this.pdb.pokemon[this.entry.species];
326
        if(monData == undefined)
327
            return;
328
        
329
        for(let i = 0; i < 4; i++) {
330
            if(monData.initial == undefined)
331
                continue;
332
333
            this.entry.moves[i].moveID = (monData.initial[i]) ? monData.initial[i].ind : 0;
334
            this.entry.moves[i].pp = 0;
335
            this.entry.moves[i].ppUp = 0;
336
        }
337
338
        this.doResetEVs();
339
340
        this.doHeal();
341
        this.updateData();
342
    }
343
344
    get isTradedMon() {
345
        return (this.entry.otID != this.file.fileDataExpanded.player.basics.playerID) ||
346
            (this.entry.otName != this.file.fileDataExpanded.player.basics.playerName)
347
    }
348
349
    makeTradeMon() {
350
        const names: string[] = this.gd.file("names").data;
351
        this.entry.otID = _.random(0, 0xFFFF, false).toString(16).toUpperCase().padStart(4, "0");
352
        this.entry.otName = names[_.random(0, names.length - 1, false)];
353
    }
354
355
    makeOwnMon() {
356
        this.entry.otID = this.file.fileDataExpanded.player.basics.playerID;
357
        this.entry.otName = this.file.fileDataExpanded.player.basics.playerName;
358
    }
359
360
    get isNicknamed() {
361
        const species = this.pdb.pokemon[this.entry.species];
362
        if(species == undefined || species.name == undefined)
363
            return null;
364
365
        let nickname = species.name.toUpperCase();
366
        if(nickname == "NIDORAN<F>")
367
            nickname = "NIDORAN<f>"
368
        else if(nickname == "NIDORAN<M>")
369
            nickname = "NIDORAN<m>"
370
371
        if(this.entry.nickname == nickname)
372
            return false;
373
        
374
        return true;
375
    }
376
377
    makeNickname() {
378
        const names: string[] = this.gd.file("names").data;
379
        this.entry.nickname = names[_.random(0, names.length - 1, false)];
380
    }
381
382
    makeOwnName() {
383
        const species = this.pdb.pokemon[this.entry.species];
384
        if(species == undefined || species.name == undefined)
385
            return;
386
387
        let nickname = species.name.toUpperCase();
388
        if(nickname == "NIDORAN<F>")
389
            nickname = "NIDORAN<f>"
390
        else if(nickname == "NIDORAN<M>")
391
            nickname = "NIDORAN<m>"
392
393
        this.entry.nickname = nickname;
394
    }
395
396
    get canEvolve() {
397
        const species = this.pdb.pokemon[this.entry.species];
398
        if(species == undefined || species.evolution == undefined)
399
            return false;
400
401
        return true;
402
    }
403
404
    doEvolve() {
405
        const species = this.pdb.pokemon[this.entry.species];
406
        if(species == undefined || species.evolution == undefined)
407
            return;
408
409
        this.entry.species = species.evolution.toName.ind;
410
        this.onSpeciesChange();
411
    }
412
413
    @Input()
414
    public entry: any = new PokemonParty();
415
416
    @Input()
417
    public disabled: boolean = false;
418
419
    @Input()
420
    public partyMon: boolean = true;
421
422
    @Output()
423
    public onWithdrawDeposit: EventEmitter<boolean> = new EventEmitter<boolean>();
424
425
    @Output()
426
    public rem: EventEmitter<any> = new EventEmitter<any>();
427
428
    // Current screen to display
429
    public screen: string = "basics/name";
430
}
431