# HG changeset patch # User Jeremy Thurgood # Date 1378046353 -7200 # Node ID 0e49648f8d74ea44788458d609aa8453b2e24de6 # Parent c62ed518e5c84ddb45517dfdaf8cc10f112d49b9 Arbitrary function condition. diff -r c62ed518e5c8 -r 0e49648f8d74 nagslang/environment.py --- a/nagslang/environment.py Sun Sep 01 16:14:35 2013 +0200 +++ b/nagslang/environment.py Sun Sep 01 16:39:13 2013 +0200 @@ -9,6 +9,11 @@ class AllConditions(ProtagonistCondition): + """Condition that is met if all provided conditions are met. + + Conditions are evaluated lazily, so the first condition to fail stops + processing. + """ def __init__(self, *conditions): self.conditions = conditions @@ -20,6 +25,11 @@ class AnyCondition(ProtagonistCondition): + """Condition that is met if any provided condition is met. + + Conditions are evaluated lazily, so the first condition to pass stops + processing. + """ def __init__(self, *conditions): self.conditions = conditions @@ -31,16 +41,22 @@ class WolfFormCondition(ProtagonistCondition): + """Condition that is met if the protagonist is in wolf form. + """ def check(self, protagonist): return protagonist.in_wolf_form() class HumanFormCondition(ProtagonistCondition): + """Condition that is met if the protagonist is in human form. + """ def check(self, protagonist): return protagonist.in_human_form() class ItemRequiredCondition(ProtagonistCondition): + """Condition that is met if the protagonist has the required item. + """ def __init__(self, required_item): self.required_item = required_item @@ -48,6 +64,16 @@ return protagonist.has_item(self.required_item) +class FunctionCondition(ProtagonistCondition): + """Condition that is met if the provided function returns `True`. + """ + def __init__(self, func): + self.func = func + + def check(self, protagonist): + return self.func(protagonist) + + class Action(object): """Representation of an action that can be performed. diff -r c62ed518e5c8 -r 0e49648f8d74 nagslang/tests/test_environment.py --- a/nagslang/tests/test_environment.py Sun Sep 01 16:14:35 2013 +0200 +++ b/nagslang/tests/test_environment.py Sun Sep 01 16:39:13 2013 +0200 @@ -88,6 +88,11 @@ self.assert_met(item, FakeProtagonist(item=True)) self.assert_not_met(item, FakeProtagonist(item=False)) + def test_function_condition(self): + condition = environment.FunctionCondition(lambda p: p.startswith('x')) + self.assert_met(condition, 'xxx') + self.assert_not_met(condition, 'yyy') + class TestActions(TestCase): def setUp(self):