main.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #include "door.h"
  2. #include "yaml-cpp/yaml.h"
  3. #include <chrono> // chrono::system_clock
  4. #include <ctime> // localtime
  5. #include <iomanip> // put_time
  6. #include <iostream>
  7. #include <random>
  8. #include <string>
  9. #include "db.h"
  10. #include "deck.h"
  11. #include "play.h"
  12. #include "scores.h"
  13. #include "utils.h"
  14. #include "version.h"
  15. #include <algorithm> // transform
  16. // configuration here -- access via extern
  17. YAML::Node config;
  18. door::ANSIColor stringToANSIColor(std::string colorCode);
  19. std::function<std::ofstream &(void)> get_logger;
  20. std::function<void(void)> cls_display_starfield;
  21. std::string return_current_time_and_date() {
  22. auto now = std::chrono::system_clock::now();
  23. auto in_time_t = std::chrono::system_clock::to_time_t(now);
  24. std::stringstream ss;
  25. // ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
  26. ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %r");
  27. return ss.str();
  28. }
  29. int press_a_key(door::Door &door) {
  30. door << door::reset << "Press a key to continue...";
  31. int r = door.sleep_key(door.inactivity);
  32. door << door::nl;
  33. return r;
  34. }
  35. // This does not seem to be working. I keep getting zero.
  36. int opt_from_string(std::string colorCode) {
  37. for (std::size_t pos = 0; pos != deck_colors.size(); ++pos) {
  38. // if (caseInsensitiveStringCompare(colorCode, deck_colors[pos]) == 0) {
  39. if (iequals(colorCode, deck_colors[pos])) {
  40. return pos;
  41. }
  42. }
  43. return 0;
  44. }
  45. int configure(door::Door &door, DBData &db) {
  46. // pre-warm the parser
  47. find_words(std::string(""));
  48. auto menu = make_config_menu();
  49. int r = 0;
  50. bool save_deckcolor = false;
  51. const char *deckcolor = "DeckColor";
  52. std::string newColor;
  53. while (r >= 0) {
  54. if (save_deckcolor) {
  55. door << menu;
  56. db.setSetting(deckcolor, newColor);
  57. save_deckcolor = false;
  58. }
  59. if (cls_display_starfield)
  60. cls_display_starfield();
  61. else
  62. door << door::reset << door::cls;
  63. r = menu.choose(door);
  64. if (r > 0) {
  65. door << door::reset << door::cls;
  66. char c = menu.which(r - 1);
  67. if (c == 'D') {
  68. // Ok, deck colors
  69. // get default
  70. std::string currentDefault = db.getSetting(deckcolor, "ALL");
  71. int currentOpt = opt_from_string(currentDefault);
  72. door << door::reset << door::cls;
  73. auto deck = make_deck_menu();
  74. deck.defaultSelection(currentOpt);
  75. if (cls_display_starfield)
  76. cls_display_starfield();
  77. else
  78. door << door::reset << door::cls;
  79. int newOpt = deck.choose(door);
  80. door << door::reset << door::cls;
  81. if (newOpt >= 0) {
  82. newOpt--;
  83. newColor = stringFromColorOptions(newOpt);
  84. if (newOpt != currentOpt) {
  85. door.log() << deckcolor << " was " << currentDefault << ", "
  86. << currentOpt << ". Now " << newColor << ", " << newOpt
  87. << std::endl;
  88. save_deckcolor = true;
  89. }
  90. }
  91. }
  92. if (c == 'V') {
  93. // view settings -- Sysop Configuration
  94. if (cls_display_starfield)
  95. cls_display_starfield();
  96. else
  97. door << door::reset << door::cls;
  98. door::Panel config_panel = make_sysop_config();
  99. // config_panel.set(1, 1);
  100. door << config_panel << door::reset << door::nl;
  101. r = press_a_key(door);
  102. if (r < 0)
  103. return r;
  104. door << door::reset << door::cls;
  105. }
  106. if (c == 'Q') {
  107. return r;
  108. }
  109. }
  110. }
  111. return r;
  112. }
  113. int main(int argc, char *argv[]) {
  114. door::Door door("space-ace", argc, argv);
  115. // store the door log so we can easily access it.
  116. get_logger = [&door]() -> ofstream & { return door.log(); };
  117. std::random_device rd;
  118. std::mt19937 rng(rd());
  119. cls_display_starfield = [&door, &rng]() -> void {
  120. display_starfield(door, rng);
  121. };
  122. DBData spacedb;
  123. spacedb.setUser(door.username);
  124. if (file_exists("space-ace.yaml")) {
  125. config = YAML::LoadFile("space-ace.yaml");
  126. }
  127. bool update_config = false;
  128. // populate with "good" defaults
  129. if (!config["hands_per_day"]) {
  130. config["hands_per_day"] = 3;
  131. update_config = true;
  132. }
  133. if (!config["date_format"]) {
  134. config["date_format"] = "%B %d"; // Month day or "%b %d,%Y" Mon,d YYYY
  135. update_config = true;
  136. }
  137. if (!config["date_score"]) {
  138. config["date_score"] = "%m/%d/%Y"; // or "%Y/%0m/%0d";
  139. update_config = true;
  140. }
  141. if (!config["makeup_per_day"]) {
  142. config["makeup_per_day"] = 5;
  143. update_config = true;
  144. }
  145. if (!config["play_days_ahead"]) {
  146. config["play_days_ahead"] = 2;
  147. update_config = true;
  148. }
  149. if (!config["date_monthly"]) {
  150. config["date_monthly"] = "%B %Y";
  151. update_config = true;
  152. }
  153. // save configuration -- something was updated
  154. if (update_config) {
  155. std::ofstream fout("space-ace.yaml");
  156. fout << config << std::endl;
  157. }
  158. // retrieve lastcall
  159. time_t last_call = std::stol(spacedb.getSetting("LastCall", "0"));
  160. std::chrono::_V2::system_clock::time_point now =
  161. std::chrono::system_clock::now();
  162. // store now as lastcall
  163. time_t now_t = std::chrono::system_clock::to_time_t(now);
  164. spacedb.setSetting("LastCall", std::to_string(now_t));
  165. // run maint
  166. {
  167. std::chrono::_V2::system_clock::time_point maint_date = now;
  168. firstOfMonthDate(maint_date);
  169. if (spacedb.expireScores(
  170. std::chrono::system_clock::to_time_t(maint_date))) {
  171. if (get_logger)
  172. get_logger() << "maint completed" << std::endl;
  173. door << "Thanks for waiting..." << door::nl;
  174. }
  175. }
  176. // Have they used this door before?
  177. if (last_call != 0) {
  178. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)
  179. << "Welcome Back!" << door::nl;
  180. auto nowClock = std::chrono::system_clock::from_time_t(now_t);
  181. auto lastClock = std::chrono::system_clock::from_time_t(last_call);
  182. auto delta = nowClock - lastClock;
  183. // int days = chrono::duration_cast<chrono::days>(delta).count(); //
  184. // c++ 20
  185. int hours = chrono::duration_cast<chrono::hours>(delta).count();
  186. int days = hours / 24;
  187. int minutes = chrono::duration_cast<chrono::minutes>(delta).count();
  188. int secs = chrono::duration_cast<chrono::seconds>(delta).count();
  189. if (days > 1) {
  190. door << door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD)
  191. << "It's been " << days << " days since you last played."
  192. << door::nl;
  193. } else {
  194. if (hours > 1) {
  195. door << door::ANSIColor(door::COLOR::CYAN) << "It's been " << hours
  196. << " hours since you last played." << door::nl;
  197. } else {
  198. if (minutes > 1) {
  199. door << door::ANSIColor(door::COLOR::CYAN) << "It's been " << minutes
  200. << " minutes since you last played." << door::nl;
  201. } else {
  202. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)
  203. << "It's been " << secs << " seconds since you last played."
  204. << door::nl;
  205. door << "It's like you never left." << door::nl;
  206. }
  207. }
  208. }
  209. door << door::reset;
  210. press_a_key(door);
  211. }
  212. int mx, my; // Max screen width/height
  213. if (door.width == 0) {
  214. // screen detect failed, use sensible defaults
  215. door.width = mx = 80;
  216. door.height = my = 23;
  217. } else {
  218. mx = door.width;
  219. my = door.height;
  220. }
  221. // We assume here that the width and height aren't something crazy like
  222. // 10x15. :P (or 24x923!)
  223. display_starfield_space_ace(door, rng);
  224. // for testing inactivity timeout
  225. // door.inactivity = 10;
  226. door::Panel timeout = make_timeout(mx, my);
  227. door::Menu m = make_main_menu();
  228. door::Panel about = make_about(); // 8 lines
  229. door::Panel help = make_help();
  230. // center the about box
  231. about.set((mx - 60) / 2, (my - 9) / 2);
  232. // center the help box
  233. help.set((mx - 60) / 2, (my - 15) / 2);
  234. PlayCards pc(door, spacedb, rng);
  235. int r = 0;
  236. while ((r >= 0) and (r != 6)) {
  237. // starfield + menu ?
  238. display_starfield(door, rng);
  239. r = m.choose(door);
  240. // need to reset the colors. (whoops!)
  241. door << door::reset << door::cls; // door::nl;
  242. // OK! The screen is blank at this point!
  243. switch (r) {
  244. case 1: // play game
  245. {
  246. // PlayCards pc(door, spacedb, rng);
  247. r = pc.play();
  248. // r = play_cards(door, spacedb, rng);
  249. }; break;
  250. case 2: // view scores
  251. // door << door::cls;
  252. {
  253. Scores score(door, spacedb);
  254. score.display_scores(door);
  255. }
  256. r = press_a_key(door);
  257. break;
  258. case 3: // configure
  259. r = configure(door, spacedb);
  260. // r = press_a_key(door);
  261. break;
  262. case 4: // help
  263. display_starfield(door, rng);
  264. door << help << door::nl;
  265. r = press_a_key(door);
  266. break;
  267. case 5: // about
  268. display_starfield(door, rng);
  269. door << about << door::nl;
  270. r = press_a_key(door);
  271. break;
  272. case 6: // quit
  273. break;
  274. }
  275. }
  276. if (r < 0) {
  277. // TIMEOUT:
  278. if (r == -1) {
  279. door.log() << "TIMEOUT" << std::endl;
  280. door << timeout << door::reset << door::nl << door::nl;
  281. } else {
  282. if (r == -3) {
  283. door.log() << "OUTTA TIME" << std::endl;
  284. door::Panel notime = make_notime(mx, my);
  285. door << notime << door::reset << door::nl;
  286. }
  287. }
  288. return 0;
  289. }
  290. door << door::nl;
  291. // door << door::reset << door::cls;
  292. display_starfield(door, rng);
  293. door << m << door::reset << door::nl;
  294. // Normal DOOR exit goes here...
  295. door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
  296. get_logger = nullptr;
  297. cls_display_starfield = nullptr;
  298. return 0;
  299. }