main.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. door::Panel make_about(void) {
  209. door::Panel about(2, 2, 60);
  210. about.setStyle(door::BorderStyle::DOUBLE_SINGLE);
  211. about.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  212. door::ATTR::BOLD));
  213. about.addLine(std::make_unique<door::Line>("About This Door", 60));
  214. /*
  215. door::Line magic("---------------------------------", 60);
  216. magic.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLACK,
  217. door::ATTR::BOLD));
  218. */
  219. about.addLine(std::make_unique<door::Line>(
  220. "---------------------------------", 60,
  221. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE, door::ATTR::BOLD)));
  222. /*
  223. 123456789012345678901234567890123456789012345678901234567890
  224. This door was written by Bugz.
  225. It is written in c++, only supports Linux, and replaces
  226. opendoors.
  227. It's written in c++, and replaces the outdated opendoors
  228. library.
  229. */
  230. about.addLine(
  231. std::make_unique<door::Line>("This door was written by Bugz.", 60));
  232. about.addLine(std::make_unique<door::Line>("", 60));
  233. about.addLine(std::make_unique<door::Line>(
  234. "It is written in c++, only support Linux, and replaces", 60));
  235. about.addLine(std::make_unique<door::Line>("opendoors.", 60));
  236. /*
  237. door::updateFunction updater = [](void) -> std::string {
  238. std::string text = "Currently: ";
  239. text.append(return_current_time_and_date());
  240. return text;
  241. };
  242. std::string current = updater();
  243. door::Line active(current, 60);
  244. active.setUpdater(updater);
  245. active.setRender(renderStatusValue(
  246. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  247. door::ATTR::BOLD), door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  248. door::ATTR::BOLD)));
  249. about.addLine(std::make_unique<door::Line>(active));
  250. */
  251. /*
  252. about.addLine(std::make_unique<door::Line>(
  253. "Status: blue", 60,
  254. statusValue(door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD),
  255. door::ANSIColor(door::COLOR::MAGENTA, door::ATTR::BLINK))));
  256. about.addLine(std::make_unique<door::Line>("Name: BUGZ", 60, rStatus));
  257. about.addLine(std::make_unique<door::Line>(
  258. "Size: 10240", 60,
  259. statusValue(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLUE,
  260. door::ATTR::BOLD),
  261. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  262. door::ATTR::BOLD, door::ATTR::BLINK))));
  263. about.addLine(std::make_unique<door::Line>("Bugz is here.", 60, rStatus));
  264. */
  265. return about;
  266. }
  267. void display_starfield(int mx, int my, door::Door &door, std::mt19937 &rng) {
  268. door << door::reset << door::cls;
  269. // display starfield
  270. const char *stars[2];
  271. stars[0] = ".";
  272. if (door::unicode) {
  273. stars[1] = "\u2219"; // "\u00b7";
  274. } else {
  275. stars[1] = "\xf9"; // "\xfa";
  276. };
  277. {
  278. // Make uniform random distribution between 1 and MAX screen size X/Y
  279. std::uniform_int_distribution<int> uni_x(1, mx);
  280. std::uniform_int_distribution<int> uni_y(1, my);
  281. door::ANSIColor white(door::COLOR::WHITE);
  282. door::ANSIColor dark(door::COLOR::BLACK, door::ATTR::BRIGHT);
  283. for (int x = 0; x < (mx * my / 100); x++) {
  284. door::Goto star_at(uni_x(rng), uni_y(rng));
  285. door << star_at;
  286. if (x % 5 < 2)
  287. door << dark;
  288. else
  289. door << white;
  290. if (x % 2 == 0)
  291. door << stars[0];
  292. else
  293. door << stars[1];
  294. }
  295. }
  296. }
  297. void display_space_ace(int mx, int my, door::Door &door) {
  298. // space_ace is 72 chars wide, 6 high
  299. int sa_x = (mx - 72) / 2;
  300. int sa_y = (my - 6) / 2;
  301. // output the SpaceAce logo -- centered!
  302. for (const auto s : space) {
  303. door::Goto sa_at(sa_x, sa_y);
  304. door << sa_at;
  305. if (door::unicode) {
  306. std::string unicode;
  307. door::cp437toUnicode(s, unicode);
  308. door << unicode; // << door::nl;
  309. } else
  310. door << s; // << door::nl;
  311. sa_y++;
  312. }
  313. // pause 5 seconds so they can enjoy our awesome logo
  314. door.sleep_key(5);
  315. }
  316. void display_starfield_space_ace(int mx, int my, door::Door &door,
  317. std::mt19937 &rng) {
  318. display_starfield(mx, my, door, rng);
  319. display_space_ace(mx, my, door);
  320. door << door::reset;
  321. }
  322. int main(int argc, char *argv[]) {
  323. door::Door door("space-ace", argc, argv);
  324. // door << door::reset << door::cls << door::nl;
  325. // door::ANSIColor ac(door::COLOR::YELLOW, door::ATTR::BOLD);
  326. // door::ANSIColor mb(door::COLOR::MAGENTA, door::ATTR::BLINK);
  327. // door << mb << "Does this work?" << door::reset << door::nl;
  328. DBData spacedb;
  329. // spacedb.init();
  330. /*
  331. std::string setting = "last_play";
  332. std::string user = door.username;
  333. std::string value;
  334. std::string blank = "<blank>";
  335. value = spacedb.getSetting(user, setting, blank);
  336. door << door::reset << "last_play: " << value << door::nl;
  337. std::this_thread::sleep_for(std::chrono::seconds(2));
  338. value = return_current_time_and_date();
  339. spacedb.setSetting(user, setting, value);
  340. */
  341. // https://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range
  342. std::random_device rd; // only used once to initialise (seed) engine
  343. std::mt19937 rng(rd());
  344. // random-number engine used (Mersenne-Twister in this case)
  345. // std::uniform_int_distribution<int> uni(min, max); // guaranteed unbiased
  346. int mx, my; // Max screen width/height
  347. if (door.width == 0) {
  348. // screen detect failed, use sensible defaults
  349. mx = 80;
  350. my = 23;
  351. } else {
  352. mx = door.width;
  353. my = door.height;
  354. }
  355. // We assume here that the width and height are something crazy like 10x15. :P
  356. display_starfield_space_ace(mx, my, door, rng);
  357. // for testing inactivity timeout
  358. // door.inactivity = 10;
  359. door::Panel timeout = make_timeout(mx, my);
  360. door::Menu m = make_main_menu();
  361. int r = m.choose(door);
  362. // need to reset the colors. (whoops!)
  363. door << door::reset << door::nl;
  364. if (r < 0) {
  365. TIMEOUT:
  366. if (r == -1) {
  367. door.log() << "TIMEOUT" << std::endl;
  368. door << timeout << door::reset << door::nl << door::nl;
  369. } else {
  370. if (r == -3) {
  371. door.log() << "OUTTA TIME" << std::endl;
  372. door::Panel notime = make_notime(mx, my);
  373. door << notime << door::reset << door::nl;
  374. }
  375. }
  376. return 0;
  377. }
  378. /*
  379. display_starfield(mx, my, door, rng);
  380. // WARNING: After starfield, cursor position is random!
  381. door << door::Goto(1, 9); // door::nl << door::nl; // door::cls;
  382. door << "Your name: "
  383. << door::ANSIColor(door::COLOR::WHITE, door::COLOR::GREEN,
  384. door::ATTR::BOLD);
  385. std::string istring;
  386. istring = door.input_string(25);
  387. door << door::reset << door::nl << "You typed in [" << istring << "]"
  388. << door::nl;
  389. door << "Hello, " << door.username << ", you chose option " << r << "!"
  390. << door::nl;
  391. door << "Press a key...";
  392. r = door.sleep_key(door.inactivity);
  393. if (r < 0)
  394. goto TIMEOUT;
  395. */
  396. door << door::nl;
  397. door::Panel about = make_about();
  398. // center the about box
  399. about.set((mx - 60) / 2, (my - 5) / 2);
  400. door << about;
  401. r = door.sleep_key(door.inactivity);
  402. if (r < 0)
  403. goto TIMEOUT;
  404. /*
  405. // magic time!
  406. door << door::reset << door::nl << "Press another key...";
  407. int x;
  408. for (x = 0; x < 60; ++x) {
  409. r = door.sleep_key(1);
  410. if (r == -1) {
  411. // ok! Expected timeout!
  412. // PROBLEM: regular "local" terminal loses current attributes
  413. // when cursor is save / restored.
  414. door << door::SaveCursor;
  415. if (about.update(door)) {
  416. // ok I need to "fix" the cursor position.
  417. // it has moved.
  418. }
  419. door << door::RestoreCursor << door::reset;
  420. } else {
  421. if (r < 0)
  422. goto TIMEOUT;
  423. if (r >= 0)
  424. break;
  425. }
  426. }
  427. if (x == 60)
  428. goto TIMEOUT;
  429. */
  430. door << door::nl;
  431. // configured by the player.
  432. door::ANSIColor deck_color;
  433. // RED, BLUE, GREEN, MAGENTA, CYAN
  434. std::uniform_int_distribution<int> rand_color(0, 4);
  435. switch (rand_color(rng)) {
  436. case 0:
  437. deck_color = door::ANSIColor(door::COLOR::RED);
  438. break;
  439. case 1:
  440. deck_color = door::ANSIColor(door::COLOR::BLUE);
  441. break;
  442. case 2:
  443. deck_color = door::ANSIColor(door::COLOR::GREEN);
  444. break;
  445. case 3:
  446. deck_color = door::ANSIColor(door::COLOR::MAGENTA);
  447. break;
  448. case 4:
  449. deck_color = door::ANSIColor(door::COLOR::CYAN);
  450. break;
  451. default:
  452. deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
  453. break;
  454. }
  455. int height = 3;
  456. Deck d(deck_color, height);
  457. door::Panel *c;
  458. door << door::reset << door::cls;
  459. // This displays the cards in the upper left corner.
  460. // We want them center, and down some.
  461. int space = 3;
  462. // int cards_dealt_width = 59; int cards_dealt_height = 9;
  463. int game_width;
  464. {
  465. int cx, cy, level;
  466. cardgo(27, space, height, cx, cy, level);
  467. game_width = cx + 5; // card width
  468. }
  469. int off_x = (mx - game_width) / 2;
  470. int off_y = (my - 9) / 2;
  471. // The idea is to see the cards with <<Something Unique to the card game>>,
  472. // Year, Month, Day, and game (like 1 of 3).
  473. // This will make the games the same/fair for everyone.
  474. std::seed_seq s1{2021, 2, 27, 1};
  475. cards deck1 = card_shuffle(s1, 1);
  476. cards state = card_states();
  477. // I tried setting the cursor before the delay and before displaying the card.
  478. // It is very hard to see / just about useless. Not worth the effort.
  479. for (int x = 0; x < 28; x++) {
  480. int cx, cy, level;
  481. cardgo(x, space, height, cx, cy, level);
  482. // This is hardly visible.
  483. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  484. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  485. c = d.back(level);
  486. c->set(cx + off_x, cy + off_y);
  487. door << *c;
  488. }
  489. /*
  490. std::this_thread::sleep_for(
  491. std::chrono::seconds(1)); // 3 secs seemed too long!
  492. */
  493. for (int x = 18; x < 28; x++) {
  494. int cx, cy, level;
  495. // usleep(1000 * 20);
  496. state.at(x) = 1;
  497. cardgo(x, space, height, cx, cy, level);
  498. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  499. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  500. c = d.card(deck1.at(x));
  501. c->set(cx + off_x, cy + off_y);
  502. door << *c;
  503. }
  504. door << door::reset;
  505. door << door::nl << door::nl;
  506. r = door.sleep_key(door.inactivity);
  507. if (r < 0)
  508. goto TIMEOUT;
  509. /*
  510. door::Panel *p = d.back(2);
  511. p->set(10, 10);
  512. door << *p;
  513. door::Panel *d8 = d.card(8);
  514. d8->set(20, 8);
  515. door << *d8;
  516. r = door.sleep_key(door.inactivity);
  517. if (r < 0)
  518. goto TIMEOUT;
  519. */
  520. // door << door::reset << door::cls;
  521. display_starfield(mx, my, door, rng);
  522. door << m << door::reset << door::nl << "This is what the menu looked liked!"
  523. << door::nl;
  524. // Normal DOOR exit goes here...
  525. door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
  526. return 0;
  527. }