def _fishing(self, rank): ''' Process the act of going fishing. ''' turn = self.state['g'].get_turn_pid() if True == self.DEBUG_FUNC: print "pid {0} going fishing.".format(turn) FAIL = '' if 0 == turn: if self.chance( self.FAIL_CHANCE): # correct len, wrong tags for this msg msg = sp.serialize_draw_request() FAIL = 'ERR_INVALID_XML' else: msg = sp.serialize_fishing() self.write(msg) if '' != FAIL: self._recv_error(FAIL) return -1 # if pool is out of cards, card == None; will get xml shell w/o a card card = self.state['g'].go_fishing() msg = sp.serialize_cards([card]) if 0 == turn: self.read(length=len(msg), expect=msg) if True == self.DEBUG_FUNC: print " fishing card:{0}, rank:{1}, msg:{2}".format( card, rank, msg) ZERO = False if (None != card) and (False == card.has_rank(rank)): msg = sp.serialize_cards([None]) ZERO = True if 0 == turn: if self.chance(self.FAIL_CHANCE ): # invalid card and incorrect for this msg if True == ZERO: msg = sp.serialize_draw_request() # just need 4 bytes else: msg = sp.serialize_cards([Card( 5, 15)]) # just need bogus card (12 bytes) FAIL = 'ERR_INVALID_CARD' self.write(msg) else: self.read(length=len(msg), expect=msg) if '' != FAIL: self._recv_error(FAIL) return -1 else: return 0
def _fishing(self, rank): ''' Process the act of going fishing. ''' turn = self.state['g'].get_turn_pid() if True == self.DEBUG_FUNC: print "pid {0} going fishing.".format(turn) FAIL = '' if 0 == turn: # these msg's need 4 chars to prevent recv timeout if self.chance(self.FAIL_CHANCE): # correct len, wrong tags for this msg msg = sp.serialize_draw_request() FAIL = 'ERR_INVALID_XML' else: msg = sp.serialize_fishing() self.write(msg) if '' != FAIL: self._recv_error(FAIL) return -1 # if pool is out of cards, card == None; will get xml shell w/o a card card = self.state['g'].go_fishing() msg = sp.serialize_cards([card]) if 0 == turn: self.read(length=len(msg), expect=msg) if True == self.DEBUG_FUNC: print " fishing card:{0}, rank:{1}, msg:{2}".format(card, rank, msg) ZERO = False if (None != card) and (False == card.has_rank(rank)): msg = sp.serialize_cards([None]) ZERO = True if 0 == turn: if self.chance(self.FAIL_CHANCE): # invalid card and incorrect for this msg if True == ZERO: msg = sp.serialize_draw_request() # just need 4 bytes else: msg = sp.serialize_cards([Card(5,15)]) # just need bogus card (12 bytes) FAIL = 'ERR_INVALID_CARD' self.write(msg) else: self.read(length=len(msg), expect=msg) if '' != FAIL: self._recv_error(FAIL) return -1 return 0
def _reload_empty_hand(self): ''' Process the act of reloading hand with cards if it is empty. ''' turn = self.state['g'].get_turn_pid() if True == self.state['g'].is_hand_empty(): if True == self.DEBUG_FUNC: print "pid {0} drawing hand.".format(turn) FAIL = '' if 0 == turn: if self.chance(self.FAIL_CHANCE ): # correct len, wrong tags for this msg msg = sp.serialize_turn(0) msg = msg[5:] FAIL = 'ERR_INVALID_XML' else: msg = sp.serialize_draw_request() self.write(msg) new_hand = self.state['g'].draw_new_hand() if 0 == turn: if '' != FAIL: msg = sp.serialize_error(FAIL) self.read(length=len(msg), expect=msg) return -1 else: msg = sp.serialize_cards(new_hand) self.read(length=len(msg), expect=msg) if True == self.DEBUG_FUNC: print " new hand: {0}".format(new_hand) return 0
def _reload_empty_hand(self): ''' Process the act of reloading hand with cards if it is empty. ''' turn = self.state['g'].get_turn_pid() if True == self.state['g'].is_hand_empty(): if True == self.DEBUG_FUNC: print "pid {0} drawing hand.".format(turn) FAIL = '' if 0 == turn: if self.chance(self.FAIL_CHANCE): # correct len, wrong tags for this msg msg = sp.serialize_turn(0) msg = msg[5:] FAIL = 'ERR_INVALID_XML' else: msg = sp.serialize_draw_request() self.write(msg) new_hand = self.state['g'].draw_new_hand() if 0 == turn: if '' != FAIL: msg = sp.serialize_error(FAIL) self.read(length=len(msg), expect=msg) return -1 else: msg = sp.serialize_cards(new_hand) self.read(length=len(msg), expect=msg) if True == self.DEBUG_FUNC: print " new hand: {0}".format(new_hand) return 0