main.cpp 29 KB

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