main.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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 = db.getSetting(key, std::string("ALL"));
  459. int currentOpt = opt_from_string(currentDefault);
  460. door << door::reset << door::cls;
  461. auto deck = make_deck_menu();
  462. deck.defaultSelection(currentOpt);
  463. int newOpt = deck.choose(door);
  464. door << door::reset << door::cls;
  465. if (newOpt >= 0) {
  466. newOpt--;
  467. std::string newColor = from_color_option(newOpt);
  468. if (newOpt != currentOpt) {
  469. door.log() << key << " was " << currentDefault << ", " << currentOpt
  470. << ". Now " << newColor << ", " << newOpt << std::endl;
  471. db.setSetting(key, newColor);
  472. }
  473. }
  474. }
  475. if (c == 'Q') {
  476. return r;
  477. }
  478. }
  479. }
  480. return r;
  481. }
  482. int play_cards(door::Door &door, DBData &db, std::mt19937 &rng) {
  483. int mx = door.width;
  484. int my = door.height;
  485. // cards color --
  486. // configured by the player.
  487. door::ANSIColor deck_color;
  488. std::string key("DeckColor");
  489. std::string currentDefault = db.getSetting(key, std::string("ALL"));
  490. door.log() << key << " shows as " << currentDefault << std::endl;
  491. deck_color = from_string(currentDefault);
  492. /*
  493. // RED, BLUE, GREEN, MAGENTA, CYAN
  494. std::uniform_int_distribution<int> rand_color(0, 4);
  495. switch (rand_color(rng)) {
  496. case 0:
  497. deck_color = door::ANSIColor(door::COLOR::RED);
  498. break;
  499. case 1:
  500. deck_color = door::ANSIColor(door::COLOR::BLUE);
  501. break;
  502. case 2:
  503. deck_color = door::ANSIColor(door::COLOR::GREEN);
  504. break;
  505. case 3:
  506. deck_color = door::ANSIColor(door::COLOR::MAGENTA);
  507. break;
  508. case 4:
  509. deck_color = door::ANSIColor(door::COLOR::CYAN);
  510. break;
  511. default:
  512. deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
  513. break;
  514. }
  515. */
  516. int height = 3;
  517. Deck d(deck_color, height);
  518. door::Panel *c;
  519. door << door::reset << door::cls;
  520. // This displays the cards in the upper left corner.
  521. // We want them center, and down some.
  522. const int space = 3;
  523. // int cards_dealt_width = 59; int cards_dealt_height = 9;
  524. int game_width;
  525. int game_height = 13; // 9;
  526. {
  527. int cx, cy, level;
  528. cardgo(27, space, height, cx, cy, level);
  529. game_width = cx + 5; // card width
  530. }
  531. int off_x = (mx - game_width) / 2;
  532. int off_y = (my - game_height) / 2;
  533. // The idea is to see the cards with <<Something Unique to the card game>>,
  534. // Year, Month, Day, and game (like 1 of 3).
  535. // This will make the games the same/fair for everyone.
  536. std::string tripeaks(" " SPACEACE " - Tri-Peaks Solitaire v" SPACEACE_VERSION
  537. " ");
  538. int tp_off_x = (mx - tripeaks.size()) / 2;
  539. door::Panel spaceAceTriPeaks(tp_off_x, off_y, tripeaks.size());
  540. spaceAceTriPeaks.setStyle(door::BorderStyle::SINGLE);
  541. spaceAceTriPeaks.setColor(
  542. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLACK));
  543. spaceAceTriPeaks.addLine(
  544. std::make_unique<door::Line>(tripeaks, tripeaks.size()));
  545. door << spaceAceTriPeaks;
  546. off_y += 3;
  547. std::seed_seq s1{2021, 2, 27, 1};
  548. cards deck1 = card_shuffle(s1, 1);
  549. cards state = card_states();
  550. // I tried setting the cursor before the delay and before displaying the
  551. // card. It is very hard to see / just about useless. Not worth the effort.
  552. for (int x = 0; x < 28; x++) {
  553. int cx, cy, level;
  554. cardgo(x, space, height, cx, cy, level);
  555. // This is hardly visible.
  556. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  557. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  558. c = d.back(level);
  559. c->set(cx + off_x, cy + off_y);
  560. door << *c;
  561. }
  562. /*
  563. std::this_thread::sleep_for(
  564. std::chrono::seconds(1)); // 3 secs seemed too long!
  565. */
  566. for (int x = 18; x < 28; x++) {
  567. int cx, cy, level;
  568. // usleep(1000 * 20);
  569. state.at(x) = 1;
  570. cardgo(x, space, height, cx, cy, level);
  571. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  572. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  573. c = d.card(deck1.at(x));
  574. c->set(cx + off_x, cy + off_y);
  575. door << *c;
  576. }
  577. door << door::reset;
  578. door << door::nl << door::nl;
  579. int r = door.sleep_key(door.inactivity);
  580. return r;
  581. }
  582. door::Panel make_about(void) {
  583. door::Panel about(60);
  584. about.setStyle(door::BorderStyle::DOUBLE_SINGLE);
  585. about.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  586. door::ATTR::BOLD));
  587. about.addLine(std::make_unique<door::Line>("About This Door", 60));
  588. about.addLine(std::make_unique<door::Line>(
  589. "---------------------------------", 60,
  590. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE, door::ATTR::BOLD)));
  591. /*
  592. 123456789012345678901234567890123456789012345678901234567890
  593. This door was written by Bugz.
  594. It is written in c++, only supports Linux, and replaces
  595. opendoors.
  596. It's written in c++, and replaces the outdated opendoors
  597. library.
  598. */
  599. about.addLine(
  600. std::make_unique<door::Line>(SPACEACE " v" SPACEACE_VERSION, 60));
  601. std::string copyright = SPACEACE_COPYRIGHT;
  602. if (door::unicode) {
  603. /*
  604. std::string textcp = "(C)";
  605. std::string utf8cp = "\u00a9";
  606. replace(copyright, textcp, utf8cp);*/
  607. replace(copyright, "(C)", "\u00a9");
  608. }
  609. about.addLine(std::make_unique<door::Line>(copyright, 60));
  610. about.addLine(std::make_unique<door::Line>("", 60));
  611. about.addLine(
  612. std::make_unique<door::Line>("This door was written by Bugz.", 60));
  613. about.addLine(std::make_unique<door::Line>("", 60));
  614. about.addLine(std::make_unique<door::Line>(
  615. "It is written in c++, only support Linux, and replaces", 60));
  616. about.addLine(std::make_unique<door::Line>("opendoors.", 60));
  617. /*
  618. door::updateFunction updater = [](void) -> std::string {
  619. std::string text = "Currently: ";
  620. text.append(return_current_time_and_date());
  621. return text;
  622. };
  623. std::string current = updater();
  624. door::Line active(current, 60);
  625. active.setUpdater(updater);
  626. active.setRender(renderStatusValue(
  627. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  628. door::ATTR::BOLD), door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  629. door::ATTR::BOLD)));
  630. about.addLine(std::make_unique<door::Line>(active));
  631. */
  632. /*
  633. about.addLine(std::make_unique<door::Line>(
  634. "Status: blue", 60,
  635. statusValue(door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD),
  636. door::ANSIColor(door::COLOR::MAGENTA,
  637. door::ATTR::BLINK)))); about.addLine(std::make_unique<door::Line>("Name:
  638. BUGZ", 60, rStatus)); about.addLine(std::make_unique<door::Line>( "Size:
  639. 10240", 60, statusValue(door::ANSIColor(door::COLOR::GREEN,
  640. door::COLOR::BLUE, door::ATTR::BOLD), door::ANSIColor(door::COLOR::YELLOW,
  641. door::COLOR::BLUE, door::ATTR::BOLD, door::ATTR::BLINK))));
  642. about.addLine(std::make_unique<door::Line>("Bugz is here.", 60, rStatus));
  643. */
  644. return about;
  645. }
  646. void display_starfield(door::Door &door, std::mt19937 &rng) {
  647. door << door::reset << door::cls;
  648. int mx = door.width;
  649. int my = door.height;
  650. // display starfield
  651. const char *stars[2];
  652. stars[0] = ".";
  653. if (door::unicode) {
  654. stars[1] = "\u2219"; // "\u00b7";
  655. } else {
  656. stars[1] = "\xf9"; // "\xfa";
  657. };
  658. {
  659. // Make uniform random distribution between 1 and MAX screen size X/Y
  660. std::uniform_int_distribution<int> uni_x(1, mx);
  661. std::uniform_int_distribution<int> uni_y(1, my);
  662. door::ANSIColor white(door::COLOR::WHITE);
  663. door::ANSIColor dark(door::COLOR::BLACK, door::ATTR::BRIGHT);
  664. // 10 is too many, 100 is too few. 40 looks ok.
  665. int MAX_STARS = ((mx * my) / 40);
  666. // door.log() << "Generating starmap using " << mx << "," << my << " : "
  667. // << MAX_STARS << " stars." << std::endl;
  668. for (int x = 0; x < MAX_STARS; x++) {
  669. door::Goto star_at(uni_x(rng), uni_y(rng));
  670. door << star_at;
  671. if (x % 5 < 2)
  672. door << dark;
  673. else
  674. door << white;
  675. if (x % 2 == 0)
  676. door << stars[0];
  677. else
  678. door << stars[1];
  679. }
  680. }
  681. }
  682. void display_space_ace(door::Door &door) {
  683. int mx = door.width;
  684. int my = door.height;
  685. // space_ace is 72 chars wide, 6 high
  686. int sa_x = (mx - 72) / 2;
  687. int sa_y = (my - 6) / 2;
  688. // output the SpaceAce logo -- centered!
  689. for (const auto s : space) {
  690. door::Goto sa_at(sa_x, sa_y);
  691. door << sa_at;
  692. if (door::unicode) {
  693. std::string unicode;
  694. door::cp437toUnicode(s, unicode);
  695. door << unicode; // << door::nl;
  696. } else
  697. door << s; // << door::nl;
  698. sa_y++;
  699. }
  700. // pause 5 seconds so they can enjoy our awesome logo -- if they want.
  701. door.sleep_key(5);
  702. }
  703. void display_starfield_space_ace(door::Door &door, std::mt19937 &rng) {
  704. // mx = door.width;
  705. // my = door.height;
  706. display_starfield(door, rng);
  707. display_space_ace(door);
  708. door << door::reset;
  709. }
  710. int main(int argc, char *argv[]) {
  711. door::Door door("space-ace", argc, argv);
  712. // door << door::reset << door::cls << door::nl;
  713. get_logger = [&door]() -> ofstream & { return door.log(); };
  714. DBData spacedb;
  715. spacedb.setUser(door.username);
  716. time_t last_call = std::stol(spacedb.getSetting("LastCall", "0"));
  717. time_t now =
  718. std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
  719. spacedb.setSetting("LastCall", std::to_string(now));
  720. if (last_call != 0) {
  721. door << "Welcome Back!" << door::nl;
  722. auto nowClock = std::chrono::system_clock::from_time_t(now);
  723. auto lastClock = std::chrono::system_clock::from_time_t(last_call);
  724. auto delta = nowClock - lastClock;
  725. // int days = chrono::duration_cast<chrono::days>(delta).count(); // c++ 20
  726. int hours = chrono::duration_cast<chrono::hours>(delta).count();
  727. int days = hours / 24;
  728. int minutes = chrono::duration_cast<chrono::minutes>(delta).count();
  729. if (days > 1) {
  730. door << "It's been " << days << " days since you last played."
  731. << door::nl;
  732. } else {
  733. if (hours > 1) {
  734. door << "It's been " << hours << " hours since you last played."
  735. << door::nl;
  736. } else {
  737. door << "It's been " << minutes << " minutes since you last played."
  738. << door::nl;
  739. }
  740. }
  741. press_a_key(door);
  742. }
  743. /*
  744. std::function<std::ofstream &(void)> get_logger;
  745. get_logger = [&door]() -> ofstream & { return door.log(); };
  746. if (get_logger) {
  747. get_logger() << "MEOW" << std::endl;
  748. get_logger() << "hey! It works!" << std::endl;
  749. }
  750. */
  751. // spacedb.init();
  752. /*
  753. // Example: How to read/set values in spacedb settings.
  754. std::string setting = "last_play";
  755. std::string user = door.username;
  756. std::string value;
  757. std::string blank = "<blank>";
  758. value = spacedb.getSetting(user, setting, blank);
  759. door << door::reset << "last_play: " << value << door::nl;
  760. std::this_thread::sleep_for(std::chrono::seconds(2));
  761. value = return_current_time_and_date();
  762. spacedb.setSetting(user, setting, value);
  763. */
  764. // https://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range
  765. std::random_device rd; // only used once to initialise (seed) engine
  766. std::mt19937 rng(rd());
  767. // random-number engine used (Mersenne-Twister in this case)
  768. // std::uniform_int_distribution<int> uni(min, max); // guaranteed unbiased
  769. int mx, my; // Max screen width/height
  770. if (door.width == 0) {
  771. // screen detect failed, use sensible defaults
  772. door.width = mx = 80;
  773. door.height = my = 23;
  774. } else {
  775. mx = door.width;
  776. my = door.height;
  777. }
  778. // We assume here that the width and height are something crazy like 10x15.
  779. // :P
  780. display_starfield_space_ace(door, rng);
  781. // for testing inactivity timeout
  782. // door.inactivity = 10;
  783. door::Panel timeout = make_timeout(mx, my);
  784. door::Menu m = make_main_menu();
  785. door::Panel about = make_about(); // 8 lines
  786. // center the about box
  787. about.set((mx - 60) / 2, (my - 9) / 2);
  788. int r = 0;
  789. while ((r >= 0) and (r != 6)) {
  790. // starfield + menu ?
  791. display_starfield(door, rng);
  792. r = m.choose(door);
  793. // need to reset the colors. (whoops!)
  794. door << door::reset << door::cls; // door::nl;
  795. // OK! The screen is blank at this point!
  796. switch (r) {
  797. case 1: // play game
  798. r = play_cards(door, spacedb, rng);
  799. break;
  800. case 2: // view scores
  801. door << "Show scores goes here!" << door::nl;
  802. r = press_a_key(door);
  803. break;
  804. case 3: // configure
  805. r = configure(door, spacedb);
  806. // r = press_a_key(door);
  807. break;
  808. case 4: // help
  809. door << "Help! Need some help here..." << door::nl;
  810. r = press_a_key(door);
  811. break;
  812. case 5: // about
  813. display_starfield(door, rng);
  814. door << about << door::nl;
  815. r = press_a_key(door);
  816. break;
  817. case 6: // quit
  818. break;
  819. }
  820. }
  821. if (r < 0) {
  822. TIMEOUT:
  823. if (r == -1) {
  824. door.log() << "TIMEOUT" << std::endl;
  825. door << timeout << door::reset << door::nl << door::nl;
  826. } else {
  827. if (r == -3) {
  828. door.log() << "OUTTA TIME" << std::endl;
  829. door::Panel notime = make_notime(mx, my);
  830. door << notime << door::reset << door::nl;
  831. }
  832. }
  833. return 0;
  834. }
  835. door << door::nl;
  836. /*
  837. // magic time!
  838. door << door::reset << door::nl << "Press another key...";
  839. int x;
  840. for (x = 0; x < 60; ++x) {
  841. r = door.sleep_key(1);
  842. if (r == -1) {
  843. // ok! Expected timeout!
  844. // PROBLEM: regular "local" terminal loses current attributes
  845. // when cursor is save / restored.
  846. door << door::SaveCursor;
  847. if (about.update(door)) {
  848. // ok I need to "fix" the cursor position.
  849. // it has moved.
  850. }
  851. door << door::RestoreCursor << door::reset;
  852. } else {
  853. if (r < 0)
  854. goto TIMEOUT;
  855. if (r >= 0)
  856. break;
  857. }
  858. }
  859. if (x == 60)
  860. goto TIMEOUT;
  861. */
  862. door << door::nl;
  863. #ifdef NNY
  864. // configured by the player.
  865. door::ANSIColor deck_color;
  866. // RED, BLUE, GREEN, MAGENTA, CYAN
  867. std::uniform_int_distribution<int> rand_color(0, 4);
  868. switch (rand_color(rng)) {
  869. case 0:
  870. deck_color = door::ANSIColor(door::COLOR::RED);
  871. break;
  872. case 1:
  873. deck_color = door::ANSIColor(door::COLOR::BLUE);
  874. break;
  875. case 2:
  876. deck_color = door::ANSIColor(door::COLOR::GREEN);
  877. break;
  878. case 3:
  879. deck_color = door::ANSIColor(door::COLOR::MAGENTA);
  880. break;
  881. case 4:
  882. deck_color = door::ANSIColor(door::COLOR::CYAN);
  883. break;
  884. default:
  885. deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
  886. break;
  887. }
  888. int height = 3;
  889. Deck d(deck_color, height);
  890. door::Panel *c;
  891. door << door::reset << door::cls;
  892. // This displays the cards in the upper left corner.
  893. // We want them center, and down some.
  894. int space = 3;
  895. // int cards_dealt_width = 59; int cards_dealt_height = 9;
  896. int game_width;
  897. {
  898. int cx, cy, level;
  899. cardgo(27, space, height, cx, cy, level);
  900. game_width = cx + 5; // card width
  901. }
  902. int off_x = (mx - game_width) / 2;
  903. int off_y = (my - 9) / 2;
  904. // The idea is to see the cards with <<Something Unique to the card game>>,
  905. // Year, Month, Day, and game (like 1 of 3).
  906. // This will make the games the same/fair for everyone.
  907. std::seed_seq s1{2021, 2, 27, 1};
  908. cards deck1 = card_shuffle(s1, 1);
  909. cards state = card_states();
  910. // I tried setting the cursor before the delay and before displaying the
  911. // card. It is very hard to see / just about useless. Not worth the effort.
  912. for (int x = 0; x < 28; x++) {
  913. int cx, cy, level;
  914. cardgo(x, space, height, cx, cy, level);
  915. // This is hardly visible.
  916. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  917. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  918. c = d.back(level);
  919. c->set(cx + off_x, cy + off_y);
  920. door << *c;
  921. }
  922. /*
  923. std::this_thread::sleep_for(
  924. std::chrono::seconds(1)); // 3 secs seemed too long!
  925. */
  926. for (int x = 18; x < 28; x++) {
  927. int cx, cy, level;
  928. // usleep(1000 * 20);
  929. state.at(x) = 1;
  930. cardgo(x, space, height, cx, cy, level);
  931. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  932. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  933. c = d.card(deck1.at(x));
  934. c->set(cx + off_x, cy + off_y);
  935. door << *c;
  936. }
  937. door << door::reset;
  938. door << door::nl << door::nl;
  939. r = door.sleep_key(door.inactivity);
  940. if (r < 0)
  941. goto TIMEOUT;
  942. #endif
  943. /*
  944. door::Panel *p = d.back(2);
  945. p->set(10, 10);
  946. door << *p;
  947. door::Panel *d8 = d.card(8);
  948. d8->set(20, 8);
  949. door << *d8;
  950. r = door.sleep_key(door.inactivity);
  951. if (r < 0)
  952. goto TIMEOUT;
  953. */
  954. // door << door::reset << door::cls;
  955. display_starfield(door, rng);
  956. door << m << door::reset << door::nl;
  957. // Normal DOOR exit goes here...
  958. door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
  959. get_logger = nullptr;
  960. return 0;
  961. }