play.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. #include "play.h"
  2. #include "db.h"
  3. #include "deck.h"
  4. #include "utils.h"
  5. #include "version.h"
  6. #include <ctime>
  7. #include <iomanip> // put_time
  8. #include <sstream>
  9. /**
  10. * @brief Allow any card to be played on any other card.
  11. *
  12. * This is for testing BONUS and scoring, etc.
  13. *
  14. */
  15. #define CHEATER "CHEAT_YOUR_ASS_OFF"
  16. // static std::function<std::ofstream &(void)> get_logger;
  17. /*
  18. static int press_a_key(door::Door &door) {
  19. door << door::reset << "Press a key to continue...";
  20. int r = door.sleep_key(door.inactivity);
  21. door << door::nl;
  22. return r;
  23. }
  24. */
  25. /*
  26. In the future, this will probably check to see if they can play today or not, as
  27. well as displaying a calendar to show what days are available to be played.
  28. For now, it will play today.
  29. */
  30. PlayCards::PlayCards(door::Door &d, DBData &dbd, std::mt19937 &r)
  31. : door{d}, db{dbd}, rng{r} {
  32. get_logger = [this]() -> ofstream & { return door.log(); };
  33. init_values();
  34. play_day = std::chrono::system_clock::now();
  35. normalizeDate(play_day);
  36. time_t play_day_t = std::chrono::system_clock::to_time_t(play_day);
  37. // check last_played
  38. time_t last_played;
  39. {
  40. std::string last_played_str = dbd.getSetting("last_played", "0");
  41. last_played = std::stoi(last_played_str);
  42. std::string days_played_str = dbd.getSetting("days_played", "0");
  43. days_played = std::stoi(days_played_str);
  44. }
  45. if (last_played != play_day_t) {
  46. // Ok, they haven't played today, so:
  47. get_logger() << "reset days_played = 0" << std::endl;
  48. dbd.setSetting("last_played", std::to_string(play_day_t));
  49. dbd.setSetting("days_played", std::to_string(0));
  50. days_played = 0;
  51. }
  52. /*
  53. * TODO: Check the date with the db. Have they already played up today? If
  54. * so, display calendar and find a day they can play. Or, play missed hands
  55. * for today.
  56. */
  57. spaceAceTriPeaks = make_tripeaks();
  58. score_panel = make_score_panel();
  59. streak_panel = make_streak_panel();
  60. left_panel = make_left_panel();
  61. cmd_panel = make_command_panel();
  62. next_quit_panel = make_next_panel();
  63. calendar = make_calendar();
  64. /*
  65. int mx = door.width;
  66. int my = door.height;
  67. */
  68. }
  69. PlayCards::~PlayCards() { get_logger = nullptr; }
  70. void PlayCards::init_values(void) {
  71. // beware of hand=1 ! We might not be playing the first hand here!
  72. hand = 1;
  73. if (config["hands_per_day"]) {
  74. total_hands = config["hands_per_day"].as<int>();
  75. } else
  76. total_hands = 3;
  77. play_card = 28;
  78. current_streak = 0;
  79. best_streak = 0;
  80. {
  81. std::string best;
  82. best = db.getSetting("best_streak", "0");
  83. best_streak = std::stoi(best);
  84. }
  85. select_card = 23;
  86. score = 0;
  87. }
  88. /**
  89. * @brief Display the bonus text, when you remove a peak.
  90. *
  91. */
  92. void PlayCards::bonus(void) {
  93. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD) << "BONUS";
  94. }
  95. int PlayCards::press_a_key(void) {
  96. door << door::reset << "Press a key to continue...";
  97. int r = door.sleep_key(door.inactivity);
  98. door << door::nl;
  99. return r;
  100. }
  101. /**
  102. * @brief PLay
  103. *
  104. * This will display the calendar (if needed), otherwise takes player into
  105. * play_cards using now() as play_day.
  106. * \sa PlayCards::play_cards()
  107. *
  108. * @return int
  109. */
  110. int PlayCards::play(void) {
  111. play_day = std::chrono::system_clock::now();
  112. normalizeDate(play_day);
  113. // check to see if played already today
  114. std::shared_ptr<door::Screen> calendar = make_calendar();
  115. door << door::reset << door::cls;
  116. door << *calendar;
  117. press_a_key();
  118. hand = 1;
  119. // possibly init_values()
  120. play_day = std::chrono::system_clock::now();
  121. normalizeDate(play_day);
  122. return play_cards();
  123. }
  124. /**
  125. * @brief play_cards
  126. *
  127. * Play cards for the given play_day and hand.
  128. *
  129. * This should be called by play with the correct #play_day set.
  130. * \sa PlayCards::play()
  131. *
  132. * @return int
  133. */
  134. int PlayCards::play_cards(void) {
  135. init_values();
  136. // Calculate the game size
  137. int game_width;
  138. int game_height = 20;
  139. {
  140. int cx, cy;
  141. cardPos(27, cx, cy);
  142. game_width = cx + 5;
  143. }
  144. int mx = door.width;
  145. int my = door.height;
  146. off_x = (mx - game_width) / 2;
  147. off_y = (my - game_height) / 2;
  148. // int true_off_y = off_y;
  149. // we can now position things properly centered
  150. int tp_off_x = (mx - spaceAceTriPeaks->getWidth()) / 2;
  151. spaceAceTriPeaks->set(tp_off_x, off_y);
  152. off_y += 3; // adjust for tripeaks panel
  153. next_hand:
  154. // Make sure we pick the deck color here. We want it to (possibly) change
  155. // between hands.
  156. std::string currentDefault = db.getSetting("DeckColor", "ALL");
  157. get_logger() << "DeckColor shows as " << currentDefault << std::endl;
  158. deck_color = stringToANSIColor(currentDefault);
  159. dp = Deck(deck_color);
  160. play_card = 28;
  161. select_card = 23;
  162. score = 0;
  163. current_streak = 0;
  164. // Use play_day to seed the rng
  165. {
  166. time_t tt = std::chrono::system_clock::to_time_t(play_day);
  167. tm local_tm = *localtime(&tt);
  168. std::seed_seq seq{local_tm.tm_year + 1900, local_tm.tm_mon + 1,
  169. local_tm.tm_mday, hand};
  170. deck = shuffleCards(seq, 1);
  171. state = makeCardStates();
  172. }
  173. /*
  174. door::Panel score_panel = make_score_panel();
  175. door::Panel streak_panel = make_streak_panel();
  176. door::Panel left_panel = make_left_panel();
  177. door::Panel cmd_panel = make_command_panel();
  178. */
  179. {
  180. int off_yp = off_y + 11;
  181. int cx, cy;
  182. int left_panel_x, right_panel_x;
  183. // find position of card, to position the panels
  184. cardPos(18, cx, cy);
  185. left_panel_x = cx;
  186. cardPos(15, cx, cy);
  187. right_panel_x = cx;
  188. score_panel->set(left_panel_x + off_x, off_yp);
  189. streak_panel->set(right_panel_x + off_x, off_yp);
  190. cmd_panel->set(left_panel_x + off_x, off_yp + 5);
  191. // next panel position (top = card 10, centered)
  192. cardPos(10, cx, cy);
  193. int next_off_x = (mx - next_quit_panel->getWidth()) / 2;
  194. next_quit_panel->set(next_off_x, cy + off_y);
  195. }
  196. bool dealing = true;
  197. int r = 0;
  198. redraw(dealing);
  199. dealing = false;
  200. left_panel->update(door);
  201. door << door::reset;
  202. shared_panel c;
  203. bool in_game = true;
  204. bool save_streak = false;
  205. while (in_game) {
  206. // time might have updated, so update score panel too.
  207. score_panel->update(door);
  208. // do the save here -- try to hide the database lag
  209. if (save_streak) {
  210. save_streak = false;
  211. std::string best = std::to_string(best_streak);
  212. db.setSetting("best_streak", best);
  213. }
  214. r = door.sleep_key(door.inactivity);
  215. if (r > 0) {
  216. // not a timeout or expire.
  217. if (r < 0x1000) // not a function key
  218. r = std::toupper(r);
  219. switch (r) {
  220. case '\x0d':
  221. if (current_streak > best_streak) {
  222. best_streak = current_streak;
  223. if (!config[CHEATER]) {
  224. save_streak = true;
  225. }
  226. streak_panel->update(door);
  227. }
  228. if (play_card < 51) {
  229. play_card++;
  230. current_streak = 0;
  231. streak_panel->update(door);
  232. // update the cards left_panel
  233. left_panel->update(door);
  234. // Ok, deal next card from the pile.
  235. int cx, cy, level;
  236. if (play_card == 51) {
  237. cardPosLevel(29, cx, cy, level);
  238. level = 0; // out of cards
  239. c = dp.back(level);
  240. c->set(cx + off_x, cy + off_y);
  241. door << *c;
  242. }
  243. cardPos(28, cx, cy);
  244. c = dp.card(deck.at(play_card));
  245. c->set(cx + off_x, cy + off_y);
  246. door << *c;
  247. }
  248. break;
  249. case 'R':
  250. redraw(false);
  251. break;
  252. case 'Q':
  253. // possibly prompt here for [N]ext hand or [Q]uit ?
  254. if (current_streak > best_streak) {
  255. best_streak = current_streak;
  256. if (!config[CHEATER]) {
  257. save_streak = true;
  258. }
  259. streak_panel->update(door);
  260. }
  261. next_quit_panel->update();
  262. door << *next_quit_panel;
  263. if (hand < total_hands) {
  264. r = door.get_one_of("CQN");
  265. } else {
  266. r = door.get_one_of("CQ");
  267. }
  268. if (r == 0) {
  269. // continue
  270. redraw(false);
  271. break;
  272. }
  273. if (r == 1) {
  274. // Ok, we are calling it quits.
  275. // save score if > 0
  276. if (score >= 50) {
  277. time_t right_now = std::chrono::system_clock::to_time_t(
  278. std::chrono::system_clock::now());
  279. db.saveScore(right_now,
  280. std::chrono::system_clock::to_time_t(play_day), hand,
  281. 0, score);
  282. }
  283. in_game = false;
  284. r = 'Q';
  285. }
  286. if (r == 2) {
  287. // no. If you want to play the next hand, we have to save this.
  288. get_logger() << "SCORE: " << score << std::endl;
  289. time_t right_now = std::chrono::system_clock::to_time_t(
  290. std::chrono::system_clock::now());
  291. db.saveScore(right_now,
  292. std::chrono::system_clock::to_time_t(play_day), hand, 0,
  293. score);
  294. hand++;
  295. goto next_hand;
  296. }
  297. // in_game = false;
  298. break;
  299. case ' ':
  300. case '5':
  301. // can we play this card?
  302. /*
  303. get_logger() << "canPlay( " << select_card << ":"
  304. << deck1.at(select_card) << "/"
  305. << d.getRank(deck1.at(select_card)) << " , "
  306. << play_card << "/" <<
  307. d.getRank(deck1.at(play_card))
  308. << ") = "
  309. << d.canPlay(deck1.at(select_card),
  310. deck1.at(play_card))
  311. << std::endl;
  312. */
  313. if (dp.canPlay(deck.at(select_card), deck.at(play_card)) or
  314. config[CHEATER]) {
  315. // if (true) {
  316. // yes we can.
  317. ++current_streak;
  318. // update best_streak when they draw the next card.
  319. /*
  320. if (current_streak > best_streak) {
  321. best_streak = current_streak;
  322. if (!config[CHEATER]) {
  323. save_streak = true;
  324. }
  325. }
  326. */
  327. streak_panel->update(door);
  328. score += 10;
  329. if (current_streak > 1)
  330. score += current_streak * 5;
  331. score_panel->update(door);
  332. /*
  333. if (get_logger)
  334. get_logger() << "score_panel update : " << score << std::endl;
  335. */
  336. // play card!
  337. state.at(select_card) = 2;
  338. {
  339. // swap the select card with play_card
  340. int temp = deck.at(select_card);
  341. deck.at(select_card) = deck.at(play_card);
  342. deck.at(play_card) = temp;
  343. // select_card is -- invalidated here! find "new" card.
  344. int cx, cy;
  345. // erase/clear select_card
  346. std::vector<int> check = dp.unblocks(select_card);
  347. bool left = false, right = false;
  348. for (const int c : check) {
  349. std::pair<int, int> blockers = dp.blocks[c];
  350. if (blockers.first == select_card)
  351. right = true;
  352. if (blockers.second == select_card)
  353. left = true;
  354. }
  355. dp.removeCard(door, select_card, off_x, off_y, left, right);
  356. /* // old way of doing this that leaves holes.
  357. cardPosLevel(select_card, cx, cy, level);
  358. c = d.back(0);
  359. c->set(cx + off_x, cy + off_y);
  360. door << *c;
  361. */
  362. // redraw play card #28. (Which is the "old" select_card)
  363. cardPos(28, cx, cy);
  364. c = dp.card(deck.at(play_card));
  365. c->set(cx + off_x, cy + off_y);
  366. door << *c;
  367. // Did we unhide a card here?
  368. // std::vector<int> check = d.unblocks(select_card);
  369. /*
  370. get_logger() << "select_card = " << select_card
  371. << " unblocks: " << check.size() << std::endl;
  372. */
  373. int new_card_shown = -1;
  374. if (!check.empty()) {
  375. for (const int to_check : check) {
  376. std::pair<int, int> blockers = dp.blocks[to_check];
  377. /*
  378. get_logger()
  379. << "Check: " << to_check << " " << blockers.first << ","
  380. << blockers.second << " " << state.at(blockers.first)
  381. << "," << state.at(blockers.second) << std::endl;
  382. */
  383. if ((state.at(blockers.first) == 2) and
  384. (state.at(blockers.second) == 2)) {
  385. // WOOT! Card uncovered.
  386. /*
  387. get_logger() << "showing: " << to_check << std::endl;
  388. */
  389. state.at(to_check) = 1;
  390. cardPos(to_check, cx, cy);
  391. c = dp.card(deck.at(to_check));
  392. c->set(cx + off_x, cy + off_y);
  393. door << *c;
  394. new_card_shown = to_check;
  395. }
  396. }
  397. } else {
  398. // top card cleared
  399. // get_logger() << "top card cleared?" << std::endl;
  400. // display something at select_card position
  401. int cx, cy;
  402. cardPos(select_card, cx, cy);
  403. door << door::Goto(cx + off_x, cy + off_y);
  404. bonus();
  405. score += 100;
  406. state.at(select_card) = 3; // handle this in the "redraw"
  407. score_panel->update(door);
  408. }
  409. // Find new "number" for select_card to be.
  410. if (new_card_shown != -1) {
  411. select_card = new_card_shown;
  412. } else {
  413. // select_card++;
  414. int new_select = findClosestActiveCard(state, select_card);
  415. if (new_select != -1) {
  416. select_card = new_select;
  417. } else {
  418. get_logger() << "Winner!" << std::endl;
  419. select_card = -1; // winner marker?
  420. // bonus for cards left
  421. int bonus = 15 * (51 - play_card);
  422. score += 15 * (51 - play_card);
  423. score_panel->update(door);
  424. // maybe display this somewhere?
  425. // door << " BONUS: " << bonus << door::nl;
  426. get_logger()
  427. << "SCORE: " << score << ", " << bonus << std::endl;
  428. // if (!config[CHEATER]) {
  429. time_t right_now = std::chrono::system_clock::to_time_t(
  430. std::chrono::system_clock::now());
  431. db.saveScore(right_now,
  432. std::chrono::system_clock::to_time_t(play_day),
  433. hand, 1, score);
  434. //}
  435. next_quit_panel->update();
  436. door << *next_quit_panel;
  437. if (hand < total_hands) {
  438. r = door.get_one_of("QN");
  439. } else {
  440. r = door.get_one_of("Q");
  441. }
  442. if (r == 1) {
  443. hand++;
  444. goto next_hand;
  445. }
  446. if (r == 0) {
  447. r = 'Q';
  448. }
  449. /*
  450. press_a_key(door);
  451. if (hand < total_hands) {
  452. hand++;
  453. // current_streak = 0;
  454. door << door::reset << door::cls;
  455. goto next_hand;
  456. }
  457. r = 'Q';
  458. */
  459. in_game = false;
  460. }
  461. }
  462. // update the select_card marker!
  463. cardPos(select_card, cx, cy);
  464. c = dp.marker(1);
  465. c->set(cx + off_x + 2, cy + off_y + 2);
  466. door << *c;
  467. }
  468. }
  469. break;
  470. case XKEY_LEFT_ARROW:
  471. case '4': {
  472. int new_select = findNextActiveCard(true, state, select_card);
  473. /*
  474. int new_active = active_card - 1;
  475. while (new_active >= 0) {
  476. if (state.at(new_active) == 1)
  477. break;
  478. --new_active;
  479. }*/
  480. if (new_select >= 0) {
  481. int cx, cy;
  482. cardPos(select_card, cx, cy);
  483. c = dp.marker(0);
  484. c->set(cx + off_x + 2, cy + off_y + 2);
  485. door << *c;
  486. select_card = new_select;
  487. cardPos(select_card, cx, cy);
  488. c = dp.marker(1);
  489. c->set(cx + off_x + 2, cy + off_y + 2);
  490. door << *c;
  491. }
  492. } break;
  493. case XKEY_RIGHT_ARROW:
  494. case '6': {
  495. int new_select = findNextActiveCard(false, state, select_card);
  496. /*
  497. int new_active = active_card + 1;
  498. while (new_active < 28) {
  499. if (state.at(new_active) == 1)
  500. break;
  501. ++new_active;
  502. }
  503. */
  504. if (new_select >= 0) { //(new_active < 28) {
  505. int cx, cy;
  506. cardPos(select_card, cx, cy);
  507. c = dp.marker(0);
  508. c->set(cx + off_x + 2, cy + off_y + 2);
  509. door << *c;
  510. select_card = new_select;
  511. cardPos(select_card, cx, cy);
  512. c = dp.marker(1);
  513. c->set(cx + off_x + 2, cy + off_y + 2);
  514. door << *c;
  515. }
  516. }
  517. break;
  518. }
  519. } else
  520. in_game = false;
  521. }
  522. if (r == 'Q') {
  523. // continue, play next hand (if applicable), or quit?
  524. // if score < 50, don't bother saving.
  525. // continue -- eat r & redraw.
  526. // quit, save score and exit (unless score is zero).
  527. }
  528. return r;
  529. }
  530. /**
  531. * @brief Redraw the entire play cards screen.
  532. *
  533. * If dealing then show delays when displaying cards, or turning them over.
  534. *
  535. * Otherwise, the user has requested a redraw -- and don't delay.
  536. *
  537. * @param dealing
  538. */
  539. void PlayCards::redraw(bool dealing) {
  540. shared_panel c;
  541. display_starfield(door, rng);
  542. // door << door::reset << door::cls;
  543. door << *spaceAceTriPeaks;
  544. {
  545. // step 1:
  546. // draw the deck "source"
  547. int cx, cy, level;
  548. cardPosLevel(29, cx, cy, level);
  549. if (play_card == 51)
  550. level = 0; // out of cards!
  551. c = dp.back(level);
  552. c->set(cx + off_x, cy + off_y);
  553. // p3 is heigh below
  554. left_panel->set(cx + off_x, cy + off_y + height);
  555. // how do I update these? (hand >1)
  556. score_panel->update();
  557. left_panel->update();
  558. streak_panel->update();
  559. cmd_panel->update();
  560. door << *score_panel << *left_panel << *streak_panel << *cmd_panel;
  561. door << *c;
  562. if (dealing)
  563. std::this_thread::sleep_for(std::chrono::seconds(1));
  564. }
  565. for (int x = 0; x < (dealing ? 28 : 29); x++) {
  566. int cx, cy, level;
  567. cardPosLevel(x, cx, cy, level);
  568. // This is hardly visible.
  569. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  570. if (dealing)
  571. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  572. if (dealing) {
  573. c = dp.back(level);
  574. c->set(cx + off_x, cy + off_y);
  575. door << *c;
  576. } else {
  577. // redrawing -- draw the cards with their correct "state"
  578. int s = state.at(x);
  579. switch (s) {
  580. case 0:
  581. c = dp.back(level);
  582. c->set(cx + off_x, cy + off_y);
  583. door << *c;
  584. break;
  585. case 1:
  586. // cardPosLevel(x, space, height, cx, cy, level);
  587. if (x == 28)
  588. c = dp.card(deck.at(play_card));
  589. else
  590. c = dp.card(deck.at(x));
  591. c->set(cx + off_x, cy + off_y);
  592. door << *c;
  593. break;
  594. case 2:
  595. // no card to draw. :)
  596. break;
  597. case 3:
  598. // peak cleared, draw bonus
  599. door << door::Goto(cx + off_x, cy + off_y);
  600. bonus();
  601. break;
  602. }
  603. }
  604. }
  605. if (dealing)
  606. for (int x = 18; x < 29; x++) {
  607. int cx, cy;
  608. state.at(x) = 1;
  609. cardPos(x, cx, cy);
  610. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  611. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  612. c = dp.card(deck.at(x));
  613. c->set(cx + off_x, cy + off_y);
  614. door << *c;
  615. }
  616. {
  617. int cx, cy;
  618. cardPos(select_card, cx, cy);
  619. c = dp.marker(1);
  620. c->set(cx + off_x + 2, cy + off_y + 2);
  621. door << *c;
  622. }
  623. }
  624. door::renderFunction PlayCards::statusValue(door::ANSIColor status,
  625. door::ANSIColor value) {
  626. door::renderFunction rf = [status,
  627. value](const std::string &txt) -> door::Render {
  628. door::Render r(txt);
  629. door::ColorOutput co;
  630. co.pos = 0;
  631. co.len = 0;
  632. co.c = status;
  633. size_t pos = txt.find(':');
  634. if (pos == std::string::npos) {
  635. // failed to find :, render digits/numbers in value color
  636. int tpos = 0;
  637. for (char const &c : txt) {
  638. if (std::isdigit(c)) {
  639. if (co.c != value) {
  640. r.outputs.push_back(co);
  641. co.reset();
  642. co.pos = tpos;
  643. co.c = value;
  644. }
  645. } else {
  646. if (co.c != status) {
  647. r.outputs.push_back(co);
  648. co.reset();
  649. co.pos = tpos;
  650. co.c = status;
  651. }
  652. }
  653. co.len++;
  654. tpos++;
  655. }
  656. if (co.len != 0)
  657. r.outputs.push_back(co);
  658. } else {
  659. pos++; // Have : in status color
  660. co.len = pos;
  661. r.outputs.push_back(co);
  662. co.reset();
  663. co.pos = pos;
  664. co.c = value;
  665. co.len = txt.length() - pos;
  666. r.outputs.push_back(co);
  667. }
  668. return r;
  669. };
  670. return rf;
  671. }
  672. /**
  673. * @brief make the score panel
  674. * This displays: Name, Score, Time Used, Hands Played.
  675. *
  676. * @return std::unique_ptr<door::Panel>
  677. */
  678. std::unique_ptr<door::Panel> PlayCards::make_score_panel() {
  679. const int W = 25;
  680. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  681. p->setStyle(door::BorderStyle::NONE);
  682. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  683. door::ATTR::BOLD);
  684. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  685. door::ATTR::BOLD);
  686. door::renderFunction svRender = statusValue(statusColor, valueColor);
  687. // or use renderStatus as defined above.
  688. // We'll stick with these for now.
  689. {
  690. std::string userString = "Name: ";
  691. userString += door.username;
  692. door::Line username(userString, W);
  693. username.setRender(svRender);
  694. p->addLine(std::make_unique<door::Line>(username));
  695. }
  696. {
  697. door::updateFunction scoreUpdate = [this](void) -> std::string {
  698. std::string text = "Score: ";
  699. text.append(std::to_string(score));
  700. return text;
  701. };
  702. std::string scoreString = scoreUpdate();
  703. door::Line scoreline(scoreString, W);
  704. scoreline.setRender(svRender);
  705. scoreline.setUpdater(scoreUpdate);
  706. p->addLine(std::make_unique<door::Line>(scoreline));
  707. }
  708. {
  709. door::updateFunction timeUpdate = [this](void) -> std::string {
  710. std::stringstream ss;
  711. std::string text;
  712. ss << "Time used: " << setw(3) << door.time_used << " / " << setw(3)
  713. << door.time_left;
  714. text = ss.str();
  715. return text;
  716. };
  717. std::string timeString = timeUpdate();
  718. door::Line time(timeString, W);
  719. time.setRender(svRender);
  720. time.setUpdater(timeUpdate);
  721. p->addLine(std::make_unique<door::Line>(time));
  722. }
  723. {
  724. door::updateFunction handUpdate = [this](void) -> std::string {
  725. std::string text = "Playing Hand ";
  726. text.append(std::to_string(hand));
  727. text.append(" of ");
  728. text.append(std::to_string(total_hands));
  729. return text;
  730. };
  731. std::string handString = handUpdate();
  732. door::Line hands(handString, W);
  733. hands.setRender(svRender);
  734. hands.setUpdater(handUpdate);
  735. p->addLine(std::make_unique<door::Line>(hands));
  736. }
  737. return p;
  738. }
  739. std::unique_ptr<door::Panel> PlayCards::make_streak_panel(void) {
  740. const int W = 20;
  741. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  742. p->setStyle(door::BorderStyle::NONE);
  743. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  744. door::ATTR::BOLD);
  745. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  746. door::ATTR::BOLD);
  747. door::renderFunction svRender = statusValue(statusColor, valueColor);
  748. {
  749. std::string text = "Playing: ";
  750. auto in_time_t = std::chrono::system_clock::to_time_t(play_day);
  751. std::stringstream ss;
  752. if (config["date_format"]) {
  753. std::string fmt = config["date_format"].as<std::string>();
  754. ss << std::put_time(std::localtime(&in_time_t), fmt.c_str());
  755. } else
  756. ss << std::put_time(std::localtime(&in_time_t), "%B %d");
  757. text.append(ss.str());
  758. door::Line current(text, W);
  759. current.setRender(svRender);
  760. p->addLine(std::make_unique<door::Line>(current));
  761. }
  762. {
  763. door::updateFunction currentUpdate = [this](void) -> std::string {
  764. std::string text = "Current Streak: ";
  765. text.append(std::to_string(current_streak));
  766. return text;
  767. };
  768. std::string currentString = currentUpdate();
  769. door::Line current(currentString, W);
  770. current.setRender(svRender);
  771. current.setUpdater(currentUpdate);
  772. p->addLine(std::make_unique<door::Line>(current));
  773. }
  774. {
  775. door::updateFunction currentUpdate = [this](void) -> std::string {
  776. std::string text = "Longest Streak: ";
  777. text.append(std::to_string(best_streak));
  778. return text;
  779. };
  780. std::string currentString = currentUpdate();
  781. door::Line current(currentString, W);
  782. current.setRender(svRender);
  783. current.setUpdater(currentUpdate);
  784. p->addLine(std::make_unique<door::Line>(current));
  785. }
  786. return p;
  787. }
  788. std::unique_ptr<door::Panel> PlayCards::make_left_panel(void) {
  789. const int W = 13;
  790. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  791. p->setStyle(door::BorderStyle::NONE);
  792. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  793. door::ATTR::BOLD);
  794. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  795. door::ATTR::BOLD);
  796. door::renderFunction svRender = statusValue(statusColor, valueColor);
  797. {
  798. door::updateFunction cardsleftUpdate = [this](void) -> std::string {
  799. std::string text = "Cards left:";
  800. text.append(std::to_string(51 - play_card));
  801. return text;
  802. };
  803. std::string cardsleftString = "Cards left:--";
  804. door::Line cardsleft(cardsleftString, W);
  805. cardsleft.setRender(svRender);
  806. cardsleft.setUpdater(cardsleftUpdate);
  807. p->addLine(std::make_unique<door::Line>(cardsleft));
  808. }
  809. return p;
  810. }
  811. door::renderFunction PlayCards::commandLineRender(door::ANSIColor bracket,
  812. door::ANSIColor inner,
  813. door::ANSIColor outer) {
  814. door::renderFunction rf = [bracket, inner,
  815. outer](const std::string &txt) -> door::Render {
  816. door::Render r(txt);
  817. door::ColorOutput co;
  818. co.pos = 0;
  819. co.len = 0;
  820. co.c = outer;
  821. bool inOuter = true;
  822. int tpos = 0;
  823. for (char const &c : txt) {
  824. if (inOuter) {
  825. // we're in the outer text
  826. if (co.c != outer) {
  827. if (co.len != 0) {
  828. r.outputs.push_back(co);
  829. co.reset();
  830. co.pos = tpos;
  831. }
  832. co.c = outer;
  833. }
  834. // check for [
  835. if (c == '[') {
  836. if (co.len != 0) {
  837. r.outputs.push_back(co);
  838. co.reset();
  839. co.pos = tpos;
  840. }
  841. inOuter = false;
  842. co.c = bracket;
  843. }
  844. } else {
  845. // We're not in the outer.
  846. if (co.c != inner) {
  847. if (co.len != 0) {
  848. r.outputs.push_back(co);
  849. co.reset();
  850. co.pos = tpos;
  851. }
  852. co.c = inner;
  853. }
  854. if (c == ']') {
  855. if (co.len != 0) {
  856. r.outputs.push_back(co);
  857. co.reset();
  858. co.pos = tpos;
  859. }
  860. inOuter = true;
  861. co.c = bracket;
  862. }
  863. }
  864. co.len++;
  865. tpos++;
  866. }
  867. if (co.len != 0)
  868. r.outputs.push_back(co);
  869. return r;
  870. };
  871. return rf;
  872. }
  873. std::unique_ptr<door::Panel> PlayCards::make_command_panel(void) {
  874. const int W = 76;
  875. std::unique_ptr<door::Panel> p = make_unique<door::Panel>(W);
  876. p->setStyle(door::BorderStyle::NONE);
  877. std::string commands;
  878. if (door::unicode) {
  879. commands = "[4/\u25c4] Left [6/\u25ba] Right [Space] Play Card [Enter] "
  880. "Draw [Q]uit "
  881. "[R]edraw [H]elp";
  882. } else {
  883. commands =
  884. "[4/\x11] Left [6/\x10] Right [Space] Play Card [Enter] Draw [Q]uit "
  885. "[R]edraw [H]elp";
  886. }
  887. door::ANSIColor bracketColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  888. door::ATTR::BOLD);
  889. door::ANSIColor innerColor(door::COLOR::CYAN, door::COLOR::BLUE,
  890. door::ATTR::BOLD);
  891. door::ANSIColor outerColor(door::COLOR::GREEN, door::COLOR::BLUE,
  892. door::ATTR::BOLD);
  893. door::renderFunction cmdRender =
  894. commandLineRender(bracketColor, innerColor, outerColor);
  895. door::Line cmd(commands, W);
  896. cmd.setRender(cmdRender);
  897. p->addLine(std::make_unique<door::Line>(cmd));
  898. return p;
  899. }
  900. std::unique_ptr<door::Panel> PlayCards::make_next_panel(void) {
  901. const int W = 50;
  902. std::unique_ptr<door::Panel> p = make_unique<door::Panel>(W);
  903. door::ANSIColor panelColor(door::COLOR::YELLOW, door::COLOR::GREEN,
  904. door::ATTR::BOLD);
  905. p->setStyle(door::BorderStyle::DOUBLE);
  906. p->setColor(panelColor);
  907. door::updateFunction nextUpdate = [this](void) -> std::string {
  908. std::string text;
  909. if (select_card == -1) {
  910. // winner
  911. if (hand < total_hands) {
  912. text = " [N]ext Hand - [Q]uit";
  913. } else
  914. text = " All hands played - [Q]uit";
  915. } else if (hand < total_hands) {
  916. text = " [C]ontinue this hand - [N]ext Hand - [Q]uit";
  917. } else {
  918. text = " [C]ontinue this hand - [Q]uit";
  919. }
  920. return text;
  921. };
  922. std::string text;
  923. text = nextUpdate();
  924. door::ANSIColor bracketColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  925. door::ATTR::BOLD);
  926. door::ANSIColor innerColor(door::COLOR::CYAN, door::COLOR::BLUE,
  927. door::ATTR::BOLD);
  928. door::ANSIColor outerColor(door::COLOR::GREEN, door::COLOR::BLUE,
  929. door::ATTR::BOLD);
  930. door::renderFunction textRender =
  931. commandLineRender(bracketColor, innerColor, outerColor);
  932. door::Line nextLine(text, W);
  933. nextLine.setRender(textRender);
  934. nextLine.setPadding(" ", panelColor);
  935. nextLine.setUpdater(nextUpdate);
  936. nextLine.fit();
  937. p->addLine(std::make_unique<door::Line>(nextLine));
  938. return p;
  939. }
  940. std::unique_ptr<door::Panel> PlayCards::make_tripeaks(void) {
  941. std::string tripeaksText(" " SPACEACE
  942. " - Tri-Peaks Solitaire v" SPACEACE_VERSION " ");
  943. std::unique_ptr<door::Panel> spaceAceTriPeaks =
  944. std::make_unique<door::Panel>(tripeaksText.size());
  945. spaceAceTriPeaks->setStyle(door::BorderStyle::SINGLE);
  946. spaceAceTriPeaks->setColor(
  947. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLACK));
  948. spaceAceTriPeaks->addLine(
  949. std::make_unique<door::Line>(tripeaksText, tripeaksText.size()));
  950. return spaceAceTriPeaks;
  951. }
  952. std::shared_ptr<door::Panel> PlayCards::make_month(std::string month) {
  953. const int W = 3;
  954. std::shared_ptr<door::Panel> p = make_shared<door::Panel>(W);
  955. door::ANSIColor panelColor(door::COLOR::YELLOW, door::COLOR::BLACK,
  956. door::ATTR::BOLD);
  957. p->setStyle(door::BorderStyle::DOUBLE);
  958. p->setColor(panelColor);
  959. for (auto c : month) {
  960. std::string text = " ";
  961. text += c;
  962. text += " ";
  963. door::Line line(text);
  964. p->addLine(std::make_unique<door::Line>(line));
  965. }
  966. return p;
  967. }
  968. std::shared_ptr<door::Panel> PlayCards::make_weekdays() {
  969. const int W = 41;
  970. std::string text = " SUN MON TUE WED THU FRI SAT ";
  971. std::shared_ptr<door::Panel> p = make_shared<door::Panel>(W);
  972. door::ANSIColor panelColor(door::COLOR::CYAN, door::COLOR::BLACK,
  973. door::ATTR::BOLD);
  974. p->setStyle(door::BorderStyle::DOUBLE);
  975. p->setColor(panelColor);
  976. door::Line line(text);
  977. p->addLine(std::make_unique<door::Line>(line));
  978. return p;
  979. }
  980. /**
  981. * @brief make calendar
  982. * We assume the calendar is for this month now()
  983. * Jaunary
  984. * February
  985. * March
  986. * April
  987. * May
  988. * June
  989. * July
  990. * August
  991. * September
  992. * October
  993. * November
  994. * December
  995. * 123456789012345678901234567890123456789012345678901234567890
  996. * ▒▒░▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀░▒▒
  997. * N ▒▒▌SUN MON TUE WED THU FRI SAT▐▒▒
  998. * O ▒▒░───▄───▄───▄───▄───▄───▄───░▒▒
  999. * V ▒▒▌ 1x│ 2x│ 3o│ 4o│ 5o│ 6o│ 7o▐▒▒ X = Day Completed
  1000. * E ▒▒▌ 8o│ 9x│10o│11o│12x│13x│14o▐▒▒ O = Day Playable
  1001. * M ▒▒▌15x│16u│17u│18u│19u│20u│21u▐▒▒ U = Day Unavailable
  1002. * B ▒▒▌22u│23u│24u│25u│26u│27u│28u▐▒▒ H = Day Uncompleted
  1003. * E ▒▒▌29u│30u│ │ │ │ │ ▐▒▒
  1004. * R ▒▒▌ │ │ │ │ │ │ ▐▒▒
  1005. * ▒▒░▄▄▄█▄▄▄█▄▄▄█▄▄▄█▄▄▄█▄▄▄█▄▄▄░▒▒
  1006. *
  1007. * 123456789012345678901234567890123456789012345678901234567890123456789012345
  1008. *
  1009. * ▒▒░▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀░▒▒
  1010. * N ▒▒▌ SUN MON TUE WED THU FRI SAT ▐▒▒
  1011. * O ▒▒░─────▄─────▄─────▄─────▄─────▄─────▄─────░▒▒
  1012. * V ▒▒▌ 1x │ 2x │ 3o │ 4o │ 5o │ 6o │ 7o ▐▒▒ X = Day Completed
  1013. * E ▒▒▌ 8o │ 9x │ 10o │ 11o │ 12x │ 13x │ 14o ▐▒▒ O = Day Playable
  1014. * M ▒▒▌ 15x │ 16u │ 17u │ 18u │ 19u │ 20u │ 21u ▐▒▒ U = Day Unavailable
  1015. * B ▒▒▌ 22u │ 23u │ 24u │ 25u │ 26u │ 27u │ 28u ▐▒▒ H = Day Uncompleted
  1016. * E ▒▒▌ 29u │ 30u │ │ │ │ │ ▐▒▒
  1017. * R ▒▒▌ │ │ │ │ │ │ ▐▒▒
  1018. * ▒▒░▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄░▒▒
  1019. * X Extra Days Allowed Per Day
  1020. *
  1021. * 123456789012345678901234567890123456789012345678901234567890123456789012345
  1022. * ╔═══╗ ╔═════════════════════════════════════════╗
  1023. * ║ N ║ ║ SUN MON TUE WED THU FRI SAT ║
  1024. * ║ O ║ ╚═════════════════════════════════════════╝
  1025. * ║ V ║ ╔═════════════════════════════════════════╗
  1026. * ║ E ║ ║ 1x 2x 3o 4o 5o 6o 7o ║
  1027. * ║ M ║ ║ 8x 9x 10o 11o 12o 13o 14o ║
  1028. * ║ B ║ ║ 15h 16h 17h 18h 19u 20u 21u ║
  1029. * ║ E ║ ║ 22u 23u 24u 25u 26u 27u 28u ║
  1030. * ║ R ║ ║ 29u 30u 31u ║
  1031. * ╚═══╝ ║ ║
  1032. * ╚═════════════════════════════════════════╝
  1033. *
  1034. *
  1035. *
  1036. *
  1037. * @return std::unique_ptr<door::Panel>
  1038. */
  1039. /**
  1040. * make_calendar
  1041. *
  1042. * This needs the screen size information so it can place the
  1043. * panels in the correct location.
  1044. */
  1045. std::unique_ptr<door::Screen> PlayCards::make_calendar() {
  1046. std::unique_ptr<door::Screen> s = std::make_unique<door::Screen>();
  1047. auto month = std::chrono::system_clock::now();
  1048. time_t month_t = std::chrono::system_clock::to_time_t(month);
  1049. // adjust to first day of the month
  1050. std::tm *month_lt = localtime(&month_t);
  1051. if (month_lt->tm_mday > 1) {
  1052. month -= 24h * (month_lt->tm_mday - 1);
  1053. }
  1054. normalizeDate(month);
  1055. month_t = std::chrono::system_clock::to_time_t(month);
  1056. get_logger() << "Month is "
  1057. << std::put_time(std::localtime(&month_t), "%c %Z") << std::endl;
  1058. std::shared_ptr<door::Panel> p = make_month("DECEMBER");
  1059. p->set(3, 3);
  1060. s->addPanel(p);
  1061. p = make_weekdays();
  1062. p->set(8, 3);
  1063. s->addPanel(p);
  1064. /*
  1065. const int W = 72;
  1066. p->setStyle(door::BorderStyle::NONE);
  1067. // Ok, that is working. I'm getting the first day of the month. So...
  1068. store the time_t for the date.
  1069. store the day in the column it needs to be in.
  1070. store any hands played (pull data from the db).
  1071. seems like this should be its own class, there's a lot of data that is
  1072. specific just to it.
  1073. */
  1074. /*
  1075. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  1076. door::ATTR::BOLD);
  1077. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  1078. door::ATTR::BOLD);
  1079. door::renderFunction svRender = statusValue(statusColor, valueColor);
  1080. // or use renderStatus as defined above.
  1081. // We'll stick with these for now.
  1082. {
  1083. std::string userString = "Name: ";
  1084. userString += door.username;
  1085. door::Line username(userString, W);
  1086. username.setRender(svRender);
  1087. p->addLine(std::make_unique<door::Line>(username));
  1088. }
  1089. {
  1090. door::updateFunction scoreUpdate = [this](void) -> std::string {
  1091. std::string text = "Score: ";
  1092. text.append(std::to_string(score));
  1093. return text;
  1094. };
  1095. std::string scoreString = scoreUpdate();
  1096. door::Line scoreline(scoreString, W);
  1097. scoreline.setRender(svRender);
  1098. scoreline.setUpdater(scoreUpdate);
  1099. p->addLine(std::make_unique<door::Line>(scoreline));
  1100. }
  1101. {
  1102. door::updateFunction timeUpdate = [this](void) -> std::string {
  1103. std::stringstream ss;
  1104. std::string text;
  1105. ss << "Time used: " << setw(3) << door.time_used << " / " << setw(3)
  1106. << door.time_left;
  1107. text = ss.str();
  1108. return text;
  1109. };
  1110. std::string timeString = timeUpdate();
  1111. door::Line time(timeString, W);
  1112. time.setRender(svRender);
  1113. time.setUpdater(timeUpdate);
  1114. p->addLine(std::make_unique<door::Line>(time));
  1115. }
  1116. {
  1117. door::updateFunction handUpdate = [this](void) -> std::string {
  1118. std::string text = "Playing Hand ";
  1119. text.append(std::to_string(hand));
  1120. text.append(" of ");
  1121. text.append(std::to_string(total_hands));
  1122. return text;
  1123. };
  1124. std::string handString = handUpdate();
  1125. door::Line hands(handString, W);
  1126. hands.setRender(svRender);
  1127. hands.setUpdater(handUpdate);
  1128. p->addLine(std::make_unique<door::Line>(hands));
  1129. }
  1130. */
  1131. return s;
  1132. }