Permalink
Browse files

Implement Switch statement for the dsl

  • Loading branch information...
jleclanche committed Mar 24, 2016
1 parent 8ce943a commit 9ee5b036dc3d822b7b74cdd5cd3c8081c0a498c7
Showing with 29 additions and 0 deletions.
  1. +1 −0 fireplace/dsl/__init__.py
  2. +28 −0 fireplace/dsl/switch.py
@@ -3,3 +3,4 @@
from .lazynum import *
from .random_picker import *
from .selector import *
from .switch import *
@@ -0,0 +1,28 @@
class Switch:
"""
Switch statement on the ID of an entity
Perform actions as described in the map
"""
def __init__(self, selector, map):
self.selector = selector
self.map = map
@property
def default(self):
return self.map.get(None, ())
return ret
def evaluate(self, source):
entities = self.selector.eval(source.game, source)
if not entities:
return self.default
assert len(entities) == 1, "Switch() on more than 1 entity: %r" % (entities)
id = entities[0].id
if id not in self.map:
return self.default
return self.map[id]
def trigger(self, source):
action = self.evaluate(source)
if action:
action.trigger(source)

0 comments on commit 9ee5b03

Please sign in to comment.