fr.quatrevieux.araknemu.game.admin.debug.FightPos   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 0
loc 82
ccs 38
cts 40
cp 0.95
rs 10
c 0
b 0
f 0
eloc 51
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A build(Builder) 0 11 1
A name() 0 3 1
A createArguments() 0 3 1
A Arguments.hide() 0 2 1
A Arguments.setAction(Action) 0 2 1
A Arguments.show() 0 2 1
B execute(AdminPerformer,Arguments) 0 35 6
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.Arrays;
34
import java.util.stream.Collectors;
35
36
/**
37
 * Show / hide fight places
38
 */
39 1
public final class FightPos extends AbstractCommand<FightPos.Arguments> {
40
    @Override
41
    protected void build(Builder builder) {
42 1
        builder
43 1
            .help(
44 1
                formatter -> formatter
45 1
                    .description("Show all fight positions")
46 1
                    .synopsis("fightpos [show|hide]")
47 1
                    .example("fightpos show", "Show the fight positions")
48 1
                    .example("fightpos hide", "Hide all fight positions")
49
            )
50 1
            .requires(Permission.DEBUG)
51
        ;
52 1
    }
53
54
    @Override
55
    public String name() {
56 1
        return "fightpos";
57
    }
58
59
    @Override
60
    public void execute(AdminPerformer performer, Arguments arguments) throws AdminException {
61 1
        final AdminUser user = AdminUser.class.cast(performer);
62
63 1
        if (arguments.hide()) {
64 1
            user.player().exploration().leave();
65 1
            user.send(new CancelFight());
66 1
            return;
67
        }
68
69 1
        final ExplorationMap map = user.player().exploration().map();
70
71 1
        if (map == null) {
72 1
            performer.error("The player is not on map");
73 1
            return;
74
        }
75
76 1
        final MapCell[][] places = new MapCell[][] {
77 1
            map.fightPlaces(0).toArray(new MapCell[0]),
78 1
            map.fightPlaces(1).toArray(new MapCell[0]),
79
        };
80
81 1
        if (places[0].length == 0 || places[1].length == 0) {
82 1
            performer.error("No fight places found");
83 1
            return;
84
        }
85
86 1
        performer.info(
87
            "Places : {} | {}",
88 1
            Arrays.stream(places[0]).mapToInt(MapCell::id).mapToObj(Integer::toString).collect(Collectors.joining(", ", "[", "]")),
89 1
            Arrays.stream(places[1]).mapToInt(MapCell::id).mapToObj(Integer::toString).collect(Collectors.joining(", ", "[", "]"))
90
        );
91
92 1
        if (arguments.show()) {
93 1
            user.send(new FightStartPositions(places, 0));
94
        }
95 1
    }
96
97
    @Override
98
    public Arguments createArguments() {
99 1
        return new Arguments();
100
    }
101
102
    @SuppressWarnings("initialization.field.uninitialized")
103 1
    public static final class Arguments {
104
        @Argument
105
        private Action action;
106
107
        public void setAction(Action action) {
108
            this.action = action;
109
        }
110
111
        public boolean hide() {
112 1
            return action == Action.HIDE;
113
        }
114
115
        public boolean show() {
116 1
            return action == Action.SHOW;
117
        }
118
119 1
        public enum Action {
120 1
            SHOW, HIDE
121
        }
122
    }
123
}
124