Skip to content

Commit

Permalink
fix: Get onFail working in actions
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Jul 29, 2024
1 parent e506c84 commit 7c14aa3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ eclipse
kotlin-js-store/

geary-benchmarks/.results
/truckconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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.ActionOnFail
import com.mineinabyss.geary.actions.event_binds.ActionRegister
import com.mineinabyss.geary.actions.event_binds.ActionWhen
import com.mineinabyss.geary.modules.geary
Expand All @@ -15,6 +16,7 @@ class ActionEntry(
val action: Action,
val conditions: List<EnsureAction>?,
val register: String?,
val onFail: ActionGroup?,
)

@Serializable(with = ActionGroup.Serializer::class)
Expand All @@ -33,19 +35,21 @@ class ActionGroup(
if (entry.register != null)
context.register(entry.register, returned)
} catch (e: ActionsCancelledException) {
entry.onFail?.execute(context)
return
}
}
}

object Serializer : InnerSerializer<List<SerializedComponents>, ActionGroup>(
class Serializer : InnerSerializer<List<SerializedComponents>, ActionGroup>(
serialName = "geary:action_group",
inner = ListSerializer(
PolymorphicListAsMapSerializer.ofComponents(
PolymorphicListAsMapSerializer.Config(
customKeys = mapOf(
"when" to ActionWhen.serializer(),
"register" to ActionRegister.serializer()
"when" to { ActionWhen.serializer() },
"register" to { ActionRegister.serializer() },
"onFail" to { ActionOnFail.serializer() }
)
)
)
Expand All @@ -56,10 +60,12 @@ class ActionGroup(
var action: Action? = null
var condition: List<EnsureAction>? = null
var register: String? = null
var onFail: ActionGroup? = null
components.forEach { comp ->
when {
comp is ActionWhen -> condition = comp.conditions
comp is ActionRegister -> register = comp.register
comp is ActionOnFail -> onFail = comp.action
action != null -> geary.logger.w { "Multiple actions defined in one block!" }
else -> action = EmitEventAction.wrapIfNotAction(comp)
}
Expand All @@ -68,7 +74,8 @@ class ActionGroup(
ActionEntry(
action = action!!,
conditions = condition,
register = register
register = register,
onFail = onFail
)
}
ActionGroup(actions)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.mineinabyss.geary.actions.actions

import com.mineinabyss.geary.actions.Action
import com.mineinabyss.geary.actions.ActionsCancelledException
import com.mineinabyss.geary.actions.ActionGroupContext
import com.mineinabyss.geary.actions.Condition
import com.mineinabyss.geary.actions.event_binds.EventBind
import com.mineinabyss.geary.actions.*
import com.mineinabyss.geary.helpers.componentId
import com.mineinabyss.geary.serialization.serializers.InnerSerializer
import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer
Expand All @@ -23,7 +19,9 @@ class EnsureAction(
flat.forEach { (id, data) ->
when (data) {
is Condition -> with(data) {
if(!execute()) throw ActionsCancelledException()
if(!execute()) {
throw ActionsCancelledException()
}
}
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 @@ -17,7 +17,7 @@ class EntityObservers(
serialName = "geary:observe",
inner = MapSerializer(
SerializableComponentId.serializer(),
ActionGroup.Serializer
ActionGroup.serializer()
),
inverseTransform = { TODO() },
transform = {
Expand All @@ -33,7 +33,7 @@ class EntityObservers(

@Serializable(with = ActionWhen.Serializer::class)
class ActionWhen(val conditions: List<EnsureAction>) {
object Serializer : InnerSerializer<List<EnsureAction>, ActionWhen>(
class Serializer : InnerSerializer<List<EnsureAction>, ActionWhen>(
serialName = "geary:when",
inner = ListSerializer(EnsureAction.serializer()),
inverseTransform = ActionWhen::conditions,
Expand All @@ -44,3 +44,13 @@ class ActionWhen(val conditions: List<EnsureAction>) {
@JvmInline
@Serializable
value class ActionRegister(val register: String)

@Serializable(with = ActionOnFail.Serializer::class)
class ActionOnFail(val action: ActionGroup) {
class Serializer : InnerSerializer<ActionGroup, ActionOnFail>(
serialName = "geary:on_fail",
inner = ActionGroup.Serializer(),
inverseTransform = ActionOnFail::action,
transform = ::ActionOnFail
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ open class PolymorphicListAsMapSerializer<T : Any>(
}

else -> {
val componentSerializer = config.customKeys[key] ?: findSerializerFor(compositeDecoder.serializersModule, namespaces, key)
val componentSerializer = config.customKeys[key]?.invoke() ?: findSerializerFor(compositeDecoder.serializersModule, namespaces, key)
.getOrElse {
if (config.onMissingSerializer != OnMissing.IGNORE) {
config.whenComponentMalformed(key)
Expand Down Expand Up @@ -122,7 +122,7 @@ open class PolymorphicListAsMapSerializer<T : Any>(
val onMissingSerializer: OnMissing = OnMissing.WARN,
val skipMalformedComponents: Boolean = true,
val whenComponentMalformed: (String) -> Unit = {},
val customKeys: Map<String, KSerializer<out T>> = mapOf(),
val customKeys: Map<String, () -> KSerializer<out T>> = mapOf(),
)

companion object {
Expand Down

0 comments on commit 7c14aa3

Please sign in to comment.