main.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 "starfield.h"
  14. #include "utils.h"
  15. #include "version.h"
  16. #include <algorithm> // transform
  17. // configuration here -- access via extern
  18. YAML::Node config;
  19. door::ANSIColor stringToANSIColor(std::string colorCode);
  20. std::function<std::ofstream &(void)> get_logger;
  21. std::function<void(void)> cls_display_starfield;
  22. std::function<int(void)> press_a_key;
  23. std::string return_current_time_and_date() {
  24. auto now = std::chrono::system_clock::now();
  25. auto in_time_t = std::chrono::system_clock::to_time_t(now);
  26. std::stringstream ss;
  27. // ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
  28. ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %r");
  29. return ss.str();
  30. }
  31. int press_any_key(door::Door &door) {
  32. door::ANSIColor green{door::COLOR::GREEN, door::ATTR::BOLD};
  33. door::ANSIColor blue{door::COLOR::BLUE, door::ATTR::BOLD};
  34. door::ANSIColor yellow{door::COLOR::YELLOW, door::ATTR::BOLD};
  35. door::ANSIColor any_caps = green;
  36. door::ANSIColor any_lower = yellow;
  37. door::renderFunction any_color =
  38. [&any_caps, &any_lower](const std::string &txt) -> door::Render {
  39. door::Render r(txt);
  40. // door::ANSIColor blue(door::COLOR::GREEN, door::ATTR::BOLD);
  41. // door::ANSIColor cyan(door::COLOR::YELLOW, door::ATTR::BOLD);
  42. for (char const &c : txt) {
  43. if (isupper(c))
  44. r.append(any_caps);
  45. else
  46. r.append(any_lower);
  47. }
  48. return r;
  49. };
  50. static std::default_random_engine generator;
  51. static std::uniform_int_distribution<int> distribution(0, 1);
  52. int use_set = distribution(generator);
  53. std::string text = "Press a key to continue...";
  54. door << door::reset;
  55. any_color(text).output(door);
  56. // << text;
  57. int r;
  58. int t = 0;
  59. // alternate animation
  60. #define ALT_ANIMATION
  61. #ifdef ALT_ANIMATION
  62. std::vector<std::pair<int, int>> words = find_words(text);
  63. std::string work_text = text;
  64. int text_length = text.size();
  65. // current word
  66. unsigned int word = 0;
  67. std::string current_word = text.substr(words[word].first, words[word].second);
  68. unsigned int wpos = 0;
  69. int ms_sleep = 0;
  70. int sleep_ms = 250;
  71. t = 0;
  72. while ((r = door.sleep_ms_key(sleep_ms)) == TIMEOUT) {
  73. ms_sleep += sleep_ms;
  74. if (ms_sleep > 1000) {
  75. ms_sleep -= 1000;
  76. t++;
  77. if (t >= door.inactivity) {
  78. // restore text
  79. door << std::string(text_length, '\b');
  80. any_color(text).output(door);
  81. // << text;
  82. door << door::nl;
  83. return TIMEOUT;
  84. }
  85. }
  86. ++wpos;
  87. if (wpos == current_word.size()) {
  88. ++word;
  89. work_text = text;
  90. wpos = 0;
  91. if (word == words.size()) {
  92. // we've reached the end
  93. word = 0;
  94. if (any_caps == green)
  95. any_caps = blue;
  96. else
  97. any_caps = green;
  98. door << std::string(text_length, '\b');
  99. any_color(work_text).output(door);
  100. current_word = text.substr(words[word].first, words[word].second);
  101. continue;
  102. }
  103. current_word = text.substr(words[word].first, words[word].second);
  104. }
  105. int c = current_word[wpos];
  106. while (!std::islower(c)) {
  107. // It is not lower case
  108. wpos++;
  109. if (wpos == current_word.size()) {
  110. // Ok, this word is done.
  111. wpos = 0;
  112. word++;
  113. work_text = text;
  114. if (word == words.size()) {
  115. word = 0;
  116. }
  117. current_word = text.substr(words[word].first, words[word].second);
  118. wpos = 0;
  119. }
  120. c = current_word[wpos];
  121. }
  122. // Ok, I found something that's lower case.
  123. work_text[words[word].first + wpos] = std::toupper(c);
  124. // std::toupper(words[word].first + wpos);
  125. door << std::string(text_length, '\b');
  126. any_color(work_text).output(door);
  127. // << work_text;
  128. }
  129. // ok, restore the original string
  130. door << std::string(text_length, '\b');
  131. any_color(text).output(door);
  132. //<< text;
  133. return r;
  134. #endif
  135. #ifdef ALT_ANIMATION_2
  136. // This can be cleaned up to be simpler -- and do the same effect. We don't
  137. // need the words here, we're not really using them.
  138. std::vector<std::pair<int, int>> words = find_words(text);
  139. std::string work_text = text;
  140. int text_length = text.size();
  141. // current word
  142. unsigned int word = 0;
  143. std::string current_word = text.substr(words[word].first, words[word].second);
  144. unsigned int wpos = 0;
  145. // DOES NOT HONOR door.inactivity (AT THIS TIME)
  146. while ((r = door.sleep_ms_key(200)) == TIMEOUT) {
  147. ++wpos;
  148. if (wpos == current_word.size()) {
  149. ++word;
  150. wpos = 0;
  151. if (word == words.size())
  152. word = 0;
  153. current_word = text.substr(words[word].first, words[word].second);
  154. }
  155. int c = current_word[wpos];
  156. while (!std::islower(c)) {
  157. // It is not lower case
  158. wpos++;
  159. if (wpos == current_word.size()) {
  160. // Ok, this word is done.
  161. wpos = 0;
  162. word++;
  163. if (word == words.size())
  164. word = 0;
  165. current_word = text.substr(words[word].first, words[word].second);
  166. wpos = 0;
  167. }
  168. c = current_word[wpos];
  169. }
  170. // Ok, I found something that's lower case.
  171. work_text = text;
  172. work_text[words[word].first + wpos] = std::toupper(c);
  173. // std::toupper(words[word].first + wpos);
  174. door << std::string(text_length, '\b') << work_text;
  175. }
  176. // ok, restore the original string
  177. door << std::string(text_length, '\b') << text;
  178. return r;
  179. #endif
  180. // animation spinner that reverses itself. /-\| and blocks.
  181. t = 0;
  182. int loop = 0;
  183. const char *loop_chars[3][4] = {{"/", "-", "\\", "|"},
  184. {"\xde", "\xdc", "\xdd", "\xdf"},
  185. {"\u2590", "\u2584", "\u258c", "\u2580"}};
  186. std::array<int, 11> sleeps = {1000, 700, 500, 300, 200, 200,
  187. 200, 300, 500, 700, 1000};
  188. // start out at
  189. int sleep_number = 5;
  190. bool forward = true;
  191. if (door::unicode) {
  192. if (use_set == 1)
  193. use_set = 2;
  194. }
  195. door << loop_chars[use_set][loop % 4];
  196. ms_sleep = 0;
  197. bool check_speed = false;
  198. while ((r = door.sleep_ms_key(sleeps[sleep_number])) == TIMEOUT) {
  199. ms_sleep += sleeps[sleep_number];
  200. if (ms_sleep > 1000) {
  201. ms_sleep -= 1000;
  202. t++;
  203. if (t >= door.inactivity) {
  204. door << "\b \b";
  205. door << door::nl;
  206. return TIMEOUT;
  207. }
  208. }
  209. if (forward) {
  210. loop++;
  211. if (loop == 4) {
  212. loop = 0;
  213. check_speed = true;
  214. }
  215. } else {
  216. --loop;
  217. if (loop < 0) {
  218. loop = 3;
  219. check_speed = true;
  220. }
  221. }
  222. if (check_speed) {
  223. sleep_number++;
  224. if (sleep_number >= (int)sleeps.size()) {
  225. sleep_number = 0;
  226. forward = !forward;
  227. }
  228. check_speed = false;
  229. }
  230. door << "\b" << loop_chars[use_set][loop];
  231. }
  232. door << "\b \b";
  233. door << door::nl;
  234. return r;
  235. }
  236. // This does not seem to be working. I keep getting zero.
  237. int opt_from_string(std::string colorCode) {
  238. for (std::size_t pos = 0; pos != deck_colors.size(); ++pos) {
  239. // if (caseInsensitiveStringCompare(colorCode, deck_colors[pos]) == 0) {
  240. if (iequals(colorCode, deck_colors[pos])) {
  241. return pos;
  242. }
  243. }
  244. return 0;
  245. }
  246. int configure(door::Door &door, DBData &db) {
  247. // pre-warm the parser
  248. find_words(std::string(""));
  249. auto menu = make_config_menu();
  250. int r = 0;
  251. bool save_deckcolor = false;
  252. const char *deckcolor = "DeckColor";
  253. std::string newColor;
  254. while (r >= 0) {
  255. if (save_deckcolor) {
  256. door << menu;
  257. db.setSetting(deckcolor, newColor);
  258. save_deckcolor = false;
  259. }
  260. if (cls_display_starfield)
  261. cls_display_starfield();
  262. else
  263. door << door::reset << door::cls;
  264. r = menu.choose(door);
  265. if (r > 0) {
  266. door << door::reset << door::cls;
  267. char c = menu.which(r - 1);
  268. if (c == 'D') {
  269. // Ok, deck colors
  270. // get default
  271. std::string currentDefault = db.getSetting(deckcolor, "ALL");
  272. int currentOpt = opt_from_string(currentDefault);
  273. door << door::reset << door::cls;
  274. auto deck = make_deck_menu();
  275. deck.defaultSelection(currentOpt);
  276. if (cls_display_starfield)
  277. cls_display_starfield();
  278. else
  279. door << door::reset << door::cls;
  280. int newOpt = deck.choose(door);
  281. door << door::reset << door::cls;
  282. if (newOpt >= 0) {
  283. newOpt--;
  284. newColor = stringFromColorOptions(newOpt);
  285. if (newOpt != currentOpt) {
  286. door.log() << deckcolor << " was " << currentDefault << ", "
  287. << currentOpt << ". Now " << newColor << ", " << newOpt
  288. << std::endl;
  289. save_deckcolor = true;
  290. }
  291. }
  292. }
  293. if (c == 'V') {
  294. // view settings -- Sysop Configuration
  295. if (cls_display_starfield)
  296. cls_display_starfield();
  297. else
  298. door << door::reset << door::cls;
  299. door::Panel config_panel = make_sysop_config();
  300. // config_panel.set(1, 1);
  301. door << config_panel << door::reset << door::nl;
  302. r = press_a_key();
  303. if (r < 0)
  304. return r;
  305. door << door::reset << door::cls;
  306. }
  307. if (c == 'Q') {
  308. return r;
  309. }
  310. }
  311. }
  312. return r;
  313. }
  314. int main(int argc, char *argv[]) {
  315. door::Door door("space-ace", argc, argv);
  316. // store the door log so we can easily access it.
  317. get_logger = [&door]() -> ofstream & { return door.log(); };
  318. press_a_key = [&door]() -> int { return press_any_key(door); };
  319. std::random_device rd;
  320. std::mt19937 rng(rd());
  321. Starfield mainfield(door, rng);
  322. cls_display_starfield = [&mainfield]() -> void { mainfield.display(); };
  323. /*
  324. AnimatedStarfield amazing(door, rng);
  325. amazing.display();
  326. int res = -1;
  327. while (res == -1) {
  328. res = door.sleep_ms_key(250);
  329. amazing.animate();
  330. }
  331. */
  332. DBData spacedb;
  333. spacedb.setUser(door.username);
  334. if (file_exists("space-ace.yaml")) {
  335. config = YAML::LoadFile("space-ace.yaml");
  336. }
  337. bool update_config = false;
  338. // populate with "good" defaults
  339. if (!config["hands_per_day"]) {
  340. config["hands_per_day"] = 3;
  341. update_config = true;
  342. }
  343. if (!config["date_format"]) {
  344. config["date_format"] = "%B %d"; // Month day or "%b %d,%Y" Mon,d YYYY
  345. update_config = true;
  346. }
  347. if (!config["date_score"]) {
  348. config["date_score"] = "%m/%d/%Y"; // or "%Y/%0m/%0d";
  349. update_config = true;
  350. }
  351. if (!config["makeup_per_day"]) {
  352. config["makeup_per_day"] = 5;
  353. update_config = true;
  354. }
  355. if (!config["play_days_ahead"]) {
  356. config["play_days_ahead"] = 2;
  357. update_config = true;
  358. }
  359. if (!config["date_monthly"]) {
  360. config["date_monthly"] = "%B %Y";
  361. update_config = true;
  362. }
  363. if (!config["_seed"]) {
  364. // pick numbers to use for seed
  365. std::string seeds;
  366. seeds += std::to_string(rng());
  367. seeds += ",";
  368. seeds += std::to_string(rng());
  369. seeds += ",";
  370. seeds += std::to_string(rng());
  371. config["_seed"] = seeds;
  372. update_config = true;
  373. }
  374. // save configuration -- something was updated
  375. if (update_config) {
  376. std::ofstream fout("space-ace.yaml");
  377. fout << config << std::endl;
  378. }
  379. // retrieve lastcall
  380. time_t last_call = std::stol(spacedb.getSetting("LastCall", "0"));
  381. std::chrono::_V2::system_clock::time_point now =
  382. std::chrono::system_clock::now();
  383. // store now as lastcall
  384. time_t now_t = std::chrono::system_clock::to_time_t(now);
  385. spacedb.setSetting("LastCall", std::to_string(now_t));
  386. // run maint
  387. {
  388. std::chrono::_V2::system_clock::time_point maint_date = now;
  389. firstOfMonthDate(maint_date);
  390. if (spacedb.expireScores(
  391. std::chrono::system_clock::to_time_t(maint_date))) {
  392. if (get_logger)
  393. get_logger() << "maint completed" << std::endl;
  394. door << "Thanks for waiting..." << door::nl;
  395. }
  396. }
  397. // Have they used this door before?
  398. if (last_call != 0) {
  399. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)
  400. << "Welcome Back!" << door::nl;
  401. auto nowClock = std::chrono::system_clock::from_time_t(now_t);
  402. auto lastClock = std::chrono::system_clock::from_time_t(last_call);
  403. auto delta = nowClock - lastClock;
  404. // int days = chrono::duration_cast<chrono::days>(delta).count(); //
  405. // c++ 20
  406. int hours = chrono::duration_cast<chrono::hours>(delta).count();
  407. int days = hours / 24;
  408. int minutes = chrono::duration_cast<chrono::minutes>(delta).count();
  409. int secs = chrono::duration_cast<chrono::seconds>(delta).count();
  410. if (days > 1) {
  411. door << door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD)
  412. << "It's been " << days << " days since you last played."
  413. << door::nl;
  414. } else {
  415. if (hours > 1) {
  416. door << door::ANSIColor(door::COLOR::CYAN) << "It's been " << hours
  417. << " hours since you last played." << door::nl;
  418. } else {
  419. if (minutes > 1) {
  420. door << door::ANSIColor(door::COLOR::CYAN) << "It's been " << minutes
  421. << " minutes since you last played." << door::nl;
  422. } else {
  423. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)
  424. << "It's been " << secs << " seconds since you last played."
  425. << door::nl;
  426. door << "It's like you never left." << door::nl;
  427. }
  428. }
  429. }
  430. if (press_a_key() < 0)
  431. return 0;
  432. }
  433. int mx, my; // Max screen width/height
  434. if (door.width == 0) {
  435. // screen detect failed, use sensible defaults
  436. door.width = mx = 80;
  437. door.height = my = 23;
  438. } else {
  439. mx = door.width;
  440. my = door.height;
  441. }
  442. // We assume here that the width and height aren't something crazy like
  443. // 10x15. :P (or 24x923!)
  444. display_starfield_space_ace(door, rng);
  445. // for testing inactivity timeout
  446. // door.inactivity = 10;
  447. door::Panel timeout = make_timeout(mx, my);
  448. door::Menu m = make_main_menu();
  449. door::Panel about = make_about(); // 8 lines
  450. door::Panel help = make_help();
  451. // center the about box
  452. about.set((mx - 60) / 2, (my - 9) / 2);
  453. // center the help box
  454. help.set((mx - 60) / 2, (my - 15) / 2);
  455. /*
  456. // animated starfield with about panel
  457. AnimatedStarfield amazing(door, rng);
  458. checkVisibleFunction about_visible = [&about](int x, int y) -> bool {
  459. int px, py;
  460. about.get(px, py);
  461. int pw = about.getWidth();
  462. int ph = about.getHeight();
  463. if ((x >= px) and (x <= (px + pw + 1)) and (y >= py) and (y <= py + ph))
  464. return false;
  465. return true;
  466. };
  467. amazing.setVisible(about_visible);
  468. amazing.regenerate();
  469. amazing.display();
  470. door << about << door::reset;
  471. int res = -1;
  472. while (res == -1) {
  473. res = door.sleep_ms_key(250);
  474. amazing.animate();
  475. }
  476. */
  477. PlayCards pc(door, spacedb, rng);
  478. Starfield menu_stars(door, rng);
  479. int r = 0;
  480. while ((r >= 0) and (r != 6)) {
  481. // starfield + menu ?
  482. // display_starfield(door, rng);
  483. menu_stars.display();
  484. r = m.choose(door);
  485. // need to reset the colors. (whoops!)
  486. door << door::reset << door::cls; // door::nl;
  487. // OK! The screen is blank at this point!
  488. switch (r) {
  489. case 1: // play game
  490. {
  491. // PlayCards pc(door, spacedb, rng);
  492. r = pc.play();
  493. // r = play_cards(door, spacedb, rng);
  494. }; break;
  495. case 2: // view scores
  496. // door << door::cls;
  497. {
  498. Scores score(door, spacedb, menu_stars);
  499. score.display_scores(door);
  500. }
  501. r = press_a_key();
  502. break;
  503. case 3: // configure
  504. r = configure(door, spacedb);
  505. // r = press_a_key();
  506. break;
  507. case 4: // help
  508. // display_starfield(door, rng);
  509. menu_stars.display();
  510. door << help << door::nl;
  511. r = press_a_key();
  512. break;
  513. case 5: // about
  514. // display_starfield(door, rng);
  515. menu_stars.display();
  516. door << about << door::nl;
  517. r = press_a_key();
  518. break;
  519. case 6: // quit
  520. break;
  521. }
  522. }
  523. if (r < 0) {
  524. // DOOR_ERROR:
  525. if (r == TIMEOUT) {
  526. door.log() << "TIMEOUT" << std::endl;
  527. door << timeout << door::reset << door::nl << door::nl;
  528. } else {
  529. if (r == OUTOFTIME) {
  530. door.log() << "OUTTA TIME" << std::endl;
  531. door::Panel notime = make_notime(mx, my);
  532. door << notime << door::reset << door::nl;
  533. }
  534. }
  535. return 0;
  536. }
  537. door << door::nl;
  538. // door << door::reset << door::cls;
  539. // display_starfield(door, rng);
  540. mainfield.display();
  541. door << m << door::reset << door::nl;
  542. // Normal DOOR exit goes here...
  543. door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
  544. press_a_key = nullptr;
  545. get_logger = nullptr;
  546. cls_display_starfield = nullptr;
  547. return 0;
  548. }