fr.quatrevieux.araknemu.game.fight.state.FinishState   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0
eloc 19
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A results(Fight) 0 13 3
A start(Fight) 0 9 1
A id() 0 3 1
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.state;
21
22
import fr.quatrevieux.araknemu.game.fight.Fight;
23
import fr.quatrevieux.araknemu.game.fight.ending.EndFightResults;
24
import fr.quatrevieux.araknemu.game.fight.ending.reward.FightRewardsSheet;
25
import fr.quatrevieux.araknemu.game.fight.event.FightFinished;
26
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
27
import fr.quatrevieux.araknemu.game.fight.team.FightTeam;
28
import fr.quatrevieux.araknemu.network.game.out.fight.FightEnd;
29
30
import java.util.ArrayList;
31
import java.util.List;
32
33
/**
34
 * The fight is terminated
35
 */
36 1
public final class FinishState implements FightState {
37
    @Override
38
    public void start(Fight fight) {
39 1
        final FightRewardsSheet rewardsSheet = fight.type().rewards().generate(results(fight));
40
41 1
        rewardsSheet.rewards().forEach(reward -> reward.fighter().dispatch(new FightFinished(reward)));
42
43 1
        fight.send(new FightEnd(rewardsSheet));
44
45 1
        fight.destroy();
46 1
    }
47
48
    @Override
49
    public int id() {
50
        return 4;
51
    }
52
53
    /**
54
     * Compute the end fight results
55
     */
56
    private EndFightResults results(Fight fight) {
57 1
        final List<Fighter> winners = new ArrayList<>();
58 1
        final List<Fighter> loosers = new ArrayList<>();
59
60 1
        for (FightTeam team : fight.teams()) {
61 1
            if (team.alive()) {
62 1
                winners.addAll(team.fighters());
63
            } else {
64 1
                loosers.addAll(team.fighters());
65
            }
66 1
        }
67
68 1
        return new EndFightResults(fight, winners, loosers);
69
    }
70
}
71