main.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. /*
  12. Cards:
  13. 4 layers deep.
  14. https://en.wikipedia.org/wiki/Code_page_437
  15. using: \xb0, 0xb1, 0xb2, 0xdb
  16. OR: \u2591, \u2592, \u2593, \u2588
  17. Like so:
  18. ##### #####
  19. ##### #####
  20. ##### #####
  21. Cards: (Black on White, or Red on White)
  22. 8D### TH###
  23. ##D## ##H##
  24. ###D8 ###HT
  25. D, H = Red, Clubs, Spades = Black.
  26. ^ Where D = Diamonds, H = Hearts
  27. ♥, ♦, ♣, ♠
  28. \x03, \x04, \x05, \x06
  29. \u2665, \u2666, \u2663, \u2660
  30. Card layout. Actual cards are 3 lines thick.
  31. ░░░░░ ░░░░░ ░░░░░
  32. ░░░░░ ░░░░░ ░░░░░
  33. ▒▒▒▒▒░▒▒▒▒▒ #####░##### #####░#####
  34. ▒▒▒▒▒ ▒▒▒▒▒ ##### ##### ##### #####
  35. ▓▓▓▓▓▒▓▓▓▓▓▒▓▓▓▓▓ #####=#####=##### #####=#####=#####
  36. ▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ##### ##### ##### ##### ##### #####
  37. █████▓█████▓█████▓#####=#####=#####=#####=#####=#####=#####
  38. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  39. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  40. #####
  41. Player Information ##### Time in: xx Time out: xx
  42. Name: ##### Playing Day: November 3rd
  43. Hand Score : Current Streak: N
  44. Todays Score : XX Cards Remaining Longest Streak: NN
  45. Monthly Score: Playing Hand X of X Most Won: xxx Lost: xxx
  46. [4] Lf [6] Rt [Space] Play Card [Enter] Draw [D]one [H]elp [R]edraw
  47. ►, ◄, ▲, ▼
  48. \x10, \x11, \x1e, \x1f
  49. \u25ba, \u25c4, \u25b2, \u25bc
  50. The Name is <- 6 to the left.
  51. # is back of card. = is upper card showing between.
  52. There's no fancy anything here. Cards overlap the last
  53. line of the previous line/card.
  54. */
  55. // The idea is that this would be defined elsewhere (maybe)
  56. int user_score = 0;
  57. // NOTE: When run as a door, we always detect CP437 (because that's what Enigma
  58. // is defaulting to)
  59. void adjust_score(int by) { user_score += by; }
  60. door::Panel make_timeout(int mx, int my) {
  61. door::ANSIColor yellowred =
  62. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
  63. std::string line_text("Sorry, you've been inactive for too long.");
  64. int msgWidth = line_text.length() + (2 * 3); // + padding * 2
  65. door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
  66. // place.setTitle(std::make_unique<door::Line>(title), 1);
  67. timeout.setStyle(door::BorderStyle::DOUBLE);
  68. timeout.setColor(yellowred);
  69. door::Line base(line_text);
  70. base.setColor(yellowred);
  71. std::string pad1(3, ' ');
  72. /*
  73. std::string pad1(3, '\xb0');
  74. if (door::unicode) {
  75. std::string unicode;
  76. door::cp437toUnicode(pad1.c_str(), unicode);
  77. pad1 = unicode;
  78. }
  79. */
  80. base.setPadding(pad1, yellowred);
  81. // base.setColor(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLACK));
  82. std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
  83. timeout.addLine(std::make_unique<door::Line>(base));
  84. return timeout;
  85. }
  86. door::Panel make_notime(int mx, int my) {
  87. door::ANSIColor yellowred =
  88. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
  89. std::string line_text("Sorry, you've used up all your time for today.");
  90. int msgWidth = line_text.length() + (2 * 3); // + padding * 2
  91. door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
  92. // place.setTitle(std::make_unique<door::Line>(title), 1);
  93. timeout.setStyle(door::BorderStyle::DOUBLE);
  94. timeout.setColor(yellowred);
  95. door::Line base(line_text);
  96. base.setColor(yellowred);
  97. std::string pad1(3, ' ');
  98. /*
  99. std::string pad1(3, '\xb0');
  100. if (door::unicode) {
  101. std::string unicode;
  102. door::cp437toUnicode(pad1.c_str(), unicode);
  103. pad1 = unicode;
  104. }
  105. */
  106. base.setPadding(pad1, yellowred);
  107. // base.setColor(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLACK));
  108. std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
  109. timeout.addLine(std::make_unique<door::Line>(base));
  110. return timeout;
  111. }
  112. door::Menu make_main_menu(void) {
  113. door::Menu m(5, 5, 25);
  114. door::Line mtitle("Space-Ace Main Menu");
  115. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  116. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  117. door::ATTR::BOLD);
  118. m.setColor(border_color);
  119. mtitle.setColor(title_color);
  120. mtitle.setPadding(" ", title_color);
  121. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  122. // m.setColorizer(true,
  123. m.setRender(true, door::Menu::makeRender(
  124. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  125. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  126. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  127. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
  128. // m.setColorizer(false,
  129. m.setRender(false, door::Menu::makeRender(
  130. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  131. door::ATTR::BOLD),
  132. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  133. door::ATTR::BOLD),
  134. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  135. door::ATTR::BOLD),
  136. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
  137. door::ATTR::BOLD)));
  138. m.addSelection('1', "Play Cards");
  139. m.addSelection('2', "View Scores");
  140. m.addSelection('3', "Configure");
  141. m.addSelection('H', "Help");
  142. m.addSelection('A', "About this game");
  143. m.addSelection('Q', "Quit");
  144. return m;
  145. }
  146. door::renderFunction statusValue(door::ANSIColor status,
  147. door::ANSIColor value) {
  148. door::renderFunction rf = [status,
  149. value](const std::string &txt) -> door::Render {
  150. door::Render r(txt);
  151. door::ColorOutput co;
  152. co.pos = 0;
  153. co.len = 0;
  154. co.c = status;
  155. size_t pos = txt.find(':');
  156. if (pos == std::string::npos) {
  157. // failed to find - use entire string as status color.
  158. co.len = txt.length();
  159. r.outputs.push_back(co);
  160. } else {
  161. pos++; // Have : in status color
  162. co.len = pos;
  163. r.outputs.push_back(co);
  164. co.reset();
  165. co.pos = pos;
  166. co.c = value;
  167. co.len = txt.length() - pos;
  168. r.outputs.push_back(co);
  169. }
  170. return r;
  171. };
  172. return rf;
  173. }
  174. door::renderFunction rStatus = [](const std::string &txt) -> door::Render {
  175. door::Render r(txt);
  176. door::ColorOutput co;
  177. // default colors STATUS: value
  178. door::ANSIColor status(door::COLOR::BLUE, door::ATTR::BOLD);
  179. door::ANSIColor value(door::COLOR::YELLOW, door::ATTR::BOLD);
  180. co.pos = 0;
  181. co.len = 0;
  182. co.c = status;
  183. size_t pos = txt.find(':');
  184. if (pos == std::string::npos) {
  185. // failed to find - use entire string as status color.
  186. co.len = txt.length();
  187. r.outputs.push_back(co);
  188. } else {
  189. pos++; // Have : in status color
  190. co.len = pos;
  191. r.outputs.push_back(co);
  192. co.reset();
  193. co.pos = pos;
  194. co.c = value;
  195. co.len = txt.length() - pos;
  196. r.outputs.push_back(co);
  197. }
  198. return r;
  199. };
  200. std::string return_current_time_and_date() {
  201. auto now = std::chrono::system_clock::now();
  202. auto in_time_t = std::chrono::system_clock::to_time_t(now);
  203. std::stringstream ss;
  204. // ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
  205. ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %r");
  206. return ss.str();
  207. }
  208. int press_a_key(door::Door &door) {
  209. door << door::reset << "Press a key to continue...";
  210. int r = door.sleep_key(door.inactivity);
  211. door << door::nl;
  212. return r;
  213. }
  214. int play_cards(door::Door &door, std::mt19937 &rng) {
  215. int mx = door.width;
  216. int my = door.height;
  217. // configured by the player.
  218. door::ANSIColor deck_color;
  219. // RED, BLUE, GREEN, MAGENTA, CYAN
  220. std::uniform_int_distribution<int> rand_color(0, 4);
  221. switch (rand_color(rng)) {
  222. case 0:
  223. deck_color = door::ANSIColor(door::COLOR::RED);
  224. break;
  225. case 1:
  226. deck_color = door::ANSIColor(door::COLOR::BLUE);
  227. break;
  228. case 2:
  229. deck_color = door::ANSIColor(door::COLOR::GREEN);
  230. break;
  231. case 3:
  232. deck_color = door::ANSIColor(door::COLOR::MAGENTA);
  233. break;
  234. case 4:
  235. deck_color = door::ANSIColor(door::COLOR::CYAN);
  236. break;
  237. default:
  238. deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
  239. break;
  240. }
  241. int height = 3;
  242. Deck d(deck_color, height);
  243. door::Panel *c;
  244. door << door::reset << door::cls;
  245. // This displays the cards in the upper left corner.
  246. // We want them center, and down some.
  247. int space = 3;
  248. // int cards_dealt_width = 59; int cards_dealt_height = 9;
  249. int game_width;
  250. {
  251. int cx, cy, level;
  252. cardgo(27, space, height, cx, cy, level);
  253. game_width = cx + 5; // card width
  254. }
  255. int off_x = (mx - game_width) / 2;
  256. int off_y = (my - 9) / 2;
  257. // The idea is to see the cards with <<Something Unique to the card game>>,
  258. // Year, Month, Day, and game (like 1 of 3).
  259. // This will make the games the same/fair for everyone.
  260. std::seed_seq s1{2021, 2, 27, 1};
  261. cards deck1 = card_shuffle(s1, 1);
  262. cards state = card_states();
  263. // I tried setting the cursor before the delay and before displaying the card.
  264. // It is very hard to see / just about useless. Not worth the effort.
  265. for (int x = 0; x < 28; x++) {
  266. int cx, cy, level;
  267. cardgo(x, space, height, cx, cy, level);
  268. // This is hardly visible.
  269. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  270. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  271. c = d.back(level);
  272. c->set(cx + off_x, cy + off_y);
  273. door << *c;
  274. }
  275. /*
  276. std::this_thread::sleep_for(
  277. std::chrono::seconds(1)); // 3 secs seemed too long!
  278. */
  279. for (int x = 18; x < 28; x++) {
  280. int cx, cy, level;
  281. // usleep(1000 * 20);
  282. state.at(x) = 1;
  283. cardgo(x, space, height, cx, cy, level);
  284. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  285. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  286. c = d.card(deck1.at(x));
  287. c->set(cx + off_x, cy + off_y);
  288. door << *c;
  289. }
  290. door << door::reset;
  291. door << door::nl << door::nl;
  292. int r = door.sleep_key(door.inactivity);
  293. return r;
  294. }
  295. door::Panel make_about(void) {
  296. door::Panel about(2, 2, 60);
  297. about.setStyle(door::BorderStyle::DOUBLE_SINGLE);
  298. about.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  299. door::ATTR::BOLD));
  300. about.addLine(std::make_unique<door::Line>("About This Door", 60));
  301. /*
  302. door::Line magic("---------------------------------", 60);
  303. magic.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLACK,
  304. door::ATTR::BOLD));
  305. */
  306. about.addLine(std::make_unique<door::Line>(
  307. "---------------------------------", 60,
  308. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE, door::ATTR::BOLD)));
  309. /*
  310. 123456789012345678901234567890123456789012345678901234567890
  311. This door was written by Bugz.
  312. It is written in c++, only supports Linux, and replaces
  313. opendoors.
  314. It's written in c++, and replaces the outdated opendoors
  315. library.
  316. */
  317. about.addLine(
  318. std::make_unique<door::Line>("This door was written by Bugz.", 60));
  319. about.addLine(std::make_unique<door::Line>("", 60));
  320. about.addLine(std::make_unique<door::Line>(
  321. "It is written in c++, only support Linux, and replaces", 60));
  322. about.addLine(std::make_unique<door::Line>("opendoors.", 60));
  323. /*
  324. door::updateFunction updater = [](void) -> std::string {
  325. std::string text = "Currently: ";
  326. text.append(return_current_time_and_date());
  327. return text;
  328. };
  329. std::string current = updater();
  330. door::Line active(current, 60);
  331. active.setUpdater(updater);
  332. active.setRender(renderStatusValue(
  333. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  334. door::ATTR::BOLD), door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  335. door::ATTR::BOLD)));
  336. about.addLine(std::make_unique<door::Line>(active));
  337. */
  338. /*
  339. about.addLine(std::make_unique<door::Line>(
  340. "Status: blue", 60,
  341. statusValue(door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD),
  342. door::ANSIColor(door::COLOR::MAGENTA, door::ATTR::BLINK))));
  343. about.addLine(std::make_unique<door::Line>("Name: BUGZ", 60, rStatus));
  344. about.addLine(std::make_unique<door::Line>(
  345. "Size: 10240", 60,
  346. statusValue(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLUE,
  347. door::ATTR::BOLD),
  348. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  349. door::ATTR::BOLD, door::ATTR::BLINK))));
  350. about.addLine(std::make_unique<door::Line>("Bugz is here.", 60, rStatus));
  351. */
  352. return about;
  353. }
  354. void display_starfield(door::Door &door, std::mt19937 &rng) {
  355. door << door::reset << door::cls;
  356. int mx = door.width;
  357. int my = door.height;
  358. // display starfield
  359. const char *stars[2];
  360. stars[0] = ".";
  361. if (door::unicode) {
  362. stars[1] = "\u2219"; // "\u00b7";
  363. } else {
  364. stars[1] = "\xf9"; // "\xfa";
  365. };
  366. {
  367. // Make uniform random distribution between 1 and MAX screen size X/Y
  368. std::uniform_int_distribution<int> uni_x(1, mx);
  369. std::uniform_int_distribution<int> uni_y(1, my);
  370. door::ANSIColor white(door::COLOR::WHITE);
  371. door::ANSIColor dark(door::COLOR::BLACK, door::ATTR::BRIGHT);
  372. // 10 is too many, 100 is too few. 40 looks ok.
  373. int MAX_STARS = ((mx * my) / 40);
  374. // door.log() << "Generating starmap using " << mx << "," << my << " : "
  375. // << MAX_STARS << " stars." << std::endl;
  376. for (int x = 0; x < MAX_STARS; x++) {
  377. door::Goto star_at(uni_x(rng), uni_y(rng));
  378. door << star_at;
  379. if (x % 5 < 2)
  380. door << dark;
  381. else
  382. door << white;
  383. if (x % 2 == 0)
  384. door << stars[0];
  385. else
  386. door << stars[1];
  387. }
  388. }
  389. }
  390. void display_space_ace(door::Door &door) {
  391. int mx = door.width;
  392. int my = door.height;
  393. // space_ace is 72 chars wide, 6 high
  394. int sa_x = (mx - 72) / 2;
  395. int sa_y = (my - 6) / 2;
  396. // output the SpaceAce logo -- centered!
  397. for (const auto s : space) {
  398. door::Goto sa_at(sa_x, sa_y);
  399. door << sa_at;
  400. if (door::unicode) {
  401. std::string unicode;
  402. door::cp437toUnicode(s, unicode);
  403. door << unicode; // << door::nl;
  404. } else
  405. door << s; // << door::nl;
  406. sa_y++;
  407. }
  408. // pause 5 seconds so they can enjoy our awesome logo
  409. door.sleep_key(5);
  410. }
  411. void display_starfield_space_ace(door::Door &door, std::mt19937 &rng) {
  412. // mx = door.width;
  413. // my = door.height;
  414. display_starfield(door, rng);
  415. display_space_ace(door);
  416. door << door::reset;
  417. }
  418. int main(int argc, char *argv[]) {
  419. door::Door door("space-ace", argc, argv);
  420. // door << door::reset << door::cls << door::nl;
  421. DBData spacedb;
  422. // spacedb.init();
  423. /*
  424. // Example: How to read/set values in spacedb settings.
  425. std::string setting = "last_play";
  426. std::string user = door.username;
  427. std::string value;
  428. std::string blank = "<blank>";
  429. value = spacedb.getSetting(user, setting, blank);
  430. door << door::reset << "last_play: " << value << door::nl;
  431. std::this_thread::sleep_for(std::chrono::seconds(2));
  432. value = return_current_time_and_date();
  433. spacedb.setSetting(user, setting, value);
  434. */
  435. // https://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range
  436. std::random_device rd; // only used once to initialise (seed) engine
  437. std::mt19937 rng(rd());
  438. // random-number engine used (Mersenne-Twister in this case)
  439. // std::uniform_int_distribution<int> uni(min, max); // guaranteed unbiased
  440. int mx, my; // Max screen width/height
  441. if (door.width == 0) {
  442. // screen detect failed, use sensible defaults
  443. door.width = mx = 80;
  444. door.height = my = 23;
  445. } else {
  446. mx = door.width;
  447. my = door.height;
  448. }
  449. // We assume here that the width and height are something crazy like 10x15. :P
  450. display_starfield_space_ace(door, rng);
  451. // for testing inactivity timeout
  452. // door.inactivity = 10;
  453. door::Panel timeout = make_timeout(mx, my);
  454. door::Menu m = make_main_menu();
  455. door::Panel about = make_about();
  456. // center the about box
  457. about.set((mx - 60) / 2, (my - 5) / 2);
  458. int r = 0;
  459. while ((r >= 0) and (r != 6)) {
  460. // starfield + menu ?
  461. display_starfield(door, rng);
  462. r = m.choose(door);
  463. // need to reset the colors. (whoops!)
  464. door << door::reset << door::cls; // door::nl;
  465. // OK! The screen is blank at this point!
  466. switch (r) {
  467. case 1: // play game
  468. r = play_cards(door, rng);
  469. break;
  470. case 2: // view scores
  471. door << "Show scores goes here!" << door::nl;
  472. r = press_a_key(door);
  473. break;
  474. case 3: // configure
  475. door << "Configure options go here" << door::nl;
  476. r = press_a_key(door);
  477. break;
  478. case 4: // help
  479. door << "Help! Need some help here..." << door::nl;
  480. r = press_a_key(door);
  481. break;
  482. case 5: // about
  483. display_starfield(door, rng);
  484. door << about << door::nl;
  485. r = press_a_key(door);
  486. break;
  487. case 6: // quit
  488. break;
  489. }
  490. }
  491. if (r < 0) {
  492. TIMEOUT:
  493. if (r == -1) {
  494. door.log() << "TIMEOUT" << std::endl;
  495. door << timeout << door::reset << door::nl << door::nl;
  496. } else {
  497. if (r == -3) {
  498. door.log() << "OUTTA TIME" << std::endl;
  499. door::Panel notime = make_notime(mx, my);
  500. door << notime << door::reset << door::nl;
  501. }
  502. }
  503. return 0;
  504. }
  505. door << door::nl;
  506. /*
  507. // magic time!
  508. door << door::reset << door::nl << "Press another key...";
  509. int x;
  510. for (x = 0; x < 60; ++x) {
  511. r = door.sleep_key(1);
  512. if (r == -1) {
  513. // ok! Expected timeout!
  514. // PROBLEM: regular "local" terminal loses current attributes
  515. // when cursor is save / restored.
  516. door << door::SaveCursor;
  517. if (about.update(door)) {
  518. // ok I need to "fix" the cursor position.
  519. // it has moved.
  520. }
  521. door << door::RestoreCursor << door::reset;
  522. } else {
  523. if (r < 0)
  524. goto TIMEOUT;
  525. if (r >= 0)
  526. break;
  527. }
  528. }
  529. if (x == 60)
  530. goto TIMEOUT;
  531. */
  532. door << door::nl;
  533. #ifdef NNY
  534. // configured by the player.
  535. door::ANSIColor deck_color;
  536. // RED, BLUE, GREEN, MAGENTA, CYAN
  537. std::uniform_int_distribution<int> rand_color(0, 4);
  538. switch (rand_color(rng)) {
  539. case 0:
  540. deck_color = door::ANSIColor(door::COLOR::RED);
  541. break;
  542. case 1:
  543. deck_color = door::ANSIColor(door::COLOR::BLUE);
  544. break;
  545. case 2:
  546. deck_color = door::ANSIColor(door::COLOR::GREEN);
  547. break;
  548. case 3:
  549. deck_color = door::ANSIColor(door::COLOR::MAGENTA);
  550. break;
  551. case 4:
  552. deck_color = door::ANSIColor(door::COLOR::CYAN);
  553. break;
  554. default:
  555. deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
  556. break;
  557. }
  558. int height = 3;
  559. Deck d(deck_color, height);
  560. door::Panel *c;
  561. door << door::reset << door::cls;
  562. // This displays the cards in the upper left corner.
  563. // We want them center, and down some.
  564. int space = 3;
  565. // int cards_dealt_width = 59; int cards_dealt_height = 9;
  566. int game_width;
  567. {
  568. int cx, cy, level;
  569. cardgo(27, space, height, cx, cy, level);
  570. game_width = cx + 5; // card width
  571. }
  572. int off_x = (mx - game_width) / 2;
  573. int off_y = (my - 9) / 2;
  574. // The idea is to see the cards with <<Something Unique to the card game>>,
  575. // Year, Month, Day, and game (like 1 of 3).
  576. // This will make the games the same/fair for everyone.
  577. std::seed_seq s1{2021, 2, 27, 1};
  578. cards deck1 = card_shuffle(s1, 1);
  579. cards state = card_states();
  580. // I tried setting the cursor before the delay and before displaying the card.
  581. // It is very hard to see / just about useless. Not worth the effort.
  582. for (int x = 0; x < 28; x++) {
  583. int cx, cy, level;
  584. cardgo(x, space, height, cx, cy, level);
  585. // This is hardly visible.
  586. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  587. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  588. c = d.back(level);
  589. c->set(cx + off_x, cy + off_y);
  590. door << *c;
  591. }
  592. /*
  593. std::this_thread::sleep_for(
  594. std::chrono::seconds(1)); // 3 secs seemed too long!
  595. */
  596. for (int x = 18; x < 28; x++) {
  597. int cx, cy, level;
  598. // usleep(1000 * 20);
  599. state.at(x) = 1;
  600. cardgo(x, space, height, cx, cy, level);
  601. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  602. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  603. c = d.card(deck1.at(x));
  604. c->set(cx + off_x, cy + off_y);
  605. door << *c;
  606. }
  607. door << door::reset;
  608. door << door::nl << door::nl;
  609. r = door.sleep_key(door.inactivity);
  610. if (r < 0)
  611. goto TIMEOUT;
  612. #endif
  613. /*
  614. door::Panel *p = d.back(2);
  615. p->set(10, 10);
  616. door << *p;
  617. door::Panel *d8 = d.card(8);
  618. d8->set(20, 8);
  619. door << *d8;
  620. r = door.sleep_key(door.inactivity);
  621. if (r < 0)
  622. goto TIMEOUT;
  623. */
  624. // door << door::reset << door::cls;
  625. display_starfield(door, rng);
  626. door << m << door::reset << door::nl;
  627. // Normal DOOR exit goes here...
  628. door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
  629. return 0;
  630. }