main.cpp 29 KB

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