def test_parking_lot_full(self):
     car1 = Car(reg_no="231A", color="Black")
     car2 = Car(reg_no="563A", color="Black")
     self.parking_lot_obj.park_vehicle(car1)
     self.parking_lot_obj.park_vehicle(car2)
     car4 = Car(reg_no="897A", color="white")
     self.assertRaises(Exception, self.parking_lot_obj.park_vehicle, car4)
     self.parking_lot_obj.unpark_vehicle(1)
     self.parking_lot_obj.unpark_vehicle(2)
 def test_get_slot_by_vehicle_reg_no(self):
     car1 = Car(reg_no="231A", color="Black")
     car2 = Car(reg_no="563A", color="White")
     self.parking_lot_obj.park_vehicle(car1)
     self.parking_lot_obj.park_vehicle(car2)
     self.assertEqual(self.parking_lot_obj.get_slot_no_by_reg_no("231A"), 1)
     self.assertEqual(self.parking_lot_obj.get_slot_no_by_reg_no("INVALID"),
                      "Not found")
     self.parking_lot_obj.unpark_vehicle(1)
     self.parking_lot_obj.unpark_vehicle(2)
 def test_get_slot_and_reg_no_by_color(self):
     car2 = Car(reg_no="231A", color="Black")
     car3 = Car(reg_no="563A", color="Black")
     self.parking_lot_obj.park_vehicle(car2)
     self.parking_lot_obj.park_vehicle(car3)
     res = self.parking_lot_obj.get_reg_no_by_color("Black")
     self.assertEqual(res, ['231A', '563A'])
     slot = self.parking_lot_obj.get_slot_no_by_color("Black")
     self.assertEqual(slot, [1, 2])
     self.parking_lot_obj.unpark_vehicle(1)
     self.parking_lot_obj.unpark_vehicle(2)
Exemple #4
0
    def move(self, me: Car, world: World, game: Game, move: Move):
        if world.tick < game.initial_freeze_duration_ticks:
            if world.tick == 0:
                self.worldWidth = world.width
                self.build_path(world)
                return
            if world.tick == 1:
                aFrom = (int(me.x//game.track_tile_size), int(me.y//game.track_tile_size))
                aTo = (me.next_waypoint_x, me.next_waypoint_y)
                print(aFrom, aTo)
                dijkstra(self.path, self.path.get_vertex(aFrom), self.path.get_vertex(aTo))

                target = self.path.get_vertex(aTo)
                path = [target.get_id()]
                shortest(target, path)
                print('The shortest path : %s' %(path[::-1]))

        nextWaypointX = (me.next_waypoint_x + 0.5) * game.track_tile_size
        nextWaypointY = (me.next_waypoint_y + 0.5) * game.track_tile_size

        angleToWaypoint = me.get_angle_to(nextWaypointX, nextWaypointY)
        speedModule = hypot(me.speed_x, me.speed_y)

        move.wheel_turn = angleToWaypoint  / pi
        move.engine_power = 0.8

        if (speedModule * speedModule * abs(angleToWaypoint) > 2.5 * 2.5 * pi):
            move.setBrake = True
            move.engine_power = -0.5
Exemple #5
0
def get_data_from_vegvesenet(license_plate):

    response = requests.get(VEGVESENET_API_ENDPOINT + license_plate).json()
    status = response.get("status")
    if status == 500:
        raise ServiceUnavailableException(
            "Vegvesenet's service is not available. Used up quota of calls.")
    if status == 404:
        raise LicensePlateNotFoundException(
            "No car registered with license plate: " + license_plate)

    car = Car()
    car = car.load_data_from_vegvesenet(response)
    if car.has_loaded_data():
        return car.get_data()
    else:
        raise CarInformationNotLoadedException(
            "Could not load car information on license plate " + license_plate)
    def __init__(self):
        #  Rectangular Unit args
        _id = 0
        mass = 1000
        x = 300
        y = 300
        speed_x = 0.0
        speed_y = 0.0
        angle = 0.0
        angular_speed = 0.0
        width = 120
        height = 140

        # Car args
        player_id = 0
        teammate_index = 0
        teammate = True
        type = CarType.BUGGY
        projectile_count = 0
        nitro_charge_count = 0
        oil_canister_count = 0
        remaining_projectile_cooldown_ticks = 0
        remaining_nitro_cooldown_ticks = 0
        remaining_oil_cooldown_ticks = 0
        remaining_nitro_ticks = 0
        remaining_oiled_ticks = 0
        durability = 100
        engine_power = 0.0
        wheel_turn = 0.0
        next_waypoint_index = 0
        next_waypoint_x = 0
        next_waypoint_y = 0
        finished_track = False

        Car.__init__(self, _id, mass, x, y, speed_x, speed_y, angle,
                     angular_speed, width, height, player_id, teammate_index,
                     teammate, type, projectile_count, nitro_charge_count,
                     oil_canister_count, remaining_projectile_cooldown_ticks,
                     remaining_nitro_cooldown_ticks,
                     remaining_oil_cooldown_ticks, remaining_nitro_ticks,
                     remaining_oiled_ticks, durability, engine_power,
                     wheel_turn, next_waypoint_index, next_waypoint_x,
                     next_waypoint_y, finished_track)
    def read_car(self):
        if not self.read_boolean():
            return None

        return Car(self.read_long(), self.read_double(), self.read_double(),
                   self.read_double(), self.read_double(), self.read_double(),
                   self.read_double(), self.read_double(), self.read_double(),
                   self.read_double(), self.read_long(), self.read_int(),
                   self.read_boolean(), self.read_enum(CarType),
                   self.read_int(), self.read_int(), self.read_int(),
                   self.read_int(), self.read_int(), self.read_int(),
                   self.read_int(), self.read_int(), self.read_double(),
                   self.read_double(), self.read_double(), self.read_int(),
                   self.read_int(), self.read_int(), self.read_boolean())
Exemple #8
0
    def process_input(self, input_str):
        """
        This function compares the input string against a set of enum commands else ignores.
        :param input_str: input command
        """
        splitted_input = input_str.split(" ")
        instruction = splitted_input[0]
        if instruction == Command.CREATE_PARKING_LOT.value:
            if len(splitted_input) == 2:
                self.parking_lot_obj = get_in_memory_dao(
                    DaoType.in_memory_dao, splitted_input[1])

        elif instruction == Command.PARK.value:
            if self.parking_lot_obj and len(splitted_input) == 3:
                car_obj = Car(reg_no=splitted_input[1],
                              color=splitted_input[2])
                ack = self.parking_lot_obj.park_vehicle(car_obj)
                print(ack)

        elif instruction == Command.LEAVE.value:
            if self.parking_lot_obj and len(splitted_input) == 2:
                ack = self.parking_lot_obj.unpark_vehicle(splitted_input[1])
                print(ack)
        elif instruction == Command.STATUS.value:
            if self.parking_lot_obj and len(splitted_input) == 1:
                self.parking_lot_obj.print_status()

        elif instruction == Command.REG_NUMBER_FOR_CARS_WITH_COLOR.value:
            if self.parking_lot_obj and len(splitted_input) == 2:
                result = self.parking_lot_obj.get_reg_no_by_color(
                    splitted_input[1])
                print(", ".join(result))

        elif instruction == Command.SLOTS_NUMBER_FOR_CARS_WITH_COLOR.value:
            if self.parking_lot_obj and len(splitted_input) == 2:
                result = self.parking_lot_obj.get_slot_no_by_color(
                    splitted_input[1])
                result = list(map(lambda x: str(x), result))
                print(", ".join(result))

        elif instruction == Command.SLOTS_NUMBER_FOR_REG_NUMBER.value:
            if self.parking_lot_obj and len(splitted_input) == 2:
                result = self.parking_lot_obj.get_slot_no_by_reg_no(
                    splitted_input[1])
                print(result)
 def test_parking_same_car_twice(self):
     car = Car(reg_no="123A", color="Silver")
     ack = self.parking_lot_obj.park_vehicle(car)
     self.assertTrue(ack.startswith("Allocated slot number"))
     self.assertRaises(Exception, self.parking_lot_obj.park_vehicle, car)
     self.parking_lot_obj.unpark_vehicle(1)
 def test_simple_parking(self):
     car = Car(reg_no="123A", color="Silver")
     ack = self.parking_lot_obj.park_vehicle(car)
     self.assertTrue(ack, "Allocated slot number: 1")
     self.parking_lot_obj.unpark_vehicle(1)
Exemple #11
0
    def move(self, me: Car, world: World, game: Game, move: Move):

        self.grid = world.tiles_x_y
        self.grid_width = len(world.tiles_x_y)
        self.grid_height = len(world.tiles_x_y[0])

        curr_tile_x = int(
            me.x // game.track_tile_size + (-1 if me.x % game.track_tile_size == 0 else 0))
        curr_tile_y = int(
            me.y // game.track_tile_size + (-1 if me.y % game.track_tile_size == 0 else 0))
        curr_tile_type = world.tiles_x_y[curr_tile_x][curr_tile_y]
        curr_speed_module = hypot(me.speed_x, me.speed_y)

        print('----------', world.tick)
        # if world.tick == 0:
        #     for j in range(self.grid_height):
        #         print(', '.join([str(self.grid[i][j]).rjust(2, '0') for i in range(self.grid_width)]))
        # print(self.pts)
        # print(me.next_waypoint_x, me.next_waypoint_y)
        # print(self.ticks_without_move, self.rear_move_ticks_remain)

        # путь до следующего way point
        # if self.route:
        #     steps = self.route[len(self.tmp_route):]
        # else:
        #     if len(self.tmp_route) > 1 and self.tmp_route[0] == (curr_tile_x, curr_tile_y):
        #         self.route = list(self.tmp_route)
        #         self.tmp_route.clear()

        steps = self.steps_to_point(curr_tile_x, curr_tile_y, me.next_waypoint_x, me.next_waypoint_y)

        # if not self.tmp_route or self.tmp_route[-1] != (curr_tile_x, curr_tile_y):
        #     self.tmp_route.append((curr_tile_x, curr_tile_y))
        # print(steps, len(steps))
        straight_moves = 1
        if steps:
            next_tile_x, next_tile_y = steps[0]

            direction = (next_tile_x - curr_tile_x, next_tile_y - curr_tile_y)
            for i in range(1, len(steps)):
                if direction == (steps[i][0] - steps[i-1][0], steps[i][1] - steps[i-1][1]):
                    straight_moves += 1
                    next_tile_x, next_tile_y = steps[i]
                else:
                    break
        else:
            next_tile_x, next_tile_y = me.next_waypoint_x, me.next_waypoint_y

        next_tile_type = world.tiles_x_y[next_tile_x][next_tile_y]
        # print(straight_moves)

        if self.state == DEFAULT_ST or self.state == ACCELERATION_ST:
            # до начала движения
            if world.tick < game.initial_freeze_duration_ticks:
                return self.move_forward_and_return(move)

            # счётчик простоя перед задним ходом
            if abs(curr_speed_module) < SMALL_SPEED:
                self.ticks_without_move += 1
            else:
                self.ticks_without_move = 0
            if self.ticks_without_move > TICKS_WITHOUT_MOVE.get(self.state):
                self.ticks_without_move = 0
                self.state = REVERSE_ST
                self.rear_move_ticks_remain = MAX_REAR_MOVE_TICKS

            if self.state == ACCELERATION_ST:
                if abs(curr_speed_module) >= SMALL_SPEED:
                    self.state = DEFAULT_ST

            # print(curr_tile_x, curr_tile_y)
            # print(next_tile_x, next_tile_y, next_tile_type)
            # print(me.next_waypoint_x, me.next_waypoint_y, next_tile_type)
            # print(self.grid_width, self.grid_height)
            # print(steps)
            # print(me.speed_x, me.speed_y)
            # print(me.x, me.y)
            # print(cars_is_close)
            # print(curr_speed_module)
            # print(curr_speed_module > BIG_SPEED and straight_moves == 1)

            # # FIXME есть ли машины рядом
            # cars_is_close = False
            # for car in world.cars:
            #     if car.id != me.id and me.get_distance_to_unit(car) <= me.width * 1.2:
            #         cars_is_close = True
            #
            # if cars_is_close and any((
            #     all((
            #         next_tile_type == TileType.VERTICAL,
            #         abs(me.speed_x) < SMALL_SPEED,
            #         # abs(me.speed_y) > MEDIUM_SPEED,
            #     )),
            #     all((
            #         next_tile_type == TileType.HORIZONTAL,
            #         abs(me.speed_y) < SMALL_SPEED,
            #         # abs(me.speed_x) > MEDIUM_SPEED,
            #     ))
            # )):
            #     return self.move_forward_and_return(move)
            # # FIXME ends

            next_x = (next_tile_x + NEXT_TILE_OFFSET) * game.track_tile_size
            next_y = (next_tile_y + NEXT_TILE_OFFSET) * game.track_tile_size

            corner_tile_offset = 0.32 * game.track_tile_size
            if next_tile_type == TileType.LEFT_TOP_CORNER:
                next_x += corner_tile_offset
                next_y += corner_tile_offset
            elif next_tile_type == TileType.RIGHT_TOP_CORNER:
                next_x -= corner_tile_offset
                next_y += corner_tile_offset
            elif next_tile_type == TileType.LEFT_BOTTOM_CORNER:
                next_x += corner_tile_offset
                next_y -= corner_tile_offset
            elif next_tile_type == TileType.RIGHT_BOTTOM_CORNER:
                next_x -= corner_tile_offset
                next_y -= corner_tile_offset
            elif next_tile_type == TileType.BOTTOM_HEADED_T and straight_moves == 1:
                next_y += corner_tile_offset
            elif next_tile_type == TileType.LEFT_HEADED_T and straight_moves == 1:
                next_x -= corner_tile_offset
            elif next_tile_type == TileType.RIGHT_HEADED_T and straight_moves == 1:
                next_x += corner_tile_offset
            elif next_tile_type == TileType.TOP_HEADED_T and straight_moves == 1:
                next_y -= corner_tile_offset

            angle_to_next_tile = me.get_angle_to(next_x, next_y)
            move.wheel_turn = angle_to_next_tile * 32.0 / pi

            if curr_speed_module ** 2 * abs(angle_to_next_tile) > 3 ** 2 * pi or \
                    curr_speed_module > BIG_SPEED and straight_moves == 1:
                move.engine_power = 0.7
                move.brake = True
            else:
                move.engine_power = 1.0

            # используем инвентарь
            if self.check_other_cars(world, me, PROJECTILE_THROW_DISTANCE, 0):
                move.throw_projectile = True
            if self.check_other_cars(world, me, OIL_SPILL_DISTANCE, pi) and curr_tile_type in TURN_TILES:
                move.spill_oil = True
            if straight_moves > 3 and angle_to_next_tile < ANGLE_DELTA:
                move.use_nitro = True

        # задний ход
        elif self.state == REVERSE_ST:
            if self.rear_move_ticks_remain > 0:
                self.rear_move_ticks_remain -= 1
                next_x = (next_tile_x + NEXT_TILE_OFFSET) * game.track_tile_size
                next_y = (next_tile_y + NEXT_TILE_OFFSET) * game.track_tile_size
                move.wheel_turn = -100 if me.get_angle_to(next_x, next_y) * 32.0 / pi > 0 else 100
                move.engine_power = -1.0
                return
            else:
                if curr_speed_module > SMALL_SPEED:
                    move.brake = True
                    move.engine_power = 1
                    return
                else:
                    self.state = ACCELERATION_ST