def _handle_plane(plane, process): """ set plane to flight status :param plane: :return: """ dt = game_now() # если пора отправляться в путь - взлететь if dt >= plane.current_schedule_item.start_time: plane.change_status_to_takeoff()
def get_timedelta(self): """ сколько времени прошло с момента последнего обновления? """ if self.last_moving is None: return None, None, None gn = game_now() lm = self.last_moving seconds_delta = (gn - lm).total_seconds() minutes_delta = seconds_delta / 60 hours_delta = minutes_delta / 60 return round(hours_delta, 4), round(minutes_delta, 2), round(seconds_delta)
def change_status_to_takeoff(self): """ change status from PREPARE to TAKEOFF :return: """ if self.flight_status != self.PREPARE: raise FromPrepareToTakeoffError() self.current_port_id = None self.flight_status = self.TAKEOFF self.last_moving = game_now() self.save() logger.log(u'%s change status to %s' % (self, self.TAKEOFF))
def change_status_to_landing(self): """ change status from FLIGHT to LANDING :return: """ if self.flight_status not in [self.TAKEOFF, self.FLIGHT]: raise FromTakeoffToFlightError() # change status self.flight_status = self.LANDING self.save() # and save start landing time self.current_schedule_item.start_landing_time = game_now() self.current_schedule_item.save() logger.log(u'%s change status to %s' % (self, self.LANDING))
def _handle_plane(plane, process): # Если расписние на потом еще не создано - нужно создать if not plane.has_future_schedule(): # создали будущее расписание plane.create_future_cycle() schedule_item = plane.get_next_schedule_item() if schedule_item: # сделать пометку о том, что начать рейс нужно через час schedule_item.start_time = game_now() + datetime.timedelta(minutes=40) schedule_item.save() # сменили статус на "подгтовка" plane.change_status_to_prepare()
def test_start_takeoff(plane_fixture): """ Проверить, что самолет взлетел вовремя :param plane_fixture: :return: """ # создать расписание под самолет GroundProcess._handle_plane(plane_fixture) # переместить начало вылета на текущее время dt = game_now() flight_time = dt - datetime.timedelta(seconds=60*60+1) # 1h 1s # получить первый участок пути plane_fixture.current_schedule_item.start_time = flight_time plane_fixture.current_schedule_item.save() # попробовать взлететь PrepareProcess._handle_plane(plane_fixture) assert plane_fixture.flight_status == plane_fixture.TAKEOFF
def move(self, latitude=None, longitude=None, altitude=None): """ move plane at some coordinates :return: """ if (not latitude) and (not longitude) and (not altitude): return hours, minutes, seconds = self.get_realy_timedelta() history_data = { 'plane_id': self.pk, 'schedule_id': self.schedule_id, 'schedule_item_id': self.current_schedule_item_id, 'from_latitude': self.latitude, 'from_longitude': self.longitude, 'from_altitude': self.altitude, 'to_latitude': latitude, 'to_longitude': longitude, 'to_altitude': altitude, 'interval': seconds, } MovingHistory.objects.create(**history_data) if isinstance(latitude, int) or isinstance(latitude, float): self.latitude = round(latitude, 2) if isinstance(longitude, int) or isinstance(longitude, float): self.longitude = round(longitude, 2) if isinstance(altitude, int) or isinstance(altitude, float): self.altitude = round(altitude, 2) self.last_moving = game_now() self.last_moving_date = datetime.datetime.now() logger.log('%s move to (%s, %s, %s)' % ( self, self.latitude, self.longitude, self.altitude )) self.save()
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()