Skip to content

Commit

Permalink
Fix Smart Action modifiers #316
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Sep 13, 2022
1 parent 7d07c6b commit 0192e0f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class GameFlowImpl<T: Any>(
this.actionDone()
return null
}
actions.types().forEach { it.impl.ruleChecks() }
if (anyPlayerHasAction()) {
this.actionDone()
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import kotlin.reflect.KClass
class SmartActionLogic<T: Any, A: Any>(
val gameContext: GameRuleContext<T>,
override val actionType: ActionType<T, A>
) : GameLogicActionType<T, A> {
) : GameLogicActionType<T, A>, SmartActionChangeScope<T, A> {
private val _handlers = mutableListOf<SmartActionBuilder<T, A>>()
val handlers get() = _handlers.toList()
override val handlers get() = _handlers.toList().asSequence()

override fun isComplex(): Boolean = true

Expand Down Expand Up @@ -63,6 +63,15 @@ class SmartActionLogic<T: Any, A: Any>(
// _handlers.flatMap { it.requires }.all { it.or() }
}

override fun ruleChecks() {
// Apply modifiers
this.handlers.forEach { handler ->
handler._modifiers.forEach { modifier ->
modifier.invoke(this)
}
}
}

fun add(handler: SmartActionBuilder<T, A>) {
this._handlers.add(handler)
}
Expand All @@ -88,6 +97,7 @@ open class SmartActionBuilder<T: Any, A: Any>: SmartActionScope<T, A> {
internal val _choices = mutableMapOf<String, ActionChoice<T, A, out Any>>()
internal val _effect = mutableListOf<ActionEffect<T, A, out Any>>()
internal val _postEffect = mutableListOf<ActionEffect<T, A, out Any>>()
internal val _modifiers = mutableListOf<SmartActionChangeScope<T, A>.() -> Unit>()
val preconditions get() = _preconditions.toList()
val requires get() = _requires.toList()
val choices get() = _choices
Expand All @@ -110,7 +120,7 @@ open class SmartActionBuilder<T: Any, A: Any>: SmartActionScope<T, A> {
}

override fun change(block: SmartActionChangeScope<T, A>.() -> Unit) {
TODO()
this._modifiers.add(block)
}

internal fun iterableToChoices(rule: ActionOptionsScope<T>.() -> Iterable<A>): ActionChoicesScope<T, A>.() -> Unit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface GameLogicActionType<T : Any, P : Any> {
fun createAction(playerIndex: Int, parameter: P): Actionable<T, P>
fun actionInfoKeys(playerIndex: Int, previouslySelected: List<Any>): List<ActionInfoKey> = withChosen(playerIndex, previouslySelected).actionKeys()
fun withChosen(playerIndex: Int, chosen: List<Any>): ActionComplexChosenStep<T, P>
fun ruleChecks() {}
}

@Deprecated("Use an ActionComplexImpl-related class instead")
Expand Down

0 comments on commit 0192e0f

Please sign in to comment.