Passed
Pull Request — master (#22)
by
unknown
08:10 queued 04:20
created

src/app/data/savefile-expanded/fragments/PokemonParty.ts   A

Complexity

Total Complexity 7
Complexity/F 1.4

Size

Lines of Code 116
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 92
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 7
mnd 2
bc 2
fnc 5
bpm 0.4
cpm 1.4
noi 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A PokemonParty.load 0 24 1
A PokemonParty.updateStats 0 12 2
A PokemonParty.convertToPokemonParty 0 6 2
A PokemonParty.convertToPokemonBox 0 9 1
A PokemonParty.save 0 24 1
1
import { SaveFileIterator } from './../SaveFileIterator';
2
import { SaveFileService } from './../../savefile.service';
3
import { PokemonBox } from './PokemonBox';
4
5
export class PokemonParty extends PokemonBox {
6
    constructor(saveFile?: SaveFileService,
7
        offset?: number,
8
        nicknameStartOffset?: number,
9
        otNameStartOffset?: number,
10
        index?: number) {
11
12
        // Due to an annoying attribute of typescript compiling variable
13
        // declarations below the super call thus resetting child class to default
14
        // after child and parent class have been initialized properly
15
        // we have to work around this annoyance by initiating a blank parent
16
        // and typescripts injected code for a blank child followed by the actual
17
        // loading process handled by the child and delegated manually to the parent
18
        // This annoyance took several hours to figure out why only the child
19
        // kept resetting to default values after loading properly
20
        super();
21
22
        if (saveFile !== undefined)
23
            this.load(saveFile as SaveFileService,
24
                offset as number,
25
                nicknameStartOffset as number,
26
                otNameStartOffset as number,
27
                index as number)
28
    }
29
30
    // Modifies a Pokemon Box to be a Pokemon Party and creates initial stats
31
    static convertToPokemonParty(pkmn: PokemonBox) {
32
        const _pkmn = pkmn as PokemonParty;
33
        Object.setPrototypeOf(_pkmn, PokemonParty.prototype);
34
        _pkmn.updateStats();
35
    }
36
37
    // Modifies a Pokemon Party to be a Pokemon Box
38
    static convertToPokemonBox(pkmn: PokemonParty) {
39
        delete pkmn.maxHP;
40
        delete pkmn.attack;
41
        delete pkmn.defense;
42
        delete pkmn.speed;
43
        delete pkmn.special;
44
        Object.setPrototypeOf(pkmn, PokemonBox.prototype);
45
    }
46
47
    public load(saveFile: SaveFileService,
48
        offset: number,
49
        nicknameStartOffset: number,
50
        otNameStartOffset: number,
51
        index: number): SaveFileIterator {
52
53
        // Mark record size at end to the expanded size of 0x2C
54
        // Pokemon Party has expanded data
55
        const it: SaveFileIterator = super.load(saveFile,
56
            offset,
57
            nicknameStartOffset,
58
            otNameStartOffset,
59
            index,
60
            0x2C);
61
62
        this.level = it.getByte();
63
        this.maxHP = it.getWord();
64
        this.attack = it.getWord();
65
        this.defense = it.getWord();
66
        this.speed = it.getWord();
67
        this.special = it.getWord();
68
69
        return it;
70
    }
71
72
    public save(saveFile: SaveFileService,
73
        offset: number,
74
        speciesStartOffset: number | null,
75
        nicknameStartOffset: number,
76
        otNameStartOffset: number,
77
        index: number): SaveFileIterator {
78
79
        const it: SaveFileIterator = super.save(saveFile,
80
            offset,
81
            speciesStartOffset,
82
            nicknameStartOffset,
83
            otNameStartOffset,
84
            index,
85
            0x2C);
86
87
        it.setByte(this.level);
88
        it.setWord(this.maxHP);
89
        it.setWord(this.attack);
90
        it.setWord(this.defense);
91
        it.setWord(this.speed);
92
        it.setWord(this.special);
93
94
        return it;
95
    }
96
97
    public updateStats() {
98
99
        // Stop here if invalid Pokemon
100
        if(this.isValidPokemon === false)
101
            return;
102
103
        this.maxHP = this.hpStat;
104
        this.attack = this.attackStat;
105
        this.defense = this.defenseStat;
106
        this.speed = this.speedStat;
107
        this.special = this.specialStat;
108
    }
109
110
    public maxHP: number = 0;
111
    public attack: number = 0;
112
    public defense: number = 0;
113
    public speed: number = 0;
114
    public special: number = 0;
115
}
116