From 4da0d7b1f079b138e1963ca104868106c654eaf5 Mon Sep 17 00:00:00 2001 From: Arindy Date: Sat, 22 Feb 2025 13:53:49 +0100 Subject: [PATCH] caps the number of allowed dice --- README.md | 2 ++ .../de/arindy/dicetower/DiceResource.kt | 28 +++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index affabc4..c588055 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ services: restart: always ports: - "8080:8080" + environment: + DICE_LIMIT: 30 # OPTIONAL: amount of dice allowed to roll (default: 30) ``` Run the container with: ```bash diff --git a/src/main/kotlin/de/arindy/dicetower/DiceResource.kt b/src/main/kotlin/de/arindy/dicetower/DiceResource.kt index ff83dc7..2cf1fff 100644 --- a/src/main/kotlin/de/arindy/dicetower/DiceResource.kt +++ b/src/main/kotlin/de/arindy/dicetower/DiceResource.kt @@ -14,12 +14,19 @@ import jakarta.ws.rs.sse.OutboundSseEvent import jakarta.ws.rs.sse.Sse import jakarta.ws.rs.sse.SseBroadcaster import jakarta.ws.rs.sse.SseEventSink +import org.eclipse.microprofile.config.inject.ConfigProperty +import java.util.Optional import java.util.UUID +private const val LIMIT = 30 + @Path("/dice/{id}") @ApplicationScoped final class DiceResource(@Context val sse: Sse) { + @ConfigProperty(name = "dice.limit") + private lateinit var diceLimit: Optional + private var eventBuilder: OutboundSseEvent.Builder = sse.newEventBuilder() private var sseBroadcasters: MutableMap = HashMap() @@ -30,15 +37,24 @@ final class DiceResource(@Context val sse: Sse) { data.user = id.split(":")[1] val results = ArrayList() + var numberOfDice = 0 data.command.split(" ", "&", "and").filter { it.isNotEmpty() }.map { it.trim() }.toTypedArray().forEach { command -> val dice = command.split("d") - val result = IntArray(dice[0].toInt()) - val sides = dice[1].split("+", "-") - val modifier = if (dice[1].contains("+")) sides[1].toInt() else if (dice[1].contains("+")) -1 * sides[1].toInt() else 0 - repeat(dice[0].toInt()) { index -> - result[index] = (Math.random() * sides[0].toInt() + 1).toInt() + var amount = dice[0].toInt() + val limit = diceLimit.orElse(LIMIT) + if (limit < numberOfDice + amount) { + amount = limit - numberOfDice + } + numberOfDice += amount + if (amount > 0) { + val result = IntArray(amount) + val sides = dice[1].split("+", "-") + val modifier = if (dice[1].contains("+")) sides[1].toInt() else if (dice[1].contains("+")) -1 * sides[1].toInt() else 0 + repeat(amount) { index -> + result[index] = (Math.random() * sides[0].toInt() + 1).toInt() + } + results.add(Results(sides[0].toInt(), modifier, result.sum() + modifier, result.map { Roll(it) }.toTypedArray())) } - results.add(Results(sides[0].toInt(), modifier, result.sum() + modifier, result.map { Roll(it) }.toTypedArray())) } val map = results.map { r -> "${r.rolls.size}d${r.sides}@${