main.cpp 11 KB

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