Test Failed
Pull Request — master (#172)
by Vincent
09:48
created

execute(AdminPerformer,Arguments)   A

Complexity

Conditions 5

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 17
dl 0
loc 25
ccs 15
cts 15
cp 1
crap 5
rs 9.0833
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-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.admin.debug;
21
22
import fr.arakne.utils.maps.MapCell;
23
import fr.quatrevieux.araknemu.common.account.Permission;
24
import fr.quatrevieux.araknemu.game.admin.AbstractCommand;
25
import fr.quatrevieux.araknemu.game.admin.AdminPerformer;
26
import fr.quatrevieux.araknemu.game.admin.AdminUser;
27
import fr.quatrevieux.araknemu.game.admin.exception.AdminException;
28
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMap;
29
import fr.quatrevieux.araknemu.network.game.out.fight.CancelFight;
30
import fr.quatrevieux.araknemu.network.game.out.game.FightStartPositions;
31
import org.kohsuke.args4j.Argument;
32
33
import java.util.List;
34
import java.util.stream.Collectors;
35
36
/**
37
 * Show / hide fight places
38 1
 */
39
public final class FightPos extends AbstractCommand<FightPos.Arguments> {
40
    @Override
41
    protected void build(Builder builder) {
42
        builder
43
            .description("Show all fight positions")
44
            .help(
45
                formatter -> formatter
46
                    .synopsis("fightpos [show|hide]")
47
                    .example("fightpos show", "Show the fight positions")
48
                    .example("fightpos hide", "Hide all fight positions")
49
            )
50
            .requires(Permission.DEBUG)
51
        ;
52
    }
53
54
    @Override
55 1
    public String name() {
56
        return "fightpos";
57
    }
58
59
    @Override
60 1
    public void execute(AdminPerformer performer, Arguments arguments) throws AdminException {
61
        final AdminUser user = AdminUser.class.cast(performer);
62 1
63 1
        if (arguments.hide()) {
64 1
            user.player().exploration().leave();
65 1
            user.send(new CancelFight());
66
            return;
67
        }
68 1
69 1
        final ExplorationMap map = user.player().exploration().map();
70 1
        final List<Integer>[] places = new List[] {
71 1
            map.fightPlaces(0).stream().map(MapCell::id).collect(Collectors.toList()),
72
            map.fightPlaces(1).stream().map(MapCell::id).collect(Collectors.toList()),
73
        };
74 1
75 1
        if (places[0].isEmpty() || places[1].isEmpty()) {
76 1
            performer.error("No fight places found");
77
            return;
78
        }
79 1
80
        performer.info("Places : {} | {}", places[0], places[1]);
81 1
82 1
        if (arguments.show()) {
83
            user.send(new FightStartPositions(places, 0));
84 1
        }
85
    }
86
87
    @Override
88
    public Arguments createArguments() {
89
        return new Arguments();
90
    }
91
92
    public static final class Arguments {
93
        @Argument
94
        private Action action;
95
96
        public void setAction(Action action) {
97
            this.action = action;
98
        }
99
100
        public boolean hide() {
101
            return action == Action.HIDE;
102
        }
103
104
        public boolean show() {
105
            return action == Action.SHOW;
106
        }
107
108
        public enum Action {
109
            SHOW, HIDE
110
        }
111
    }
112
}
113