Skip to content

Commit

Permalink
Added timings, fixed bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jul 5, 2020
1 parent 7f75caa commit 85cd89b
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 43 deletions.
28 changes: 15 additions & 13 deletions src/main/java/world/bentobox/level/LevelsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import com.google.common.collect.Multiset;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.events.addon.AddonBaseEvent;
import world.bentobox.bentobox.api.events.addon.AddonEvent;
import world.bentobox.bentobox.api.panels.PanelItem;
Expand Down Expand Up @@ -128,7 +127,7 @@ public CompletableFuture<Results> calculateLevel(UUID targetPlayer, Island islan
}
// Save result
addon.logWarning("Saving results");
setIslandResults(island.getWorld(), island.getOwner(), r.getLevel(), r.getUwCount(), r.getMdCount());
setIslandResults(island.getWorld(), island.getOwner(), r);
// Save top ten
addon.logWarning("Saving top ten");
addon.getManager().saveTopTen(island.getWorld());
Expand Down Expand Up @@ -307,7 +306,7 @@ public String getIslandLevelString(@NonNull World world, @Nullable UUID targetPl
}

/**
* Load a level data for the island owner from the cache or database. Only island onwers are stored.
* Load a level data for the island owner from the cache or database. Only island owners are stored.
* @param islandOwner - UUID of island owner
* @return LevelsData object or null if not found
*/
Expand Down Expand Up @@ -399,6 +398,9 @@ void loadTopTens() {
* @param uuid - the player's uuid
*/
public void removeEntry(World world, UUID uuid) {
// Load the user if they haven't yet done anything to put them in the cache
this.getLevelsData(uuid);
// Remove them
if (levelsCache.containsKey(uuid)) {
levelsCache.get(uuid).remove(world);
// Save
Expand Down Expand Up @@ -433,7 +435,9 @@ public void saveTopTen(World world) {
* @param lv - initial island level
*/
public void setInitialIslandLevel(@NonNull Island island, long lv) {
BentoBox.getInstance().logDebug("Setting initial island level " + island +" " + lv);
if (island.getOwner() == null || island.getWorld() == null) return;
BentoBox.getInstance().logDebug("saving");
levelsCache.computeIfAbsent(island.getOwner(), LevelsData::new).setInitialLevel(island.getWorld(), lv);
handler.saveObjectAsync(levelsCache.get(island.getOwner()));
}
Expand All @@ -454,17 +458,15 @@ public void setIslandLevel(@NonNull World world, @NonNull UUID targetPlayer, lon
/**
* Set the island level for the owner of the island that targetPlayer is a member
* @param world - world
* @param owner
* @param level
* @param uwCount
* @param mdCount
* @param owner - owner of the island
* @param r - results of the calculation
*/
private void setIslandResults(World world, @Nullable UUID owner, long level, Multiset<Material> uwCount,
Multiset<Material> mdCount) {
private void setIslandResults(World world, @Nullable UUID owner, Results r) {
LevelsData ld = levelsCache.computeIfAbsent(owner, LevelsData::new);
ld.setLevel(world, level);
ld.setUwCount(world, uwCount);
ld.setMdCount(world, mdCount);
ld.setLevel(world, r.getLevel());
ld.setUwCount(world, r.getUwCount());
ld.setMdCount(world, r.getMdCount());
ld.setPointsToNextLevel(world, r.getPointsToNextLevel());
levelsCache.put(owner, ld);
handler.saveObjectAsync(ld);
// Update TopTen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,20 @@ void nextChar() {


private final Results results;
private long duration;

/**
* Constructor to get the level for an island
* @param addon - Level addon
* @param island - the island to scan
* @param r - completeable result that will be completed when the calculation is complete
* @param r - completable result that will be completed when the calculation is complete
*/
public IslandLevelCalculator(Level addon, Island island, CompletableFuture<Results> r) {
this.addon = addon;
this.island = island;
this.r = r;
results = new Results();
duration = System.currentTimeMillis();
chunksToCheck = getChunksToScan(island);
this.limitCount = new HashMap<>(addon.getBlockConfig().getBlockLimits());
}
Expand Down Expand Up @@ -310,7 +312,7 @@ private CompletableFuture<Chunk> getWorldChunk(@NonNull World world, Environment
if (addon.getSettings().isNether()) {
World nether = addon.getPlugin().getIWM().getNetherWorld(island.getWorld());
if (nether != null) {
return Util.getChunkAtAsync(nether, x, z, false);
return Util.getChunkAtAsync(nether, x, z, true);
}
}
// There is no chunk to scan, so return a null chunk
Expand All @@ -319,13 +321,13 @@ private CompletableFuture<Chunk> getWorldChunk(@NonNull World world, Environment
if (addon.getSettings().isEnd()) {
World end = addon.getPlugin().getIWM().getEndWorld(island.getWorld());
if (end != null) {
return Util.getChunkAtAsync(end, x, z, false);
return Util.getChunkAtAsync(end, x, z, true);
}
}
// There is no chunk to scan, so return a null chunk
return CompletableFuture.completedFuture(null);
default:
return Util.getChunkAtAsync(world, x, z, false);
return Util.getChunkAtAsync(world, x, z, true);

}
}
Expand Down Expand Up @@ -530,6 +532,8 @@ private void tidyUp() {

// Report
results.report = getReport();
// Set the duration
addon.getPipeliner().setTime(System.currentTimeMillis() - duration);
// All done.
}
}
26 changes: 25 additions & 1 deletion src/main/java/world/bentobox/level/calculators/Pipeliner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
*/
public class Pipeliner {

private static final int START_DURATION = 10; // 10 seconds
private final Queue<IslandLevelCalculator> processQueue;
private final BukkitTask task;
private boolean inProcess;
private final Level addon;
private long time;
private long count;

/**
* Construct the pipeliner
Expand Down Expand Up @@ -53,8 +56,11 @@ private void cancel() {
task.cancel();
}

/**
* @return number of islands currently in the queue or in process
*/
public int getIslandsInQueue() {
return processQueue.size();
return inProcess ? processQueue.size() + 1 : processQueue.size();
}

/**
Expand Down Expand Up @@ -95,8 +101,26 @@ private void scanChunk(IslandLevelCalculator iD) {
public CompletableFuture<Results> addIsland(Island island) {
CompletableFuture<Results> r = new CompletableFuture<>();
processQueue.add(new IslandLevelCalculator(addon, island, r));
count++;
return r;
}

/**
* Get the average time it takes to run a level check
* @return the average time in seconds
*/
public int getTime() {
return time == 0 || count == 0 ? START_DURATION : (int)((double)time/count/1000);
}

/**
* Submit how long a level check took
* @param time the time to set
*/
public void setTime(long time) {
// Running average
this.time += time;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ private boolean scanIsland(User user, UUID playerUUID) {
return false;

}
user.sendMessage("island.level.calculating");
int inQueue = addon.getPipeliner().getIslandsInQueue();
user.sendMessage("island.level.calculating");
user.sendMessage("island.level.estimated-wait", TextVariables.NUMBER, String.valueOf(addon.getPipeliner().getTime() * (inQueue + 1)));
if (inQueue > 1) {
user.sendMessage("island.level.in-queue", TextVariables.NUMBER, String.valueOf(inQueue + 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.events.island.IslandEvent.IslandCreatedEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.IslandPreclearEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.IslandRegisteredEvent;
Expand Down Expand Up @@ -38,27 +39,32 @@ public IslandActivitiesListeners(Level addon) {

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onNewIsland(IslandCreatedEvent e) {
BentoBox.getInstance().logDebug(e.getEventName());
zeroIsland(e.getIsland());
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onNewIsland(IslandResettedEvent e) {
BentoBox.getInstance().logDebug(e.getEventName());
zeroIsland(e.getIsland());
}

private void zeroIsland(final Island island) {
// Clear the island setting
if (island.getOwner() != null && island.getWorld() != null) {
BentoBox.getInstance().logDebug("Zeroing island");
addon.getPipeliner().addIsland(island).thenAccept(results ->
addon.getManager().setInitialIslandLevel(island, results.getLevel()));
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onIslandDelete(IslandPreclearEvent e) {
BentoBox.getInstance().logDebug(e.getEventName());
// Remove player from the top ten and level
UUID uuid = e.getIsland().getOwner();
World world = e.getIsland().getWorld();
BentoBox.getInstance().logDebug(uuid + " " + world);
remove(world, uuid);
}

Expand All @@ -70,36 +76,42 @@ private void remove(World world, UUID uuid) {

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onNewIslandOwner(TeamSetownerEvent e) {
BentoBox.getInstance().logDebug(e.getEventName());
// Remove player from the top ten and level
remove(e.getIsland().getWorld(), e.getIsland().getOwner());
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(TeamJoinedEvent e) {
BentoBox.getInstance().logDebug(e.getEventName());
// Remove player from the top ten and level
remove(e.getIsland().getWorld(), e.getPlayerUUID());
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(IslandUnregisteredEvent e) {
BentoBox.getInstance().logDebug(e.getEventName());
// Remove player from the top ten and level
remove(e.getIsland().getWorld(), e.getPlayerUUID());
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(IslandRegisteredEvent e) {
BentoBox.getInstance().logDebug(e.getEventName());
// Remove player from the top ten and level
remove(e.getIsland().getWorld(), e.getPlayerUUID());
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(TeamLeaveEvent e) {
BentoBox.getInstance().logDebug(e.getEventName());
// Remove player from the top ten and level
remove(e.getIsland().getWorld(), e.getPlayerUUID());
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(TeamKickEvent e) {
BentoBox.getInstance().logDebug(e.getEventName());
// Remove player from the top ten and level
remove(e.getIsland().getWorld(), e.getPlayerUUID());
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/world/bentobox/level/objects/LevelsData.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.common.collect.Multiset;
import com.google.gson.annotations.Expose;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;

Expand Down Expand Up @@ -141,8 +142,12 @@ public long getInitialLevel(World world) {
* @param world - world to remove
*/
public void remove(World world) {
levels.remove(world.getName().toLowerCase(Locale.ENGLISH));
initialLevel.remove(world.getName().toLowerCase(Locale.ENGLISH));
BentoBox.getInstance().logDebug("Removing world");
this.levels.remove(world.getName().toLowerCase(Locale.ENGLISH));
this.initialLevel.remove(world.getName().toLowerCase(Locale.ENGLISH));
this.pointsToNextLevel.remove(world.getName().toLowerCase(Locale.ENGLISH));
this.mdCount.remove(world.getName().toLowerCase(Locale.ENGLISH));
this.uwCount.remove(world.getName().toLowerCase(Locale.ENGLISH));
}

/**
Expand Down Expand Up @@ -211,6 +216,9 @@ public void setMdCount(World world, Multiset<Material> mdCount) {
* @return the uwCount
*/
public Map<Material, Integer> getUwCount(World world) {
if (this.uwCount == null) {
this.uwCount = new HashMap<>();
}
return uwCount.getOrDefault(world.getName(), Collections.emptyMap());
}

Expand All @@ -219,6 +227,9 @@ public Map<Material, Integer> getUwCount(World world) {
* @return the mdCount
*/
public Map<Material, Integer> getMdCount(World world) {
if (this.mdCount == null) {
this.mdCount = new HashMap<>();
}
return mdCount.getOrDefault(world.getName(), Collections.emptyMap());
}

Expand Down

0 comments on commit 85cd89b

Please sign in to comment.