caps the number of allowed dice
This commit is contained in:
parent
2ca9ad6f5a
commit
4da0d7b1f0
@ -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
|
||||
|
@ -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<Int>
|
||||
|
||||
private var eventBuilder: OutboundSseEvent.Builder = sse.newEventBuilder()
|
||||
private var sseBroadcasters: MutableMap<String, SseBroadcaster> = HashMap()
|
||||
|
||||
@ -30,16 +37,25 @@ final class DiceResource(@Context val sse: Sse) {
|
||||
data.user = id.split(":")[1]
|
||||
|
||||
val results = ArrayList<Results>()
|
||||
var numberOfDice = 0
|
||||
data.command.split(" ", "&", "and").filter { it.isNotEmpty() }.map { it.trim() }.toTypedArray<String>().forEach { command ->
|
||||
val dice = command.split("d")
|
||||
val result = IntArray(dice[0].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(dice[0].toInt()) { index ->
|
||||
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()))
|
||||
}
|
||||
}
|
||||
val map = results.map { r ->
|
||||
"${r.rolls.size}d${r.sides}@${
|
||||
if (r.sides == 100) r.rolls.map { roll -> Roll(roll.value / 10 * 10) }
|
||||
|
Loading…
x
Reference in New Issue
Block a user