Ignore:
Timestamp:
06/18/17 11:17:51 (7 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Flight booking works (re #304).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/rpc.py

    r858 r859  
    6666        "arrivalTime": lambda value: ScheduledFlight._decodeTime(value),
    6767        "duration": lambda value: ScheduledFlight._decodeDuration(value),
    68         "type": int,
    69         "spec": int
     68        "spec": int,
     69        "validFrom": lambda value: ScheduledFlight._decodeDate(value),
     70        "validTo": lambda value: ScheduledFlight._decodeDate(value),
     71        "date": lambda value: ScheduledFlight._decodeDate(value)
    7072        }
    7173
     
    7476        """Decode the given value as a time value."""
    7577        return datetime.datetime.strptime(value, "%H:%M:%S").time()
     78
     79    @staticmethod
     80    def _decodeDate(value):
     81        """Decode the given value as a date value."""
     82        if not value or value=="0000-00-00":
     83            return const.defaultDate
     84        else:
     85            return datetime.datetime.strptime(value, "%Y-%m-%d").date()
    7686
    7787    @staticmethod
     
    90100        del self.typeCode
    91101
     102        self.type = ScheduledFlight.TYPE_VIP if self.spec==1 \
     103          else ScheduledFlight.TYPE_NORMAL
     104        del self.spec
     105
     106    def compareBy(self, other, name):
     107        """Compare this flight with the other one according to the given
     108        attribute name."""
     109        if name=="callsign":
     110            try:
     111                cs1 = int(self.callsign[2:])
     112                cs2 = int(other.callsign[2:])
     113                return cmp(cs1, cs2)
     114            except:
     115                return cmp(self.callsign, other.callsign)
     116        else:
     117            return cmp(getattr(self, name), getattr(other, name))
     118
    92119    def __repr__(self):
    93120        return "ScheduledFlight<%d, %d, %s, %s (%s) - %s (%s) -> %d, %d>" % \
     
    95122           self.departureICAO, str(self.departureTime),
    96123           self.arrivalICAO, str(self.arrivalTime),
    97            self.duration, self.spec)
     124           self.duration, self.type)
    98125
    99126#---------------------------------------------------------------------------------------
     
    104131    Occasionally, one of the flights may be missing."""
    105132    @staticmethod
    106     def scheduledFlights2Pairs(scheduledFlights):
     133    def scheduledFlights2Pairs(scheduledFlights, date):
    107134        """Convert the given list of scheduled flights into a list of flight
    108135        pairs."""
     136        weekday = str(date.weekday()+1)
     137
    109138        flights = {}
     139        weekdayFlights = {}
    110140        for flight in scheduledFlights:
    111141            flights[flight.id] = flight
     142            if (flight.type==ScheduledFlight.TYPE_NORMAL and
     143                flight.arrivalICAO!="LHBP" and weekday in flight.days and
     144                flight.validFrom<=date and flight.validTo>=date) or \
     145               flight.type==ScheduledFlight.TYPE_VIP:
     146                weekdayFlights[flight.id] = flight
    112147
    113148        flightPairs = []
    114149
    115         while flights:
    116             (id, flight) = flights.popitem()
    117             pairID = flight.pairID
    118             if pairID in flights:
    119                 pairFlight = flights[pairID]
    120                 if flight.departureICAO=="LHBP" or \
    121                   (pairFlight.departureICAO!="LHBP" and id<pairID):
    122                     flightPairs.append(ScheduledFlightPair(flight, pairFlight))
    123                 else:
    124                     flightPairs.append(ScheduledFlightPair(pairFlight, flight))
    125                 del flights[pairID]
    126             else:
     150        while weekdayFlights:
     151            (id, flight) = weekdayFlights.popitem()
     152            if flight.type==ScheduledFlight.TYPE_NORMAL:
     153                pairID = flight.pairID
     154                if pairID in flights:
     155                    pairFlight = flights[pairID]
     156                    if flight.departureICAO=="LHBP" or \
     157                      (pairFlight.departureICAO!="LHBP" and
     158                       flight.callsign<pairFlight.callsign):
     159                        flightPairs.append(ScheduledFlightPair(flight, pairFlight))
     160                    else:
     161                        flightPairs.append(ScheduledFlightPair(pairFlight, flight))
     162                    del flights[pairID]
     163                    if pairID in weekdayFlights:
     164                        del weekdayFlights[pairID]
     165            elif flight.type==ScheduledFlight.TYPE_VIP:
    127166                flightPairs.append(ScheduledFlightPair(flight))
     167
     168        flightPairs.sort(cmp = lambda pair1, pair2:
     169                         cmp(pair1.flight0.date, pair2.flight0.date))
    128170
    129171        return flightPairs
     
    133175        self.flight0 = flight0
    134176        self.flight1 = flight1
     177
     178    def compareBy(self, other, name):
     179        """Compare this flight pair with the other one according to the given
     180        attribute name, considering the first flights."""
     181        return self.flight0.compareBy(other.flight0, name)
     182
     183    def __repr__(self):
     184        return "ScheduledFlightPair<%s, %s, %s>" % \
     185          (self.flight0.callsign, self.flight0.departureICAO,
     186           self.flight0.arrivalICAO)
    135187
    136188#---------------------------------------------------------------------------------------
     
    280332    _instructions = {
    281333        "status" : lambda value: rpccommon.Plane.str2status(value),
    282         "gateNumber" : lambda value: value if value else None
     334        "gateNumber" : lambda value: value if value else None,
     335        "typeCode": lambda value: BookedFlight._decodeAircraftType(value)
    283336        }
    284337
     
    286339        """Construct the plane."""
    287340        RPCObject.__init__(self, value, instructions = Plane._instructions)
     341        self.aircraftType = self.typeCode
     342        del self.typeCode
    288343
    289344#---------------------------------------------------------------------------------------
     
    524579                                   self._server.getTimetable(sessionID,
    525580                                                             date.strftime("%Y-%m-%d"),
    526                                                              date.weekday() + 1,
     581                                                             date.weekday()+1,
    527582                                                             typeCodes))
    528583        return ScheduledFlightPair.scheduledFlights2Pairs([ScheduledFlight(value)
    529                                                           for value in values])
     584                                                          for value in values],
     585                                                          date)
     586
     587    def bookFlights(self, flightIDs, date, tailNumber):
     588        """Book the flights with the given IDs on the given date to be flown
     589        with the plane of the given tail number."""
     590        values = self._performCall(lambda sessionID:
     591                                   self._server.bookFlights(sessionID,
     592                                                            flightIDs,
     593                                                            date.strftime("%Y-%m-%d"),
     594                                                            tailNumber))
     595        return [BookedFlight(value) for value in values]
    530596
    531597    def _performCall(self, callFn, acceptResults = []):
Note: See TracChangeset for help on using the changeset viewer.