main.cpp 32 KB

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