Passed
Push — master ( dce7e6...e2e6ab )
by Vincent
06:59 queued 12s
created

name()   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 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
eloc 3
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.spell;
21
22
import fr.quatrevieux.araknemu.data.world.entity.SpellTemplate;
23
import fr.quatrevieux.araknemu.data.world.repository.SpellTemplateRepository;
24
import fr.quatrevieux.araknemu.game.PreloadableService;
25
import fr.quatrevieux.araknemu.game.spell.adapter.SpellLevelAdapter;
26
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffectService;
27
import org.apache.logging.log4j.Logger;
28
import org.checkerframework.checker.index.qual.Positive;
29
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.concurrent.ConcurrentHashMap;
33
import java.util.concurrent.ConcurrentMap;
34
35
/**
36
 * Service for handle spells
37
 */
38
public final class SpellService implements PreloadableService {
39
    private final SpellTemplateRepository repository;
40
    private final SpellEffectService effectService;
41
42 1
    private final ConcurrentMap<Integer, SpellLevels> spells = new ConcurrentHashMap<>();
43
44 1
    public SpellService(SpellTemplateRepository repository, SpellEffectService effectService) {
45 1
        this.repository = repository;
46 1
        this.effectService = effectService;
47 1
    }
48
49
    @Override
50
    public void preload(Logger logger) {
51 1
        logger.info("Loading spells...");
52
53 1
        for (SpellTemplate template : repository.load()) {
54 1
            spells.put(template.id(), load(template));
55 1
        }
56
57 1
        logger.info("{} spells loaded", spells.size());
58 1
    }
59
60
    @Override
61
    public String name() {
62 1
        return "spell";
63
    }
64
65
    /**
66
     * Get a spell
67
     *
68
     * @param spellId The spell id
69
     */
70
    public SpellLevels get(int spellId) {
71 1
        return spells.computeIfAbsent(spellId, id -> load(repository.get(id)));
72
    }
73
74
    /**
75
     * Load the spell levels
76
     *
77
     * @param template The spell template
78
     */
79
    private SpellLevels load(SpellTemplate template) {
80 1
        final List<Spell> levels = new ArrayList<>(template.levels().length);
81
82 1
        for (int i = 0; i < template.levels().length; ++i) {
83 1
            final SpellTemplate.Level level = template.levels()[i];
84
85 1
            if (level == null) {
86 1
                break;
87
            }
88
89 1
            levels.add(makeLevel(i + 1, template, level));
90
        }
91
92 1
        return new SpellLevels(template, levels.toArray(new Spell[levels.size()]));
93
    }
94
95
    /**
96
     * Make one spell level
97
     */
98
    private Spell makeLevel(@Positive int level, SpellTemplate template, SpellTemplate.Level data) {
99 1
        return new SpellLevelAdapter(
100
            level,
101
            template,
102
            data,
103 1
            effectService.makeAll(data.effects(), data.effectAreas(), template.targets()),
104 1
            effectService.makeAll(
105 1
                data.criticalEffects(),
106 1
                data.effectAreas().subList(data.effects().size(), data.effectAreas().size()),
107 1
                template.targets()
108
            )
109
        );
110
    }
111
}
112