Skip to content

Commit

Permalink
fix(map): race condition on loading map
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent4vx committed May 1, 2024
1 parent 5f1183b commit 7a65526
Showing 1 changed file with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ public ExplorationMapService(MapTemplateRepository repository, FightService figh
* Load the exploration map
*/
public ExplorationMap load(@NonNegative int mapId) throws EntityNotFoundException {
final ExplorationMap loadedMap = maps.get(mapId);

if (loadedMap == null) {
return createMap(repository.get(mapId));
}
return maps.computeIfAbsent(mapId, id -> createMap(repository.get(id)));
}

return loadedMap;
/**
* Load the exploration map using the map template
*/
public ExplorationMap load(MapTemplate template) {
return maps.computeIfAbsent(template.id(), id -> createMap(template));
}

@Override
Expand All @@ -90,7 +91,11 @@ public void preload(Logger logger) {

final long start = System.currentTimeMillis();

repository.all().forEach(this::createMap);
repository.all().forEach(template -> {
final ExplorationMap map = createMap(template);

maps.put(map.id(), map);
});

final long time = System.currentTimeMillis() - start;

Expand Down Expand Up @@ -139,24 +144,9 @@ public Class<FightCreated> event() {
};
}

/**
* Load the exploration map
*/
public ExplorationMap load(MapTemplate template) {
final ExplorationMap loadedMap = maps.get(template.id());

if (loadedMap == null) {
return createMap(template);
}

return loadedMap;
}

private ExplorationMap createMap(MapTemplate template) {
final ExplorationMap map = new ExplorationMap(template, loader, areaService.get(template.subAreaId()));

maps.put(map.id(), map);

map.dispatcher().add(new SendNewSprite(map));
map.dispatcher().add(new SendSpriteRemoved(map));
map.dispatcher().add(new SendPlayerChangeCell(map));
Expand Down

0 comments on commit 7a65526

Please sign in to comment.