Passed
Push — master ( 6700dd...d2c69e )
by Vincent
05:34 queued 12s
created

handle(CastScope,EffectScope)   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 1
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-2021 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.characteristic.point;
21
22
import fr.quatrevieux.araknemu.game.fight.Fight;
23
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
24
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
25
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffEffect;
26
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
27
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
28
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
29
import fr.quatrevieux.araknemu.game.fight.turn.Turn;
30
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
31
32
/**
33
 * Base effect for steal turn points
34
 *
35
 * @see StealActionPointHandler
36
 * @see StealMovementPointHandler
37
 */
38
public abstract class AbstractStealPointHandler implements EffectHandler {
39
    private final Fight fight;
40
    private final AbstractPointLostApplier removePointApplier;
41
    private final AlterPointHook addPointHook;
42
    private final int addPointEffect;
43
44
    /**
45
     * @param fight Fight where the effect will be applied
46
     * @param removePointApplier Applied use to remove points from targets
47
     * @param addPointHook Applied use to add points to caster
48
     * @param addPointEffect Effect id used by the "add point" buff
49
     */
50 1
    public AbstractStealPointHandler(Fight fight, AbstractPointLostApplier removePointApplier, AlterPointHook addPointHook, int addPointEffect) {
51 1
        this.fight = fight;
52 1
        this.removePointApplier = removePointApplier;
53 1
        this.addPointHook = addPointHook;
54 1
        this.addPointEffect = addPointEffect;
55 1
    }
56
57
    @Override
58
    public void handle(CastScope cast, CastScope.EffectScope effect) {
59 1
        final ActiveFighter caster = cast.caster();
60 1
        final int stolen = apply(cast, effect);
61
62 1
        if (stolen > 0) {
63 1
            fight.turnList().current()
64 1
                .filter(turn -> turn.fighter().equals(caster))
65 1
                .ifPresent(turn -> applyOnCurrentTurn(fight, turn, caster, stolen))
66
            ;
67
        }
68 1
    }
69
70
    @Override
71
    public void buff(CastScope cast, CastScope.EffectScope effect) {
72 1
        final ActiveFighter caster = cast.caster();
73 1
        final int stolen = apply(cast, effect);
74
75 1
        if (stolen > 0) {
76 1
            caster.buffs().add(
77
                new Buff(
78 1
                    BuffEffect.withCustomEffect(effect.effect(), addPointEffect, stolen),
79 1
                    cast.action(),
80
                    caster,
81
                    caster,
82
                    addPointHook
83
                )
84
            );
85
        }
86 1
    }
87
88
    /**
89
     * Apply the add point effect to the current turn
90
     * This method is called only if the effect is used as direct effect (i.e. without duration)
91
     * This method should update turn points and send to client the current turn point modification
92
     *
93
     * @param fight The active fight
94
     * @param turn The active turn
95
     * @param toAdd Number of points to add. This value is always >= 1
96
     */
97
    protected abstract void applyOnCurrentTurn(Fight fight, Turn turn, ActiveFighter caster, int toAdd);
98
99
    /**
100
     * Apply to all targets and compute the stolen points
101
     *
102
     * @return Stolen action points. 0 if target has dodged
103
     */
104
    private int apply(CastScope cast, CastScope.EffectScope effect) {
105 1
        final ActiveFighter caster = cast.caster();
106 1
        final SpellEffect baseEffect = effect.effect();
107
108 1
        int stolen = 0;
109
110 1
        for (PassiveFighter target : effect.targets()) {
111 1
            if (!target.equals(caster)) {
112 1
                stolen += removePointApplier.apply(cast, target, baseEffect);
113
            }
114 1
        }
115
116 1
        return stolen;
117
    }
118
}
119