Test Failed
Pull Request — master (#190)
by Vincent
16:13
created

start(ActionQueue)   B

Complexity

Conditions 6

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 18
dl 0
loc 27
ccs 15
cts 15
cp 1
crap 6
rs 8.5666
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.exploration.interaction.action.move;
21
22
import fr.arakne.utils.maps.path.Path;
23
import fr.arakne.utils.maps.path.PathStep;
24
import fr.quatrevieux.araknemu.game.exploration.ExplorationPlayer;
25
import fr.quatrevieux.araknemu.game.exploration.interaction.action.ActionQueue;
26
import fr.quatrevieux.araknemu.game.exploration.interaction.action.ActionType;
27
import fr.quatrevieux.araknemu.game.exploration.interaction.action.BlockingAction;
28
import fr.quatrevieux.araknemu.game.exploration.interaction.action.move.validator.PathValidationException;
29
import fr.quatrevieux.araknemu.game.exploration.interaction.action.move.validator.PathValidator;
30
import fr.quatrevieux.araknemu.game.exploration.map.cell.ExplorationMapCell;
31
import fr.quatrevieux.araknemu.network.game.out.game.action.GameActionResponse;
32
33
/**
34
 * Move the player
35
 */
36
public final class Move implements BlockingAction {
37
    private final ExplorationPlayer player;
38
    private final PathValidator[] validators;
39
    private Path<ExplorationMapCell> path;
40
41
    private int id;
42
43 1
    public Move(ExplorationPlayer player, Path<ExplorationMapCell> path, PathValidator[] validators) {
44 1
        this.player = player;
45 1
        this.path = path;
46 1
        this.validators = validators;
47 1
    }
48
49
    @Override
50
    public void start(ActionQueue queue) {
51 1
        if (path.isEmpty()) {
52 1
            throw new IllegalArgumentException("Empty path");
53
        }
54
55
        if (!path.get(0).cell().equals(performer().cell())) {
56 1
            throw new IllegalArgumentException("Start cell do not match with player cell");
57 1
        }
58
59 1
        try {
60 1
            for (PathValidator validator : validators) {
61 1
                path = validator.validate(this, path);
62 1
            }
63 1
        } catch (PathValidationException e) {
64
            player.send(GameActionResponse.NOOP);
65 1
            e.errorPacket().ifPresent(player::send);
66 1
            return;
67 1
        }
68
69
        if (path.target().equals(player.cell())) {
70 1
            player.send(GameActionResponse.NOOP);
71 1
            return;
72 1
        }
73
74
        queue.setPending(this);
75
        player.map().send(new GameActionResponse(this));
76 1
    }
77 1
78
    @Override
79
    public void cancel(String argument) {
80 1
        if (argument == null) {
81
            return;
82 1
        }
83 1
84 1
        final int cellId = Integer.parseInt(argument);
85
86 1
        for (PathStep<ExplorationMapCell> step : path) {
87
            if (step.cell().id() == cellId) {
88 1
                player.move(step.cell(), step.direction());
89
90 1
                return;
91
            }
92
        }
93
94
        throw new IllegalArgumentException("Invalid cell");
95 1
    }
96 1
97
    @Override
98
    public void end() {
99
        player.move(path.target(), path.last().direction());
100 1
    }
101
102
    @Override
103
    public int id() {
104
        return id;
105 1
    }
106 1
107
    @Override
108
    public void setId(int id) {
109
        this.id = id;
110 1
    }
111
112
    @Override
113
    public ExplorationPlayer performer() {
114
        return player;
115 1
    }
116
117
    @Override
118
    public ActionType type() {
119
        return ActionType.MOVE;
120 1
    }
121
122
    @Override
123
    public Object[] arguments() {
124 1
        return new Object[] { path.encodeWithStartCell() };
125
    }
126
127
    public Path<ExplorationMapCell> path() {
128
        return path;
129
    }
130
}
131