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
def game(self): ''' Run the game logic. ''' if True == self.DEBUG_FUNC: print "Starting game." if 0 > self._name(): return -1 # recv initial cards my_init_hand = self.state['g'].deal() msg = sp.serialize_cards(my_init_hand) self.read(length=len(msg), expect=msg) if True == self.DEBUG_FUNC: print " initial hand: {0}.".format(my_init_hand) # do turns while (False == self.state['g'].is_game_over()): turn = self.state['g'].get_turn_pid() if True == self.DEBUG_FUNC: score = self.state['g'].get_score() print "pid {0} turn; books (p0, p1):{1}".format(turn, score) msg = sp.serialize_turn(turn) self.read(length=len(msg), expect=msg) if 0 > self._reload_empty_hand(): return -1 if 0 > self._ask(): return -1 if 0 > self._books(): return -1 self.state['g'].turn_complete() # recv final results score = self.state['g'].get_score() msg = sp.serialize_final_results(score[0], score[1]) self.read(length=len(msg), expect=msg) return