randomgenerator.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "randomgenerator.h"
  2. #include "src/database/database.h"
  3. #include "src/functions/calc.h"
  4. #include "src/opl.h"
  5. namespace OPL {
  6. RandomGenerator::RandomGenerator(bool safe_mode) :
  7. safeMode(safe_mode)
  8. {
  9. m_numberOfAirports = DB->getLastEntry(OPL::DbTable::Airports);
  10. m_numberOfTails = DB->getLastEntry(OPL::DbTable::Tails);
  11. m_numberOfPilots = DB->getLastEntry(OPL::DbTable::Pilots);
  12. }
  13. const FlightEntry RandomGenerator::randomFlight()
  14. {
  15. const QDateTime dept_dt = randomDateTime();
  16. const QDateTime dest_dt = dept_dt.addSecs(QRandomGenerator::global()->bounded(900, 50000));
  17. const QString doft = dept_dt.date().toString(Qt::ISODate);
  18. OPL::Time tofb = OPL::Time::fromString(dept_dt.time().toString(Qt::ISODate), OPL::DateTimeFormat());
  19. OPL::Time tonb = OPL::Time::fromString(dest_dt.time().toString(Qt::ISODate), OPL::DateTimeFormat());
  20. int pic = randomPilot();
  21. int acft = randomTail();
  22. const QString dept = randomAirport();
  23. const QString dest = randomAirport();
  24. int tblk = OPL::Time::blockMinutes(tofb, tonb);
  25. int tNight = OPL::Calc::calculateNightTime(dept, dest, dept_dt, tblk, 6);
  26. auto flt_data = OPL::RowData_T();
  27. flt_data.insert(OPL::FlightEntry::DOFT, doft);
  28. flt_data.insert(OPL::FlightEntry::DEPT, dept);
  29. flt_data.insert(OPL::FlightEntry::DEST, dest);
  30. flt_data.insert(OPL::FlightEntry::PIC, pic);
  31. flt_data.insert(OPL::FlightEntry::ACFT, acft);
  32. flt_data.insert(OPL::FlightEntry::TOFB, tofb.toMinutes());
  33. flt_data.insert(OPL::FlightEntry::TONB, tonb.toMinutes());
  34. flt_data.insert(OPL::FlightEntry::TBLK, tblk);
  35. if (tNight > 0) flt_data.insert(OPL::FlightEntry::TNIGHT, tNight);
  36. bool pf = randomBool(); // Pilot Flying
  37. // Take-Off and Landing
  38. if (pf) {
  39. flt_data.insert(OPL::FlightEntry::PILOTFLYING, 1);
  40. if (OPL::Calc::isNight(dept, dept_dt, 6))
  41. flt_data.insert(OPL::FlightEntry::TONIGHT, 1);
  42. else
  43. flt_data.insert(OPL::FlightEntry::TODAY, 1);
  44. if (OPL::Calc::isNight(dest, dest_dt, 6))
  45. flt_data.insert(OPL::FlightEntry::LDGNIGHT, 1);
  46. else
  47. flt_data.insert(OPL::FlightEntry::LDGDAY, 1);
  48. }
  49. int function;
  50. if (pic == 1) {
  51. flt_data.insert(OPL::FlightEntry::TPIC, tblk);
  52. flt_data.insert(OPL::FlightEntry::SECONDPILOT, randomPilot());
  53. } else {
  54. function = QRandomGenerator::global()->bounded(1,4);
  55. flt_data.insert(OPL::FlightEntry::SECONDPILOT, 1);
  56. flt_data.insert(m_function_times[function], tblk);
  57. }
  58. return OPL::FlightEntry(flt_data);
  59. }
  60. const QTime RandomGenerator::randomTime()
  61. {
  62. int h = QRandomGenerator::global()->bounded(0, 23);
  63. int m = QRandomGenerator::global()->bounded(0, 59);
  64. return QTime(h,m);
  65. }
  66. const QDate RandomGenerator::randomDate()
  67. {
  68. int year = QRandomGenerator::global()->bounded(2000, 2021);
  69. int month = QRandomGenerator::global()->bounded(1,12);
  70. int day = QRandomGenerator::global()->bounded(1, 28);
  71. return QDate(year, month, day);
  72. }
  73. const QDateTime RandomGenerator::randomDateTime()
  74. {
  75. int year = QRandomGenerator::global()->bounded(2000, 2021);
  76. int month = QRandomGenerator::global()->bounded(1,12);
  77. int day = QRandomGenerator::global()->bounded(1, 28);
  78. int hour = QRandomGenerator::global()->bounded(0, 23);
  79. int minute = QRandomGenerator::global()->bounded(0,59);
  80. return QDateTime(QDate(year, month, day), QTime(hour, minute));
  81. }
  82. const QString RandomGenerator::randomAirport()
  83. {
  84. return DB->getAirportEntry(QRandomGenerator::global()->bounded(1, m_numberOfAirports)).getIcaoCode();
  85. }
  86. const int RandomGenerator::randomPilot()
  87. {
  88. if (!safeMode)
  89. return QRandomGenerator::global()->bounded(1, m_numberOfPilots);
  90. // verify entry exists before returning
  91. int pilot;
  92. do {
  93. pilot = QRandomGenerator::global()->bounded(1, m_numberOfPilots);
  94. } while (!DB->exists(Row(OPL::DbTable::Pilots, pilot)));
  95. return pilot;
  96. }
  97. const int RandomGenerator::randomTail()
  98. {
  99. if (!safeMode)
  100. return QRandomGenerator::global()->bounded(1, m_numberOfTails);
  101. // verify entry exists before returning
  102. int acft;
  103. do {
  104. acft = QRandomGenerator::global()->bounded(1, m_numberOfTails);
  105. } while (!DB->exists(Row(DbTable::Tails, acft)));
  106. return acft;
  107. }
  108. const bool RandomGenerator::randomBool()
  109. {
  110. #if HAVE_ARC4RANDOM
  111. return arc4random() > (RAND_MAX / 2);
  112. #else
  113. auto gen = std::bind(std::uniform_int_distribution<>(0,1),std::default_random_engine());
  114. return gen();
  115. #endif
  116. }
  117. } // namespace OPL