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.exploration.area; |
21
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.core.dbal.repository.EntityNotFoundException; |
23
|
|
|
import fr.quatrevieux.araknemu.core.event.EventsSubscriber; |
24
|
|
|
import fr.quatrevieux.araknemu.core.event.Listener; |
25
|
|
|
import fr.quatrevieux.araknemu.data.world.entity.environment.area.Area; |
26
|
|
|
import fr.quatrevieux.araknemu.data.world.entity.environment.area.SubArea; |
27
|
|
|
import fr.quatrevieux.araknemu.data.world.repository.environment.area.AreaRepository; |
28
|
|
|
import fr.quatrevieux.araknemu.data.world.repository.environment.area.SubAreaRepository; |
29
|
|
|
import fr.quatrevieux.araknemu.game.PreloadableService; |
30
|
|
|
import fr.quatrevieux.araknemu.game.listener.player.InitializeAreas; |
31
|
|
|
import fr.quatrevieux.araknemu.game.player.event.PlayerLoaded; |
32
|
|
|
import org.apache.logging.log4j.Logger; |
33
|
|
|
|
34
|
|
|
import java.util.Collection; |
35
|
|
|
import java.util.HashMap; |
36
|
|
|
import java.util.Map; |
37
|
|
|
import java.util.function.Function; |
38
|
|
|
import java.util.stream.Collectors; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Service for handle areas |
42
|
|
|
*/ |
43
|
|
|
public final class AreaService implements PreloadableService, EventsSubscriber { |
44
|
|
|
private final AreaRepository areaRepository; |
45
|
|
|
private final SubAreaRepository subAreaRepository; |
46
|
|
|
|
47
|
1 |
|
private final Map<Integer, ExplorationSubArea> subAreas = new HashMap<>(); |
48
|
|
|
|
49
|
1 |
|
public AreaService(AreaRepository areaRepository, SubAreaRepository subAreaRepository) { |
50
|
1 |
|
this.areaRepository = areaRepository; |
51
|
1 |
|
this.subAreaRepository = subAreaRepository; |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all available areas |
56
|
|
|
*/ |
57
|
|
|
public Collection<ExplorationSubArea> list() { |
58
|
1 |
|
return subAreas.values(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get a sub area by its id |
63
|
|
|
*/ |
64
|
|
|
public ExplorationSubArea get(int id) { |
65
|
1 |
|
final ExplorationSubArea loadedSubArea = subAreas.get(id); |
66
|
|
|
|
67
|
1 |
|
if (loadedSubArea != null) { |
68
|
1 |
|
return loadedSubArea; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
final SubArea subArea = subAreaRepository.get(id); |
72
|
1 |
|
final Area area = areaRepository.get(subArea.area()); |
73
|
1 |
|
final ExplorationSubArea explorationSubArea = new ExplorationSubArea(subArea, area); |
74
|
|
|
|
75
|
1 |
|
subAreas.put(explorationSubArea.id(), explorationSubArea); |
76
|
|
|
|
77
|
1 |
|
return explorationSubArea; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
@Override |
81
|
|
|
public void preload(Logger logger) { |
82
|
1 |
|
logger.info("Loading areas..."); |
83
|
|
|
|
84
|
1 |
|
final Map<Integer, Area> areas = areaRepository.all().stream().collect(Collectors.toMap(Area::id, Function.identity())); |
85
|
|
|
|
86
|
1 |
|
for (SubArea subArea : subAreaRepository.all()) { |
87
|
1 |
|
final Area area = areas.get(subArea.area()); |
88
|
|
|
|
89
|
1 |
|
if (area == null) { |
90
|
|
|
throw new EntityNotFoundException("Cannot find area " + subArea.area()); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
subAreas.put(subArea.id(), new ExplorationSubArea(subArea, area)); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
logger.info("{} subareas loaded", subAreas.size()); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
@Override |
100
|
|
|
public String name() { |
101
|
1 |
|
return "map.area"; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
@Override |
105
|
|
|
public Listener[] listeners() { |
106
|
1 |
|
return new Listener[] { |
107
|
1 |
|
new Listener<PlayerLoaded>() { |
108
|
|
|
@Override |
109
|
|
|
public void on(PlayerLoaded event) { |
110
|
1 |
|
event.player().dispatcher().add(new InitializeAreas(event.player(), AreaService.this)); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
@Override |
114
|
|
|
public Class<PlayerLoaded> event() { |
115
|
1 |
|
return PlayerLoaded.class; |
116
|
|
|
} |
117
|
|
|
}, |
118
|
|
|
}; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|