Passed
Pull Request — master (#266)
by Vincent
14:00
created

handle(CastScope,EffectScope)   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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-2022 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.characteristic;
21
22
import fr.quatrevieux.araknemu.data.constant.Characteristic;
23
import fr.quatrevieux.araknemu.game.fight.Fight;
24
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
25
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
26
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffEffect;
27
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffHook;
28
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
29
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
30
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
31
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
32
import org.checkerframework.checker.index.qual.NonNegative;
33
import org.checkerframework.checker.index.qual.Positive;
34
35
import java.util.HashMap;
36
import java.util.Map;
37
38
/**
39
 * Effect handler for punishment
40
 *
41
 * Add a characteristic boost when the fight suffer from direct damage
42
 * Characteristics mapping must be added using {@link AddCharacteristicOnDamageHandler#register(int, Characteristic)} method
43
 *
44
 * Parameters:
45
 * - first (i.e. min) is the effect used as characteristic boost
46
 * - second (i.e. max) is the maximal boost value per turn
47
 * - third (i.e. special) is the boost duration
48
 *
49
 * Note: this buff cannot be dispelled, but the boosted characteristics can
50
 */
51
public final class AddCharacteristicOnDamageHandler implements EffectHandler, BuffHook {
52
    private final Fight fight;
53
54
    /**
55
     * Map first effect parameter to related characteristic hook
56
     */
57 1
    private final Map<Integer, AlterCharacteristicHook> hooksMapping = new HashMap<>();
58
59 1
    public AddCharacteristicOnDamageHandler(Fight fight) {
60 1
        this.fight = fight;
61 1
    }
62
63
    @Override
64
    public void handle(CastScope cast, CastScope.EffectScope effect) {
65 1
        throw new UnsupportedOperationException("Alter characteristic effect must be used as a buff");
66
    }
67
68
    @Override
69
    public void buff(CastScope cast, CastScope.EffectScope effect) {
70 1
        final SpellEffect spellEffect = effect.effect();
71 1
        final ActiveFighter caster = cast.caster();
72
73 1
        for (PassiveFighter target : cast.targets()) {
74 1
            target.buffs().add(new Buff(
75
                spellEffect,
76 1
                cast.action(),
77
                caster,
78
                target,
79
                this,
80
                false
81
            ));
82 1
        }
83 1
    }
84
85
    /**
86
     * Register an handled characteristic
87
     *
88
     * @param effectId Handled effect id. This is the first parameter of the spell effect
89
     * @param hook Alter characteristic hook to use
90
     *
91
     * @return This instance
92
     */
93
    public AddCharacteristicOnDamageHandler register(int effectId, AlterCharacteristicHook hook) {
94 1
        hooksMapping.put(effectId, hook);
95
96 1
        return this;
97
    }
98
99
    /**
100
     * Register an handled characteristic
101
     *
102
     * @param effectId Handled effect id. This is the first parameter of the spell effect
103
     * @param characteristic The mapped characteristic
104
     *
105
     * @return This instance
106
     */
107
    public AddCharacteristicOnDamageHandler register(int effectId, Characteristic characteristic) {
108 1
        return register(effectId, AlterCharacteristicHook.add(fight, characteristic));
109
    }
110
111
    @Override
112
    public void onDirectDamageApplied(Buff buff, ActiveFighter caster, @Positive int damage) {
113 1
        final PassiveFighter target = buff.target();
114 1
        final int boostEffectId = buff.effect().min();
115 1
        final AlterCharacteristicHook hook = hooksMapping.get(boostEffectId);
116
117 1
        if (hook == null) {
118 1
            throw new IllegalArgumentException("Unsupported effect " + boostEffectId + " for punishment effect");
119
        }
120
121 1
        final int maximalValue = buff.effect().max() - currentBoostValue(buff, caster);
122
123 1
        if (maximalValue <= 0) {
124 1
            return;
125
        }
126
127 1
        target.buffs().add(new Buff(
128 1
            BuffEffect.withCustomEffectAndDuration(buff.effect(), boostEffectId, Math.min(maximalValue, damage), buff.effect().special()),
129 1
            buff.action(),
130
            caster,
131
            target,
132
            hook
133
        ));
134 1
    }
135
136
    private @NonNegative int currentBoostValue(Buff buff, ActiveFighter caster) {
137
        // Add 1 to duration in case of self damage
138 1
        final int expectedEffectDuration = buff.effect().special() + (caster.equals(buff.target()) ? 1 : 0);
139
140 1
        int boost = 0;
141
142 1
        for (Buff activeBuff : buff.target().buffs()) {
143 1
            if (activeBuff.effect().effect() == buff.effect().min()
144 1
                && activeBuff.remainingTurns() == expectedEffectDuration
145 1
                && activeBuff.action().equals(buff.action())
146
            ) {
147 1
                boost += activeBuff.effect().min();
148
            }
149 1
        }
150
151 1
        return boost;
152
    }
153
}
154