From 6df01dd13727841ef782dbcfe1aa92efb4663ddd Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 11 Aug 2020 10:42:42 +0200 Subject: [PATCH] Add progress report to tasks generating to the worldborder - Closes #90 --- .../kotlin/net/trivernis/chunkmaster/commands/CmdList.kt | 9 ++++----- .../trivernis/chunkmaster/lib/database/PendingChunks.kt | 6 ++++-- .../chunkmaster/lib/generation/GenerationManager.kt | 8 +++++--- .../lib/generation/taskentry/RunningTaskEntry.kt | 7 ++++--- .../net/trivernis/chunkmaster/lib/shapes/Circle.kt | 8 ++++++-- .../kotlin/net/trivernis/chunkmaster/lib/shapes/Shape.kt | 2 +- .../net/trivernis/chunkmaster/lib/shapes/Spiral.kt | 8 ++++++-- 7 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/net/trivernis/chunkmaster/commands/CmdList.kt b/src/main/kotlin/net/trivernis/chunkmaster/commands/CmdList.kt index df5b6ed..370d21b 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/commands/CmdList.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/commands/CmdList.kt @@ -6,6 +6,7 @@ import net.trivernis.chunkmaster.lib.generation.taskentry.TaskEntry import org.bukkit.command.Command import org.bukkit.command.CommandSender import kotlin.math.ceil +import kotlin.math.pow class CmdList(private val chunkmaster: Chunkmaster) : Subcommand { override val name = "list" @@ -49,14 +50,12 @@ class CmdList(private val chunkmaster: Chunkmaster) : Subcommand { */ private fun getGenerationEntry(task: TaskEntry): String { val genTask = task.generationTask - val percentage = if (genTask.radius > 0) - " (%.1f".format(genTask.shape.progress() * 100) + "%)." - else - "" + val progress = genTask.shape.progress(if (genTask.radius < 0) (genTask.world.worldBorder.size / 32).toInt() else null) + val percentage = " (%.1f".format(progress * 100) + "%)." val count = if (genTask.radius > 0) { "${genTask.count} / ${ceil(genTask.shape.total()).toInt()}" } else { - genTask.count.toString() + "${genTask.count} / worldborder ${(genTask.world.worldBorder.size / 16).pow(2).toInt()}" } return "\n" + chunkmaster.langManager.getLocalized( "TASKS_ENTRY", diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/database/PendingChunks.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/database/PendingChunks.kt index 9c61671..614ae48 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/database/PendingChunks.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/database/PendingChunks.kt @@ -5,6 +5,8 @@ import java.util.concurrent.CompletableFuture import kotlin.math.ceil class PendingChunks(private val sqliteManager: SqliteManager) { + + private val insertionCount = 300 /** * Returns a list of pending chunks for a taskId */ @@ -33,10 +35,10 @@ class PendingChunks(private val sqliteManager: SqliteManager) { fun addPendingChunks(taskId: Int, pendingChunks: List): CompletableFuture { val futures = ArrayList>() - val statementCount = ceil(pendingChunks.size.toDouble() / 100.0).toInt() + val statementCount = ceil(pendingChunks.size.toDouble() / insertionCount).toInt() for (i in 0 until statementCount) { - futures.add(insertPendingChunks(taskId, pendingChunks.subList(i * 100, ((i * 100) + 100).coerceAtMost(pendingChunks.size)))) + futures.add(insertPendingChunks(taskId, pendingChunks.subList(i * insertionCount, ((i * insertionCount) + insertionCount).coerceAtMost(pendingChunks.size)))) } if (futures.size > 0) { diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt index fbc4d41..2b9bc8c 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt @@ -292,10 +292,12 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server private fun reportGenerationProgress(task: RunningTaskEntry) { val genTask = task.generationTask val (speed, chunkSpeed) = task.generationSpeed - val percentage = if (genTask.radius > 0) "(${"%.2f".format(genTask.shape.progress() * 100)}%)" else "" + val progress = genTask.shape.progress(if (genTask.radius < 0) (genTask.world.worldBorder.size / 32).toInt() else null) + val percentage = + "(${"%.2f".format(progress * 100)}%)" - val eta = if (genTask.radius > 0 && speed!! > 0) { - val remaining = 1 - genTask.shape.progress() + val eta = if (speed!! > 0) { + val remaining = 1 - progress val etaSeconds = remaining / speed val hours: Int = (etaSeconds / 3600).toInt() val minutes: Int = ((etaSeconds % 3600) / 60).toInt() diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/taskentry/RunningTaskEntry.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/taskentry/RunningTaskEntry.kt index 6db86d3..3594bc0 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/taskentry/RunningTaskEntry.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/taskentry/RunningTaskEntry.kt @@ -18,8 +18,9 @@ class RunningTaskEntry( get() { var generationSpeed: Double? = null var chunkGenerationSpeed: Double? = null + val progress = generationTask.shape.progress(if (generationTask.radius < 0) (generationTask.world.worldBorder.size / 32).toInt() else null) if (lastProgress != null) { - val progressDiff = generationTask.shape.progress() - lastProgress!!.second + val progressDiff = progress - lastProgress!!.second val timeDiff = (System.currentTimeMillis() - lastProgress!!.first).toDouble() / 1000 generationSpeed = progressDiff / timeDiff } @@ -28,13 +29,13 @@ class RunningTaskEntry( val timeDiff = (System.currentTimeMillis() - lastChunkCount!!.first).toDouble() / 1000 chunkGenerationSpeed = chunkDiff / timeDiff } - lastProgress = Pair(System.currentTimeMillis(), generationTask.shape.progress()) + lastProgress = Pair(System.currentTimeMillis(), progress) lastChunkCount = Pair(System.currentTimeMillis(), generationTask.count) return Pair(generationSpeed, chunkGenerationSpeed) } init { - lastProgress = Pair(System.currentTimeMillis(), generationTask.shape.progress()) + lastProgress = Pair(System.currentTimeMillis(), generationTask.shape.progress(null)) lastChunkCount = Pair(System.currentTimeMillis(), generationTask.count) } diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Circle.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Circle.kt index 4ece528..4a6ca57 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Circle.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Circle.kt @@ -20,9 +20,13 @@ class Circle(center: Pair, start: Pair, radius: Int) : Shape return (PI * radius.toFloat().pow(2)) } - override fun progress(): Double { + override fun progress(maxRadius: Int?): Double { // TODO: Radius inner progress - return (count / (PI * radius.toFloat().pow(2))).coerceAtMost(1.0) + return if (maxRadius != null) { + (count / (PI * maxRadius.toFloat().pow(2))).coerceAtMost(1.0) + } else { + (count / (PI * radius.toFloat().pow(2))).coerceAtMost(1.0) + } } override fun currentRadius(): Int { diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Shape.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Shape.kt index f4071ff..b79b750 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Shape.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Shape.kt @@ -19,7 +19,7 @@ abstract class Shape(protected val center: Pair, start: Pair /** * Returns the progress of the shape */ - abstract fun progress(): Double + abstract fun progress(maxRadius: Int?): Double /** * The total number of chunks to generate diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Spiral.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Spiral.kt index 931c48d..15b5f35 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Spiral.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/shapes/Spiral.kt @@ -18,8 +18,12 @@ class Spiral(center: Pair, start: Pair, radius: Int) : Shape return (radius * 2).toDouble().pow(2) } - override fun progress(): Double { - return (count / (radius * 2).toDouble().pow(2)).coerceAtMost(1.0) + override fun progress(maxRadius: Int?): Double { + return if (maxRadius != null) { + (count / (maxRadius * 2).toDouble().pow(2)).coerceAtMost(1.0) + } else { + (count / (radius * 2).toDouble().pow(2)).coerceAtMost(1.0) + } } override fun currentRadius(): Int {