Skip to content

Commit

Permalink
Store round number in Initiative (#2)
Browse files Browse the repository at this point in the history
### What's done:
* Added property
* Updated display message
  • Loading branch information
petertrr committed Aug 29, 2021
1 parent 8ca5260 commit 3177b5c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ data class Failure(val t: Throwable) : CommandResult()

data class CountdownStarted(val combatant: Combatant, val period: Long) : CommandResult()

data class RoundResult(val combatants: Sequence<Combatant>) : CommandResult()
data class RoundResult(
val roundIdx: Int,
val combatants: Sequence<Combatant>
) : CommandResult()
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Initiative(
private val isInitiativeStarted = AtomicBoolean(false)
internal val isRoundStarted = AtomicBoolean(false)
private val currentCombatantIdx = AtomicInteger(0)
private val currentRound = AtomicInteger(0)

fun hasNextCombatant(): Boolean = currentCombatantIdx.get() <= members.lastIndex

Expand Down Expand Up @@ -83,11 +84,12 @@ class Initiative(
Failure(IllegalStateException("Round is already in progress, next combatant is ${members[currentCombatantIdx.get()].name}. To finish round, call `next` for all participants and then `end-round` before starting the new one"))
} else {
currentCombatantIdx.set(0)
currentRound.incrementAndGet()
// sort members according to current initiative *in place*
members.sortByDescending {
it.getCurrentInitiativeSafe()
}
RoundResult(members.asSequence())
RoundResult(currentRound.get(), members.asSequence())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class InitiativeBot(private val botConfiguration: BotConfiguration) {
val longestLineLength = roundResult.combatants.maxOf {
it.name.length + it.currentInitiative!!.toString().length
}
return "Starting a new round of initiative:" +
return "Starting **round ${roundResult.roundIdx}** of initiative:" +
roundResult.combatants.joinToString(System.lineSeparator(), prefix = System.lineSeparator()) { combatant ->
val owner = userNameByCharacterNameByChannel[channelId]!!.filterValues {
combatant.name in it.characterNames
Expand Down

0 comments on commit 3177b5c

Please sign in to comment.