Esempio n. 1
0
def validateMortgageSequence(sequence):
    if not (isinstance(sequence, list)
            or isinstance(sequence, tuple)) or len(sequence) == 0:
        return False

    sequence = [typecast(prop, int, -1) for prop in sequence]
    return True
Esempio n. 2
0
def validateSellingSequence(sequence):
	if not ( isinstance(sequence, list) or isinstance(sequence, tuple) ) or len(sequence) == 0:
		return False
	
	for index,prop in enumerate(sequence):
		if not ( isinstance(prop, list) or isinstance(prop, tuple) ) or len(prop) < 2:
			return False
		
		arg1 = typecast(prop[0],int,-1)
		arg2 = typecast(prop[1],int,-1)
		sequence[index] = (arg1,arg2)
		if sequence[index][0]<0 or sequence[index][0]>BOARD_SIZE-1:
			return False
		if sequence[index][1]<0 or sequence[index][1]>5:
			return False

	return True
Esempio n. 3
0
def subscribe(context, responses):
	currentPlayerId,response = list(responses.items())[0]
	response = typecast(response, bool, False)

	if response:
		handle_buy_property(context.state)
	else:
		context.auctionStarted = True
		
	return Phase.MORTGAGE
Esempio n. 4
0
    def checkActionType(self, action):
        if not (isinstance(action, list)
                or isinstance(action, tuple)) or len(action) < 2:
            return "N"

        type = action[0]
        if type == "BHS":
            if not (isinstance(action[1], list)
                    or isinstance(action[1], tuple)):
                return "N"
            else:
                if len(action[1]) <= 0:
                    return "N"
                for index, prop in enumerate(action[1]):
                    if not (isinstance(prop, list)
                            or isinstance(prop, tuple)) or len(prop) < 2:
                        return "N"
                    else:
                        action[1][index] = (typecast(prop[0], int, -1),
                                            typecast(prop[1], int, -1))
                        if action[1][index][0] < 0 or action[1][index][
                                0] > self.BOARD_SIZE - 1:
                            return "N"
                        if action[1][index][1] < 0 or action[1][index][1] > 4:
                            return "N"
        elif type == "S":
            if not (isinstance(action[1], list)
                    or isinstance(action[1], tuple)):
                return "N"
            else:
                if len(action[1]) <= 0:
                    return "N"
                for index, prop in enumerate(action[1]):
                    if not (isinstance(prop, list)
                            or isinstance(prop, tuple)) or len(prop) < 3:
                        return "N"
                    else:
                        action[1][index] = (typecast(prop[0], int, -1),
                                            typecast(prop[1], int, -1),
                                            typecast(prop[2], bool, False))
                        if action[1][index][0] < 0 or action[1][index][
                                0] > self.BOARD_SIZE - 1:
                            return "N"
                        if action[1][index][1] < 0 or action[1][index][1] > 4:
                            return "N"
        elif type == "M" or type == "BHT":
            if not isinstance(action[1], list) and not isinstance(
                    action[1], tuple):
                return "N"
            else:
                if len(action[1]) <= 0:
                    return "N"
                action[1] = [typecast(prop, int, -1) for prop in action[1]]
                for prop in action[1]:
                    if prop < 0 or prop > self.BOARD_SIZE - 1:
                        return "N"
        return type
Esempio n. 5
0
 def validPropertyToTrade(self, playerId, propertyId):
     propertyId = typecast(propertyId, int, -1)
     if propertyId < 0 or propertyId > self.BOARD_SIZE + 1:
         return False
     if not self.state.rightOwner(playerId, propertyId):
         return False
     if propertyId > self.BOARD_SIZE - 1:
         return True
     if board[propertyId]['class'] == "Railroad" and board[propertyId][
             'class'] == "Utility":
         return True
     if board[propertyId]['class'] != "Street":
         return False
     if self.state.getNumberOfHouses(propertyId) > 0:
         return False
     for monopolyElement in board[propertyId]['monopoly_group_elements']:
         if self.state.getNumberOfHouses(monopolyElement) > 0:
             return False
     return True
Esempio n. 6
0
    def subscribe(self, *args):
        """
		Stopping conditions for Trade:
		1. If during a given turn, no player gave a valid Trade, Trade ends.
		2. A player can only make MAX_TRADE_REQUESTS number of requests in a given Trade Phase.
		"""
        agentId = None
        tradeResponse = None
        if len(args) > 0:
            agentId = args[0]
        if len(args) > 1:
            tradeResponse = args[1]

        if self.canAccessSubscribe(agentId):
            self.tradeResponseList.append((agentId, tradeResponse))

            if self.validSubs >= self.number_of_requests:
                #if all the trade requests have been received
                for agentId, tradeResponses in self.tradeResponseList:
                    for tradeResponse in tradeResponses:
                        tradeResponse = typecast(tradeResponse, bool, False)
                        if tradeResponse:
                            otherAgentId, cashOffer, propertiesOffer, cashRequest, propertiesRequest = self.validTradeRequests[
                                agentId]
                            if self.validateTradeAction(
                                    agentId, otherAgentId, cashOffer,
                                    propertiesOffer, cashRequest,
                                    propertiesRequest):
                                #we do trade request validation here because if a user accepts multiple trades that were
                                #presented to him at a time, and if he accepts 2 conflicting trades, the adjudicator should
                                #not let both not let both of them through
                                self.processTradeSuccess(
                                    agentId, self.validTradeRequests[agentId])

                self.context.handleTrade.tradeCount += 1
                if self.context.handleTrade.tradeCount >= self.MAX_TRADE_REQUESTS:
                    #end trade and start the post Trade BSM phase
                    self.publishBSM(self.context.conductBSM.nextAction)
                else:
                    #start the next trade
                    self.context.handleTrade.setContext(self.context)
                    self.context.handleTrade.publish()
Esempio n. 7
0
def subscribe(context, responses):
    state = context.state
    agentId, tradeResponse = list(responses.items())[0]

    tradeResponse = typecast(tradeResponse, bool, False)
    if tradeResponse:
        processTradeSuccess(context, agentId)
    else:
        log(
            "game", "Agent {} refused trade offer from {}!".format(
                agentId,
                state.getPhasePayload()[0]))

    otherAgentId = state.getPhasePayload()[0]
    state.setPhasePayload(None)
    context.tradeCounter += 1
    if context.tradeCounter < MAX_TRADES:
        # continue trades for the same agent
        return Phase.TRADE

    noPlayers = len(context.state.players)
    nextIndex = (state.getPlayerIndex(otherAgentId) + 1) % noPlayers
    nextAgentId = state.getPlayerId(nextIndex)
    currentAgentId = state.getCurrentPlayerId()
    context.bsmAgentId = nextAgentId

    # resetting trade counter for other agents
    context.tradeCounter = 0

    if isBuyDecision(state) and otherAgentId == currentAgentId:
        # trading is done for all agents in this turn
        return Phase.BUY

    if nextAgentId == currentAgentId:
        # trading is done for all agents in this turn
        if context.auctionStarted:
            return Phase.AUCTION
        return Phase.UNMORTGAGE

    # do mortgage, selling and trade for the next agent
    return Phase.MORTGAGE