play.cpp 28 KB

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