play.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. #include "play.h"
  2. #include "db.h"
  3. #include "deck.h"
  4. #include "version.h"
  5. #include <iomanip> // put_time
  6. #include <sstream>
  7. // configuration settings access
  8. #include "yaml-cpp/yaml.h"
  9. extern YAML::Node config;
  10. #define CHEATER "CHEAT_YOUR_ASS_OFF"
  11. static std::function<std::ofstream &(void)> get_logger;
  12. static int press_a_key(door::Door &door) {
  13. door << door::reset << "Press a key to continue...";
  14. int r = door.sleep_key(door.inactivity);
  15. door << door::nl;
  16. return r;
  17. }
  18. /*
  19. In the future, this will probably check to see if they can play today or not, as
  20. well as displaying a calendar to show what days are available to be played.
  21. For now, it will play today.
  22. */
  23. PlayCards::PlayCards(door::Door &d, DBData &dbd) : door{d}, db{dbd} {
  24. get_logger = [this]() -> ofstream & { return door.log(); };
  25. init_values();
  26. play_day = std::chrono::system_clock::now();
  27. // adjustment
  28. time_t time_play_day = std::chrono::system_clock::to_time_t(play_day);
  29. if (get_logger) {
  30. get_logger() << "before: "
  31. << std::put_time(std::localtime(&time_play_day), "%F %R")
  32. << std::endl;
  33. };
  34. normalizeDate(time_play_day);
  35. play_day = std::chrono::system_clock::from_time_t(time_play_day);
  36. if (get_logger) {
  37. get_logger() << "after: "
  38. << std::put_time(std::localtime(&time_play_day), "%F %R")
  39. << std::endl;
  40. };
  41. spaceAceTriPeaks = make_tripeaks();
  42. score_panel = make_score_panel();
  43. streak_panel = make_streak_panel();
  44. left_panel = make_left_panel();
  45. cmd_panel = make_command_panel();
  46. next_quit_panel = make_next_panel();
  47. /*
  48. int mx = door.width;
  49. int my = door.height;
  50. */
  51. }
  52. PlayCards::~PlayCards() { get_logger = nullptr; }
  53. void PlayCards::init_values(void) {
  54. hand = 1;
  55. if (config["hands_per_day"]) {
  56. total_hands = config["hands_per_day"].as<int>();
  57. } else
  58. total_hands = 3;
  59. play_card = 28;
  60. current_streak = 0;
  61. best_streak = 0;
  62. std::string best;
  63. best = db.getSetting("best_streak", "0");
  64. best_streak = std::stoi(best);
  65. select_card = 23;
  66. score = 0;
  67. }
  68. void PlayCards::bonus(void) {
  69. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD) << "BONUS";
  70. }
  71. int PlayCards::play_cards(void) {
  72. init_values();
  73. std::string currentDefault = db.getSetting("DeckColor", "ALL");
  74. get_logger() << "DeckColor shows as " << currentDefault << std::endl;
  75. deck_color = stringToANSIColor(currentDefault);
  76. dp = Deck(deck_color);
  77. // Calculate the game size
  78. int game_width;
  79. int game_height = 20;
  80. {
  81. int cx, cy;
  82. cardPos(27, cx, cy);
  83. game_width = cx + 5;
  84. }
  85. int mx = door.width;
  86. int my = door.height;
  87. off_x = (mx - game_width) / 2;
  88. off_y = (my - game_height) / 2;
  89. // int true_off_y = off_y;
  90. // we can now position things properly centered
  91. int tp_off_x = (mx - spaceAceTriPeaks->getWidth()) / 2;
  92. spaceAceTriPeaks->set(tp_off_x, off_y);
  93. off_y += 3; // adjust for tripeaks panel
  94. next_hand:
  95. play_card = 28;
  96. select_card = 23;
  97. score = 0;
  98. current_streak = 0;
  99. // Use play_day to seed the rng
  100. {
  101. time_t tt = std::chrono::system_clock::to_time_t(play_day);
  102. tm local_tm = *localtime(&tt);
  103. std::seed_seq seq{local_tm.tm_year + 1900, local_tm.tm_mon + 1,
  104. local_tm.tm_mday, hand};
  105. deck = shuffleCards(seq, 1);
  106. state = makeCardStates();
  107. }
  108. /*
  109. door::Panel score_panel = make_score_panel();
  110. door::Panel streak_panel = make_streak_panel();
  111. door::Panel left_panel = make_left_panel();
  112. door::Panel cmd_panel = make_command_panel();
  113. */
  114. {
  115. int off_yp = off_y + 11;
  116. int cx, cy;
  117. int left_panel_x, right_panel_x;
  118. // find position of card, to position the panels
  119. cardPos(18, cx, cy);
  120. left_panel_x = cx;
  121. cardPos(15, cx, cy);
  122. right_panel_x = cx;
  123. score_panel->set(left_panel_x + off_x, off_yp);
  124. streak_panel->set(right_panel_x + off_x, off_yp);
  125. cmd_panel->set(left_panel_x + off_x, off_yp + 5);
  126. // next panel position
  127. cardPos(10, cx, cy);
  128. int next_off_x = (mx - next_quit_panel->getWidth()) / 2;
  129. next_quit_panel->set(next_off_x, cy + off_y);
  130. }
  131. bool dealing = true;
  132. int r = 0;
  133. redraw(dealing);
  134. dealing = false;
  135. left_panel->update(door);
  136. door << door::reset;
  137. shared_panel c;
  138. bool in_game = true;
  139. bool save_streak = false;
  140. while (in_game) {
  141. // time might have updated, so update score panel too.
  142. score_panel->update(door);
  143. // do the save here -- try to hide the database lag
  144. if (save_streak) {
  145. save_streak = false;
  146. std::string best = std::to_string(best_streak);
  147. db.setSetting("best_streak", best);
  148. }
  149. r = door.sleep_key(door.inactivity);
  150. if (r > 0) {
  151. // not a timeout or expire.
  152. if (r < 0x1000) // not a function key
  153. r = std::toupper(r);
  154. switch (r) {
  155. case '\x0d':
  156. if (play_card < 51) {
  157. play_card++;
  158. current_streak = 0;
  159. streak_panel->update(door);
  160. // update the cards left_panel
  161. left_panel->update(door);
  162. // Ok, deal next card from the pile.
  163. int cx, cy, level;
  164. if (play_card == 51) {
  165. cardPosLevel(29, cx, cy, level);
  166. level = 0; // out of cards
  167. c = dp.back(level);
  168. c->set(cx + off_x, cy + off_y);
  169. door << *c;
  170. }
  171. cardPos(28, cx, cy);
  172. c = dp.card(deck.at(play_card));
  173. c->set(cx + off_x, cy + off_y);
  174. door << *c;
  175. }
  176. break;
  177. case 'R':
  178. redraw(false);
  179. break;
  180. case 'Q':
  181. // possibly prompt here for [N]ext hand or [Q]uit ?
  182. next_quit_panel->update();
  183. door << *next_quit_panel;
  184. if (hand < total_hands) {
  185. r = door.get_one_of("CQN");
  186. } else {
  187. r = door.get_one_of("CQ");
  188. }
  189. if (r == 0) {
  190. // continue
  191. redraw(false);
  192. break;
  193. }
  194. if (r == 1) {
  195. // Ok, we are calling it quits.
  196. // save score if > 0
  197. if (score >= 50) {
  198. time_t right_now = std::chrono::system_clock::to_time_t(
  199. std::chrono::system_clock::now());
  200. db.saveScore(right_now,
  201. std::chrono::system_clock::to_time_t(play_day), hand,
  202. score);
  203. }
  204. in_game = false;
  205. r = 'Q';
  206. }
  207. if (r == 2) {
  208. // no. If you want to play the next hand, we have to save this.
  209. get_logger() << "SCORE: " << score << std::endl;
  210. time_t right_now = std::chrono::system_clock::to_time_t(
  211. std::chrono::system_clock::now());
  212. db.saveScore(right_now,
  213. std::chrono::system_clock::to_time_t(play_day), hand,
  214. score);
  215. hand++;
  216. goto next_hand;
  217. }
  218. // in_game = false;
  219. break;
  220. case ' ':
  221. case '5':
  222. // can we play this card?
  223. /*
  224. get_logger() << "canPlay( " << select_card << ":"
  225. << deck1.at(select_card) << "/"
  226. << d.getRank(deck1.at(select_card)) << " , "
  227. << play_card << "/" <<
  228. d.getRank(deck1.at(play_card))
  229. << ") = "
  230. << d.canPlay(deck1.at(select_card),
  231. deck1.at(play_card))
  232. << std::endl;
  233. */
  234. if (dp.canPlay(deck.at(select_card), deck.at(play_card)) or
  235. config[CHEATER]) {
  236. // if (true) {
  237. // yes we can.
  238. ++current_streak;
  239. if (current_streak > best_streak) {
  240. best_streak = current_streak;
  241. if (!config[CHEATER]) {
  242. save_streak = true;
  243. }
  244. }
  245. streak_panel->update(door);
  246. score += 10;
  247. if (current_streak > 1)
  248. score += current_streak * 5;
  249. score_panel->update(door);
  250. /*
  251. if (get_logger)
  252. get_logger() << "score_panel update : " << score << std::endl;
  253. */
  254. // play card!
  255. state.at(select_card) = 2;
  256. {
  257. // swap the select card with play_card
  258. int temp = deck.at(select_card);
  259. deck.at(select_card) = deck.at(play_card);
  260. deck.at(play_card) = temp;
  261. // select_card is -- invalidated here! find "new" card.
  262. int cx, cy;
  263. // erase/clear select_card
  264. std::vector<int> check = dp.unblocks(select_card);
  265. bool left = false, right = false;
  266. for (const int c : check) {
  267. std::pair<int, int> blockers = dp.blocks[c];
  268. if (blockers.first == select_card)
  269. right = true;
  270. if (blockers.second == select_card)
  271. left = true;
  272. }
  273. dp.removeCard(door, select_card, off_x, off_y, left, right);
  274. /* // old way of doing this that leaves holes.
  275. cardPosLevel(select_card, cx, cy, level);
  276. c = d.back(0);
  277. c->set(cx + off_x, cy + off_y);
  278. door << *c;
  279. */
  280. // redraw play card #28. (Which is the "old" select_card)
  281. cardPos(28, cx, cy);
  282. c = dp.card(deck.at(play_card));
  283. c->set(cx + off_x, cy + off_y);
  284. door << *c;
  285. // Did we unhide a card here?
  286. // std::vector<int> check = d.unblocks(select_card);
  287. /*
  288. get_logger() << "select_card = " << select_card
  289. << " unblocks: " << check.size() << std::endl;
  290. */
  291. int new_card_shown = -1;
  292. if (!check.empty()) {
  293. for (const int to_check : check) {
  294. std::pair<int, int> blockers = dp.blocks[to_check];
  295. /*
  296. get_logger()
  297. << "Check: " << to_check << " " << blockers.first << ","
  298. << blockers.second << " " << state.at(blockers.first)
  299. << "," << state.at(blockers.second) << std::endl;
  300. */
  301. if ((state.at(blockers.first) == 2) and
  302. (state.at(blockers.second) == 2)) {
  303. // WOOT! Card uncovered.
  304. /*
  305. get_logger() << "showing: " << to_check << std::endl;
  306. */
  307. state.at(to_check) = 1;
  308. cardPos(to_check, cx, cy);
  309. c = dp.card(deck.at(to_check));
  310. c->set(cx + off_x, cy + off_y);
  311. door << *c;
  312. new_card_shown = to_check;
  313. }
  314. }
  315. } else {
  316. // top card cleared
  317. // get_logger() << "top card cleared?" << std::endl;
  318. // display something at select_card position
  319. int cx, cy;
  320. cardPos(select_card, cx, cy);
  321. door << door::Goto(cx + off_x, cy + off_y);
  322. bonus();
  323. score += 100;
  324. state.at(select_card) = 3; // handle this in the "redraw"
  325. score_panel->update(door);
  326. }
  327. // Find new "number" for select_card to be.
  328. if (new_card_shown != -1) {
  329. select_card = new_card_shown;
  330. } else {
  331. // select_card++;
  332. int new_select = findClosestActiveCard(state, select_card);
  333. if (new_select != -1) {
  334. select_card = new_select;
  335. } else {
  336. get_logger() << "Winner!" << std::endl;
  337. select_card = -1; // winner marker?
  338. // bonus for cards left
  339. int bonus = 15 * (51 - play_card);
  340. score += 15 * (51 - play_card);
  341. score_panel->update(door);
  342. // maybe display this somewhere?
  343. // door << " BONUS: " << bonus << door::nl;
  344. get_logger()
  345. << "SCORE: " << score << ", " << bonus << std::endl;
  346. // if (!config[CHEATER]) {
  347. time_t right_now = std::chrono::system_clock::to_time_t(
  348. std::chrono::system_clock::now());
  349. db.saveScore(right_now,
  350. std::chrono::system_clock::to_time_t(play_day),
  351. hand, score);
  352. //}
  353. next_quit_panel->update();
  354. door << *next_quit_panel;
  355. if (hand < total_hands) {
  356. r = door.get_one_of("QN");
  357. } else {
  358. r = door.get_one_of("Q");
  359. }
  360. if (r == 1) {
  361. hand++;
  362. goto next_hand;
  363. }
  364. if (r == 0) {
  365. r = 'Q';
  366. }
  367. /*
  368. press_a_key(door);
  369. if (hand < total_hands) {
  370. hand++;
  371. // current_streak = 0;
  372. door << door::reset << door::cls;
  373. goto next_hand;
  374. }
  375. r = 'Q';
  376. */
  377. in_game = false;
  378. }
  379. }
  380. // update the select_card marker!
  381. cardPos(select_card, cx, cy);
  382. c = dp.marker(1);
  383. c->set(cx + off_x + 2, cy + off_y + 2);
  384. door << *c;
  385. }
  386. }
  387. break;
  388. case XKEY_LEFT_ARROW:
  389. case '4': {
  390. int new_select = findNextActiveCard(true, state, select_card);
  391. /*
  392. int new_active = active_card - 1;
  393. while (new_active >= 0) {
  394. if (state.at(new_active) == 1)
  395. break;
  396. --new_active;
  397. }*/
  398. if (new_select >= 0) {
  399. int cx, cy;
  400. cardPos(select_card, cx, cy);
  401. c = dp.marker(0);
  402. c->set(cx + off_x + 2, cy + off_y + 2);
  403. door << *c;
  404. select_card = new_select;
  405. cardPos(select_card, cx, cy);
  406. c = dp.marker(1);
  407. c->set(cx + off_x + 2, cy + off_y + 2);
  408. door << *c;
  409. }
  410. } break;
  411. case XKEY_RIGHT_ARROW:
  412. case '6': {
  413. int new_select = findNextActiveCard(false, state, select_card);
  414. /*
  415. int new_active = active_card + 1;
  416. while (new_active < 28) {
  417. if (state.at(new_active) == 1)
  418. break;
  419. ++new_active;
  420. }
  421. */
  422. if (new_select >= 0) { //(new_active < 28) {
  423. int cx, cy;
  424. cardPos(select_card, cx, cy);
  425. c = dp.marker(0);
  426. c->set(cx + off_x + 2, cy + off_y + 2);
  427. door << *c;
  428. select_card = new_select;
  429. cardPos(select_card, cx, cy);
  430. c = dp.marker(1);
  431. c->set(cx + off_x + 2, cy + off_y + 2);
  432. door << *c;
  433. }
  434. }
  435. break;
  436. }
  437. } else
  438. in_game = false;
  439. }
  440. if (r == 'Q') {
  441. // continue, play next hand (if applicable), or quit?
  442. // if score < 50, don't bother saving.
  443. // continue -- eat r & redraw.
  444. // quit, save score and exit (unless score is zero).
  445. }
  446. return r;
  447. }
  448. void PlayCards::redraw(bool dealing) {
  449. shared_panel c;
  450. door << door::reset << door::cls;
  451. door << *spaceAceTriPeaks;
  452. {
  453. // step 1:
  454. // draw the deck "source"
  455. int cx, cy, level;
  456. cardPosLevel(29, cx, cy, level);
  457. if (play_card == 51)
  458. level = 0; // out of cards!
  459. c = dp.back(level);
  460. c->set(cx + off_x, cy + off_y);
  461. // p3 is heigh below
  462. left_panel->set(cx + off_x, cy + off_y + height);
  463. // how do I update these? (hand >1)
  464. score_panel->update();
  465. left_panel->update();
  466. streak_panel->update();
  467. cmd_panel->update();
  468. door << *score_panel << *left_panel << *streak_panel << *cmd_panel;
  469. door << *c;
  470. if (dealing)
  471. std::this_thread::sleep_for(std::chrono::seconds(1));
  472. }
  473. for (int x = 0; x < (dealing ? 28 : 29); x++) {
  474. int cx, cy, level;
  475. cardPosLevel(x, cx, cy, level);
  476. // This is hardly visible.
  477. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  478. if (dealing)
  479. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  480. if (dealing) {
  481. c = dp.back(level);
  482. c->set(cx + off_x, cy + off_y);
  483. door << *c;
  484. } else {
  485. // redrawing -- draw the cards with their correct "state"
  486. int s = state.at(x);
  487. switch (s) {
  488. case 0:
  489. c = dp.back(level);
  490. c->set(cx + off_x, cy + off_y);
  491. door << *c;
  492. break;
  493. case 1:
  494. // cardPosLevel(x, space, height, cx, cy, level);
  495. if (x == 28)
  496. c = dp.card(deck.at(play_card));
  497. else
  498. c = dp.card(deck.at(x));
  499. c->set(cx + off_x, cy + off_y);
  500. door << *c;
  501. break;
  502. case 2:
  503. // no card to draw. :)
  504. break;
  505. case 3:
  506. // peak cleared, draw bonus
  507. door << door::Goto(cx + off_x, cy + off_y);
  508. bonus();
  509. break;
  510. }
  511. }
  512. }
  513. if (dealing)
  514. for (int x = 18; x < 29; x++) {
  515. int cx, cy;
  516. state.at(x) = 1;
  517. cardPos(x, cx, cy);
  518. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  519. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  520. c = dp.card(deck.at(x));
  521. c->set(cx + off_x, cy + off_y);
  522. door << *c;
  523. }
  524. {
  525. int cx, cy;
  526. cardPos(select_card, cx, cy);
  527. c = dp.marker(1);
  528. c->set(cx + off_x + 2, cy + off_y + 2);
  529. door << *c;
  530. }
  531. }
  532. door::renderFunction PlayCards::statusValue(door::ANSIColor status,
  533. door::ANSIColor value) {
  534. door::renderFunction rf = [status,
  535. value](const std::string &txt) -> door::Render {
  536. door::Render r(txt);
  537. door::ColorOutput co;
  538. co.pos = 0;
  539. co.len = 0;
  540. co.c = status;
  541. size_t pos = txt.find(':');
  542. if (pos == std::string::npos) {
  543. // failed to find :, render digits/numbers in value color
  544. int tpos = 0;
  545. for (char const &c : txt) {
  546. if (std::isdigit(c)) {
  547. if (co.c != value) {
  548. r.outputs.push_back(co);
  549. co.reset();
  550. co.pos = tpos;
  551. co.c = value;
  552. }
  553. } else {
  554. if (co.c != status) {
  555. r.outputs.push_back(co);
  556. co.reset();
  557. co.pos = tpos;
  558. co.c = status;
  559. }
  560. }
  561. co.len++;
  562. tpos++;
  563. }
  564. if (co.len != 0)
  565. r.outputs.push_back(co);
  566. } else {
  567. pos++; // Have : in status color
  568. co.len = pos;
  569. r.outputs.push_back(co);
  570. co.reset();
  571. co.pos = pos;
  572. co.c = value;
  573. co.len = txt.length() - pos;
  574. r.outputs.push_back(co);
  575. }
  576. return r;
  577. };
  578. return rf;
  579. }
  580. std::unique_ptr<door::Panel> PlayCards::make_score_panel() {
  581. const int W = 25;
  582. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  583. p->setStyle(door::BorderStyle::NONE);
  584. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  585. door::ATTR::BOLD);
  586. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  587. door::ATTR::BOLD);
  588. door::renderFunction svRender = statusValue(statusColor, valueColor);
  589. // or use renderStatus as defined above.
  590. // We'll stick with these for now.
  591. {
  592. std::string userString = "Name: ";
  593. userString += door.username;
  594. door::Line username(userString, W);
  595. username.setRender(svRender);
  596. p->addLine(std::make_unique<door::Line>(username));
  597. }
  598. {
  599. door::updateFunction scoreUpdate = [this](void) -> std::string {
  600. std::string text = "Score: ";
  601. text.append(std::to_string(score));
  602. return text;
  603. };
  604. std::string scoreString = scoreUpdate();
  605. door::Line scoreline(scoreString, W);
  606. scoreline.setRender(svRender);
  607. scoreline.setUpdater(scoreUpdate);
  608. p->addLine(std::make_unique<door::Line>(scoreline));
  609. }
  610. {
  611. door::updateFunction timeUpdate = [this](void) -> std::string {
  612. std::stringstream ss;
  613. std::string text;
  614. ss << "Time used: " << setw(3) << door.time_used << " / " << setw(3)
  615. << door.time_left;
  616. text = ss.str();
  617. return text;
  618. };
  619. std::string timeString = timeUpdate();
  620. door::Line time(timeString, W);
  621. time.setRender(svRender);
  622. time.setUpdater(timeUpdate);
  623. p->addLine(std::make_unique<door::Line>(time));
  624. }
  625. {
  626. door::updateFunction handUpdate = [this](void) -> std::string {
  627. std::string text = "Playing Hand ";
  628. text.append(std::to_string(hand));
  629. text.append(" of ");
  630. text.append(std::to_string(total_hands));
  631. return text;
  632. };
  633. std::string handString = handUpdate();
  634. door::Line hands(handString, W);
  635. hands.setRender(svRender);
  636. hands.setUpdater(handUpdate);
  637. p->addLine(std::make_unique<door::Line>(hands));
  638. }
  639. return p;
  640. }
  641. std::unique_ptr<door::Panel> PlayCards::make_streak_panel(void) {
  642. const int W = 20;
  643. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  644. p->setStyle(door::BorderStyle::NONE);
  645. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  646. door::ATTR::BOLD);
  647. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  648. door::ATTR::BOLD);
  649. door::renderFunction svRender = statusValue(statusColor, valueColor);
  650. {
  651. std::string text = "Playing: ";
  652. auto in_time_t = std::chrono::system_clock::to_time_t(play_day);
  653. std::stringstream ss;
  654. if (config["date_format"]) {
  655. std::string fmt = config["date_format"].as<std::string>();
  656. ss << std::put_time(std::localtime(&in_time_t), fmt.c_str());
  657. } else
  658. ss << std::put_time(std::localtime(&in_time_t), "%B %d");
  659. text.append(ss.str());
  660. door::Line current(text, W);
  661. current.setRender(svRender);
  662. p->addLine(std::make_unique<door::Line>(current));
  663. }
  664. {
  665. door::updateFunction currentUpdate = [this](void) -> std::string {
  666. std::string text = "Current Streak: ";
  667. text.append(std::to_string(current_streak));
  668. return text;
  669. };
  670. std::string currentString = currentUpdate();
  671. door::Line current(currentString, W);
  672. current.setRender(svRender);
  673. current.setUpdater(currentUpdate);
  674. p->addLine(std::make_unique<door::Line>(current));
  675. }
  676. {
  677. door::updateFunction currentUpdate = [this](void) -> std::string {
  678. std::string text = "Longest Streak: ";
  679. text.append(std::to_string(best_streak));
  680. return text;
  681. };
  682. std::string currentString = currentUpdate();
  683. door::Line current(currentString, W);
  684. current.setRender(svRender);
  685. current.setUpdater(currentUpdate);
  686. p->addLine(std::make_unique<door::Line>(current));
  687. }
  688. return p;
  689. }
  690. std::unique_ptr<door::Panel> PlayCards::make_left_panel(void) {
  691. const int W = 13;
  692. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  693. p->setStyle(door::BorderStyle::NONE);
  694. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  695. door::ATTR::BOLD);
  696. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  697. door::ATTR::BOLD);
  698. door::renderFunction svRender = statusValue(statusColor, valueColor);
  699. {
  700. door::updateFunction cardsleftUpdate = [this](void) -> std::string {
  701. std::string text = "Cards left:";
  702. text.append(std::to_string(51 - play_card));
  703. return text;
  704. };
  705. std::string cardsleftString = "Cards left:--";
  706. door::Line cardsleft(cardsleftString, W);
  707. cardsleft.setRender(svRender);
  708. cardsleft.setUpdater(cardsleftUpdate);
  709. p->addLine(std::make_unique<door::Line>(cardsleft));
  710. }
  711. return p;
  712. }
  713. door::renderFunction PlayCards::commandLineRender(door::ANSIColor bracket,
  714. door::ANSIColor inner,
  715. door::ANSIColor outer) {
  716. door::renderFunction rf = [bracket, inner,
  717. outer](const std::string &txt) -> door::Render {
  718. door::Render r(txt);
  719. door::ColorOutput co;
  720. co.pos = 0;
  721. co.len = 0;
  722. co.c = outer;
  723. bool inOuter = true;
  724. int tpos = 0;
  725. for (char const &c : txt) {
  726. if (inOuter) {
  727. // we're in the outer text
  728. if (co.c != outer) {
  729. if (co.len != 0) {
  730. r.outputs.push_back(co);
  731. co.reset();
  732. co.pos = tpos;
  733. }
  734. co.c = outer;
  735. }
  736. // check for [
  737. if (c == '[') {
  738. if (co.len != 0) {
  739. r.outputs.push_back(co);
  740. co.reset();
  741. co.pos = tpos;
  742. }
  743. inOuter = false;
  744. co.c = bracket;
  745. }
  746. } else {
  747. // We're not in the outer.
  748. if (co.c != inner) {
  749. if (co.len != 0) {
  750. r.outputs.push_back(co);
  751. co.reset();
  752. co.pos = tpos;
  753. }
  754. co.c = inner;
  755. }
  756. if (c == ']') {
  757. if (co.len != 0) {
  758. r.outputs.push_back(co);
  759. co.reset();
  760. co.pos = tpos;
  761. }
  762. inOuter = true;
  763. co.c = bracket;
  764. }
  765. }
  766. co.len++;
  767. tpos++;
  768. }
  769. if (co.len != 0)
  770. r.outputs.push_back(co);
  771. return r;
  772. };
  773. return rf;
  774. }
  775. std::unique_ptr<door::Panel> PlayCards::make_command_panel(void) {
  776. const int W = 76;
  777. std::unique_ptr<door::Panel> p = make_unique<door::Panel>(W);
  778. p->setStyle(door::BorderStyle::NONE);
  779. std::string commands;
  780. if (door::unicode) {
  781. commands = "[4/\u25c4] Left [6/\u25ba] Right [Space] Play Card [Enter] "
  782. "Draw [Q]uit "
  783. "[R]edraw [H]elp";
  784. } else {
  785. commands =
  786. "[4/\x11] Left [6/\x10] Right [Space] Play Card [Enter] Draw [Q]uit "
  787. "[R]edraw [H]elp";
  788. }
  789. door::ANSIColor bracketColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  790. door::ATTR::BOLD);
  791. door::ANSIColor innerColor(door::COLOR::CYAN, door::COLOR::BLUE,
  792. door::ATTR::BOLD);
  793. door::ANSIColor outerColor(door::COLOR::GREEN, door::COLOR::BLUE,
  794. door::ATTR::BOLD);
  795. door::renderFunction cmdRender =
  796. commandLineRender(bracketColor, innerColor, outerColor);
  797. door::Line cmd(commands, W);
  798. cmd.setRender(cmdRender);
  799. p->addLine(std::make_unique<door::Line>(cmd));
  800. return p;
  801. }
  802. std::unique_ptr<door::Panel> PlayCards::make_next_panel(void) {
  803. const int W = 50;
  804. std::unique_ptr<door::Panel> p = make_unique<door::Panel>(W);
  805. door::ANSIColor panelColor(door::COLOR::YELLOW, door::COLOR::GREEN,
  806. door::ATTR::BOLD);
  807. p->setStyle(door::BorderStyle::DOUBLE);
  808. p->setColor(panelColor);
  809. door::updateFunction nextUpdate = [this](void) -> std::string {
  810. std::string text;
  811. if (select_card == -1) {
  812. // winner
  813. if (hand < total_hands) {
  814. text = " [N]ext Hand - [Q]uit";
  815. } else
  816. text = " All hands played - [Q]uit";
  817. } else if (hand < total_hands) {
  818. text = " [C]ontinue this hand - [N]ext Hand - [Q]uit";
  819. } else {
  820. text = " [C]ontinue this hand - [Q]uit";
  821. }
  822. return text;
  823. };
  824. std::string text;
  825. text = nextUpdate();
  826. door::ANSIColor bracketColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  827. door::ATTR::BOLD);
  828. door::ANSIColor innerColor(door::COLOR::CYAN, door::COLOR::BLUE,
  829. door::ATTR::BOLD);
  830. door::ANSIColor outerColor(door::COLOR::GREEN, door::COLOR::BLUE,
  831. door::ATTR::BOLD);
  832. door::renderFunction textRender =
  833. commandLineRender(bracketColor, innerColor, outerColor);
  834. door::Line nextLine(text, W);
  835. nextLine.setRender(textRender);
  836. // nextLine.setPadding(" ", panelColor);
  837. nextLine.setUpdater(nextUpdate);
  838. p->addLine(std::make_unique<door::Line>(nextLine));
  839. return p;
  840. }
  841. std::unique_ptr<door::Panel> PlayCards::make_tripeaks(void) {
  842. std::string tripeaksText(" " SPACEACE
  843. " - Tri-Peaks Solitaire v" SPACEACE_VERSION " ");
  844. std::unique_ptr<door::Panel> spaceAceTriPeaks =
  845. std::make_unique<door::Panel>(tripeaksText.size());
  846. spaceAceTriPeaks->setStyle(door::BorderStyle::SINGLE);
  847. spaceAceTriPeaks->setColor(
  848. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLACK));
  849. spaceAceTriPeaks->addLine(
  850. std::make_unique<door::Line>(tripeaksText, tripeaksText.size()));
  851. return spaceAceTriPeaks;
  852. }