play.cpp 35 KB

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