def __init__(self, jid, password, pollPeriod, dealerjid, testCase): self.dealerjid = dealerjid self.testCase = testCase self.jid = jid self.q = Queue.Queue() self.pollPeriod = pollPeriod self.messenger = XmppMessenger(jid, password) self.messenger.evt_messageReceived += self.on_messageReceived self.messenger.listen('localhost', 5222)
def __init__(self, jid, password): self.jid = jid self.password = password self.messenger = XmppMessenger(jid, password) self.messenger.evt_messageReceived += self.on_messageReceived self.messenger.listen('localhost', 5222) self.messenger.sendMessage('dealer@pokerchat', 'JOIN') self.chips = 0 self.cards = [] self.action = { 'GO': self.makeBet, 'BET': self.recordBet, 'CARD': self.acceptCard, 'WON': self.recordWinnings, 'DEALING': self.initialiseHand, 'CHIPS': self.acceptChips }
def __init__(self, jid, password): self.jid = jid self.password = password self.messenger = XmppMessenger(jid, password) self.messenger.evt_messageReceived += self.on_messageReceived self.messenger.listen('localhost', 5222) self.messenger.sendMessage('dealer@pokerchat', 'JOIN') self.chips = 0 self.cards = [] self.action = {'GO': self.makeBet, 'BET': self.recordBet, 'CARD': self.acceptCard, 'WON': self.recordWinnings, 'DEALING': self.initialiseHand, 'CHIPS': self.acceptChips}
class SamplePlayer(object): def __init__(self, jid, password): self.jid = jid self.password = password self.messenger = XmppMessenger(jid, password) self.messenger.evt_messageReceived += self.on_messageReceived self.messenger.listen('localhost', 5222) self.messenger.sendMessage('dealer@pokerchat', 'JOIN') self.chips = 0 self.cards = [] self.action = {'GO': self.makeBet, 'BET': self.recordBet, 'CARD': self.acceptCard, 'WON': self.recordWinnings, 'DEALING': self.initialiseHand, 'CHIPS': self.acceptChips} def on_messageReceived(self, sender, msg): args = msg[1].split(' ') self.action[args[0]](args[1:]) def acceptChips(self, args): self.chips = int(args[0]) def initialiseHand(self, args): self.bets = dict(zip(args, [0 for player in args])) print 'Chips', self.chips def recordWinnings(self, args): winner = args[0] loser = args[1] amount = int(args[2]) if self.jid == winner: self.chips += amount if self.jid == loser: self.chips[loser] -= amount def makeBet(self, args): self.messenger.sendMessage('dealer@pokerchat', self.minBet()) def recordBet(self, args): player = args[0] amount = int(args[1]) self.bets[player] += amount def acceptCard(self, args): cards = [(card[0:-1], card[-1]) for card in args] self.cards = self.cards + cards def minBet(self): highest = max([self.bets[k] for k in self.bets]) myContribution = self.bets[self.jid] bet = min(max(highest - myContribution, 100), self.chips) self.chips -= bet return bet
def swallowMessages(self): m = XmppMessenger('dealer@pokerchat', 'password') m.listen('localhost', 5222) time.sleep(2) m.finish() m = XmppMessenger('audience@pokerchat', 'password') m.listen('localhost', 5222) time.sleep(2) m.finish()
def __init__(self, opts): self.opts = opts self.done = False self.msngr = XmppMessenger(self.opts.dealerjid, self.opts.dealerpwd)
class RunsPokerGame(object): def __init__(self, opts): self.opts = opts self.done = False self.msngr = XmppMessenger(self.opts.dealerjid, self.opts.dealerpwd) def onDone(self, sender=None, args=None): self.msngr.sendMessage(self.opts.audiencejid, 'Game Over') self.msngr.finish() self.done = True def start(self): try: self.msngr.listen(self.opts.domain, self.opts.port) self.msngr.addTarget(self.opts.audiencejid) interacts = InteractsWithPlayers(self.msngr, self.opts.chips) startMessage = 'Game started, waiting for players' self.msngr.sendMessage(self.opts.audiencejid, startMessage) Thread(target=countdown, args=(self.opts.wait,)).start() time.sleep(self.opts.wait) except: print traceback.format_exc() if not interacts.players: msg = 'No players joined so quitting' self.msngr.sendMessage(self.opts.audiencejid, msg) print msg elif len(interacts.players) == 1: msg = 'Not enough players for a game so quitting' self.msngr.sendMessage(self.opts.audiencejid, msg) print msg else: try: game = PlaysTournament(interacts) game.evt_done += self.onDone game.start() while not self.done: time.sleep(1) except: print traceback.format_exc()
class FakeParticipant(): def __init__(self, jid, password, pollPeriod, dealerjid, testCase): self.dealerjid = dealerjid self.testCase = testCase self.jid = jid self.q = Queue.Queue() self.pollPeriod = pollPeriod self.messenger = XmppMessenger(jid, password) self.messenger.evt_messageReceived += self.on_messageReceived self.messenger.listen('localhost', 5222) def says(self, said): self.messenger.sendMessage(self.dealerjid, said) def _getMessage(self): end = time.time() + self.pollPeriod message = None while time.time() <= end and not message: try: message = self.q.get_nowait() except Queue.Empty: pass return message def hears(self, shouldHear): message = self._getMessage() self.assertMessage(message, shouldHear) def eventuallyHears(self, shouldHear): end = time.time() + self.pollPeriod message = None while time.time() <= end: try: message = self.q.get_nowait() if message and message.startswith(shouldHear[:-3]): break except Queue.Empty: pass self.assertMessage(message, shouldHear) def assertMessage(self, message, shouldHear): if not message: self.testCase.assertFalse(True, self.jid + " did not hear '" + shouldHear + "'") elif shouldHear.endswith('...'): failMessage = self.jid + " expected '" + shouldHear + "' but heard '" + message + "'" self.testCase.assertTrue(message.startswith(shouldHear[:-3]), failMessage) else: failMessage = self.jid + " expected '" + shouldHear + "' but heard '" + message + "'" self.testCase.assertEqual(message, shouldHear, failMessage) print self.jid + ' ' + message def on_messageReceived(self, sender, msg): self.q.put(msg[1]) def stop(self): self.messenger.finish()
class SamplePlayer(object): def __init__(self, jid, password): self.jid = jid self.password = password self.messenger = XmppMessenger(jid, password) self.messenger.evt_messageReceived += self.on_messageReceived self.messenger.listen('localhost', 5222) self.messenger.sendMessage('dealer@pokerchat', 'JOIN') self.chips = 0 self.cards = [] self.action = { 'GO': self.makeBet, 'BET': self.recordBet, 'CARD': self.acceptCard, 'WON': self.recordWinnings, 'DEALING': self.initialiseHand, 'CHIPS': self.acceptChips } def on_messageReceived(self, sender, msg): args = msg[1].split(' ') self.action[args[0]](args[1:]) def acceptChips(self, args): self.chips = int(args[0]) def initialiseHand(self, args): self.bets = dict(zip(args, [0 for player in args])) print 'Chips', self.chips def recordWinnings(self, args): winner = args[0] loser = args[1] amount = int(args[2]) if self.jid == winner: self.chips += amount if self.jid == loser: self.chips[loser] -= amount def makeBet(self, args): self.messenger.sendMessage('dealer@pokerchat', self.minBet()) def recordBet(self, args): player = args[0] amount = int(args[1]) self.bets[player] += amount def acceptCard(self, args): cards = [(card[0:-1], card[-1]) for card in args] self.cards = self.cards + cards def minBet(self): highest = max([self.bets[k] for k in self.bets]) myContribution = self.bets[self.jid] bet = min(max(highest - myContribution, 100), self.chips) self.chips -= bet return bet
class FakeParticipant(): def __init__(self, jid, password, pollPeriod, dealerjid, testCase): self.dealerjid = dealerjid self.testCase = testCase self.jid = jid self.q = Queue.Queue() self.pollPeriod = pollPeriod self.messenger = XmppMessenger(jid, password) self.messenger.evt_messageReceived += self.on_messageReceived self.messenger.listen('localhost', 5222) def says(self, said): self.messenger.sendMessage(self.dealerjid, said) def _getMessage(self): end = time.time() + self.pollPeriod message = None while time.time() <= end and not message: try: message = self.q.get_nowait() except Queue.Empty: pass return message def hears(self, shouldHear): message = self._getMessage() self.assertMessage(message, shouldHear) def eventuallyHears(self, shouldHear): end = time.time() + self.pollPeriod message = None while time.time() <= end: try: message = self.q.get_nowait() if message and message.startswith(shouldHear[:-3]): break except Queue.Empty: pass self.assertMessage(message, shouldHear) def assertMessage(self, message, shouldHear): if not message: self.testCase.assertFalse( True, self.jid + " did not hear '" + shouldHear + "'") elif shouldHear.endswith('...'): failMessage = self.jid + " expected '" + shouldHear + "' but heard '" + message + "'" self.testCase.assertTrue(message.startswith(shouldHear[:-3]), failMessage) else: failMessage = self.jid + " expected '" + shouldHear + "' but heard '" + message + "'" self.testCase.assertEqual(message, shouldHear, failMessage) print self.jid + ' ' + message def on_messageReceived(self, sender, msg): self.q.put(msg[1]) def stop(self): self.messenger.finish()