Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(map): race condition on loading map #345

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading