Passed
Pull Request — master (#281)
by Vincent
15:23
created

join(Fighter)   A

Complexity

Conditions 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 14
c 0
b 0
f 0
dl 0
loc 20
ccs 9
cts 9
cp 1
crap 5
rs 9.2333

2 Methods

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