Skip to content

Commit

Permalink
feat: with tag for actions to support passing environment
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Aug 23, 2024
1 parent c72731a commit 07f082d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.mineinabyss.geary.actions
import kotlin.jvm.JvmName

interface Action {
/** Should this action create a copy of [ActionGroupContext] to run or not? */
val useSubcontext: Boolean get() = true

fun ActionGroupContext.execute(): Any?

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.mineinabyss.geary.actions

import com.mineinabyss.geary.actions.actions.EmitEventAction
import com.mineinabyss.geary.actions.actions.EnsureAction
import com.mineinabyss.geary.actions.event_binds.ActionLoop
import com.mineinabyss.geary.actions.event_binds.ActionOnFail
import com.mineinabyss.geary.actions.event_binds.ActionRegister
import com.mineinabyss.geary.actions.event_binds.ActionWhen
import com.mineinabyss.geary.actions.event_binds.*
import com.mineinabyss.geary.actions.expressions.Expression
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.serialization.serializers.InnerSerializer
Expand All @@ -20,15 +17,18 @@ class ActionEntry(
val register: String?,
val onFail: ActionGroup?,
val loop: Expression<List<Any>>?,
val environment: Map<String, Expression<Any>>?,
)

@Serializable(with = ActionGroup.Serializer::class)
class ActionGroup(
val actions: List<ActionEntry>,
): Action {
) : Action {
override fun ActionGroupContext.execute() {
val context = this
actions.forEach { entry ->
val context = if (entry.action.useSubcontext)
entry.environment?.let { env -> this.plus(env.mapValues { eval(it.value) }) } ?: this
else this
try {
if (entry.loop != null) {
entry.loop.evaluate(context).forEach { loopEntry ->
Expand Down Expand Up @@ -78,12 +78,16 @@ class ActionGroup(
var register: String? = null
var loop: Expression<List<Any>>? = null
var onFail: ActionGroup? = null
var environment: Map<String, Expression<Any>>? = null
components.forEach { comp ->
when {
comp is ActionWhen -> condition = comp.conditions
comp is ActionRegister -> register = comp.register
comp is ActionOnFail -> onFail = comp.action
comp is ActionLoop -> loop = Expression.parseExpression(comp.expression, serializersModule) as Expression<List<Any>>
comp is ActionLoop -> loop =
Expression.parseExpression(comp.expression, serializersModule) as Expression<List<Any>>

comp is ActionEnvironment -> environment = comp.environment
action != null -> geary.logger.w { "Multiple actions defined in one block!" }
else -> action = EmitEventAction.wrapIfNotAction(comp)
}
Expand All @@ -94,7 +98,8 @@ class ActionGroup(
conditions = condition,
register = register,
onFail = onFail,
loop = loop
loop = loop,
environment = environment
)
}
ActionGroup(actions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class ActionGroupContext() {
this.entity = entity
}

var entity: GearyEntity
get() = environment["entity"] as GearyEntity
var entity: GearyEntity?
get() = environment["entity"] as? GearyEntity
set(value) {
environment["entity"] = value
}
Expand All @@ -28,5 +28,11 @@ class ActionGroupContext() {
return newContext
}

fun plus(newEnvironment: Map<String, Any?>): ActionGroupContext {
val newContext = copy()
newContext.environment.putAll(newEnvironment)
return newContext
}

companion object
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import kotlinx.serialization.Serializable
class BecomeAction(
val become: EntityExpression,
) : Action {
override val useSubcontext: Boolean = false

override fun ActionGroupContext.execute() {
entity = become.evaluate(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class EmitEventAction(
val data: Any?,
) : Action {
override fun ActionGroupContext.execute() {
entity.emit(event = eventId, data = data)
entity?.emit(event = eventId, data = data)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class EnsureAction(
}
}

else -> entity.emit(id, data) //TODO use geary condition system if we get one
else -> entity?.emit(id, data) //TODO use geary condition system if we get one
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package com.mineinabyss.geary.actions.event_binds

import com.mineinabyss.geary.actions.ActionGroup
import com.mineinabyss.geary.actions.actions.EnsureAction
import com.mineinabyss.geary.actions.expressions.Expression
import com.mineinabyss.geary.serialization.serializers.InnerSerializer
import com.mineinabyss.geary.serialization.serializers.SerializableComponentId
import kotlinx.serialization.Contextual
import kotlinx.serialization.ContextualSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.builtins.serializer
import kotlin.jvm.JvmInline

@Serializable(with = EntityObservers.Serializer::class)
Expand Down Expand Up @@ -58,3 +62,13 @@ class ActionOnFail(val action: ActionGroup) {
@JvmInline
@Serializable
value class ActionLoop(val expression: String)

@Serializable
class ActionEnvironment(val environment: Map<String, Expression<@Contextual Any>>) {
object Serializer : InnerSerializer<Map<String, Expression<@Contextual Any>>, ActionEnvironment>(
serialName = "geary:with",
inner = MapSerializer(String.serializer(), Expression.serializer(ContextualSerializer(Any::class))),
inverseTransform = ActionEnvironment::environment,
transform = { ActionEnvironment(it) }
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ value class EntityExpression(
val expression: String,
) : Expression<GearyEntity> {
override fun evaluate(context: ActionGroupContext): GearyEntity {
return if (expression == "parent") context.entity.parent!!
return if (expression == "parent") context.entity?.parent!!
else Expression.Variable<GearyEntity>(expression).evaluate(context)
}
}

0 comments on commit 07f082d

Please sign in to comment.