dispatchers.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #ifndef DISPATCHERS_H
  2. #define DISPATCHERS_H
  3. #include <map>
  4. #include <memory>
  5. #include <string>
  6. #include "director.h"
  7. #include "session_common.h"
  8. /*
  9. Item of the day:
  10. class Result {
  11. // holds unique_ptr or shared_ptr to the "dispatcher"
  12. // when I'm done -- delete the result, cleaning up
  13. // the dispatcher
  14. }
  15. How does this call another?
  16. How does it return a result?
  17. possibly: io_service::post( DONE ); !
  18. */
  19. class Dispatch {
  20. protected:
  21. Director &director;
  22. notifyFunc notify_;
  23. public:
  24. std::string name;
  25. Dispatch(Director &, const char * called=nullptr);
  26. virtual ~Dispatch();
  27. bool aborted;
  28. void setNotify(notifyFunc nf);
  29. void notify(void);
  30. virtual void activate(void) = 0;
  31. virtual void deactivate(void);
  32. const std::string &get_prompt(void);
  33. void to_server(const std::string &send);
  34. void to_client(const std::string &send);
  35. void activate_chain(std::shared_ptr<Dispatch> new_chain);
  36. // default to chain calls
  37. void chain_client_input(const std::string &input);
  38. void chain_server_line(const std::string &line, const std::string &raw_line);
  39. void chain_server_prompt(const std::string &prompt);
  40. virtual void client_input(const std::string &input);
  41. virtual void server_line(const std::string &line, const std::string &raw_line);
  42. virtual void server_prompt(const std::string &prompt);
  43. };
  44. /*
  45. * Some options for input:
  46. *
  47. * numeric only
  48. *
  49. */
  50. class InputDispatch : public Dispatch {
  51. private:
  52. public:
  53. InputDispatch(Director &, const char * called=nullptr);
  54. ~InputDispatch();
  55. std::string prompt;
  56. size_t max_length;
  57. std::string input;
  58. bool numeric;
  59. void activate(void) override;
  60. void deactivate(void) override;
  61. // optional here
  62. void server_line(const std::string &line,
  63. const std::string &raw_line) override;
  64. // void server_prompt(const std::string &prompt);
  65. void client_input(const std::string &cinput) override;
  66. };
  67. class MenuDispatch : public Dispatch {
  68. private:
  69. void help(void);
  70. void menubox(void);
  71. size_t max_width;
  72. size_t max_option_width;
  73. bool instant = false;
  74. void calculate_widths(void);
  75. std::string centered(int length, const std::string &);
  76. public:
  77. MenuDispatch(Director &, const char * called=nullptr);
  78. ~MenuDispatch();
  79. std::string menu_box_color;
  80. std::string menu_text_color;
  81. std::string menu_title;
  82. std::string menu_options_color;
  83. std::string menu_prompt;
  84. bool lazy = true;
  85. std::map<std::string, std::string> menu;
  86. bool case_sensitive = false;
  87. std::string input;
  88. int choice;
  89. void activate(void) override;
  90. void deactivate(void) override;
  91. // optional here
  92. void server_line(const std::string &line, const std::string &raw_line) override;
  93. // void server_prompt(const std::string &prompt);
  94. void client_input(const std::string &cinput) override;
  95. };
  96. class CIMDispatch : public Dispatch {
  97. public:
  98. CIMDispatch(Director &, const char * called=nullptr);
  99. int count;
  100. void activate(void) override;
  101. void deactivate(void) override;
  102. void server_line(const std::string &line, const std::string &raw_line) override;
  103. };
  104. class MoveDispatch : public Dispatch {
  105. public:
  106. MoveDispatch(Director &, const char * called=nullptr);
  107. ~MoveDispatch();
  108. sector_type move_to;
  109. sector_type starting;
  110. int state;
  111. int success;
  112. int warp_pos;
  113. bool use_express;
  114. std::string why_failed;
  115. std::string at_destination;
  116. std::vector<int> warp_lane;
  117. void activate(void) override;
  118. void deactivate(void) override;
  119. // optional here
  120. void server_line(const std::string &line, const std::string &raw_line) override;
  121. void server_prompt(const std::string &prompt) override;
  122. void client_input(const std::string &input) override;
  123. private:
  124. //bool density_clear(density d); // int sector, int density);
  125. };
  126. class TraderDispatch : public Dispatch {
  127. private:
  128. public:
  129. TraderDispatch(Director &, const char * called=nullptr);
  130. ~TraderDispatch();
  131. char foe[4] = "foe";
  132. bool trade_end_empty;
  133. // success / failure ?
  134. bool success;
  135. std::string why_failed;
  136. /**
  137. * internal state
  138. *
  139. * 1 = <Info> query.
  140. * 2 = move to active port
  141. * 3 = trade
  142. * 4 = if (burnt), stop, otherwise toggle active_port and state = 2
  143. *
  144. * NEW: set port[1] to 0 for buy-only ability.
  145. * Percent 20 doesn't work with Cargo Trans' 250 holds.
  146. * Maybe look at the amount instead? If < holds = burnt.
  147. */
  148. int state;
  149. float percent;
  150. bool buying;
  151. int initial_offer;
  152. int last_offer;
  153. int final_offer;
  154. int product; // product we are buying/selling 0,1,2 foe.
  155. int stop_percent;
  156. bool try_again;
  157. // should this be 0/1 ? right now it is the port's sector number.
  158. int active_port; // port trading with
  159. // information from the find_best_trades function + others.
  160. int port[2];
  161. int active;
  162. // I don't care about trade type, just trades.
  163. int type;
  164. buysell trades;
  165. buysell port_buysell[2];
  166. void activate(void) override;
  167. void deactivate(void) override;
  168. void server_line(const std::string &line,
  169. const std::string &raw_line) override;
  170. void server_prompt(const std::string &prompt) override;
  171. void client_input(const std::string &cinput) override;
  172. };
  173. class CoreDispatch : public Dispatch {
  174. public:
  175. CoreDispatch(Director &, const char * called=nullptr);
  176. void activate(void) override;
  177. void deactivate(void) override;
  178. // optional here
  179. void server_line(const std::string &line, const std::string &raw_line) override;
  180. void server_prompt(const std::string &prompt) override;
  181. void client_input(const std::string &input) override;
  182. };
  183. #endif