From c72731a65c01ef11921b9701bf6e5138757f39a5 Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Wed, 21 Aug 2024 22:36:20 -0400 Subject: [PATCH] feat: add conditionsMet helper to EnsureAction feat: Use environment for entity in ActionGroupContext, allow creating without an entity --- .../mineinabyss/geary/actions/ActionGroup.kt | 4 ++-- .../geary/actions/ActionGroupContext.kt | 18 ++++++++++++++---- .../geary/actions/actions/EnsureAction.kt | 14 ++++++++++++-- gradle.properties | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroup.kt b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroup.kt index 375a0348..2d0cdde9 100644 --- a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroup.kt +++ b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroup.kt @@ -47,10 +47,10 @@ class ActionGroup( private fun executeEntry(context: ActionGroupContext, entry: ActionEntry) { entry.conditions?.forEach { condition -> - with(condition) { context.execute() } + condition.execute(context) } - val returned = with(entry.action) { context.execute() } + val returned = entry.action.execute(context) if (entry.register != null) context.register(entry.register, returned) diff --git a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroupContext.kt b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroupContext.kt index 5f070db4..bc8e7244 100644 --- a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroupContext.kt +++ b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroupContext.kt @@ -3,9 +3,17 @@ package com.mineinabyss.geary.actions import com.mineinabyss.geary.actions.expressions.Expression import com.mineinabyss.geary.datatypes.GearyEntity -class ActionGroupContext( - var entity: GearyEntity, -) { +class ActionGroupContext() { + constructor(entity: GearyEntity) : this() { + this.entity = entity + } + + var entity: GearyEntity + get() = environment["entity"] as GearyEntity + set(value) { + environment["entity"] = value + } + val environment: MutableMap = mutableMapOf() fun eval(expression: Expression): T = expression.evaluate(this) @@ -15,8 +23,10 @@ class ActionGroupContext( } fun copy(): ActionGroupContext { - val newContext = ActionGroupContext(entity) + val newContext = ActionGroupContext() newContext.environment.putAll(environment) return newContext } + + companion object } diff --git a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/EnsureAction.kt b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/EnsureAction.kt index 5b673a5e..cec7525f 100644 --- a/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/EnsureAction.kt +++ b/addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/EnsureAction.kt @@ -19,16 +19,26 @@ class EnsureAction( flat.forEach { (id, data) -> when (data) { is Condition -> with(data) { - if(!execute()) { + if (!execute()) { throw ActionsCancelledException() } } + else -> entity.emit(id, data) //TODO use geary condition system if we get one } } } - object Serializer: InnerSerializer( + fun conditionsMet(context: ActionGroupContext): Boolean { + try { + execute(context) + } catch (e: ActionsCancelledException) { + return false + } + return true + } + + object Serializer : InnerSerializer( serialName = "geary:ensure", inner = PolymorphicListAsMapSerializer.ofComponents(), inverseTransform = { it.conditions }, diff --git a/gradle.properties b/gradle.properties index f426544d..d28ee949 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,5 +2,5 @@ group=com.mineinabyss version=0.26 # Workaround for dokka builds failing on CI, see https://github.com/Kotlin/dokka/issues/1405 #org.gradle.jvmargs=-XX:MaxMetaspaceSize=512m -idofrontVersion=0.24.0 +idofrontVersion=0.25.3 kotlin.native.ignoreDisabledTargets=true diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a4413138..9355b415 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME