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

packet()   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.handler.game;
21
22
import fr.quatrevieux.araknemu.core.network.exception.CloseImmediately;
23
import fr.quatrevieux.araknemu.core.network.parser.PacketHandler;
24
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMap;
25
import fr.quatrevieux.araknemu.game.fight.Fight;
26
import fr.quatrevieux.araknemu.game.fight.FightService;
27
import fr.quatrevieux.araknemu.game.fight.state.PlacementState;
28
import fr.quatrevieux.araknemu.game.fight.team.FightTeam;
29
import fr.quatrevieux.araknemu.game.fight.team.TeamOptions;
30
import fr.quatrevieux.araknemu.network.game.GameSession;
31
import fr.quatrevieux.araknemu.network.game.in.game.AskExtraInfo;
32
import fr.quatrevieux.araknemu.network.game.out.fight.FightOption;
33
import fr.quatrevieux.araknemu.network.game.out.fight.exploration.AddTeamFighters;
34
import fr.quatrevieux.araknemu.network.game.out.fight.exploration.FightsCount;
35
import fr.quatrevieux.araknemu.network.game.out.fight.exploration.ShowFight;
36
import fr.quatrevieux.araknemu.network.game.out.game.AddSprites;
37
import fr.quatrevieux.araknemu.network.game.out.game.MapReady;
38
39
import java.util.Collection;
40
41
/**
42
 * Load map extra info for join the map
43
 *
44
 * @todo refactor with extract info provider
0 ignored issues
show
introduced by
Comment matches to-do format '(TODO:)|(@todo )'.
Loading history...
45
 */
46
public final class LoadExtraInfo implements PacketHandler<GameSession, AskExtraInfo> {
47
    private final FightService fightService;
48
49 1
    public LoadExtraInfo(FightService fightService) {
50 1
        this.fightService = fightService;
51 1
    }
52
53
    @Override
54
    public void handle(GameSession session, AskExtraInfo packet) throws Exception {
55 1
        final ExplorationMap map = session.exploration().map();
56
57 1
        if (map == null) {
58
            throw new CloseImmediately("A map should be loaded before get extra info");
59
        }
60
61 1
        session.send(new AddSprites(map.sprites()));
62
63 1
        final Collection<Fight> fights = fightService.fightsByMap(map.id());
64
65 1
        for (Fight fight : fights) {
66 1
            if (!(fight.state() instanceof PlacementState)) {
67 1
                continue;
68
            }
69
70 1
            session.send(new ShowFight(fight));
71
72 1
            for (FightTeam team : fight.teams()) {
73 1
                session.send(new AddTeamFighters(team));
74
75 1
                final TeamOptions options = team.options();
76
77 1
                if (options.allowJoinTeamHasBeenUpdated()) {
78 1
                    session.send(FightOption.blockJoiner(options));
79
                }
80
81 1
                if (options.allowSpectatorHasBeenUpdated()) {
82 1
                    session.send(FightOption.blockSpectators(options));
83
                }
84
85 1
                if (options.needHelpHasBeenUpdated()) {
86 1
                    session.send(FightOption.needHelp(options));
87
                }
88 1
            }
89 1
        }
90
91 1
        session.send(new FightsCount(fights.size()));
92
93 1
        session.send(new MapReady());
94 1
    }
95
96
    @Override
97
    public Class<AskExtraInfo> packet() {
98 1
        return AskExtraInfo.class;
99
    }
100
}
101