Ejemplo n.º 1
0
    def test_noOutsideWall(self):
        gameState, pointsOfInterest = parseTestCase("""
                ..#..
                A.#.B
                ..#..
            """)
        srcPos = pointsOfInterest["A"]
        destPos = pointsOfInterest["B"]

        with pytest.raises(NoPathToTargetError):
            findPath(gameState, srcPos, destPos)
Ejemplo n.º 2
0
    def test_noOutsideWall(self):
        gameState, pointsOfInterest = parseTestCase(
            """
                ..#..
                A.#.B
                ..#..
            """
        )
        srcPos  = pointsOfInterest["A"]
        destPos = pointsOfInterest["B"]

        with pytest.raises(NoPathToTargetError):
            findPath(gameState, srcPos, destPos)
Ejemplo n.º 3
0
    def test_diagonallyBlocked(self):
        gameState, pointsOfInterest = parseTestCase("""
                ######
                ###A.#
                ###..#
                #..###
                #.B###
                ######
            """)
        srcPos = pointsOfInterest["A"]
        destPos = pointsOfInterest["B"]

        with pytest.raises(NoPathToTargetError):
            findPath(gameState, srcPos, destPos)
Ejemplo n.º 4
0
    def test_diagonallyBlocked(self):
        gameState, pointsOfInterest = parseTestCase(
            """
                ######
                ###A.#
                ###..#
                #..###
                #.B###
                ######
            """
        )
        srcPos  = pointsOfInterest["A"]
        destPos = pointsOfInterest["B"]

        with pytest.raises(NoPathToTargetError):
            findPath(gameState, srcPos, destPos)
Ejemplo n.º 5
0
    def test_aroundCorner(self):
        gameState, pointsOfInterest = parseTestCase("""
                ######
                ####B#
                ####.#
                ####.#
                #A...#
                ######
            """)
        srcPos = pointsOfInterest["A"]
        destPos = pointsOfInterest["B"]

        path = findPath(gameState, srcPos, destPos)
        checkWaypointsPassable(path, gameState.groundTypes)
Ejemplo n.º 6
0
    def test_tbone(self):
        gameState, pointsOfInterest = parseTestCase("""
                #######
                #.A#B.#
                #.###.#
                #.....#
                #######
            """)
        # TODO: Maybe create the GameState in parseTestCase?
        srcPos = pointsOfInterest["A"]
        destPos = pointsOfInterest["B"]

        path = findPath(gameState, srcPos, destPos)
        checkWaypointsPassable(path, gameState.groundTypes)
Ejemplo n.º 7
0
    def test_aroundCorner(self):
        gameState, pointsOfInterest = parseTestCase(
            """
                ######
                ####B#
                ####.#
                ####.#
                #A...#
                ######
            """
        )
        srcPos  = pointsOfInterest["A"]
        destPos = pointsOfInterest["B"]

        path = findPath(gameState, srcPos, destPos)
        checkWaypointsPassable(path, gameState.groundTypes)
Ejemplo n.º 8
0
    def test_tbone(self):
        gameState, pointsOfInterest = parseTestCase(
            """
                #######
                #.A#B.#
                #.###.#
                #.....#
                #######
            """
        )
        # TODO: Maybe create the GameState in parseTestCase?
        srcPos  = pointsOfInterest["A"]
        destPos = pointsOfInterest["B"]

        path = findPath(gameState, srcPos, destPos)
        checkWaypointsPassable(path, gameState.groundTypes)
Ejemplo n.º 9
0
    def test_orthogWall(self):
        gameState, pointsOfInterest = parseTestCase("""
                #########
                #.......#
                #...#...#
                #...#...#
                #.A.#.B.#
                #...#...#
                #.......#
                #########
            """)
        srcPos = pointsOfInterest["A"]
        destPos = pointsOfInterest["B"]

        path = findPath(gameState, srcPos, destPos)
        checkWaypointsPassable(path, gameState.groundTypes)
Ejemplo n.º 10
0
    def test_orthogWall(self):
        gameState, pointsOfInterest = parseTestCase(
            """
                #########
                #.......#
                #...#...#
                #...#...#
                #.A.#.B.#
                #...#...#
                #.......#
                #########
            """
        )
        srcPos  = pointsOfInterest["A"]
        destPos = pointsOfInterest["B"]

        path = findPath(gameState, srcPos, destPos)
        checkWaypointsPassable(path, gameState.groundTypes)
Ejemplo n.º 11
0
    def stringReceived(self, playerId, data):
        # Rate-limit the client.
        self.messageCounts[playerId] += 1
        if self.messageCounts[playerId] == MAXIMUM_MESSAGES_PER_TICK:
            log.warning("Received too many messages this tick from player %s",
                        playerId)
        if self.messageCounts[playerId] >= MAXIMUM_MESSAGES_PER_TICK:
            return

        try:
            message = deserializeMessage(data)
            gameState = self.gameStateManager.gameState
            if isinstance(message, messages.OrderNew):
                # TODO: This should be handled in the game state manager.
                self.gameStateManager.unitOrders.createNewUnit(
                    playerId, message.unitType, message.pos)
            elif isinstance(message, messages.OrderDel):
                for unitId in message.unitSet:
                    # TODO: Factor out this pair of checks? We're probably
                    # going to be doing them a *lot*.
                    if not playerId == unitToPlayer(unitId):
                        badEMessageArgument(
                            message,
                            log,
                            clientId=playerId,
                            reason="Can't delete other player's unit")
                    elif not gameState.isUnitIdValid(unitId):
                        badEMessageArgument(message,
                                            log,
                                            clientId=playerId,
                                            reason="No such unit")
                    else:
                        self.gameStateManager.unitOrders.giveOrders(
                            unitId, [DelUnitOrder()])
            elif isinstance(message, messages.OrderMove):
                unitSet = message.unitSet
                for unitId in unitSet:
                    if not playerId == unitToPlayer(unitId):
                        badEMessageArgument(
                            message,
                            log,
                            clientId=playerId,
                            reason="Can't order other player's unit")
                    elif not gameState.isUnitIdValid(unitId):
                        badEMessageArgument(message,
                                            log,
                                            clientId=playerId,
                                            reason="No such unit")
                    else:
                        try:
                            srcPos = gameState.getPos(unitId)
                            path = findPath(gameState, srcPos, message.dest)
                            log.debug("Issuing orders to unit %s: %s.", unitId,
                                      path)
                            orders = map(MoveUnitOrder, path)
                            self.gameStateManager.unitOrders.giveOrders(
                                unitId, orders)
                        except NoPathToTargetError:
                            log.debug(
                                "Can't order unit %s to %s: "
                                "no path to target.", unitId, message.dest)
                            # If the target position is not reachable, just
                            # drop the command.
            else:
                badEMessageCommand(message, log, clientId=playerId)
        except InvalidMessageError as error:
            illFormedEMessage(error, log, clientId=playerId)
Ejemplo n.º 12
0
    def stringReceived(self, playerId, data):
        # Rate-limit the client.
        self.messageCounts[playerId] += 1
        if self.messageCounts[playerId] == MAXIMUM_MESSAGES_PER_TICK:
            log.warning("Received too many messages this tick from player %s",
                        playerId)
        if self.messageCounts[playerId] >= MAXIMUM_MESSAGES_PER_TICK:
            return

        try:
            message = deserializeMessage(data)
            gameState = self.gameStateManager.gameState
            if isinstance(message, messages.OrderNew):
                # TODO: This should be handled in the game state manager.
                self.gameStateManager.unitOrders.createNewUnit(
                    playerId, message.unitType, message.pos
                )
            elif isinstance(message, messages.OrderDel):
                for unitId in message.unitSet:
                    # TODO: Factor out this pair of checks? We're probably
                    # going to be doing them a *lot*.
                    if not playerId == unitToPlayer(unitId):
                        badEMessageArgument(
                            message, log, clientId=playerId,
                            reason="Can't delete other player's unit"
                        )
                    elif not gameState.isUnitIdValid(unitId):
                        badEMessageArgument(message, log, clientId=playerId,
                                            reason="No such unit")
                    else:
                        self.gameStateManager.unitOrders.giveOrders(
                            unitId, [DelUnitOrder()]
                        )
            elif isinstance(message, messages.OrderMove):
                unitSet = message.unitSet
                for unitId in unitSet:
                    if not playerId == unitToPlayer(unitId):
                        badEMessageArgument(
                            message, log, clientId=playerId,
                            reason="Can't order other player's unit"
                        )
                    elif not gameState.isUnitIdValid(unitId):
                        badEMessageArgument(message, log, clientId=playerId,
                                            reason="No such unit")
                    else:
                        try:
                            srcPos = gameState.getPos(unitId)
                            path = findPath(gameState, srcPos, message.dest)
                            log.debug("Issuing orders to unit %s: %s.",
                                      unitId, path)
                            orders = map(MoveUnitOrder, path)
                            self.gameStateManager.unitOrders.giveOrders(
                                unitId, orders
                            )
                        except NoPathToTargetError:
                            log.debug("Can't order unit %s to %s: "
                                      "no path to target.",
                                      unitId, message.dest)
                            # If the target position is not reachable, just
                            # drop the command.
            else:
                badEMessageCommand(message, log, clientId=playerId)
        except InvalidMessageError as error:
            illFormedEMessage(error, log, clientId=playerId)