Passed
Pull Request — master (#195)
by Vincent
16:08 queued 04:32
created

start(ActionQueue)   A

Complexity

Conditions 5

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0488

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
c 1
b 0
f 0
dl 0
loc 27
ccs 14
cts 16
cp 0.875
crap 5.0488
rs 9.0333
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.exploration.interaction.action.fight;
21
22
import fr.quatrevieux.araknemu.game.exploration.ExplorationPlayer;
23
import fr.quatrevieux.araknemu.game.exploration.interaction.action.Action;
24
import fr.quatrevieux.araknemu.game.exploration.interaction.action.ActionQueue;
25
import fr.quatrevieux.araknemu.game.exploration.interaction.action.ActionType;
26
import fr.quatrevieux.araknemu.game.fight.Fight;
27
import fr.quatrevieux.araknemu.game.fight.JoinFightError;
28
import fr.quatrevieux.araknemu.game.fight.spectator.Spectator;
29
import fr.quatrevieux.araknemu.game.fight.spectator.SpectatorFactory;
30
import fr.quatrevieux.araknemu.network.game.out.game.action.GameActionResponse;
31
import fr.quatrevieux.araknemu.network.game.out.info.Error;
32
33
/**
34
 * Try to join a fight as spectator
35
 */
36
public final class JoinFightAsSpectator implements Action {
37
    private final SpectatorFactory spectatorFactory;
38
    private final ExplorationPlayer player;
39
    private final Fight fight;
40
41 1
    public JoinFightAsSpectator(SpectatorFactory spectatorFactory, ExplorationPlayer player, Fight fight) {
42 1
        this.spectatorFactory = spectatorFactory;
43 1
        this.player = player;
44 1
        this.fight = fight;
45 1
    }
46
47
    @Override
48
    public void start(ActionQueue queue) {
49 1
        if (player.interactions().busy()) {
50 1
            error(JoinFightError.CANT_YOU_R_BUSY);
51 1
            return;
52
        }
53
54 1
        if (player.map().id() != fight.map().id()) {
55 1
            error(JoinFightError.CANT_BECAUSE_MAP);
56 1
            return;
57
        }
58
59 1
        if (!fight.active()) {
60 1
            player.send(Error.cantJoinFightAsSpectator());
61 1
            return;
62
        }
63
64 1
        final Spectator spectator = spectatorFactory.create(player.player(), fight);
65
66 1
        fight.execute(() -> {
67 1
            if (!fight.active()) {
68
                player.send(Error.cantJoinFightAsSpectator());
69
                return;
70
            }
71
72 1
            player.player().stop(player);
73 1
            spectator.join();
74 1
        });
75 1
    }
76
77
    @Override
78
    public ExplorationPlayer performer() {
79
        return player;
80
    }
81
82
    @Override
83
    public ActionType type() {
84
        return ActionType.JOIN_FIGHT;
85
    }
86
87
    @Override
88
    public Object[] arguments() {
89
        return new Object[0];
90
    }
91
92
    private void error(JoinFightError error) {
93 1
        player.send(
94 1
            new GameActionResponse("", ActionType.JOIN_FIGHT, player.id(), Character.toString(error.error()))
95
        );
96 1
    }
97
}
98