Passed
Pull Request — master (#207)
by Vincent
10:53
created

onReflectedDamage(ReflectedDamage)   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable.effect.buff;
21
22
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
23
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
24
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.ReflectedDamage;
25
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
26
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
27
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
28
import fr.quatrevieux.araknemu.network.game.out.fight.AddBuff;
29
30
import java.util.Collection;
31
import java.util.Iterator;
32
import java.util.LinkedList;
33
import java.util.function.Predicate;
34
import java.util.stream.Stream;
35
import java.util.stream.StreamSupport;
36
37
/**
38
 * Handle buff list for a fighter
39
 */
40
public final class BuffList implements Iterable<Buff>, Buffs {
41
    private final Fighter fighter;
42 1
    private final Collection<Buff> buffs = new LinkedList<>();
43
44 1
    public BuffList(Fighter fighter) {
45 1
        this.fighter = fighter;
46 1
    }
47
48
    @Override
49
    public Iterator<Buff> iterator() {
50 1
        return buffs.iterator();
51
    }
52
53
    /**
54
     * Get the Buff stream
55
     */
56
    public Stream<Buff> stream() {
57 1
        return StreamSupport.stream(spliterator(), false);
58
    }
59
60
    @Override
61
    public void add(Buff buff) {
62 1
        buffs.add(buff);
63 1
        buff.hook().onBuffStarted(buff);
64
65 1
        if (buff.remainingTurns() == 0) {
66 1
            buff.incrementRemainingTurns();
67
        }
68
69 1
        fighter.fight().send(new AddBuff(buff));
70
71
        // Add one turn on self-buff
72 1
        if (fighter.equals(buff.caster())) {
73 1
            buff.incrementRemainingTurns();
74
        }
75 1
    }
76
77
    @Override
78
    public boolean onStartTurn() {
79 1
        boolean result = true;
80
81 1
        for (Buff buff : buffs) {
82 1
            result &= buff.hook().onStartTurn(buff);
83 1
        }
84
85 1
        return result;
86
    }
87
88
    @Override
89
    public void onEndTurn() {
90 1
        for (Buff buff : buffs) {
91 1
            buff.hook().onEndTurn(buff);
92 1
        }
93 1
    }
94
95
    @Override
96
    public boolean onCastTarget(CastScope cast) {
97 1
        for (Buff buff : buffs) {
98 1
            if (!buff.hook().onCastTarget(buff, cast)) {
99 1
                return false;
100
            }
101 1
        }
102
103 1
        return true;
104
    }
105
106
    @Override
107
    public void onDirectDamage(ActiveFighter caster, Damage value) {
108 1
        for (Buff buff : buffs) {
109 1
            buff.hook().onDirectDamage(buff, caster, value);
110 1
        }
111 1
    }
112
113
    @Override
114
    public void onBuffDamage(Buff poison, Damage value) {
115 1
        for (Buff buff : buffs) {
116 1
            buff.hook().onBuffDamage(buff, poison, value);
117 1
        }
118 1
    }
119
120
    @Override
121
    public void onLifeAltered(int value) {
122 1
        for (Buff buff : buffs) {
123 1
            buff.hook().onLifeAltered(buff, value);
124 1
        }
125 1
    }
126
127
    @Override
128
    public void onReflectedDamage(ReflectedDamage damage) {
129 1
        for (Buff buff : buffs) {
130 1
            buff.hook().onReflectedDamage(buff, damage);
131 1
        }
132 1
    }
133
134
    @Override
135
    public void refresh() {
136 1
        removeIf(buff -> {
137 1
            buff.decrementRemainingTurns();
138
139 1
            return !buff.valid();
140
        });
141 1
    }
142
143
    @Override
144
    public boolean removeAll() {
145 1
        return removeIf(Buff::canBeDispelled);
146
    }
147
148
    @Override
149
    public boolean removeByCaster(PassiveFighter caster) {
150 1
        return removeIf(buff -> buff.caster().equals(caster));
151
    }
152
153
    /**
154
     * Remove buff by a predicate
155
     *
156
     * @param predicate Takes the buff as parameter, and return true to delete (and terminate) the buff
157
     *
158
     * @return true if there is a change (i.e. a buff is terminated)
159
     */
160
    private boolean removeIf(Predicate<Buff> predicate) {
161 1
        final Iterator<Buff> iterator = buffs.iterator();
162
163 1
        boolean hasChanged = false;
164
165 1
        while (iterator.hasNext()) {
166 1
            final Buff buff = iterator.next();
167
168 1
            if (predicate.test(buff)) {
169 1
                iterator.remove();
170 1
                buff.hook().onBuffTerminated(buff);
171 1
                hasChanged = true;
172
            }
173 1
        }
174
175 1
        return hasChanged;
176
    }
177
}
178