Exemplo n.º 1
0
	def _ask_give_cards(self, rank):
		'''
		Process the action of giving cards to other player
		as a result of an ask request.
		'''
		turn = self.state['g'].get_turn_pid()
		if True == self.DEBUG_FUNC:
			print "pid {0} accepting cards.".format(turn)

		FAIL = ''
		cards = self.state['g'].ask_response_give(rank)
		msg = sp.serialize_cards(cards)
		if 0 == turn:
			self.read(length=len(msg), expect=msg)
		else:
			cnt = len(cards)
			if self.chance(self.FAIL_CHANCE) and 1 <= cnt: #  invalid card and incorrect for this msg
				msg = sp.serialize_cards([Card(5,15)])
				FAIL = 'ERR_INVALID_CARD'

			self.write(msg)

		if '' != FAIL:
			self._recv_error
			return -1

		return 0
Exemplo n.º 2
0
    def _ask_give_cards(self, rank):
        '''
		Process the action of giving cards to other player
		as a result of an ask request.
		'''
        turn = self.state['g'].get_turn_pid()
        if True == self.DEBUG_FUNC:
            print "pid {0} accepting cards.".format(turn)

        FAIL = ''
        cards = self.state['g'].ask_response_give(rank)
        msg = sp.serialize_cards(cards)
        if 0 == turn:
            self.read(length=len(msg), expect=msg)
        else:
            cnt = len(cards)
            if self.chance(
                    self.FAIL_CHANCE
            ) and 1 <= cnt:  #  invalid card and incorrect for this msg
                msg = sp.serialize_cards([Card(5, 15)])
                FAIL = 'ERR_INVALID_CARD'

            self.write(msg)

        if '' != FAIL:
            self._recv_error
            return -1

        return 0
Exemplo n.º 3
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:
            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
Exemplo n.º 4
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
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
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