Exemple #1
0
def create_schedule():
    """
    create schedule item
    :return: Schedule instance
    """
    start_port = create_port()
    finish_port = create_port()

    schedule = Schedule.objects.create()

    # attach two ports to schedule
    SchedulePort.objects.create(port=start_port, schedule=schedule)
    SchedulePort.objects.create(port=finish_port, schedule=schedule)
    return schedule
def test_full_flight_process(plane_fixture, set_gn):

    p1 = create_port()
    p1.latitude = -1000000
    p1.longitude = -1000000
    p1.altitude = -100
    p1.save()

    p2 = create_port()
    p2.latitude = 2000000
    p2.longitude = 3000000
    p2.altitude = 300
    p2.save()

    schedule = Schedule.objects.create()
    SchedulePort.objects.create(port=p1, schedule=schedule)
    SchedulePort.objects.create(port=p2, schedule=schedule)

    plane_fixture.schedule = schedule
    plane_fixture.modification.altitude_increase_speed = 20000  # 20km/h
    plane_fixture.modification.altitude_decrease_speed = 10000  # 10km/h
    plane_fixture.modification.speed = 500000  # 500km/h
    plane_fixture.modification.save()
    plane_fixture.save()

    get_plane = lambda: Plane.objects.get(pk=plane_fixture.pk)
    plane_fixture.create_future_cycle()

    now = game_now()

    set_gn(now)
    gp = GroundProcess()
    gp.start()

    assert get_plane().flight_status == plane_fixture.PREPARE

    # вылетать нужно немедленно
    set_gn(now + datetime.timedelta(minutes=40))

    pp = PrepareProcess()
    pp.start()

    plane = get_plane()
    assert plane.flight_status == plane_fixture.TAKEOFF

    # самолет находится в нужной точке
    assert plane.latitude == -1000000
    assert plane.longitude == -1000000
    assert plane.altitude == -100

    # по прошествии двух минут нужно пересчитать положение самолета
    set_gn(now + datetime.timedelta(minutes=42))

    hours, minutes, seconds = get_plane().get_timedelta()

    fp = FlightProcess()
    fp.start()

    plane = get_plane()

    assert plane.altitude == round(20000 * hours, 2) - 100

    # через 59:59 после взлета мы должны быть на высоте чуть меньше, чем 10км
    set_gn(now + datetime.timedelta(minutes=69, seconds=59))

    fp = FlightProcess()
    fp.start()

    assert get_plane().flight_status == Plane.TAKEOFF

    # а вот через 60:00 мы уже должны достигнуть нужной высоты
    set_gn(now + datetime.timedelta(minutes=70, seconds=18))

    fp = FlightProcess()
    fp.start()

    assert get_plane().flight_status == Plane.FLIGHT

    # прошло 5 часов полета
    set_gn(now + datetime.timedelta(hours=5, minutes=40))

    fp = FlightProcess()
    fp.start()

    assert get_plane().flight_status == Plane.FLIGHT

    # За секунду до начала снижения мы еще должны находиться в состоянии "полет"
    set_gn(now + datetime.timedelta(hours=9, minutes=1, seconds=48))

    fp = FlightProcess()
    fp.start()

    assert get_plane().flight_status == Plane.FLIGHT

    # а в эту секунду начать снижаться
    set_gn(now + datetime.timedelta(hours=9, minutes=41, seconds=49))

    fp = FlightProcess()
    fp.start()

    assert get_plane().flight_status == Plane.LANDING

    # снижение должно длиться 58m12s
    set_gn(now + datetime.timedelta(hours=9, minutes=99, seconds=60))
    fp = FlightProcess()
    fp.start()
    assert get_plane().flight_status == Plane.LANDING

    set_gn(now + datetime.timedelta(hours=9, minutes=99, seconds=61))
    fp = FlightProcess()
    fp.start()
    assert get_plane().altitude == 300

    # теперь, через секунду, мы должны были завершить текущий полет
    set_gn(now + datetime.timedelta(hours=9, minutes=99, seconds=65))

    fp = FlightProcess()
    fp.start()

    assert get_plane().flight_status == Plane.GROUND
def test_check_schedule_items_queue(plane_fixture, set_gn):

    p1 = create_port()
    p1.latitude = -1000000
    p1.longitude = -1000000
    p1.altitude = -100
    p1.save()

    p2 = create_port()
    p2.latitude = 2000000
    p2.longitude = 3000000
    p2.altitude = 300
    p2.save()

    p3 = create_port()
    p3.latitude = 20000000
    p3.longitude = 20000000
    p3.altitude = 20000000
    p3.save()

    schedule = Schedule.objects.create()
    SchedulePort.objects.create(port=p1, schedule=schedule)
    SchedulePort.objects.create(port=p2, schedule=schedule)
    SchedulePort.objects.create(port=p3, schedule=schedule)

    plane_fixture.schedule = schedule
    plane_fixture.save()

    get_plane = lambda: Plane.objects.get(pk=plane_fixture.pk)
    plane_fixture.create_future_cycle()

    now = game_now()
    gp = GroundProcess()
    pp = PrepareProcess()
    fp = FlightProcess()

    set_gn(now)
    gp.start()

    assert get_plane().flight_status == plane_fixture.PREPARE

    set_gn(now + datetime.timedelta(minutes=40))
    pp.start()
    assert get_plane().current_schedule_item.start_port == p1
    assert get_plane().current_schedule_item.finish_port == p2
    get_plane().complete_flight()

    gp.start()
    set_gn(now + datetime.timedelta(minutes=80))
    pp.start()
    assert get_plane().current_schedule_item.start_port == p2
    assert get_plane().current_schedule_item.finish_port == p3
    get_plane().complete_flight()

    gp.start()
    set_gn(now + datetime.timedelta(minutes=120))
    pp.start()
    assert get_plane().current_schedule_item.start_port == p3
    assert get_plane().current_schedule_item.finish_port == p2
    get_plane().complete_flight()

    gp.start()
    set_gn(now + datetime.timedelta(minutes=160))
    pp.start()
    assert get_plane().current_schedule_item.start_port == p2
    assert get_plane().current_schedule_item.finish_port == p1
    get_plane().complete_flight()

    gp.start()
    set_gn(now + datetime.timedelta(minutes=200))
    pp.start()
    assert get_plane().current_schedule_item.start_port == p1
    assert get_plane().current_schedule_item.finish_port == p2
    get_plane().complete_flight()