From 9bde1fdb76e99721fed2309d79c9a5500a0090c7 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Wed, 21 Sep 2011 17:40:54 -0400 Subject: [PATCH] select: listen to multiple events at once --- engine.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/engine.py b/engine.py index 55f9211..4929ada 100644 --- a/engine.py +++ b/engine.py @@ -34,6 +34,33 @@ class Event(TPrs): if ev in tok.mpevDgoob and tok.mpevDgoob[ev](value): raise Event.OobBreakException() + @classmethod + def select(cls, tok, *rgev): + for ev in rgev: + ev.rgtk.append(tok) + chNotify = tok.chEv + fOob = False + while True: + evRcv, value = chNotify.receive() + if evRcv in tok.mpevDgoob: + if tok.mpevDgoob[ev](value): + fOob = True + break + else: + break + for ev in rgev: + if ev != evRcv: + ev.rgtk.remove(tok) + if fOob: + raise Event.OobBreakException() + return evRcv, value + + @classmethod + def selectDg(cls, tok, *rgev_dg): + rgev, rgdg = zip(*rgev_dg) + ev, value = cls.select(tok, *rgev) + rgdg[rgev.index(ev)](value) + def fire(self, value = None): self.stkValue.append(value) self.game.queueEv.append(self)