Skip to content

Commit

Permalink
feat: add conditionsMet helper to EnsureAction
Browse files Browse the repository at this point in the history
feat: Use environment for entity in ActionGroupContext, allow creating without an entity
  • Loading branch information
0ffz committed Aug 22, 2024
1 parent 1c136d4 commit c72731a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any?> = mutableMapOf()

fun <T> eval(expression: Expression<T>): T = expression.evaluate(this)
Expand All @@ -15,8 +23,10 @@ class ActionGroupContext(
}

fun copy(): ActionGroupContext {
val newContext = ActionGroupContext(entity)
val newContext = ActionGroupContext()
newContext.environment.putAll(environment)
return newContext
}

companion object
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SerializedComponents, EnsureAction>(
fun conditionsMet(context: ActionGroupContext): Boolean {
try {
execute(context)
} catch (e: ActionsCancelledException) {
return false
}
return true
}

object Serializer : InnerSerializer<SerializedComponents, EnsureAction>(
serialName = "geary:ensure",
inner = PolymorphicListAsMapSerializer.ofComponents(),
inverseTransform = { it.conditions },
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit c72731a

Please sign in to comment.