From a36c4bf7cee718fa71872df7997fbf9c0d98d506 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Wed, 21 Sep 2011 17:41:57 -0400 Subject: [PATCH] add LeaveJoinToken, which exercises new select API, and refactor various tokens to use it --- basetoken.py | 69 +++++++++++++++++++++++++-------------------------- whiteboard.py | 7 +----- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/basetoken.py b/basetoken.py index bb503aa..9cbfb86 100644 --- a/basetoken.py +++ b/basetoken.py @@ -181,7 +181,13 @@ class Terminal(Token): def setColBg(self, colBg): self.colBg = colBg -class AutoJoiner(Token): + +class LeaveJoinToken(Token): + def run(self): + while True: + Event.selectDg(self, (self.game.evLeave, self.OnLeave), (self.game.evJoin, self.OnJoin)) + +class AutoJoiner(LeaveJoinToken): def InitPersistent(self, owner, *rgclsTok): Token.InitPersistent(self, owner) self.rgclsTok = rgclsTok @@ -189,24 +195,22 @@ class AutoJoiner(Token): def InitTransient(self): Token.InitTransient(self) self.mpclient_rgtok = {} - def run(self): - def dgoobLeave(client): - for tok in self.mpclient_rgtok[client]: - print "killing ", tok - tok.die() - while True: - with self.game.evLeave.oob(self, dgoobLeave): - client = self.game.evJoin.receive(self) - rgtok = [] - for clsTok in self.rgclsTok: - if FIter(clsTok): - tok = clsTok[0](self.game, client, *clsTok[1:]) - else: - tok = clsTok(self.game, client) - rgtok.append(tok) - self.mpclient_rgtok[client] = rgtok - -class AliveWithPlayers(Token): + + def OnLeave(self, client): + for tok in self.mpclient_rgtok[client]: + print "killing ", tok + tok.die() + def OnJoin(self, client): + rgtok = [] + for clsTok in self.rgclsTok: + if FIter(clsTok): + tok = clsTok[0](self.game, client, *clsTok[1:]) + else: + tok = clsTok(self.game, client) + rgtok.append(tok) + self.mpclient_rgtok[client] = rgtok + +class AliveWithPlayers(LeaveJoinToken): def InitPersistent(self, owner, *rgclsTok): Token.InitPersistent(self, owner) self.rgclsTok = rgclsTok @@ -220,19 +224,16 @@ class AliveWithPlayers(Token): if len(self.rgclient) == 0: for tok in [x for x in self.rgtokOwn]: tok.die() + def OnJoin(self, client): + self.rgclient.append(client) + if len(self.rgclient) == 1: + for clsTok in self.rgclsTok: + if FIter(clsTok): + tok = clsTok[0](self, clsTok[1:]) + else: + tok = clsTok(self) - def run(self): - with self.game.evLeave.oob(self, self.OnLeave): - while True: - self.rgclient.append(self.game.evJoin.receive(self)) - if len(self.rgclient) == 1: - for clsTok in self.rgclsTok: - if FIter(clsTok): - tok = clsTok[0](self, clsTok[1:]) - else: - tok = clsTok(self) - -class AutoKiller(Token): +class AutoKiller(LeaveJoinToken): def InitTransient(self): Token.InitTransient(self) self.rgclient = [] @@ -240,10 +241,8 @@ class AutoKiller(Token): self.rgclient.remove(client) if len(self.rgclient) == 0: self.game.finish() - def run(self): - with self.game.evLeave.oob(self, self.OnLeave): - while True: - self.rgclient.append(self.game.evJoin.receive(self)) + def OnJoin(self, client): + self.rgclient.append(client) #selection oracle class Selor(TPrs): diff --git a/whiteboard.py b/whiteboard.py index 16df774..5fe8d27 100644 --- a/whiteboard.py +++ b/whiteboard.py @@ -535,7 +535,7 @@ class MiChat(Mi): ascr.PutAch(ansi.MkAch(chr(186), ansi.RED, ansi.WHITE), xBar, yBar + 1) ascr.PutAch(ansi.MkAch("Z", ansi.RED, ansi.WHITE), xBar, yBar + 2) -class Joiner(Token): +class Joiner(LeaveJoinToken): def InitPersistent(self, owner): Token.InitPersistent(self, owner) self.msgScroller = self.game.rgtoken("msgscroller")[0] @@ -556,11 +556,6 @@ class Joiner(Token): self.msgScroller.evPost.fire(client.cld.user + " has left! :(") self.board.chat.Add(ChlSys(client, "has left")) - def run(self): - while True: - with self.game.evJoin.oob(self, self.OnJoin): - self.OnLeave(self.game.evLeave.receive(self)) - class Lobby(TokenClient): def InitPersistent(self, owner, client): TokenClient.InitPersistent(self, owner, client)