buysell.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef BUYSELL_H
  2. #define BUYSELL_H
  3. #include <iostream>
  4. typedef uint8_t port_type;
  5. enum PRODUCT { FUEL = 0, ORG = 1, EQU = 2 };
  6. // Class 0 : Special
  7. // Class 9 : StarDock BBB
  8. // typedef std::array<bool, 3> buysell;
  9. struct buysell {
  10. bool foe[3]; // TRUE = BUY
  11. bool operator==(const buysell& rhs) const;
  12. friend std::ostream& operator<<(std::ostream& os, const buysell& bs);
  13. };
  14. // ostream& operator<<(std::ostream& os, const buysell& bs);
  15. // typedef std::array<char, 3> buysell_text;
  16. struct buysell_text {
  17. char txt[3];
  18. bool operator==(const buysell_text& rhs) const;
  19. friend std::ostream& operator<<(std::ostream& os, const buysell_text& bst);
  20. };
  21. /*
  22. 1 : "BBS", TTF
  23. 2 : "BSB", TFT
  24. 3 : "SBB", FTT
  25. 4 : "SSB", FFT
  26. 5 : "SBS", FTF
  27. 6 : "BSS", TFF
  28. 7 : "SSS", FFF
  29. 8 : "BBB", TTT
  30. 9 : "BBB", TTT
  31. */
  32. /* convert type to buysell flag values, buy = true */
  33. constexpr buysell get_buysell(port_type type) {
  34. switch (type) {
  35. case 1: // BBS TTF
  36. return {true, true, false};
  37. case 2: // BSB TFT
  38. return {true, false, true};
  39. case 3: // SBB FTT
  40. return {false, true, true};
  41. case 4: // SSB FFT
  42. return {false, false, true};
  43. case 5: // SBS FTF
  44. return {false, true, false};
  45. case 6: // BSS TFF
  46. return {true, false, false};
  47. case 7: // SSS FFF
  48. return {false, false, false};
  49. case 8: // BBB TTT
  50. case 9:
  51. return {true, true, true};
  52. default:
  53. throw std::invalid_argument("invalid buysell type");
  54. }
  55. }
  56. constexpr buysell_text text_from_buysell(const buysell market) {
  57. buysell_text text{'?', '?', '?'};
  58. for (int x = 0; x < 3; ++x) {
  59. text.txt[x] = market.foe[x] ? 'B' : 'S';
  60. }
  61. return text;
  62. }
  63. constexpr buysell_text text_from_type(port_type type) {
  64. switch (type) {
  65. case 1:
  66. return buysell_text{'B', 'B', 'S'};
  67. case 2:
  68. return buysell_text{'B', 'S', 'B'};
  69. case 3:
  70. return buysell_text{'S', 'B', 'B'};
  71. case 4:
  72. return buysell_text{'S', 'S', 'B'};
  73. case 5:
  74. return buysell_text{'S', 'B', 'S'};
  75. case 6:
  76. return buysell_text{'B', 'S', 'S'};
  77. case 7:
  78. return buysell_text{'S', 'S', 'S'};
  79. case 8:
  80. case 9:
  81. return buysell_text{'B', 'B', 'B'};
  82. default:
  83. throw std::invalid_argument("invalid text_from_type type");
  84. }
  85. }
  86. constexpr uint8_t type_from_buysell(const buysell market) {
  87. if (market.foe[0]) {
  88. if (market.foe[1]) {
  89. if (market.foe[2]) {
  90. return 8; // BBB TTT
  91. } else
  92. return 1; // BBS TTF
  93. } else {
  94. if (market.foe[2]) {
  95. return 2; // BSB TFT
  96. } else
  97. return 6; // BSS TFF
  98. }
  99. } else {
  100. if (market.foe[1]) {
  101. if (market.foe[2]) {
  102. return 3; // SBB FTT
  103. } else
  104. return 5; // SBS FTF
  105. } else {
  106. if (market.foe[2]) {
  107. return 4; // SSB FFT
  108. } else
  109. return 7; // SSS FFF
  110. }
  111. }
  112. }
  113. constexpr buysell invert_buysell(const buysell market) {
  114. if (market.foe[0]) {
  115. if (market.foe[1]) {
  116. if (market.foe[2]) {
  117. return {false, false, false}; // BBB TTT
  118. } else
  119. return {false, false, true}; // BBS TTF
  120. } else {
  121. if (market.foe[2]) {
  122. return {false, true, false}; // BSB TFT
  123. } else
  124. return {false, true, true}; // BSS TFF
  125. }
  126. } else {
  127. if (market.foe[1]) {
  128. if (market.foe[2]) {
  129. return {true, false, false}; // SBB FTT
  130. } else
  131. return {true, false, true}; // SBS FTF
  132. } else {
  133. if (market.foe[2]) {
  134. return {true, true, false}; // SSB FFT
  135. } else
  136. return {true, true, true}; // SSS FFF
  137. }
  138. }
  139. }
  140. #endif