Passed
Pull Request — master (#281)
by Vincent
13:51
created

stop()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
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-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.turn;
21
22
import fr.quatrevieux.araknemu.game.fight.Fight;
23
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
24
import fr.quatrevieux.araknemu.game.fight.turn.event.NextTurnInitiated;
25
import fr.quatrevieux.araknemu.game.fight.turn.event.TurnListChanged;
26
import fr.quatrevieux.araknemu.game.fight.turn.order.FighterOrderStrategy;
27
import org.checkerframework.checker.nullness.qual.Nullable;
28
import org.checkerframework.checker.nullness.util.NullnessUtil;
29
30
import java.util.List;
31
import java.util.NoSuchElementException;
32
import java.util.Optional;
33
import java.util.concurrent.atomic.AtomicBoolean;
34
35
/**
36
 * Handle fight turns
37
 */
38
public final class FightTurnList {
39
    private final Fight fight;
40
41
    private final List<Fighter> fighters;
42
    private Fighter current;
43
    private @Nullable FightTurn turn;
44
    private int index;
45 1
    private final AtomicBoolean active = new AtomicBoolean(false);
46
47 1
    public FightTurnList(Fight fight, FighterOrderStrategy orderStrategy) {
48 1
        this.fight = fight;
49 1
        this.fighters = orderStrategy.compute(fight.teams());
50
51 1
        if (fighters.isEmpty()) {
52 1
            throw new IllegalStateException("Cannot initialise turn list without fighters");
53
        }
54
55 1
        current = fighters.get(0); // Always init the first fighter
56 1
    }
57
58
    /**
59
     * Get all fighters ordered by their turn order
60
     */
61
    public List<Fighter> fighters() {
62 1
        return fighters;
63
    }
64
65
    /**
66
     * Remove a fighter from turn list
67
     *
68
     * @param fighter Fighter to remove
69
     *
70
     * @see TurnListChanged Event triggered after the list is updated
71
     */
72
    public void remove(Fighter fighter) {
73 1
        final int index = fighters.indexOf(fighter);
0 ignored issues
show
Comprehensibility introduced by
The variable indexshadows a field with the same name declared in line 44. Consider renaming it.
Loading history...
74
75 1
        if (index == -1) {
76 1
            throw new NoSuchElementException("Fighter " + fighter.id() + " is not found on the turn list");
77
        }
78
79 1
        fighters.remove(index);
80
81
        // The removed fighter is the current fighter or before on the list
82
        // so removing it will shift the list to the left relatively to the cursor (current)
83
        // which cause that the next fighter will be skipped
84
        // See: https://github.com/Arakne/Araknemu/issues/127
85 1
        if (index <= this.index) {
86
            // If current is negative, move cursor to the end
87 1
            if (--this.index < 0) {
88 1
                this.index += fighters.size();
89
            }
90
        }
91
92 1
        fight.dispatch(new TurnListChanged(this));
93 1
    }
94
95
    /**
96
     * Add a fighter after the current one
97
     *
98
     * @param fighter Fighter to add
99
     *
100
     * @see TurnListChanged Event triggered after the list is updated
101
     */
102
    public void add(Fighter fighter) {
103 1
        fighters.add(index + 1, fighter);
104 1
        fight.dispatch(new TurnListChanged(this));
105 1
    }
106
107
    /**
108
     * Get the current turn
109
     */
110
    public Optional<FightTurn> current() {
111 1
        return Optional.ofNullable(turn);
112
    }
113
114
    /**
115
     * Get the current turn fighter
116
     */
117
    public Fighter currentFighter() {
118 1
        return current;
119
    }
120
121
    /**
122
     * Start the turn system
123
     */
124
    public void start() {
125 1
        if (active.getAndSet(true)) {
126 1
            throw new IllegalStateException("TurnList already started");
127
        }
128
129 1
        index = -1;
130
131 1
        next();
132 1
    }
133
134
    /**
135
     * Stop the turn system
136
     */
137
    public void stop() {
138 1
        if (!active.getAndSet(false)) {
139 1
            return;
140
        }
141
142 1
        if (turn != null) {
143 1
            turn.stop();
144 1
            turn = null;
145
        }
146 1
    }
147
148
    /**
149
     * Stop the current turn and start the next
150
     *
151
     * @todo test start with return false
0 ignored issues
show
introduced by
Comment matches to-do format '(TODO:)|(@todo )'.
Loading history...
152
     */
153
    void next() {
154
        // next is called internally by turn system, and fighters if used only with active turn system
155
        // so fighters is always non-null
156 1
        final List<Fighter> fighters = NullnessUtil.castNonNull(this.fighters);
0 ignored issues
show
Comprehensibility introduced by
The variable fightersshadows a field with the same name declared in line 41. Consider renaming it.
Loading history...
157
158 1
        turn = null;
159 1
        fight.dispatch(new NextTurnInitiated());
160
161 1
        while (active.get()) {
162 1
            if (++index >= fighters.size()) {
163 1
                index = 0;
164
            }
165
166 1
            if (fighters.get(index).dead()) {
167 1
                continue;
168
            }
169
170 1
            current = fighters.get(index);
171 1
            turn = new FightTurn(current, fight, fight.type().turnDuration());
172
173 1
            if (turn.start()) {
174 1
                break;
175
            }
176
        }
177 1
    }
178
}
179