Passed
Branch master (3db100)
by Vincent
12:21
created

fighters()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
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.team;
21
22
import fr.quatrevieux.araknemu.data.constant.Alignment;
23
import fr.quatrevieux.araknemu.game.fight.Fight;
24
import fr.quatrevieux.araknemu.game.fight.JoinFightError;
25
import fr.quatrevieux.araknemu.game.fight.exception.JoinFightException;
26
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
27
import fr.quatrevieux.araknemu.game.fight.fighter.operation.FighterOperation;
28
import fr.quatrevieux.araknemu.game.fight.fighter.player.PlayerFighter;
29
30
import java.util.ArrayList;
31
import java.util.Collection;
32
import java.util.Collections;
33
import java.util.List;
34
35
/**
36
 * Simple fight team for player fighters
37
 */
38
public final class SimpleTeam implements FightTeam {
39
    private final PlayerFighter leader;
40
    private final List<PlayerFighter> fighters;
41
    private final List<Integer> startPlaces;
42
    private final int number;
43
44
    private final ConfigurableTeamOptions options;
45
46 1
    public SimpleTeam(PlayerFighter leader, List<Integer> startPlaces, int number) {
47 1
        this.leader = leader;
48 1
        this.fighters = new ArrayList<>();
49 1
        this.fighters.add(leader);
50 1
        this.startPlaces = startPlaces;
51 1
        this.number = number;
52 1
        this.options = new ConfigurableTeamOptions(this);
53
54 1
        leader.setTeam(this);
55 1
    }
56
57
    @Override
58
    public Fighter leader() {
59 1
        return leader;
60
    }
61
62
    @Override
63
    public int id() {
64 1
        return leader.id();
65
    }
66
67
    @Override
68
    public int cell() {
69 1
        return leader.player().position().cell();
70
    }
71
72
    @Override
73
    public int type() {
74 1
        return 0;
75
    }
76
77
    @Override
78
    public Alignment alignment() {
79 1
        return Alignment.NONE;
80
    }
81
82
    @Override
83
    public int number() {
84 1
        return number;
85
    }
86
87
    @Override
88
    public List<Integer> startPlaces() {
89 1
        return startPlaces;
90
    }
91
92
    @Override
93
    public Collection<Fighter> fighters() {
94 1
        return Collections.unmodifiableCollection(fighters);
95
    }
96
97
    @Override
98
    public void send(Object packet) {
99 1
        fighters.forEach(fighter -> fighter.send(packet));
100 1
    }
101
102
    @Override
103
    public boolean alive() {
104 1
        return fighters.stream().anyMatch(fighter -> !fighter.dead());
105
    }
106
107
    @Override
108
    public ConfigurableTeamOptions options() {
109 1
        return options;
110
    }
111
112
    @Override
113
    public void join(Fighter fighter) throws JoinFightException {
114 1
        if (!options.allowJoinTeam()) {
115 1
            throw new JoinFightException(JoinFightError.TEAM_CLOSED);
116
        }
117
118 1
        fighter.apply(new FighterOperation() {
119
            @Override
120
            public void onPlayer(PlayerFighter fighter) {
121 1
                if (fighters.size() >= startPlaces.size()) {
122 1
                    throw new JoinFightException(JoinFightError.TEAM_FULL);
123
                }
124
125 1
                fighter.setTeam(SimpleTeam.this);
126 1
                fighters.add(fighter);
127 1
            }
128
129
            @Override
130
            public void onGenericFighter(Fighter fighter) {
131 1
                throw new JoinFightException(JoinFightError.TEAM_CLOSED);
132
            }
133
        });
134 1
    }
135
136
    @Override
137
    public void kick(Fighter fighter) {
138 1
        fighters.remove(fighter);
139 1
    }
140
141
    @Override
142
    public void setFight(Fight fight) {
143 1
        options.setFight(fight);
144 1
    }
145
}
146