1
0
Fork 0

add support for parameters to logic triggers

This commit is contained in:
Sean Sube 2024-05-19 17:12:53 -05:00
parent 132682f015
commit adeb17551c
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
1 changed files with 24 additions and 5 deletions

View File

@ -1,7 +1,7 @@
from functools import partial, wraps
from logging import getLogger
from random import random
from typing import Callable, Dict, List, Optional
from typing import Dict, List, Optional, Protocol
from pydantic import Field
from rule_engine import Rule
@ -22,6 +22,12 @@ class LogicLabel:
rule: Optional[str] = None
@dataclass
class LogicRuleTrigger:
function: str
parameters: Optional[Attributes] = Field(default_factory=dict)
@dataclass
class LogicRule:
chance: float = 1.0
@ -30,7 +36,7 @@ class LogicRule:
remove: Optional[List[str]] = None
rule: Optional[str] = None
set: Optional[Attributes] = None
trigger: Optional[List[str]] = None
trigger: Optional[List[str | LogicRuleTrigger]] = None
@dataclass
@ -39,7 +45,12 @@ class LogicTable:
labels: List[LogicLabel] = Field(default_factory=list)
LogicTrigger = Callable[[WorldEntity], None]
class LogicTrigger(Protocol):
def __call__(self, entity: WorldEntity, **kwargs):
# comment to keep lint from combining this with the next line
...
TriggerTable = Dict[str, LogicTrigger]
@ -104,8 +115,16 @@ def update_attributes(
if rule.trigger:
for trigger in rule.trigger:
if trigger in triggers:
triggers[trigger](entity)
if isinstance(trigger, str):
trigger_name = trigger
trigger_params: Attributes = {}
else:
trigger_name = trigger.function
trigger_params = trigger.parameters or {}
if trigger_name in triggers:
trigger_function = triggers[trigger_name]
trigger_function(entity, **trigger_params)
def update_logic(