main.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. #include "door.h"
  2. #include "space.h"
  3. #include "yaml-cpp/yaml.h"
  4. #include <chrono> // chrono::system_clock
  5. #include <ctime> // localtime
  6. #include <iomanip> // put_time
  7. #include <iostream>
  8. #include <random>
  9. #include <string>
  10. #include "db.h"
  11. #include "deck.h"
  12. #include "play.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. /*
  21. unsigned long score = 0;
  22. int hand = 1;
  23. int total_hands = 3;
  24. int card_number = 28;
  25. int current_streak = 0;
  26. int best_streak = 0;
  27. int active_card = 23;
  28. std::chrono::_V2::system_clock::time_point play_day;
  29. */
  30. /*
  31. Cards:
  32. 4 layers deep.
  33. https://en.wikipedia.org/wiki/Code_page_437
  34. using: \xb0, 0xb1, 0xb2, 0xdb
  35. OR: \u2591, \u2592, \u2593, \u2588
  36. Like so:
  37. ##### #####
  38. ##### #####
  39. ##### #####
  40. Cards: (Black on White, or Red on White)
  41. 8D### TH###
  42. ##D## ##H##
  43. ###D8 ###HT
  44. D, H = Red, Clubs, Spades = Black.
  45. ^ Where D = Diamonds, H = Hearts
  46. ♥, ♦, ♣, ♠
  47. \x03, \x04, \x05, \x06
  48. \u2665, \u2666, \u2663, \u2660
  49. Card layout. Actual cards are 3 lines thick.
  50. ░░░░░ ░░░░░ ░░░░░
  51. ░░░░░ ░░░░░ ░░░░░
  52. ▒▒▒▒▒░▒▒▒▒▒ #####░##### #####░#####
  53. ▒▒▒▒▒ ▒▒▒▒▒ ##### ##### ##### #####
  54. ▓▓▓▓▓▒▓▓▓▓▓▒▓▓▓▓▓ #####=#####=##### #####=#####=#####
  55. ▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ##### ##### ##### ##### ##### #####
  56. █████▓█████▓█████▓#####=#####=#####=#####=#####=#####=#####
  57. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  58. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  59. #####
  60. Player Information ##### Time in: xx Time out: xx
  61. Name: ##### Playing Day: November 3rd
  62. Hand Score : Current Streak: N
  63. Todays Score : XX Cards Remaining Longest Streak: NN
  64. Monthly Score: Playing Hand X of X Most Won: xxx Lost: xxx
  65. [4] Lf [6] Rt [Space] Play Card [Enter] Draw [D]one [H]elp [R]edraw
  66. ►, ◄, ▲, ▼
  67. \x10, \x11, \x1e, \x1f
  68. \u25ba, \u25c4, \u25b2, \u25bc
  69. The Name is <- 6 to the left.
  70. # is back of card. = is upper card showing between.
  71. There's no fancy anything here. Cards overlap the last
  72. line of the previous line/card.
  73. */
  74. door::Panel make_timeout(int mx, int my) {
  75. door::ANSIColor yellowred =
  76. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
  77. std::string line_text("Sorry, you've been inactive for too long.");
  78. int msgWidth = line_text.length() + (2 * 3); // + padding * 2
  79. door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
  80. // place.setTitle(std::make_unique<door::Line>(title), 1);
  81. timeout.setStyle(door::BorderStyle::DOUBLE);
  82. timeout.setColor(yellowred);
  83. door::Line base(line_text);
  84. base.setColor(yellowred);
  85. std::string pad1(3, ' ');
  86. /*
  87. std::string pad1(3, '\xb0');
  88. if (door::unicode) {
  89. std::string unicode;
  90. door::cp437toUnicode(pad1.c_str(), unicode);
  91. pad1 = unicode;
  92. }
  93. */
  94. base.setPadding(pad1, yellowred);
  95. // base.setColor(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLACK));
  96. std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
  97. timeout.addLine(std::make_unique<door::Line>(base));
  98. return timeout;
  99. }
  100. door::Panel make_notime(int mx, int my) {
  101. door::ANSIColor yellowred =
  102. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
  103. std::string line_text("Sorry, you've used up all your time for today.");
  104. int msgWidth = line_text.length() + (2 * 3); // + padding * 2
  105. door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
  106. timeout.setStyle(door::BorderStyle::DOUBLE);
  107. timeout.setColor(yellowred);
  108. door::Line base(line_text);
  109. base.setColor(yellowred);
  110. std::string pad1(3, ' ');
  111. /*
  112. std::string pad1(3, '\xb0');
  113. if (door::unicode) {
  114. std::string unicode;
  115. door::cp437toUnicode(pad1.c_str(), unicode);
  116. pad1 = unicode;
  117. }
  118. */
  119. base.setPadding(pad1, yellowred);
  120. std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
  121. timeout.addLine(std::make_unique<door::Line>(base));
  122. return timeout;
  123. }
  124. door::Menu make_main_menu(void) {
  125. door::Menu m(5, 5, 25);
  126. door::Line mtitle(SPACEACE " Main Menu");
  127. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  128. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  129. door::ATTR::BOLD);
  130. m.setColor(border_color);
  131. mtitle.setColor(title_color);
  132. mtitle.setPadding(" ", title_color);
  133. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  134. // m.setColorizer(true,
  135. m.setRender(true, door::Menu::makeRender(
  136. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  137. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  138. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  139. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
  140. // m.setColorizer(false,
  141. m.setRender(false, door::Menu::makeRender(
  142. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  143. door::ATTR::BOLD),
  144. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  145. door::ATTR::BOLD),
  146. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  147. door::ATTR::BOLD),
  148. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
  149. door::ATTR::BOLD)));
  150. m.addSelection('P', "Play Cards");
  151. m.addSelection('S', "View Scores");
  152. m.addSelection('C', "Configure");
  153. m.addSelection('H', "Help");
  154. m.addSelection('A', "About this game");
  155. m.addSelection('Q', "Quit");
  156. return m;
  157. }
  158. door::renderFunction statusValue(door::ANSIColor status,
  159. door::ANSIColor value) {
  160. door::renderFunction rf = [status,
  161. value](const std::string &txt) -> door::Render {
  162. door::Render r(txt);
  163. door::ColorOutput co;
  164. co.pos = 0;
  165. co.len = 0;
  166. co.c = status;
  167. size_t pos = txt.find(':');
  168. if (pos == std::string::npos) {
  169. // failed to find :, render digits/numbers in value color
  170. int tpos = 0;
  171. for (char const &c : txt) {
  172. if (std::isdigit(c)) {
  173. if (co.c != value) {
  174. r.outputs.push_back(co);
  175. co.reset();
  176. co.pos = tpos;
  177. co.c = value;
  178. }
  179. } else {
  180. if (co.c != status) {
  181. r.outputs.push_back(co);
  182. co.reset();
  183. co.pos = tpos;
  184. co.c = status;
  185. }
  186. }
  187. co.len++;
  188. tpos++;
  189. }
  190. if (co.len != 0)
  191. r.outputs.push_back(co);
  192. } else {
  193. pos++; // Have : in status color
  194. co.len = pos;
  195. r.outputs.push_back(co);
  196. co.reset();
  197. co.pos = pos;
  198. co.c = value;
  199. co.len = txt.length() - pos;
  200. r.outputs.push_back(co);
  201. }
  202. return r;
  203. };
  204. return rf;
  205. }
  206. /*
  207. door::renderFunction rStatus = [](const std::string &txt) -> door::Render {
  208. door::Render r(txt);
  209. door::ColorOutput co;
  210. // default colors STATUS: value
  211. door::ANSIColor status(door::COLOR::BLUE, door::ATTR::BOLD);
  212. door::ANSIColor value(door::COLOR::YELLOW, door::ATTR::BOLD);
  213. co.pos = 0;
  214. co.len = 0;
  215. co.c = status;
  216. size_t pos = txt.find(':');
  217. if (pos == std::string::npos) {
  218. // failed to find - use entire string as status color.
  219. co.len = txt.length();
  220. r.outputs.push_back(co);
  221. } else {
  222. pos++; // Have : in status color
  223. co.len = pos;
  224. r.outputs.push_back(co);
  225. co.reset();
  226. co.pos = pos;
  227. co.c = value;
  228. co.len = txt.length() - pos;
  229. r.outputs.push_back(co);
  230. }
  231. return r;
  232. };
  233. */
  234. std::string return_current_time_and_date() {
  235. auto now = std::chrono::system_clock::now();
  236. auto in_time_t = std::chrono::system_clock::to_time_t(now);
  237. std::stringstream ss;
  238. // ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
  239. ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %r");
  240. return ss.str();
  241. }
  242. int press_a_key(door::Door &door) {
  243. door << door::reset << "Press a key to continue...";
  244. int r = door.sleep_key(door.inactivity);
  245. door << door::nl;
  246. return r;
  247. }
  248. door::Menu make_config_menu(void) {
  249. door::Menu m(5, 5, 31);
  250. door::Line mtitle(SPACEACE " Configuration Menu");
  251. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  252. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  253. door::ATTR::BOLD);
  254. m.setColor(border_color);
  255. mtitle.setColor(title_color);
  256. mtitle.setPadding(" ", title_color);
  257. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  258. // m.setColorizer(true,
  259. m.setRender(true, door::Menu::makeRender(
  260. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  261. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  262. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  263. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
  264. // m.setColorizer(false,
  265. m.setRender(false, door::Menu::makeRender(
  266. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  267. door::ATTR::BOLD),
  268. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  269. door::ATTR::BOLD),
  270. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  271. door::ATTR::BOLD),
  272. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
  273. door::ATTR::BOLD)));
  274. m.addSelection('D', "Deck Colors");
  275. m.addSelection('Q', "Quit");
  276. return m;
  277. }
  278. /*
  279. // all the possible deck colors
  280. vector<std::string> deck_colors = {std::string("All"), std::string("Blue"),
  281. std::string("Cyan"), std::string("Green"),
  282. std::string("Magenta"), std::string("Red")};
  283. */
  284. door::Menu make_deck_menu(void) {
  285. door::Menu m(5, 5, 31);
  286. door::Line mtitle(SPACEACE " Deck Menu");
  287. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  288. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  289. door::ATTR::BOLD);
  290. m.setColor(border_color);
  291. mtitle.setColor(title_color);
  292. mtitle.setPadding(" ", title_color);
  293. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  294. m.setRender(true, makeColorRender(
  295. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  296. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  297. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD)));
  298. m.setRender(false, makeColorRender(
  299. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  300. door::ATTR::BOLD),
  301. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  302. door::ATTR::BOLD),
  303. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  304. door::ATTR::BOLD)));
  305. // build the menu options from the colors. First character = single letter
  306. // option trigger.
  307. for (auto iter = deck_colors.begin(); iter != deck_colors.end(); ++iter) {
  308. char c = (*iter)[0];
  309. m.addSelection(c, (*iter).c_str());
  310. }
  311. /*
  312. m.addSelection('A', "All");
  313. m.addSelection('B', "Blue");
  314. m.addSelection('C', "Cyan");
  315. m.addSelection('G', "Green");
  316. m.addSelection('M', "Magenta");
  317. m.addSelection('R', "Red");
  318. */
  319. return m;
  320. }
  321. // DOES THIS WORK?
  322. bool iequals(const string &a, const string &b) {
  323. unsigned int sz = a.size();
  324. if (b.size() != sz)
  325. return false;
  326. for (unsigned int i = 0; i < sz; ++i)
  327. if (tolower(a[i]) != tolower(b[i]))
  328. return false;
  329. return true;
  330. }
  331. // This does not seem to be working. I keep getting zero.
  332. int opt_from_string(std::string colorCode) {
  333. for (std::size_t pos = 0; pos != deck_colors.size(); ++pos) {
  334. // if (caseInsensitiveStringCompare(colorCode, deck_colors[pos]) == 0) {
  335. if (iequals(colorCode, deck_colors[pos])) {
  336. return pos;
  337. }
  338. }
  339. return 0;
  340. }
  341. int configure(door::Door &door, DBData &db) {
  342. auto menu = make_config_menu();
  343. int r = 0;
  344. bool save_deckcolor = false;
  345. const char *deckcolor = "DeckColor";
  346. std::string newColor;
  347. while (r >= 0) {
  348. if (save_deckcolor) {
  349. door << menu;
  350. db.setSetting(deckcolor, newColor);
  351. save_deckcolor = false;
  352. }
  353. r = menu.choose(door);
  354. if (r > 0) {
  355. door << door::reset << door::cls;
  356. char c = menu.which(r - 1);
  357. if (c == 'D') {
  358. // Ok, deck colors
  359. // get default
  360. std::string currentDefault = db.getSetting(deckcolor, "ALL");
  361. int currentOpt = opt_from_string(currentDefault);
  362. door << door::reset << door::cls;
  363. auto deck = make_deck_menu();
  364. deck.defaultSelection(currentOpt);
  365. int newOpt = deck.choose(door);
  366. door << door::reset << door::cls;
  367. if (newOpt >= 0) {
  368. newOpt--;
  369. newColor = stringFromColorOptions(newOpt);
  370. if (newOpt != currentOpt) {
  371. door.log() << deckcolor << " was " << currentDefault << ", "
  372. << currentOpt << ". Now " << newColor << ", " << newOpt
  373. << std::endl;
  374. save_deckcolor = true;
  375. }
  376. }
  377. }
  378. if (c == 'Q') {
  379. return r;
  380. }
  381. }
  382. }
  383. return r;
  384. }
  385. /*
  386. door::Panel make_score_panel(door::Door &door) {
  387. const int W = 25;
  388. door::Panel p(W);
  389. p.setStyle(door::BorderStyle::NONE);
  390. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  391. door::ATTR::BOLD);
  392. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  393. door::ATTR::BOLD);
  394. door::renderFunction svRender = statusValue(statusColor, valueColor);
  395. // or use renderStatus as defined above.
  396. // We'll stick with these for now.
  397. {
  398. std::string userString = "Name: ";
  399. userString += door.username;
  400. door::Line username(userString, W);
  401. username.setRender(svRender);
  402. p.addLine(std::make_unique<door::Line>(username));
  403. }
  404. {
  405. door::updateFunction scoreUpdate = [](void) -> std::string {
  406. std::string text = "Score: ";
  407. text.append(std::to_string(score));
  408. return text;
  409. };
  410. std::string scoreString = scoreUpdate();
  411. door::Line score(scoreString, W);
  412. score.setRender(svRender);
  413. score.setUpdater(scoreUpdate);
  414. p.addLine(std::make_unique<door::Line>(score));
  415. }
  416. {
  417. door::updateFunction timeUpdate = [&door](void) -> std::string {
  418. std::stringstream ss;
  419. std::string text;
  420. ss << "Time used: " << setw(3) << door.time_used << " / " << setw(3)
  421. << door.time_left;
  422. text = ss.str();
  423. return text;
  424. };
  425. std::string timeString = timeUpdate();
  426. door::Line time(timeString, W);
  427. time.setRender(svRender);
  428. time.setUpdater(timeUpdate);
  429. p.addLine(std::make_unique<door::Line>(time));
  430. }
  431. {
  432. door::updateFunction handUpdate = [](void) -> std::string {
  433. std::string text = "Playing Hand ";
  434. text.append(std::to_string(hand));
  435. text.append(" of ");
  436. text.append(std::to_string(total_hands));
  437. return text;
  438. };
  439. std::string handString = handUpdate();
  440. door::Line hands(handString, W);
  441. hands.setRender(svRender);
  442. hands.setUpdater(handUpdate);
  443. p.addLine(std::make_unique<door::Line>(hands));
  444. }
  445. return p;
  446. }
  447. */
  448. door::Panel make_about(void) {
  449. const int W = 60;
  450. door::Panel about(W);
  451. about.setStyle(door::BorderStyle::DOUBLE_SINGLE);
  452. about.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  453. door::ATTR::BOLD));
  454. about.addLine(std::make_unique<door::Line>("About This Door", W));
  455. about.addLine(std::make_unique<door::Line>(
  456. "---------------------------------", W,
  457. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE, door::ATTR::BOLD)));
  458. /*
  459. 123456789012345678901234567890123456789012345678901234567890-60
  460. This door was written by Bugz.
  461. It is written in c++, only supports Linux, and replaces
  462. opendoors.
  463. It's written in c++, and replaces the outdated opendoors
  464. library.
  465. */
  466. about.addLine(
  467. std::make_unique<door::Line>(SPACEACE " v" SPACEACE_VERSION, W));
  468. std::string copyright = SPACEACE_COPYRIGHT;
  469. if (door::unicode) {
  470. replace(copyright, "(C)", "\u00a9");
  471. }
  472. about.addLine(std::make_unique<door::Line>(copyright, W));
  473. about.addLine(std::make_unique<door::Line>("", W));
  474. about.addLine(
  475. std::make_unique<door::Line>("This door was written by Bugz.", W));
  476. about.addLine(std::make_unique<door::Line>("", W));
  477. about.addLine(std::make_unique<door::Line>(
  478. "It is written in c++, only support Linux, and replaces", W));
  479. about.addLine(std::make_unique<door::Line>("opendoors.", W));
  480. /*
  481. door::updateFunction updater = [](void) -> std::string {
  482. std::string text = "Currently: ";
  483. text.append(return_current_time_and_date());
  484. return text;
  485. };
  486. std::string current = updater();
  487. door::Line active(current, 60);
  488. active.setUpdater(updater);
  489. active.setRender(statusValue(
  490. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  491. door::ATTR::BOLD), door::ANSIColor(door::COLOR::YELLOW,
  492. door::COLOR::BLUE, door::ATTR::BOLD)));
  493. about.addLine(std::make_unique<door::Line>(active));
  494. */
  495. return about;
  496. }
  497. void display_starfield(door::Door &door, std::mt19937 &rng) {
  498. door << door::reset << door::cls;
  499. int mx = door.width;
  500. int my = door.height;
  501. // display starfield
  502. const char *stars[2];
  503. stars[0] = ".";
  504. if (door::unicode) {
  505. stars[1] = "\u2219"; // "\u00b7";
  506. } else {
  507. stars[1] = "\xf9"; // "\xfa";
  508. };
  509. {
  510. // Make uniform random distribution between 1 and MAX screen size X/Y
  511. std::uniform_int_distribution<int> uni_x(1, mx);
  512. std::uniform_int_distribution<int> uni_y(1, my);
  513. door::ANSIColor white(door::COLOR::WHITE);
  514. door::ANSIColor dark(door::COLOR::BLACK, door::ATTR::BRIGHT);
  515. // 10 is too many, 100 is too few. 40 looks ok.
  516. int MAX_STARS = ((mx * my) / 40);
  517. // door.log() << "Generating starmap using " << mx << "," << my << " : "
  518. // << MAX_STARS << " stars." << std::endl;
  519. for (int x = 0; x < MAX_STARS; x++) {
  520. door::Goto star_at(uni_x(rng), uni_y(rng));
  521. door << star_at;
  522. if (x % 5 < 2)
  523. door << dark;
  524. else
  525. door << white;
  526. if (x % 2 == 0)
  527. door << stars[0];
  528. else
  529. door << stars[1];
  530. }
  531. }
  532. }
  533. void display_space_ace(door::Door &door) {
  534. int mx = door.width;
  535. int my = door.height;
  536. // space_ace is 72 chars wide, 6 high
  537. int sa_x = (mx - 72) / 2;
  538. int sa_y = (my - 6) / 2;
  539. // output the SpaceAce logo -- centered!
  540. for (const auto s : space) {
  541. door::Goto sa_at(sa_x, sa_y);
  542. door << sa_at;
  543. if (door::unicode) {
  544. std::string unicode;
  545. door::cp437toUnicode(s, unicode);
  546. door << unicode; // << door::nl;
  547. } else
  548. door << s; // << door::nl;
  549. sa_y++;
  550. }
  551. // pause 5 seconds so they can enjoy our awesome logo -- if they want.
  552. door.sleep_key(5);
  553. }
  554. void display_starfield_space_ace(door::Door &door, std::mt19937 &rng) {
  555. // mx = door.width;
  556. // my = door.height;
  557. display_starfield(door, rng);
  558. display_space_ace(door);
  559. door << door::reset;
  560. }
  561. int main(int argc, char *argv[]) {
  562. door::Door door("space-ace", argc, argv);
  563. // store the door log so we can easily access it.
  564. get_logger = [&door]() -> ofstream & { return door.log(); };
  565. DBData spacedb;
  566. spacedb.setUser(door.username);
  567. if (file_exists("space-ace.yaml")) {
  568. config = YAML::LoadFile("space-ace.yaml");
  569. }
  570. bool update_config = false;
  571. // populate with "good" defaults
  572. if (!config["hands_per_day"]) {
  573. config["hands_per_day"] = 3;
  574. update_config = true;
  575. }
  576. if (!config["date_format"]) {
  577. config["date_format"] = "%B %d"; // Month day or "%b %d,%Y" Mon,d YYYY
  578. update_config = true;
  579. }
  580. if (!config["date_score"]) {
  581. config["date_score"] = "%m/%d/%Y"; // or "%Y/%0m/%0d";
  582. update_config = true;
  583. }
  584. /*
  585. if (config["hands_per_day"]) {
  586. get_logger() << "hands_per_day: " << config["hands_per_day"].as<int>()
  587. << std::endl;
  588. }
  589. */
  590. // save configuration -- something was updated
  591. if (update_config) {
  592. std::ofstream fout("space-ace.yaml");
  593. fout << config << std::endl;
  594. }
  595. // retrieve lastcall
  596. time_t last_call = std::stol(spacedb.getSetting("LastCall", "0"));
  597. // store now as lastcall
  598. time_t now =
  599. std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
  600. spacedb.setSetting("LastCall", std::to_string(now));
  601. // Have they used this door before?
  602. if (last_call != 0) {
  603. door << "Welcome Back!" << door::nl;
  604. auto nowClock = std::chrono::system_clock::from_time_t(now);
  605. auto lastClock = std::chrono::system_clock::from_time_t(last_call);
  606. auto delta = nowClock - lastClock;
  607. // int days = chrono::duration_cast<chrono::days>(delta).count(); //
  608. // c++ 20
  609. int hours = chrono::duration_cast<chrono::hours>(delta).count();
  610. int days = hours / 24;
  611. int minutes = chrono::duration_cast<chrono::minutes>(delta).count();
  612. int secs = chrono::duration_cast<chrono::seconds>(delta).count();
  613. if (days > 1) {
  614. door << "It's been " << days << " days since you last played."
  615. << door::nl;
  616. } else {
  617. if (hours > 1) {
  618. door << "It's been " << hours << " hours since you last played."
  619. << door::nl;
  620. } else {
  621. if (minutes > 1) {
  622. door << "It's been " << minutes << " minutes since you last played."
  623. << door::nl;
  624. } else {
  625. door << "It's been " << secs << " seconds since you last played."
  626. << door::nl;
  627. door << "It's like you never left." << door::nl;
  628. }
  629. }
  630. }
  631. press_a_key(door);
  632. }
  633. /*
  634. // example: saving the door.log() for global use.
  635. std::function<std::ofstream &(void)> get_logger;
  636. get_logger = [&door]() -> ofstream & { return door.log(); };
  637. if (get_logger) {
  638. get_logger() << "MEOW" << std::endl;
  639. get_logger() << "hey! It works!" << std::endl;
  640. }
  641. get_logger = nullptr; // before door destruction
  642. */
  643. // https://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range
  644. std::random_device rd; // only used once to initialise (seed) engine
  645. std::mt19937 rng(rd());
  646. // random-number engine used (Mersenne-Twister in this case)
  647. // std::uniform_int_distribution<int> uni(min, max); // guaranteed
  648. // unbiased
  649. int mx, my; // Max screen width/height
  650. if (door.width == 0) {
  651. // screen detect failed, use sensible defaults
  652. door.width = mx = 80;
  653. door.height = my = 23;
  654. } else {
  655. mx = door.width;
  656. my = door.height;
  657. }
  658. // We assume here that the width and height aren't something crazy like
  659. // 10x15. :P (or 24x923!)
  660. display_starfield_space_ace(door, rng);
  661. // for testing inactivity timeout
  662. // door.inactivity = 10;
  663. door::Panel timeout = make_timeout(mx, my);
  664. door::Menu m = make_main_menu();
  665. door::Panel about = make_about(); // 8 lines
  666. // center the about box
  667. about.set((mx - 60) / 2, (my - 9) / 2);
  668. int r = 0;
  669. while ((r >= 0) and (r != 6)) {
  670. // starfield + menu ?
  671. display_starfield(door, rng);
  672. r = m.choose(door);
  673. // need to reset the colors. (whoops!)
  674. door << door::reset << door::cls; // door::nl;
  675. // OK! The screen is blank at this point!
  676. switch (r) {
  677. case 1: // play game
  678. {
  679. PlayCards pc(door, spacedb);
  680. r = pc.play_cards();
  681. // r = play_cards(door, spacedb, rng);
  682. }; break;
  683. case 2: // view scores
  684. {
  685. door << door::cls;
  686. auto all_scores = spacedb.getScores();
  687. for (auto it : all_scores) {
  688. time_t on_this_date = it.first;
  689. std::string nice_date = convertDateToDateScoreFormat(on_this_date);
  690. door << " *** " << nice_date << " ***" << door::nl;
  691. scores_data merge;
  692. for (auto sd : it.second) {
  693. if (merge.user.empty())
  694. merge = sd;
  695. else {
  696. if (merge.user == sd.user) {
  697. // merge in the information
  698. merge.hand = sd.hand;
  699. merge.won += sd.won;
  700. merge.score += sd.score;
  701. } else {
  702. // Ok, output the merged data and reset
  703. door << setw(15) << merge.user << " " << merge.hand << " "
  704. << merge.won << " " << sd.score << door::nl;
  705. merge = sd;
  706. }
  707. }
  708. }
  709. door << setw(15) << merge.user << " " << merge.hand << " " << merge.won
  710. << " " << merge.score << door::nl;
  711. }
  712. door << "====================" << door::nl;
  713. }
  714. r = press_a_key(door);
  715. break;
  716. case 3: // configure
  717. r = configure(door, spacedb);
  718. // r = press_a_key(door);
  719. break;
  720. case 4: // help
  721. door << "Help! Need some help here..." << door::nl;
  722. r = press_a_key(door);
  723. break;
  724. case 5: // about
  725. display_starfield(door, rng);
  726. door << about << door::nl;
  727. r = press_a_key(door);
  728. break;
  729. case 6: // quit
  730. break;
  731. }
  732. }
  733. if (r < 0) {
  734. // TIMEOUT:
  735. if (r == -1) {
  736. door.log() << "TIMEOUT" << std::endl;
  737. door << timeout << door::reset << door::nl << door::nl;
  738. } else {
  739. if (r == -3) {
  740. door.log() << "OUTTA TIME" << std::endl;
  741. door::Panel notime = make_notime(mx, my);
  742. door << notime << door::reset << door::nl;
  743. }
  744. }
  745. return 0;
  746. }
  747. door << door::nl;
  748. /*
  749. // magic time!
  750. door << door::reset << door::nl << "Press another key...";
  751. int x;
  752. for (x = 0; x < 60; ++x) {
  753. r = door.sleep_key(1);
  754. if (r == -1) {
  755. // ok! Expected timeout!
  756. // PROBLEM: regular "local" terminal loses current attributes
  757. // when cursor is save / restored.
  758. door << door::SaveCursor;
  759. if (about.update(door)) {
  760. // ok I need to "fix" the cursor position.
  761. // it has moved.
  762. }
  763. door << door::RestoreCursor << door::reset;
  764. } else {
  765. if (r < 0)
  766. goto TIMEOUT;
  767. if (r >= 0)
  768. break;
  769. }
  770. }
  771. if (x == 60)
  772. goto TIMEOUT;
  773. */
  774. #ifdef NNY
  775. // configured by the player.
  776. door::ANSIColor deck_color;
  777. // RED, BLUE, GREEN, MAGENTA, CYAN
  778. std::uniform_int_distribution<int> rand_color(0, 4);
  779. switch (rand_color(rng)) {
  780. case 0:
  781. deck_color = door::ANSIColor(door::COLOR::RED);
  782. break;
  783. case 1:
  784. deck_color = door::ANSIColor(door::COLOR::BLUE);
  785. break;
  786. case 2:
  787. deck_color = door::ANSIColor(door::COLOR::GREEN);
  788. break;
  789. case 3:
  790. deck_color = door::ANSIColor(door::COLOR::MAGENTA);
  791. break;
  792. case 4:
  793. deck_color = door::ANSIColor(door::COLOR::CYAN);
  794. break;
  795. default:
  796. deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
  797. break;
  798. }
  799. int height = 3;
  800. Deck d(deck_color, height);
  801. door::Panel *c;
  802. door << door::reset << door::cls;
  803. // This displays the cards in the upper left corner.
  804. // We want them center, and down some.
  805. int space = 3;
  806. // int cards_dealt_width = 59; int cards_dealt_height = 9;
  807. int game_width;
  808. {
  809. int cx, cy, level;
  810. cardPosLevel(27, space, height, cx, cy, level);
  811. game_width = cx + 5; // card width
  812. }
  813. int off_x = (mx - game_width) / 2;
  814. int off_y = (my - 9) / 2;
  815. // The idea is to see the cards with <<Something Unique to the card
  816. // game>>, Year, Month, Day, and game (like 1 of 3). This will make the
  817. // games the same/fair for everyone.
  818. std::seed_seq s1{2021, 2, 27, 1};
  819. cards deck1 = shuffleCards(s1, 1);
  820. cards state = makeCardStates();
  821. // I tried setting the cursor before the delay and before displaying the
  822. // card. It is very hard to see / just about useless. Not worth the
  823. // effort.
  824. for (int x = 0; x < 28; x++) {
  825. int cx, cy, level;
  826. cardPosLevel(x, space, height, cx, cy, level);
  827. // This is hardly visible.
  828. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  829. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  830. c = d.back(level);
  831. c->set(cx + off_x, cy + off_y);
  832. door << *c;
  833. }
  834. /*
  835. std::this_thread::sleep_for(
  836. std::chrono::seconds(1)); // 3 secs seemed too long!
  837. */
  838. for (int x = 18; x < 28; x++) {
  839. int cx, cy, level;
  840. // usleep(1000 * 20);
  841. state.at(x) = 1;
  842. cardPosLevel(x, space, height, cx, cy, level);
  843. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  844. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  845. c = d.card(deck1.at(x));
  846. c->set(cx + off_x, cy + off_y);
  847. door << *c;
  848. }
  849. door << door::reset;
  850. door << door::nl << door::nl;
  851. r = door.sleep_key(door.inactivity);
  852. if (r < 0)
  853. goto TIMEOUT;
  854. #endif
  855. /*
  856. door::Panel *p = d.back(2);
  857. p->set(10, 10);
  858. door << *p;
  859. door::Panel *d8 = d.card(8);
  860. d8->set(20, 8);
  861. door << *d8;
  862. r = door.sleep_key(door.inactivity);
  863. if (r < 0)
  864. goto TIMEOUT;
  865. */
  866. // door << door::reset << door::cls;
  867. display_starfield(door, rng);
  868. door << m << door::reset << door::nl;
  869. // Normal DOOR exit goes here...
  870. door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
  871. get_logger = nullptr;
  872. return 0;
  873. }