add LeaveJoinToken, which exercises new select API, and refactor various tokens to use it

This commit is contained in:
Jeremy Penner 2011-09-21 17:41:57 -04:00
parent 9bde1fdb76
commit a36c4bf7ce
2 changed files with 35 additions and 41 deletions

View file

@ -181,7 +181,13 @@ class Terminal(Token):
def setColBg(self, colBg): def setColBg(self, colBg):
self.colBg = 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): def InitPersistent(self, owner, *rgclsTok):
Token.InitPersistent(self, owner) Token.InitPersistent(self, owner)
self.rgclsTok = rgclsTok self.rgclsTok = rgclsTok
@ -189,24 +195,22 @@ class AutoJoiner(Token):
def InitTransient(self): def InitTransient(self):
Token.InitTransient(self) Token.InitTransient(self)
self.mpclient_rgtok = {} self.mpclient_rgtok = {}
def run(self):
def dgoobLeave(client): def OnLeave(self, client):
for tok in self.mpclient_rgtok[client]: for tok in self.mpclient_rgtok[client]:
print "killing ", tok print "killing ", tok
tok.die() tok.die()
while True: def OnJoin(self, client):
with self.game.evLeave.oob(self, dgoobLeave): rgtok = []
client = self.game.evJoin.receive(self) for clsTok in self.rgclsTok:
rgtok = [] if FIter(clsTok):
for clsTok in self.rgclsTok: tok = clsTok[0](self.game, client, *clsTok[1:])
if FIter(clsTok): else:
tok = clsTok[0](self.game, client, *clsTok[1:]) tok = clsTok(self.game, client)
else: rgtok.append(tok)
tok = clsTok(self.game, client) self.mpclient_rgtok[client] = rgtok
rgtok.append(tok)
self.mpclient_rgtok[client] = rgtok class AliveWithPlayers(LeaveJoinToken):
class AliveWithPlayers(Token):
def InitPersistent(self, owner, *rgclsTok): def InitPersistent(self, owner, *rgclsTok):
Token.InitPersistent(self, owner) Token.InitPersistent(self, owner)
self.rgclsTok = rgclsTok self.rgclsTok = rgclsTok
@ -220,19 +224,16 @@ class AliveWithPlayers(Token):
if len(self.rgclient) == 0: if len(self.rgclient) == 0:
for tok in [x for x in self.rgtokOwn]: for tok in [x for x in self.rgtokOwn]:
tok.die() 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): class AutoKiller(LeaveJoinToken):
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):
def InitTransient(self): def InitTransient(self):
Token.InitTransient(self) Token.InitTransient(self)
self.rgclient = [] self.rgclient = []
@ -240,10 +241,8 @@ class AutoKiller(Token):
self.rgclient.remove(client) self.rgclient.remove(client)
if len(self.rgclient) == 0: if len(self.rgclient) == 0:
self.game.finish() self.game.finish()
def run(self): def OnJoin(self, client):
with self.game.evLeave.oob(self, self.OnLeave): self.rgclient.append(client)
while True:
self.rgclient.append(self.game.evJoin.receive(self))
#selection oracle #selection oracle
class Selor(TPrs): class Selor(TPrs):

View file

@ -535,7 +535,7 @@ class MiChat(Mi):
ascr.PutAch(ansi.MkAch(chr(186), ansi.RED, ansi.WHITE), xBar, yBar + 1) 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) ascr.PutAch(ansi.MkAch("Z", ansi.RED, ansi.WHITE), xBar, yBar + 2)
class Joiner(Token): class Joiner(LeaveJoinToken):
def InitPersistent(self, owner): def InitPersistent(self, owner):
Token.InitPersistent(self, owner) Token.InitPersistent(self, owner)
self.msgScroller = self.game.rgtoken("msgscroller")[0] self.msgScroller = self.game.rgtoken("msgscroller")[0]
@ -556,11 +556,6 @@ class Joiner(Token):
self.msgScroller.evPost.fire(client.cld.user + " has left! :(") self.msgScroller.evPost.fire(client.cld.user + " has left! :(")
self.board.chat.Add(ChlSys(client, "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): class Lobby(TokenClient):
def InitPersistent(self, owner, client): def InitPersistent(self, owner, client):
TokenClient.InitPersistent(self, owner, client) TokenClient.InitPersistent(self, owner, client)