Passed
Pull Request — master (#281)
by Vincent
13:51
created

createTeamFactory(TeamFactory,int)   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
eloc 2
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-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.builder;
21
22
import fr.arakne.utils.value.helper.RandomUtil;
23
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMap;
24
import fr.quatrevieux.araknemu.game.fight.Fight;
25
import fr.quatrevieux.araknemu.game.fight.FightService;
26
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
27
import fr.quatrevieux.araknemu.game.fight.map.FightMap;
28
import fr.quatrevieux.araknemu.game.fight.state.ActiveState;
29
import fr.quatrevieux.araknemu.game.fight.state.FinishState;
30
import fr.quatrevieux.araknemu.game.fight.state.InitialiseState;
31
import fr.quatrevieux.araknemu.game.fight.state.NullState;
32
import fr.quatrevieux.araknemu.game.fight.state.PlacementState;
33
import fr.quatrevieux.araknemu.game.fight.state.StatesFlow;
34
import fr.quatrevieux.araknemu.game.fight.team.FightTeam;
35
import fr.quatrevieux.araknemu.game.fight.type.FightType;
36
import org.checkerframework.checker.index.qual.NonNegative;
37
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
38
import org.checkerframework.checker.nullness.qual.Nullable;
39
import org.checkerframework.checker.nullness.util.NullnessUtil;
40
41
import java.util.ArrayList;
42
import java.util.List;
43
44
/**
45
 * Base builder for fight
46
 */
47
public final class BaseBuilder implements FightBuilder {
48
    private final FightService service;
49
    private final @Nullable RandomUtil random;
50
    private final FightType type;
51
52
    private @MonotonicNonNull FightMap map;
53 1
    private final List<TeamFactory> teamFactories = new ArrayList<>();
54
55 1
    public BaseBuilder(FightService service, @Nullable RandomUtil random, FightType type) {
56 1
        this.service = service;
57 1
        this.random = random;
58 1
        this.type = type;
59 1
    }
60
61
    @Override
62
    public Fight build(int fightId) {
63 1
        if (map == null || teamFactories.size() < 2) {
64 1
            throw new IllegalStateException("Missing map or teams");
65
        }
66
67 1
        return service.create(fightId, type, map, buildTeams(), statesFlow());
68
    }
69
70
    /**
71
     * Set the fight map
72
     */
73
    public void map(ExplorationMap map) {
74 1
        this.map = service.map(map);
75 1
    }
76
77
    public void addTeam(TeamFactory factory) {
78 1
        teamFactories.add(factory);
79 1
    }
80
81
    private List<FightTeam.Factory> buildTeams() {
82 1
        final List<TeamFactory> factories = random != null ? random.shuffle(teamFactories) : teamFactories;
83 1
        final List<FightTeam.Factory> teams = new ArrayList<>(factories.size());
84
85 1
        for (int number = 0; number < factories.size(); ++number) {
86 1
            teams.add(createTeamFactory(factories.get(number), number));
87
        }
88
89 1
        return teams;
90
    }
91
92
    private StatesFlow statesFlow() {
93 1
        return new StatesFlow(
94
            new NullState(),
95
            new InitialiseState(),
96
            new PlacementState(),
97
            new ActiveState(),
98
            new FinishState()
99
        );
100
    }
101
102
    private FightTeam.Factory createTeamFactory(TeamFactory teamFactory, @NonNegative int number) {
103 1
        return fight -> teamFactory.create(fight, number, NullnessUtil.castNonNull(map).startPlaces(number));
104
    }
105
106
    public interface TeamFactory {
107
        /**
108
         * Creates the fight team
109
         *
110
         * @param number The team number
111
         * @param startPlaces The available start places
112
         */
113
        public FightTeam create(Fight fight, int number, List<FightCell> startPlaces);
114
    }
115
}
116