Passed
Push — master ( b3961b...2fc6ac )
by Vincent
06:35 queued 11s
created

alive()   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
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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.monster.MonsterFighter;
28
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
29
import fr.quatrevieux.araknemu.game.monster.Monster;
30
import fr.quatrevieux.araknemu.game.monster.group.MonsterGroup;
31
import org.checkerframework.checker.nullness.qual.Nullable;
32
33
import java.util.ArrayList;
34
import java.util.Collection;
35
import java.util.Collections;
36
import java.util.List;
37
38
/**
39
 * Fight team for {@link MonsterGroup}
40
 *
41
 * This team is read only, and contains exactly all group monsters as {@link MonsterFighter}
42
 * This team has no leader
43
 */
44
public final class MonsterGroupTeam implements FightTeam {
45
    private final MonsterGroup monsterGroup;
46
    private final int number;
47
    private final List<FightCell> startPlaces;
48
49
    private final List<MonsterFighter> fighters;
50
    private final TeamOptions options;
51
52
    @SuppressWarnings({"assignment", "argument"})
53 1
    public MonsterGroupTeam(MonsterGroup monsterGroup, List<FightCell> startPlaces, int number) {
54 1
        this.monsterGroup = monsterGroup;
55 1
        this.number = number;
56 1
        this.startPlaces = startPlaces;
57
58 1
        this.fighters = makeFighters(this, monsterGroup.monsters());
59 1
        this.options = new DefaultTeamOptions(this);
60 1
    }
61
62
    @Override
63
    public @Nullable Fighter leader() {
64 1
        return null;
65
    }
66
67
    @Override
68
    public int id() {
69 1
        return monsterGroup.id();
70
    }
71
72
    @Override
73
    public int cell() {
74 1
        return monsterGroup.cell().id();
75
    }
76
77
    @Override
78
    public int type() {
79 1
        return 1;
80
    }
81
82
    @Override
83
    public Alignment alignment() {
84 1
        return Alignment.NONE;
85
    }
86
87
    @Override
88
    public int number() {
89 1
        return number;
90
    }
91
92
    @Override
93
    public List<FightCell> startPlaces() {
94 1
        return startPlaces;
95
    }
96
97
    @Override
98
    public Collection<Fighter> fighters() {
99 1
        return Collections.unmodifiableCollection(fighters);
100
    }
101
102
    @Override
103
    public void send(Object packet) {
104
        // No op : monster do not receive packets
105 1
    }
106
107
    @Override
108
    public boolean alive() {
109 1
        return fighters.stream().anyMatch(fighter -> !fighter.dead());
110
    }
111
112
    @Override
113
    public TeamOptions options() {
114 1
        return options;
115
    }
116
117
    @Override
118
    public void join(Fighter fighter) throws JoinFightException {
119 1
        throw new JoinFightException(JoinFightError.TEAM_CLOSED);
120
    }
121
122
    @Override
123
    public void kick(Fighter fighter) {
124 1
        throw new UnsupportedOperationException("Read-only team");
125
    }
126
127
    @Override
128
    public void setFight(Fight fight) {
129
        // No-op
130 1
    }
131
132
    /**
133
     * Get the monster group related to this team
134
     */
135
    public MonsterGroup group() {
136 1
        return monsterGroup;
137
    }
138
139
    /**
140
     * Creates fighters from monsters of the group
141
     * Ids of monsters are negative integer sequence (starting at -1 for the first monster)
142
     */
143
    private static List<MonsterFighter> makeFighters(MonsterGroupTeam team, List<Monster> monsters) {
144 1
        final List<MonsterFighter> fighters = new ArrayList<>(monsters.size());
145
146 1
        int id = 0;
147
148 1
        for (Monster monster : monsters) {
149 1
            fighters.add(new MonsterFighter(--id, monster, team));
150 1
        }
151
152 1
        return fighters;
153
    }
154
}
155