Passed
Pull Request — master (#148)
by
unknown
10:40
created

tion$.onInvocation(InvocationFighter)

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
ccs 2
cts 3
cp 0.6667
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.JoinFightError;
24
import fr.quatrevieux.araknemu.game.fight.exception.JoinFightException;
25
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
26
import fr.quatrevieux.araknemu.game.fight.fighter.monster.InvocationFighter;
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<Fighter> fighters;
41
    private final List<Integer> startPlaces;
42
    private final int number;
43
44 1
    public SimpleTeam(PlayerFighter leader, List<Integer> startPlaces, int number) {
45 1
        this.leader = leader;
46 1
        this.fighters = new ArrayList<>();
47 1
        this.fighters.add(leader);
48 1
        this.startPlaces = startPlaces;
49 1
        this.number = number;
50
51 1
        leader.setTeam(this);
52 1
    }
53
54
    @Override
55
    public Fighter leader() {
56 1
        return leader;
57
    }
58
59
    @Override
60
    public int id() {
61 1
        return leader.id();
62
    }
63
64
    @Override
65
    public int cell() {
66 1
        return leader.player().position().cell();
67
    }
68
69
    @Override
70
    public int type() {
71 1
        return 0;
72
    }
73
74
    @Override
75
    public Alignment alignment() {
76 1
        return Alignment.NONE;
77
    }
78
79
    @Override
80
    public int number() {
81 1
        return number;
82
    }
83
84
    @Override
85
    public List<Integer> startPlaces() {
86 1
        return startPlaces;
87
    }
88
89
    @Override
90
    public Collection<Fighter> fighters() {
91 1
        return Collections.unmodifiableCollection(fighters);
92
    }
93
94
    @Override
95
    public void send(Object packet) {
96 1
        fighters.forEach(fighter -> {
0 ignored issues
show
Java 8 introduced by
Remove useless curly braces around statement
Loading history...
97 1
            fighter.apply(new FighterOperation(){
0 ignored issues
show
introduced by
'{' is not preceded with whitespace.
Loading history...
98
                @Override
99
                public void onPlayer(PlayerFighter player) {
100 1
                    player.send(packet);
101 1
                }
102
            });
103 1
        });
104 1
    }
105
106
    @Override
107
    public boolean alive() {
108 1
        return fighters.stream().anyMatch(fighter -> !fighter.dead());
109
    }
110
111
    @Override
112
    public void join(Fighter fighter) throws JoinFightException {
113 1
        fighter.apply(new FighterOperation() {
0 ignored issues
show
introduced by
Anonymous inner class length is 25 lines (max allowed is 20).
Loading history...
114
            @Override
115
            public void onPlayer(PlayerFighter fighter) {
116 1
                if (fighters.size() >= startPlaces.size()) {
117 1
                    throw new JoinFightException(JoinFightError.TEAM_FULL);
118
                }
119
120 1
                fighter.setTeam(SimpleTeam.this);
121 1
                fighters.add(fighter);
122 1
            }
123
124
            @Override
125
            public void onGenericFighter(Fighter fighter) {
126 1
                throw new JoinFightException(JoinFightError.TEAM_CLOSED);
127
            }
128
129
            @Override
130
            public void onInvocation(InvocationFighter fighter) {
131 1
                if (!fighter.invoker().isPresent()) {
132
                    throw new JoinFightException(JoinFightError.TEAM_CLOSED);
133
                }
134
135 1
                fighters.add(fighter);
136 1
            }
137
        });
138 1
    }
139
140
    @Override
141
    public void kick(Fighter fighter) {
142 1
        fighters.remove(fighter);
143 1
    }
144
}
145