main.cpp 29 KB

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