Skip to content

Commit

Permalink
Remove old-style event triggers #320
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Dec 4, 2022
1 parent 1a6c93d commit 23aefaa
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import net.zomis.games.api.MetaScope
import net.zomis.games.api.UsageScope
import net.zomis.games.common.PlayerIndex
import net.zomis.games.dsl.events.EventFactory
import net.zomis.games.dsl.events.EventSource
import net.zomis.games.dsl.flow.GameMetaScope
import net.zomis.games.dsl.impl.ActionOptionsContext
import net.zomis.games.dsl.impl.GameMarker
Expand Down Expand Up @@ -121,21 +120,3 @@ interface ActionChoicesScope<T : Any, P : Any> : UsageScope {
fun <E : Any> options(options: ActionOptionsScope<T>.() -> Iterable<E>, next: ActionChoicesScope<T, P>.(E) -> Unit)
fun <E : Any> optionsWithIds(options: ActionOptionsScope<T>.() -> Iterable<Pair<String, E>>, next: ActionChoicesScope<T, P>.(E) -> Unit)
}

@GameMarker
@Deprecated("Use Events instead")
interface GameRuleTriggerScope<T, E> : UsageScope {
val game: T
val trigger: E
val replayable: ReplayStateI
val eliminations: PlayerEliminationsWrite
}

@Deprecated("old-style event system. Use Event class instead")
interface GameRuleTrigger<T : Any, E : Any> {
fun effect(effect: GameRuleTriggerScope<T, E>.() -> Unit): GameRuleTrigger<T, E>
fun map(mapping: GameRuleTriggerScope<T, E>.() -> E): GameRuleTrigger<T, E>
fun after(effect: GameRuleTriggerScope<T, E>.() -> Unit): GameRuleTrigger<T, E>
fun ignoreEffectIf(condition: GameRuleTriggerScope<T, E>.() -> Boolean): GameRuleTrigger<T, E>
operator fun invoke(trigger: E): E?
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.zomis.games.dsl.impl

import net.zomis.games.PlayerEliminationsWrite
import net.zomis.games.common.PlayerIndex
import net.zomis.games.dsl.*
import net.zomis.games.dsl.events.EventFactory
Expand Down Expand Up @@ -273,54 +272,3 @@ class GameActionRuleContext<T : Any, A : Any>(
override fun isComplex(): Boolean = this.choices != null

}

@Deprecated("old-style event system. Use Event class instead")
data class GameRuleTriggerContext<T : Any, E : Any>(
override val game: T,
override val trigger: E,
override val replayable: ReplayStateI,
override val eliminations: PlayerEliminationsWrite
): GameRuleTriggerScope<T, E>

@Deprecated("old-style event system. Use Event class instead")
class GameRuleTriggerImpl<T : Any, E : Any>(
val gameContext: GameMetaScope<T>,
) : GameRuleTrigger<T, E> {
private val effects = mutableListOf<GameRuleTriggerScope<T, E>.() -> Unit>()
private val mappings = mutableListOf<GameRuleTriggerScope<T, E>.() -> E>()
private val ignoreConditions = mutableListOf<GameRuleTriggerScope<T, E>.() -> Boolean>()
private val after = mutableListOf<GameRuleTriggerScope<T, E>.() -> Unit>()

override fun effect(effect: GameRuleTriggerScope<T, E>.() -> Unit): GameRuleTrigger<T, E> {
this.effects.add(effect)
return this
}

override fun map(mapping: GameRuleTriggerScope<T, E>.() -> E): GameRuleTrigger<T, E> {
this.mappings.add(mapping)
return this
}

override fun after(effect: GameRuleTriggerScope<T, E>.() -> Unit): GameRuleTrigger<T, E> {
this.after.add(effect)
return this
}

override fun ignoreEffectIf(condition: GameRuleTriggerScope<T, E>.() -> Boolean): GameRuleTrigger<T, E> {
this.ignoreConditions.add(condition)
return this
}

override fun invoke(trigger: E): E? {
val result = mappings.fold(GameRuleTriggerContext(gameContext.game, trigger, gameContext.replayable, gameContext.eliminations)) {
acc, next -> acc.copy(trigger = next.invoke(acc))
}
val process = ignoreConditions.none { it.invoke(result) }
if (process) {
effects.forEach { it.invoke(result) }
}
after.forEach { it.invoke(result) }
return if (process) result.trigger else null
}

}

0 comments on commit 23aefaa

Please sign in to comment.