// Record a new flight option public void RecordOption(string origin, string destination, DateTime start, DateTime end, int price, int distance, DbFace dbface) { lock (dbface) { dbface.Execute(string.Format("insert into flights (origin, destination, date_leave, date_return, price, km, source, user) values ('{0}', '{1}', '{2}', '{3}', {4}, {5}, '{6}', '{7}') on duplicate key update price = {4}, source = '{6}', user = '******'", origin, destination, MyDate(start), MyDate(end), price, distance, source, user), false); } lock (results) { Flight flight = new Flight(origin, destination, Airport.GetAirport(destination, dbface).Country, start, end, price, distance, DateTime.Now); results.Add(flight.Score, flight); } }
// Record a new flight option public void RecordAtoCOption(string pointa, string pointb, string pointc, DateTime start, DateTime end, int priceatob, int pricebtoc, int distanceatob, int distancebtoc, DbFace dbface) { lock (dbface) { dbface.Execute(string.Format("insert into oneways (origin, destination, date, price, km, source, user) values ('{0}', '{1}', '{2}', {3}, {4}, '{5}', '{6}') on duplicate key update price = {3}, source = '{5}', user = '******'", pointa, pointb, MyDate(start), priceatob, distanceatob, source, user), false); dbface.Execute(string.Format("insert into oneways (origin, destination, date, price, km, source, user) values ('{0}', '{1}', '{2}', {3}, {4}, '{5}', '{6}') on duplicate key update price = {3}, source = '{5}', user = '******'", pointb, pointc, MyDate(end), pricebtoc, distancebtoc, source, user), false); } lock (results) { AtoCFlight flight = new AtoCFlight(pointa, pointb, pointc, start, end, (uint) priceatob, (uint) pricebtoc, (uint) distanceatob, (uint) distancebtoc, DateTime.Now); results.Add(flight.Score, flight); } }
// Check a previously queried flight for the current price // if nextstart = 0, don't try any more public bool CheckOldFlight(DbFace dbface, int start, out int nextstart) { List<Flight> flights = FindStoredFlights(dbface, "(km / price) * datediff(now(), created) desc", start, 10, 1, out nextstart); if (flights.Count == 0) { nextstart = 0; return false; } Flight flight = flights[0]; // Did this happen today? if so, we're done TimeSpan ago = DateTime.Now.Subtract(flight.LastChecked); if (ago.TotalDays < 1) { nextstart = 0; return false; } // Lookup this price int price = GetPrice(flight.Origin, flight.Destination, flight.DateLeave, flight.DateReturn); if (price == int.MaxValue) return false; // failed // succeeded! update the database dbface.Execute(string.Format("update flights set price = {0}, created = now(), user = '******' where id = {2}", price, user, flight.Id), false); // replace in the results results.Remove(flight.Score, flight.ToString()); flight.Price = price; results.Add(flight.Score, flight); nextstart--; // because won't be there! status.Add(string.Format("Old result updated: {0} to {1} is ${2}", flight.Origin, flight.Destination, price)); return true; }
// Get the distance between two airports public int GetDistance(string from, string to, DbFace dbface) { lock (dbface) { // Try to get the distance from the database int? distance = dbface.GetValue<int>(string.Format("select km from distances where origin = '{0}' and destination = '{1}'", from, to)); if (distance.HasValue) return distance.Value; if (from == "NYC") from = "JFK"; if (from == "LON") from = "LHR"; // Otherwise, query www.world-airport.codes.com CookieContainer cookies = new CookieContainer(); string output = WebUtilities.GetPage("http://www.world-airport-codes.com/dist/?a1=" + from + "&a2=" + to, ref cookies); int kiloi = output.IndexOf("kilometres"); if (kiloi < 0) return 0; // error! string kilostr = output.Substring(kiloi - 13, 13); int? value = GetNum(kilostr, true); if (!value.HasValue) { status.Add(string.Format("Could not find distance from {0} to {1}", from, to)); return 0; } dbface.Execute(string.Format("insert into distances (origin, destination, km) values ('{0}', '{1}', {2})", from, to, value.Value), false); return value.Value; } }
// Check a previously queried a-to-c flight for the current price // if nextstart = 0, don't try any more public bool CheckOldAtoCFlight(DbFace dbface, int start, out int nextstart) { List<AtoCFlight> flights = FindStoredAtoCFlights(dbface, "((atob.km + btoc.km) / (atob.price + btoc.price)) * (datediff(now(), atob.date_created) * datediff(now(), btoc.date_created)) desc", start, 10, 1, out nextstart); if (flights.Count == 0) { nextstart = 0; return false; } AtoCFlight flight = flights[0]; // Did this happen today? if so, we're done TimeSpan ago = DateTime.Now.Subtract(flight.LastChecked); if (ago.TotalDays < 1) { nextstart = 0; return false; } // Lookup this prices int price_atob = GetPrice(flight.PointA, flight.PointB, flight.Date_AtoB, null); if (price_atob == int.MaxValue) return false; // failed int price_btoc = GetPrice(flight.PointB, flight.PointC, flight.Date_BtoC, null); if (price_btoc == int.MaxValue) return false; // failed // succeeded! update the database dbface.Execute(string.Format("update flights set price = {0}, created = now(), user = '******' where id = {2}", price_atob, user, flight.Id_AtoB), false); dbface.Execute(string.Format("update flights set price = {0}, created = now(), user = '******' where id = {2}", price_btoc, user, flight.Id_BtoC), false); // replace in the results results.Remove(flight.Score, flight.ToString()); flight.Price_AtoB = (uint) price_atob; flight.Price_BtoC = (uint) price_btoc; results.Add(flight.Score, flight); nextstart--; // because won't be there! status.Add(string.Format("Old result updated: {0} to {1} to {2} is ${3}", flight.PointA, flight.PointB, flight.PointC, price_atob + price_btoc)); return true; }