Passed
Push — master ( 668211...ef514d )
by Vincent
08:40 queued 13s
created

on(FightActionStarted)   B

Complexity

Conditions 7

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 1
b 0
f 0
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 7
rs 8
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.listener.fight.turn.action;
21
22
import fr.quatrevieux.araknemu.core.event.Listener;
23
import fr.quatrevieux.araknemu.game.fight.Fight;
24
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
25
import fr.quatrevieux.araknemu.game.fight.turn.action.ActionResult;
26
import fr.quatrevieux.araknemu.game.fight.turn.action.event.FightActionStarted;
27
import fr.quatrevieux.araknemu.game.world.util.Sender;
28
import fr.quatrevieux.araknemu.network.game.out.fight.FighterPositions;
29
import fr.quatrevieux.araknemu.network.game.out.fight.action.FightAction;
30
import fr.quatrevieux.araknemu.network.game.out.fight.action.StartFightAction;
31
32
/**
33
 * Send the fight action
34
 */
35
public final class SendFightAction implements Listener<FightActionStarted> {
36
    private final Fight fight;
37
38 1
    public SendFightAction(Fight fight) {
39 1
        this.fight = fight;
40 1
    }
41
42
    @Override
43
    public void on(FightActionStarted event) {
44 1
        final ActiveFighter performer = event.action().performer();
45 1
        final Sender sender = performer instanceof Sender ? (Sender) performer : null;
46 1
        final ActionResult result = event.result();
47
48 1
        if (result.success() && sender != null) {
49 1
            sender.send(new StartFightAction(event.action()));
50
        }
51
52 1
        if (!result.secret()) {
53
            // Refresh caster position if hidden
54 1
            if (performer.hidden()) {
55 1
                fight.send(new FighterPositions(performer));
56
            }
57
58 1
            fight.send(new FightAction(result));
59 1
            return;
60
        }
61
62 1
        if (sender != null) {
63 1
            sender.send(new FightAction(result));
64
        }
65 1
    }
66
67
    @Override
68
    public Class<FightActionStarted> event() {
69 1
        return FightActionStarted.class;
70
    }
71
}
72