play.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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::unique_ptr<door::Screen> calendar = make_calendar();
  115. door << door::reset << door::cls;
  116. door << *calendar;
  117. int r = press_a_key();
  118. if (r < 0) // timeout! exit!
  119. return r;
  120. // return 0;
  121. hand = 1;
  122. // possibly init_values()
  123. play_day = std::chrono::system_clock::now();
  124. normalizeDate(play_day);
  125. return play_cards();
  126. }
  127. /**
  128. * @brief play_cards
  129. *
  130. * Play cards for the given play_day and hand.
  131. *
  132. * This should be called by play with the correct #play_day set.
  133. * \sa PlayCards::play()
  134. *
  135. * @return int
  136. */
  137. int PlayCards::play_cards(void) {
  138. init_values();
  139. // Calculate the game size
  140. int game_width;
  141. int game_height = 20;
  142. {
  143. int cx, cy;
  144. cardPos(27, cx, cy);
  145. game_width = cx + 5;
  146. }
  147. int mx = door.width;
  148. int my = door.height;
  149. off_x = (mx - game_width) / 2;
  150. off_y = (my - game_height) / 2;
  151. // int true_off_y = off_y;
  152. // we can now position things properly centered
  153. int tp_off_x = (mx - spaceAceTriPeaks->getWidth()) / 2;
  154. spaceAceTriPeaks->set(tp_off_x, off_y);
  155. off_y += 3; // adjust for tripeaks panel
  156. next_hand:
  157. // Make sure we pick the deck color here. We want it to (possibly) change
  158. // between hands.
  159. std::string currentDefault = db.getSetting("DeckColor", "ALL");
  160. get_logger() << "DeckColor shows as " << currentDefault << std::endl;
  161. deck_color = stringToANSIColor(currentDefault);
  162. dp = Deck(deck_color);
  163. play_card = 28;
  164. select_card = 23;
  165. score = 0;
  166. current_streak = 0;
  167. // Use play_day to seed the rng
  168. {
  169. time_t tt = std::chrono::system_clock::to_time_t(play_day);
  170. tm local_tm = *localtime(&tt);
  171. std::seed_seq seq{local_tm.tm_year + 1900, local_tm.tm_mon + 1,
  172. local_tm.tm_mday, hand};
  173. deck = shuffleCards(seq, 1);
  174. state = makeCardStates();
  175. }
  176. /*
  177. door::Panel score_panel = make_score_panel();
  178. door::Panel streak_panel = make_streak_panel();
  179. door::Panel left_panel = make_left_panel();
  180. door::Panel cmd_panel = make_command_panel();
  181. */
  182. {
  183. int off_yp = off_y + 11;
  184. int cx, cy;
  185. int left_panel_x, right_panel_x;
  186. // find position of card, to position the panels
  187. cardPos(18, cx, cy);
  188. left_panel_x = cx;
  189. cardPos(15, cx, cy);
  190. right_panel_x = cx;
  191. score_panel->set(left_panel_x + off_x, off_yp);
  192. streak_panel->set(right_panel_x + off_x, off_yp);
  193. cmd_panel->set(left_panel_x + off_x, off_yp + 5);
  194. // next panel position (top = card 10, centered)
  195. cardPos(10, cx, cy);
  196. int next_off_x = (mx - next_quit_panel->getWidth()) / 2;
  197. next_quit_panel->set(next_off_x, cy + off_y);
  198. }
  199. bool dealing = true;
  200. int r = 0;
  201. redraw(dealing);
  202. dealing = false;
  203. left_panel->update(door);
  204. door << door::reset;
  205. shared_panel c;
  206. bool in_game = true;
  207. bool save_streak = false;
  208. while (in_game) {
  209. // time might have updated, so update score panel too.
  210. score_panel->update(door);
  211. // do the save here -- try to hide the database lag
  212. if (save_streak) {
  213. save_streak = false;
  214. std::string best = std::to_string(best_streak);
  215. db.setSetting("best_streak", best);
  216. }
  217. r = door.sleep_key(door.inactivity);
  218. if (r > 0) {
  219. // not a timeout or expire.
  220. if (r < 0x1000) // not a function key
  221. r = std::toupper(r);
  222. switch (r) {
  223. case '\x0d':
  224. if (current_streak > best_streak) {
  225. best_streak = current_streak;
  226. if (!config[CHEATER]) {
  227. save_streak = true;
  228. }
  229. streak_panel->update(door);
  230. }
  231. if (play_card < 51) {
  232. play_card++;
  233. current_streak = 0;
  234. streak_panel->update(door);
  235. // update the cards left_panel
  236. left_panel->update(door);
  237. // Ok, deal next card from the pile.
  238. int cx, cy, level;
  239. if (play_card == 51) {
  240. cardPosLevel(29, cx, cy, level);
  241. level = 0; // out of cards
  242. c = dp.back(level);
  243. c->set(cx + off_x, cy + off_y);
  244. door << *c;
  245. }
  246. cardPos(28, cx, cy);
  247. c = dp.card(deck.at(play_card));
  248. c->set(cx + off_x, cy + off_y);
  249. door << *c;
  250. }
  251. break;
  252. case 'R':
  253. redraw(false);
  254. break;
  255. case 'Q':
  256. // possibly prompt here for [N]ext hand or [Q]uit ?
  257. if (current_streak > best_streak) {
  258. best_streak = current_streak;
  259. if (!config[CHEATER]) {
  260. save_streak = true;
  261. }
  262. streak_panel->update(door);
  263. }
  264. next_quit_panel->update();
  265. door << *next_quit_panel;
  266. if (hand < total_hands) {
  267. r = door.get_one_of("CQN");
  268. } else {
  269. r = door.get_one_of("CQ");
  270. }
  271. if (r == 0) {
  272. // continue
  273. redraw(false);
  274. break;
  275. }
  276. if (r == 1) {
  277. // Ok, we are calling it quits.
  278. // save score if > 0
  279. if (score >= 50) {
  280. time_t right_now = std::chrono::system_clock::to_time_t(
  281. std::chrono::system_clock::now());
  282. db.saveScore(right_now,
  283. std::chrono::system_clock::to_time_t(play_day), hand,
  284. 0, score);
  285. }
  286. in_game = false;
  287. r = 'Q';
  288. }
  289. if (r == 2) {
  290. // no. If you want to play the next hand, we have to save this.
  291. get_logger() << "SCORE: " << score << std::endl;
  292. time_t right_now = std::chrono::system_clock::to_time_t(
  293. std::chrono::system_clock::now());
  294. db.saveScore(right_now,
  295. std::chrono::system_clock::to_time_t(play_day), hand, 0,
  296. score);
  297. hand++;
  298. goto next_hand;
  299. }
  300. // in_game = false;
  301. break;
  302. case ' ':
  303. case '5':
  304. // can we play this card?
  305. /*
  306. get_logger() << "canPlay( " << select_card << ":"
  307. << deck1.at(select_card) << "/"
  308. << d.getRank(deck1.at(select_card)) << " , "
  309. << play_card << "/" <<
  310. d.getRank(deck1.at(play_card))
  311. << ") = "
  312. << d.canPlay(deck1.at(select_card),
  313. deck1.at(play_card))
  314. << std::endl;
  315. */
  316. if (dp.canPlay(deck.at(select_card), deck.at(play_card)) or
  317. config[CHEATER]) {
  318. // if (true) {
  319. // yes we can.
  320. ++current_streak;
  321. // update best_streak when they draw the next card.
  322. /*
  323. if (current_streak > best_streak) {
  324. best_streak = current_streak;
  325. if (!config[CHEATER]) {
  326. save_streak = true;
  327. }
  328. }
  329. */
  330. streak_panel->update(door);
  331. score += 10;
  332. if (current_streak > 1)
  333. score += current_streak * 5;
  334. score_panel->update(door);
  335. /*
  336. if (get_logger)
  337. get_logger() << "score_panel update : " << score << std::endl;
  338. */
  339. // play card!
  340. state.at(select_card) = 2;
  341. {
  342. // swap the select card with play_card
  343. int temp = deck.at(select_card);
  344. deck.at(select_card) = deck.at(play_card);
  345. deck.at(play_card) = temp;
  346. // select_card is -- invalidated here! find "new" card.
  347. int cx, cy;
  348. // erase/clear select_card
  349. std::vector<int> check = dp.unblocks(select_card);
  350. bool left = false, right = false;
  351. for (const int c : check) {
  352. std::pair<int, int> blockers = dp.blocks[c];
  353. if (blockers.first == select_card)
  354. right = true;
  355. if (blockers.second == select_card)
  356. left = true;
  357. }
  358. dp.removeCard(door, select_card, off_x, off_y, left, right);
  359. /* // old way of doing this that leaves holes.
  360. cardPosLevel(select_card, cx, cy, level);
  361. c = d.back(0);
  362. c->set(cx + off_x, cy + off_y);
  363. door << *c;
  364. */
  365. // redraw play card #28. (Which is the "old" select_card)
  366. cardPos(28, cx, cy);
  367. c = dp.card(deck.at(play_card));
  368. c->set(cx + off_x, cy + off_y);
  369. door << *c;
  370. // Did we unhide a card here?
  371. // std::vector<int> check = d.unblocks(select_card);
  372. /*
  373. get_logger() << "select_card = " << select_card
  374. << " unblocks: " << check.size() << std::endl;
  375. */
  376. int new_card_shown = -1;
  377. if (!check.empty()) {
  378. for (const int to_check : check) {
  379. std::pair<int, int> blockers = dp.blocks[to_check];
  380. /*
  381. get_logger()
  382. << "Check: " << to_check << " " << blockers.first << ","
  383. << blockers.second << " " << state.at(blockers.first)
  384. << "," << state.at(blockers.second) << std::endl;
  385. */
  386. if ((state.at(blockers.first) == 2) and
  387. (state.at(blockers.second) == 2)) {
  388. // WOOT! Card uncovered.
  389. /*
  390. get_logger() << "showing: " << to_check << std::endl;
  391. */
  392. state.at(to_check) = 1;
  393. cardPos(to_check, cx, cy);
  394. c = dp.card(deck.at(to_check));
  395. c->set(cx + off_x, cy + off_y);
  396. door << *c;
  397. new_card_shown = to_check;
  398. }
  399. }
  400. } else {
  401. // top card cleared
  402. // get_logger() << "top card cleared?" << std::endl;
  403. // display something at select_card position
  404. int cx, cy;
  405. cardPos(select_card, cx, cy);
  406. door << door::Goto(cx + off_x, cy + off_y);
  407. bonus();
  408. score += 100;
  409. state.at(select_card) = 3; // handle this in the "redraw"
  410. score_panel->update(door);
  411. }
  412. // Find new "number" for select_card to be.
  413. if (new_card_shown != -1) {
  414. select_card = new_card_shown;
  415. } else {
  416. // select_card++;
  417. int new_select = findClosestActiveCard(state, select_card);
  418. if (new_select != -1) {
  419. select_card = new_select;
  420. } else {
  421. get_logger() << "Winner!" << std::endl;
  422. select_card = -1; // winner marker?
  423. // bonus for cards left
  424. int bonus = 15 * (51 - play_card);
  425. score += 15 * (51 - play_card);
  426. score_panel->update(door);
  427. // maybe display this somewhere?
  428. // door << " BONUS: " << bonus << door::nl;
  429. get_logger()
  430. << "SCORE: " << score << ", " << bonus << std::endl;
  431. // if (!config[CHEATER]) {
  432. time_t right_now = std::chrono::system_clock::to_time_t(
  433. std::chrono::system_clock::now());
  434. db.saveScore(right_now,
  435. std::chrono::system_clock::to_time_t(play_day),
  436. hand, 1, score);
  437. //}
  438. next_quit_panel->update();
  439. door << *next_quit_panel;
  440. if (hand < total_hands) {
  441. r = door.get_one_of("QN");
  442. } else {
  443. r = door.get_one_of("Q");
  444. }
  445. if (r == 1) {
  446. hand++;
  447. goto next_hand;
  448. }
  449. if (r == 0) {
  450. r = 'Q';
  451. }
  452. /*
  453. press_a_key(door);
  454. if (hand < total_hands) {
  455. hand++;
  456. // current_streak = 0;
  457. door << door::reset << door::cls;
  458. goto next_hand;
  459. }
  460. r = 'Q';
  461. */
  462. in_game = false;
  463. }
  464. }
  465. // update the select_card marker!
  466. cardPos(select_card, cx, cy);
  467. c = dp.marker(1);
  468. c->set(cx + off_x + 2, cy + off_y + 2);
  469. door << *c;
  470. }
  471. }
  472. break;
  473. case XKEY_LEFT_ARROW:
  474. case '4': {
  475. int new_select = findNextActiveCard(true, state, select_card);
  476. /*
  477. int new_active = active_card - 1;
  478. while (new_active >= 0) {
  479. if (state.at(new_active) == 1)
  480. break;
  481. --new_active;
  482. }*/
  483. if (new_select >= 0) {
  484. int cx, cy;
  485. cardPos(select_card, cx, cy);
  486. c = dp.marker(0);
  487. c->set(cx + off_x + 2, cy + off_y + 2);
  488. door << *c;
  489. select_card = new_select;
  490. cardPos(select_card, cx, cy);
  491. c = dp.marker(1);
  492. c->set(cx + off_x + 2, cy + off_y + 2);
  493. door << *c;
  494. }
  495. } break;
  496. case XKEY_RIGHT_ARROW:
  497. case '6': {
  498. int new_select = findNextActiveCard(false, state, select_card);
  499. /*
  500. int new_active = active_card + 1;
  501. while (new_active < 28) {
  502. if (state.at(new_active) == 1)
  503. break;
  504. ++new_active;
  505. }
  506. */
  507. if (new_select >= 0) { //(new_active < 28) {
  508. int cx, cy;
  509. cardPos(select_card, cx, cy);
  510. c = dp.marker(0);
  511. c->set(cx + off_x + 2, cy + off_y + 2);
  512. door << *c;
  513. select_card = new_select;
  514. cardPos(select_card, cx, cy);
  515. c = dp.marker(1);
  516. c->set(cx + off_x + 2, cy + off_y + 2);
  517. door << *c;
  518. }
  519. }
  520. break;
  521. }
  522. } else
  523. in_game = false;
  524. }
  525. if (r == 'Q') {
  526. // continue, play next hand (if applicable), or quit?
  527. // if score < 50, don't bother saving.
  528. // continue -- eat r & redraw.
  529. // quit, save score and exit (unless score is zero).
  530. }
  531. return r;
  532. }
  533. /**
  534. * @brief Redraw the entire play cards screen.
  535. *
  536. * If dealing then show delays when displaying cards, or turning them over.
  537. *
  538. * Otherwise, the user has requested a redraw -- and don't delay.
  539. *
  540. * @param dealing
  541. */
  542. void PlayCards::redraw(bool dealing) {
  543. shared_panel c;
  544. display_starfield(door, rng);
  545. // door << door::reset << door::cls;
  546. door << *spaceAceTriPeaks;
  547. {
  548. // step 1:
  549. // draw the deck "source"
  550. int cx, cy, level;
  551. cardPosLevel(29, cx, cy, level);
  552. if (play_card == 51)
  553. level = 0; // out of cards!
  554. c = dp.back(level);
  555. c->set(cx + off_x, cy + off_y);
  556. // p3 is heigh below
  557. left_panel->set(cx + off_x, cy + off_y + height);
  558. // how do I update these? (hand >1)
  559. score_panel->update();
  560. left_panel->update();
  561. streak_panel->update();
  562. cmd_panel->update();
  563. door << *score_panel << *left_panel << *streak_panel << *cmd_panel;
  564. door << *c;
  565. if (dealing)
  566. std::this_thread::sleep_for(std::chrono::seconds(1));
  567. }
  568. for (int x = 0; x < (dealing ? 28 : 29); x++) {
  569. int cx, cy, level;
  570. cardPosLevel(x, cx, cy, level);
  571. // This is hardly visible.
  572. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  573. if (dealing)
  574. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  575. if (dealing) {
  576. c = dp.back(level);
  577. c->set(cx + off_x, cy + off_y);
  578. door << *c;
  579. } else {
  580. // redrawing -- draw the cards with their correct "state"
  581. int s = state.at(x);
  582. switch (s) {
  583. case 0:
  584. c = dp.back(level);
  585. c->set(cx + off_x, cy + off_y);
  586. door << *c;
  587. break;
  588. case 1:
  589. // cardPosLevel(x, space, height, cx, cy, level);
  590. if (x == 28)
  591. c = dp.card(deck.at(play_card));
  592. else
  593. c = dp.card(deck.at(x));
  594. c->set(cx + off_x, cy + off_y);
  595. door << *c;
  596. break;
  597. case 2:
  598. // no card to draw. :)
  599. break;
  600. case 3:
  601. // peak cleared, draw bonus
  602. door << door::Goto(cx + off_x, cy + off_y);
  603. bonus();
  604. break;
  605. }
  606. }
  607. }
  608. if (dealing)
  609. for (int x = 18; x < 29; x++) {
  610. int cx, cy;
  611. state.at(x) = 1;
  612. cardPos(x, cx, cy);
  613. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  614. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  615. c = dp.card(deck.at(x));
  616. c->set(cx + off_x, cy + off_y);
  617. door << *c;
  618. }
  619. {
  620. int cx, cy;
  621. cardPos(select_card, cx, cy);
  622. c = dp.marker(1);
  623. c->set(cx + off_x + 2, cy + off_y + 2);
  624. door << *c;
  625. }
  626. }
  627. door::renderFunction PlayCards::statusValue(door::ANSIColor status,
  628. door::ANSIColor value) {
  629. door::renderFunction rf = [status,
  630. value](const std::string &txt) -> door::Render {
  631. door::Render r(txt);
  632. door::ColorOutput co;
  633. co.pos = 0;
  634. co.len = 0;
  635. co.c = status;
  636. size_t pos = txt.find(':');
  637. if (pos == std::string::npos) {
  638. // failed to find :, render digits/numbers in value color
  639. int tpos = 0;
  640. for (char const &c : txt) {
  641. if (std::isdigit(c)) {
  642. if (co.c != value) {
  643. r.outputs.push_back(co);
  644. co.reset();
  645. co.pos = tpos;
  646. co.c = value;
  647. }
  648. } else {
  649. if (co.c != status) {
  650. r.outputs.push_back(co);
  651. co.reset();
  652. co.pos = tpos;
  653. co.c = status;
  654. }
  655. }
  656. co.len++;
  657. tpos++;
  658. }
  659. if (co.len != 0)
  660. r.outputs.push_back(co);
  661. } else {
  662. pos++; // Have : in status color
  663. co.len = pos;
  664. r.outputs.push_back(co);
  665. co.reset();
  666. co.pos = pos;
  667. co.c = value;
  668. co.len = txt.length() - pos;
  669. r.outputs.push_back(co);
  670. }
  671. return r;
  672. };
  673. return rf;
  674. }
  675. /**
  676. * @brief make the score panel
  677. * This displays: Name, Score, Time Used, Hands Played.
  678. *
  679. * @return std::unique_ptr<door::Panel>
  680. */
  681. std::unique_ptr<door::Panel> PlayCards::make_score_panel() {
  682. const int W = 25;
  683. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  684. p->setStyle(door::BorderStyle::NONE);
  685. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  686. door::ATTR::BOLD);
  687. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  688. door::ATTR::BOLD);
  689. door::renderFunction svRender = statusValue(statusColor, valueColor);
  690. // or use renderStatus as defined above.
  691. // We'll stick with these for now.
  692. {
  693. std::string userString = "Name: ";
  694. userString += door.username;
  695. door::Line username(userString, W);
  696. username.setRender(svRender);
  697. p->addLine(std::make_unique<door::Line>(username));
  698. }
  699. {
  700. door::updateFunction scoreUpdate = [this](void) -> std::string {
  701. std::string text = "Score: ";
  702. text.append(std::to_string(score));
  703. return text;
  704. };
  705. std::string scoreString = scoreUpdate();
  706. door::Line scoreline(scoreString, W);
  707. scoreline.setRender(svRender);
  708. scoreline.setUpdater(scoreUpdate);
  709. p->addLine(std::make_unique<door::Line>(scoreline));
  710. }
  711. {
  712. door::updateFunction timeUpdate = [this](void) -> std::string {
  713. std::stringstream ss;
  714. std::string text;
  715. ss << "Time used: " << setw(3) << door.time_used << " / " << setw(3)
  716. << door.time_left;
  717. text = ss.str();
  718. return text;
  719. };
  720. std::string timeString = timeUpdate();
  721. door::Line time(timeString, W);
  722. time.setRender(svRender);
  723. time.setUpdater(timeUpdate);
  724. p->addLine(std::make_unique<door::Line>(time));
  725. }
  726. {
  727. door::updateFunction handUpdate = [this](void) -> std::string {
  728. std::string text = "Playing Hand ";
  729. text.append(std::to_string(hand));
  730. text.append(" of ");
  731. text.append(std::to_string(total_hands));
  732. return text;
  733. };
  734. std::string handString = handUpdate();
  735. door::Line hands(handString, W);
  736. hands.setRender(svRender);
  737. hands.setUpdater(handUpdate);
  738. p->addLine(std::make_unique<door::Line>(hands));
  739. }
  740. return p;
  741. }
  742. std::unique_ptr<door::Panel> PlayCards::make_streak_panel(void) {
  743. const int W = 20;
  744. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  745. p->setStyle(door::BorderStyle::NONE);
  746. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  747. door::ATTR::BOLD);
  748. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  749. door::ATTR::BOLD);
  750. door::renderFunction svRender = statusValue(statusColor, valueColor);
  751. {
  752. std::string text = "Playing: ";
  753. auto in_time_t = std::chrono::system_clock::to_time_t(play_day);
  754. std::stringstream ss;
  755. if (config["date_format"]) {
  756. std::string fmt = config["date_format"].as<std::string>();
  757. ss << std::put_time(std::localtime(&in_time_t), fmt.c_str());
  758. } else
  759. ss << std::put_time(std::localtime(&in_time_t), "%B %d");
  760. text.append(ss.str());
  761. door::Line current(text, W);
  762. current.setRender(svRender);
  763. p->addLine(std::make_unique<door::Line>(current));
  764. }
  765. {
  766. door::updateFunction currentUpdate = [this](void) -> std::string {
  767. std::string text = "Current Streak: ";
  768. text.append(std::to_string(current_streak));
  769. return text;
  770. };
  771. std::string currentString = currentUpdate();
  772. door::Line current(currentString, W);
  773. current.setRender(svRender);
  774. current.setUpdater(currentUpdate);
  775. p->addLine(std::make_unique<door::Line>(current));
  776. }
  777. {
  778. door::updateFunction currentUpdate = [this](void) -> std::string {
  779. std::string text = "Longest Streak: ";
  780. text.append(std::to_string(best_streak));
  781. return text;
  782. };
  783. std::string currentString = currentUpdate();
  784. door::Line current(currentString, W);
  785. current.setRender(svRender);
  786. current.setUpdater(currentUpdate);
  787. p->addLine(std::make_unique<door::Line>(current));
  788. }
  789. return p;
  790. }
  791. std::unique_ptr<door::Panel> PlayCards::make_left_panel(void) {
  792. const int W = 13;
  793. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  794. p->setStyle(door::BorderStyle::NONE);
  795. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  796. door::ATTR::BOLD);
  797. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  798. door::ATTR::BOLD);
  799. door::renderFunction svRender = statusValue(statusColor, valueColor);
  800. {
  801. door::updateFunction cardsleftUpdate = [this](void) -> std::string {
  802. std::string text = "Cards left:";
  803. text.append(std::to_string(51 - play_card));
  804. return text;
  805. };
  806. std::string cardsleftString = "Cards left:--";
  807. door::Line cardsleft(cardsleftString, W);
  808. cardsleft.setRender(svRender);
  809. cardsleft.setUpdater(cardsleftUpdate);
  810. p->addLine(std::make_unique<door::Line>(cardsleft));
  811. }
  812. return p;
  813. }
  814. door::renderFunction PlayCards::commandLineRender(door::ANSIColor bracket,
  815. door::ANSIColor inner,
  816. door::ANSIColor outer) {
  817. door::renderFunction rf = [bracket, inner,
  818. outer](const std::string &txt) -> door::Render {
  819. door::Render r(txt);
  820. door::ColorOutput co;
  821. co.pos = 0;
  822. co.len = 0;
  823. co.c = outer;
  824. bool inOuter = true;
  825. int tpos = 0;
  826. for (char const &c : txt) {
  827. if (inOuter) {
  828. // we're in the outer text
  829. if (co.c != outer) {
  830. if (co.len != 0) {
  831. r.outputs.push_back(co);
  832. co.reset();
  833. co.pos = tpos;
  834. }
  835. co.c = outer;
  836. }
  837. // check for [
  838. if (c == '[') {
  839. if (co.len != 0) {
  840. r.outputs.push_back(co);
  841. co.reset();
  842. co.pos = tpos;
  843. }
  844. inOuter = false;
  845. co.c = bracket;
  846. }
  847. } else {
  848. // We're not in the outer.
  849. if (co.c != inner) {
  850. if (co.len != 0) {
  851. r.outputs.push_back(co);
  852. co.reset();
  853. co.pos = tpos;
  854. }
  855. co.c = inner;
  856. }
  857. if (c == ']') {
  858. if (co.len != 0) {
  859. r.outputs.push_back(co);
  860. co.reset();
  861. co.pos = tpos;
  862. }
  863. inOuter = true;
  864. co.c = bracket;
  865. }
  866. }
  867. co.len++;
  868. tpos++;
  869. }
  870. if (co.len != 0)
  871. r.outputs.push_back(co);
  872. return r;
  873. };
  874. return rf;
  875. }
  876. std::unique_ptr<door::Panel> PlayCards::make_command_panel(void) {
  877. const int W = 76;
  878. std::unique_ptr<door::Panel> p = make_unique<door::Panel>(W);
  879. p->setStyle(door::BorderStyle::NONE);
  880. std::string commands;
  881. if (door::unicode) {
  882. commands = "[4/\u25c4] Left [6/\u25ba] Right [Space] Play Card [Enter] "
  883. "Draw [Q]uit "
  884. "[R]edraw [H]elp";
  885. } else {
  886. commands =
  887. "[4/\x11] Left [6/\x10] Right [Space] Play Card [Enter] Draw [Q]uit "
  888. "[R]edraw [H]elp";
  889. }
  890. door::ANSIColor bracketColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  891. door::ATTR::BOLD);
  892. door::ANSIColor innerColor(door::COLOR::CYAN, door::COLOR::BLUE,
  893. door::ATTR::BOLD);
  894. door::ANSIColor outerColor(door::COLOR::GREEN, door::COLOR::BLUE,
  895. door::ATTR::BOLD);
  896. door::renderFunction cmdRender =
  897. commandLineRender(bracketColor, innerColor, outerColor);
  898. door::Line cmd(commands, W);
  899. cmd.setRender(cmdRender);
  900. p->addLine(std::make_unique<door::Line>(cmd));
  901. return p;
  902. }
  903. std::unique_ptr<door::Panel> PlayCards::make_next_panel(void) {
  904. const int W = 50;
  905. std::unique_ptr<door::Panel> p = make_unique<door::Panel>(W);
  906. door::ANSIColor panelColor(door::COLOR::YELLOW, door::COLOR::GREEN,
  907. door::ATTR::BOLD);
  908. p->setStyle(door::BorderStyle::DOUBLE);
  909. p->setColor(panelColor);
  910. door::updateFunction nextUpdate = [this](void) -> std::string {
  911. std::string text;
  912. if (select_card == -1) {
  913. // winner
  914. if (hand < total_hands) {
  915. text = " [N]ext Hand - [Q]uit";
  916. } else
  917. text = " All hands played - [Q]uit";
  918. } else if (hand < total_hands) {
  919. text = " [C]ontinue this hand - [N]ext Hand - [Q]uit";
  920. } else {
  921. text = " [C]ontinue this hand - [Q]uit";
  922. }
  923. return text;
  924. };
  925. std::string text;
  926. text = nextUpdate();
  927. door::ANSIColor bracketColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  928. door::ATTR::BOLD);
  929. door::ANSIColor innerColor(door::COLOR::CYAN, door::COLOR::BLUE,
  930. door::ATTR::BOLD);
  931. door::ANSIColor outerColor(door::COLOR::GREEN, door::COLOR::BLUE,
  932. door::ATTR::BOLD);
  933. door::renderFunction textRender =
  934. commandLineRender(bracketColor, innerColor, outerColor);
  935. door::Line nextLine(text, W);
  936. nextLine.setRender(textRender);
  937. nextLine.setPadding(" ", panelColor);
  938. nextLine.setUpdater(nextUpdate);
  939. nextLine.fit();
  940. p->addLine(std::make_unique<door::Line>(nextLine));
  941. return p;
  942. }
  943. std::unique_ptr<door::Panel> PlayCards::make_tripeaks(void) {
  944. std::string tripeaksText(" " SPACEACE
  945. " - Tri-Peaks Solitaire v" SPACEACE_VERSION " ");
  946. std::unique_ptr<door::Panel> spaceAceTriPeaks =
  947. std::make_unique<door::Panel>(tripeaksText.size());
  948. spaceAceTriPeaks->setStyle(door::BorderStyle::SINGLE);
  949. spaceAceTriPeaks->setColor(
  950. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLACK));
  951. spaceAceTriPeaks->addLine(
  952. std::make_unique<door::Line>(tripeaksText, tripeaksText.size()));
  953. return spaceAceTriPeaks;
  954. }
  955. std::unique_ptr<door::Panel> PlayCards::make_month(std::string month) {
  956. const int W = 3;
  957. std::unique_ptr<door::Panel> p = make_unique<door::Panel>(W);
  958. door::ANSIColor panelColor(door::COLOR::YELLOW, door::COLOR::BLACK,
  959. door::ATTR::BOLD);
  960. p->setStyle(door::BorderStyle::DOUBLE);
  961. p->setColor(panelColor);
  962. for (auto c : month) {
  963. std::string text = " ";
  964. text += c;
  965. text += " ";
  966. door::Line line(text);
  967. p->addLine(std::make_unique<door::Line>(line));
  968. }
  969. return p;
  970. }
  971. std::unique_ptr<door::Panel> PlayCards::make_weekdays(void) {
  972. const int W = 41;
  973. std::string text = " SUN MON TUE WED THU FRI SAT ";
  974. std::unique_ptr<door::Panel> p = make_unique<door::Panel>(W);
  975. door::ANSIColor panelColor(door::COLOR::CYAN, door::COLOR::BLACK,
  976. door::ATTR::BOLD);
  977. p->setStyle(door::BorderStyle::DOUBLE);
  978. p->setColor(panelColor);
  979. door::Line line(text);
  980. p->addLine(std::make_unique<door::Line>(line));
  981. return p;
  982. }
  983. std::unique_ptr<door::Panel> PlayCards::make_calendar_panel(void) {
  984. const int W = 41;
  985. std::unique_ptr<door::Panel> p = make_unique<door::Panel>(W);
  986. p->setStyle(door::BorderStyle::DOUBLE);
  987. p->setColor(door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLACK));
  988. door::renderFunction calendarRender =
  989. [](const std::string &txt) -> door::Render {
  990. door::Render r(txt);
  991. // normal digits color
  992. door::ANSIColor digits(door::COLOR::CYAN, door::COLOR::BLACK);
  993. // digits/days that can be played
  994. door::ANSIColor digits_play(door::COLOR::CYAN, door::COLOR::BLACK,
  995. door::ATTR::BOLD);
  996. // spaces color
  997. door::ANSIColor spaces(door::COLOR::WHITE, door::COLOR::BLACK);
  998. // o - open days
  999. door::ANSIColor open(door::COLOR::GREEN, door::COLOR::BLACK,
  1000. door::ATTR::BOLD);
  1001. // h - hands can be played
  1002. door::ANSIColor hands(door::COLOR::YELLOW, door::COLOR::BLACK,
  1003. door::ATTR::BOLD);
  1004. // x - already played
  1005. door::ANSIColor full(door::COLOR::RED, door::COLOR::BLACK);
  1006. // u - No, Not Yet! unavailable
  1007. door::ANSIColor nny(door::COLOR::BLACK, door::COLOR::BLACK,
  1008. door::ATTR::BOLD);
  1009. int days;
  1010. /*
  1011. if (get_logger) {
  1012. get_logger() << "Line [" << txt << "]" << std::endl;
  1013. }
  1014. */
  1015. // B _ BBBB _ BBBB _ BBBB _ BBBB _ BBBB _ BBBB _ B
  1016. for (days = 0; days < 7; ++days) {
  1017. std::string dayText;
  1018. if (days == 0) {
  1019. r.append(spaces);
  1020. // dayText = txt.substr(1, 3);
  1021. } // else {
  1022. /*
  1023. if (get_logger)
  1024. get_logger() << days << " " << 1 + (days * 6) << std::endl;
  1025. */
  1026. dayText = txt.substr(1 + (days * 6), 3);
  1027. // }
  1028. /*
  1029. if (get_logger) {
  1030. get_logger() << days << " "
  1031. << "[" << dayText << "] " << std::endl;
  1032. }
  1033. */
  1034. if (dayText[1] == ' ') {
  1035. // Ok, nothing there!
  1036. r.append(spaces, 3);
  1037. } else {
  1038. // Something is there
  1039. char cday = dayText[2];
  1040. if ((cday == 'o') or (cday == 'h'))
  1041. r.append(digits_play, 2);
  1042. else
  1043. r.append(digits, 2);
  1044. switch (dayText[2]) {
  1045. case 'o':
  1046. r.append(open);
  1047. break;
  1048. case 'h':
  1049. r.append(hands);
  1050. break;
  1051. case 'x':
  1052. r.append(full);
  1053. break;
  1054. case 'u':
  1055. r.append(nny);
  1056. break;
  1057. }
  1058. }
  1059. if (days == 6)
  1060. r.append(spaces);
  1061. else
  1062. r.append(spaces, 3);
  1063. }
  1064. return r;
  1065. };
  1066. int row;
  1067. for (row = 0; row < 6; ++row) {
  1068. door::updateFunction calendarUpdate = [this, row]() -> std::string {
  1069. std::string text;
  1070. for (int d = 0; d < 7; ++d) {
  1071. text += " ";
  1072. int v = this->calendar_panel_days[(row * 7) + d];
  1073. if (v == 0)
  1074. text += " ";
  1075. else {
  1076. std::string number = std::to_string(v);
  1077. if (number.length() == 1)
  1078. text += " ";
  1079. text += number;
  1080. // Now, translate the status
  1081. int status = this->calendar_day_status[v - 1];
  1082. switch (status) {
  1083. case 0:
  1084. text += "o";
  1085. break;
  1086. case 1:
  1087. text += "h";
  1088. break;
  1089. case 2:
  1090. text += "x";
  1091. break;
  1092. case 3:
  1093. text += "u";
  1094. break;
  1095. }
  1096. }
  1097. if (d == 6)
  1098. text += " ";
  1099. else
  1100. text += " ";
  1101. }
  1102. return text;
  1103. };
  1104. std::string text;
  1105. text = calendarUpdate();
  1106. door::Line line(text, W);
  1107. line.setRender(calendarRender);
  1108. line.setUpdater(calendarUpdate);
  1109. p->addLine(std::make_unique<door::Line>(line));
  1110. }
  1111. return p;
  1112. }
  1113. /**
  1114. * @brief make calendar
  1115. * We assume the calendar is for this month now()
  1116. * Jaunary
  1117. * February
  1118. * March
  1119. * April
  1120. * May
  1121. * June
  1122. * July
  1123. * August
  1124. * September
  1125. * October
  1126. * November
  1127. * December
  1128. * 123456789012345678901234567890123456789012345678901234567890
  1129. * ▒▒░▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀░▒▒
  1130. * N ▒▒▌SUN MON TUE WED THU FRI SAT▐▒▒
  1131. * O ▒▒░───▄───▄───▄───▄───▄───▄───░▒▒
  1132. * V ▒▒▌ 1x│ 2x│ 3o│ 4o│ 5o│ 6o│ 7o▐▒▒ X = Day Completed
  1133. * E ▒▒▌ 8o│ 9x│10o│11o│12x│13x│14o▐▒▒ O = Day Playable
  1134. * M ▒▒▌15x│16u│17u│18u│19u│20u│21u▐▒▒ U = Day Unavailable
  1135. * B ▒▒▌22u│23u│24u│25u│26u│27u│28u▐▒▒ H = Day Uncompleted
  1136. * E ▒▒▌29u│30u│ │ │ │ │ ▐▒▒
  1137. * R ▒▒▌ │ │ │ │ │ │ ▐▒▒
  1138. * ▒▒░▄▄▄█▄▄▄█▄▄▄█▄▄▄█▄▄▄█▄▄▄█▄▄▄░▒▒
  1139. *
  1140. * 123456789012345678901234567890123456789012345678901234567890123456789012345
  1141. *
  1142. * ▒▒░▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀░▒▒
  1143. * N ▒▒▌ SUN MON TUE WED THU FRI SAT ▐▒▒
  1144. * O ▒▒░─────▄─────▄─────▄─────▄─────▄─────▄─────░▒▒
  1145. * V ▒▒▌ 1x │ 2x │ 3o │ 4o │ 5o │ 6o │ 7o ▐▒▒ X = Day Completed
  1146. * E ▒▒▌ 8o │ 9x │ 10o │ 11o │ 12x │ 13x │ 14o ▐▒▒ O = Day Playable
  1147. * M ▒▒▌ 15x │ 16u │ 17u │ 18u │ 19u │ 20u │ 21u ▐▒▒ U = Day Unavailable
  1148. * B ▒▒▌ 22u │ 23u │ 24u │ 25u │ 26u │ 27u │ 28u ▐▒▒ H = Day Uncompleted
  1149. * E ▒▒▌ 29u │ 30u │ │ │ │ │ ▐▒▒
  1150. * R ▒▒▌ │ │ │ │ │ │ ▐▒▒
  1151. * ▒▒░▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄█▄▄▄▄▄░▒▒
  1152. * X Extra Days Allowed Per Day
  1153. *
  1154. * 123456789012345678901234567890123456789012345678901234567890123456789012345
  1155. * ╔═══╗ ╔═════════════════════════════════════════╗
  1156. * ║ N ║ ║ SUN MON TUE WED THU FRI SAT ║
  1157. * ║ O ║ ╚═════════════════════════════════════════╝
  1158. * ║ V ║ ╔═════════════════════════════════════════╗
  1159. * ║ E ║ ║ 1x 2x 3o 4o 5o 6o 7o ║
  1160. * ║ M ║ ║ 8x 9x 10o 11o 12o 13o 14o ║
  1161. * ║ B ║ ║ 15h 16h 17h 18h 19u 20u 21u ║
  1162. * ║ E ║ ║ 22u 23u 24u 25u 26u 27u 28u ║
  1163. * ║ R ║ ║ 29u 30u 31u ║
  1164. * ╚═══╝ ║ ║
  1165. * ╚═════════════════════════════════════════╝
  1166. *
  1167. *
  1168. *
  1169. *
  1170. * @return std::unique_ptr<door::Panel>
  1171. */
  1172. std::string
  1173. PlayCards::current_month(std::chrono::_V2::system_clock::time_point now) {
  1174. time_t now_t = std::chrono::system_clock::to_time_t(now);
  1175. std::tm *now_tm = localtime(&now_t);
  1176. ostringstream os;
  1177. std::string text;
  1178. os << std::put_time(now_tm, "%B");
  1179. text = os.str();
  1180. return text;
  1181. }
  1182. /**
  1183. * make_calendar
  1184. *
  1185. * This needs the screen size information so it can place the
  1186. * panels in the correct location.
  1187. */
  1188. std::unique_ptr<door::Screen> PlayCards::make_calendar() {
  1189. std::unique_ptr<door::Screen> s = std::make_unique<door::Screen>();
  1190. /*
  1191. * This has potential of jumping ahead if player is on after midnight.
  1192. * I'd rather this be stable. When they start the game, is where
  1193. * we count things. (Also last day of month + midnight -- means
  1194. * we need maint ran! No!)
  1195. */
  1196. auto month = std::chrono::system_clock::now();
  1197. time_t month_t = std::chrono::system_clock::to_time_t(month);
  1198. // adjust to first day of the month
  1199. std::tm month_lt;
  1200. localtime_r(&month_t, &month_lt);
  1201. int this_month = month_lt.tm_mon;
  1202. int this_day = month_lt.tm_mday;
  1203. int this_year = month_lt.tm_year + 1900;
  1204. if (month_lt.tm_mday > 1) {
  1205. month -= 24h * (month_lt.tm_mday - 1);
  1206. }
  1207. normalizeDate(month);
  1208. month_t = std::chrono::system_clock::to_time_t(month);
  1209. get_logger() << "1st of Month is "
  1210. << std::put_time(std::localtime(&month_t), "%c %Z") << std::endl;
  1211. localtime_r(&month_t, &month_lt);
  1212. const int FIRST_WEEKDAY = month_lt.tm_wday; // 0-6
  1213. get_logger() << "1st of the Month starts on " << FIRST_WEEKDAY << std::endl;
  1214. // find the last day of this month.
  1215. time_t month_last_day_t = month_t;
  1216. std::tm mld_tm;
  1217. localtime_r(&month_last_day_t, &mld_tm);
  1218. // increment the month, if > 11 (we've entered a new year)
  1219. mld_tm.tm_mon += 1;
  1220. if (mld_tm.tm_mon > 11) {
  1221. mld_tm.tm_mon = 0;
  1222. mld_tm.tm_year++;
  1223. }
  1224. month_last_day_t = std::mktime(&mld_tm);
  1225. // Ok, this should be the 1st of next month.
  1226. month_last_day_t -= (60 * 60 * 24);
  1227. localtime_r(&month_last_day_t, &mld_tm);
  1228. month_last_day = mld_tm.tm_mday;
  1229. get_logger() << "Last day is " << month_last_day << std::endl;
  1230. calendar_panel_days.fill(0);
  1231. int row = 0;
  1232. for (int x = 0; x < month_last_day; x++) {
  1233. int dow = (x + FIRST_WEEKDAY) % 7;
  1234. if ((x != 0) and (dow == 0))
  1235. row++;
  1236. /*
  1237. get_logger() << "x = " << x << " dow = " << dow << " row = " << row
  1238. << std::endl;
  1239. */
  1240. // we actually want x+1 (1- month_last_day)
  1241. // get_logger() << row * 7 + dow << " = " << x + 1 << std::endl;
  1242. calendar_panel_days[row * 7 + dow] = x + 1;
  1243. }
  1244. calendar_day_status.fill(0);
  1245. auto last_played = db.whenPlayed();
  1246. int play_days_ahead = 0;
  1247. if (config["play_days_ahead"]) {
  1248. play_days_ahead = config["play_days_ahead"].as<int>();
  1249. }
  1250. // until maint is setup, we need to verify that the month and year is correct.
  1251. for (auto played : last_played) {
  1252. get_logger() << "played " << played.first << " hands: " << played.second
  1253. << std::endl;
  1254. time_t play_t = played.first;
  1255. std::tm played_tm;
  1256. localtime_r(&play_t, &played_tm);
  1257. if (get_logger) {
  1258. get_logger() << played_tm.tm_mon + 1 << "/" << played_tm.tm_mday << "/"
  1259. << played_tm.tm_year + 1900 << " : " << played.second << " "
  1260. << play_t << std::endl;
  1261. }
  1262. if ((played_tm.tm_mon == this_month) &&
  1263. (played_tm.tm_year + 1900 == this_year)) {
  1264. // Ok!
  1265. int hands = played.second;
  1266. if (hands < total_hands) {
  1267. calendar_day_status[played_tm.tm_mday - 1] = 1;
  1268. } else {
  1269. if (hands >= total_hands) {
  1270. calendar_day_status[played_tm.tm_mday - 1] = 2;
  1271. }
  1272. }
  1273. }
  1274. }
  1275. if (get_logger) {
  1276. get_logger() << "last day " << month_last_day << " today " << this_day
  1277. << " plays ahead " << play_days_ahead << std::endl;
  1278. }
  1279. // mark days ahead as NNY.
  1280. for (int d = 0; d < month_last_day; ++d) {
  1281. if (this_day + play_days_ahead - 1 < d) {
  1282. calendar_day_status[d] = 3;
  1283. }
  1284. }
  1285. {
  1286. ofstream &of = get_logger();
  1287. of << "Calendar_panel_days:" << std::endl;
  1288. for (int x = 0; x < (int)calendar_panel_days.size(); ++x) {
  1289. of << std::setw(2) << calendar_panel_days[x] << ":";
  1290. int c = calendar_panel_days[x];
  1291. if (c == 0)
  1292. of << " ";
  1293. else {
  1294. of << calendar_day_status[c - 1];
  1295. };
  1296. of << " ";
  1297. if ((x != 0) and (((x + 1) % 7) == 0)) {
  1298. of << std::endl;
  1299. }
  1300. }
  1301. of << std::endl;
  1302. }
  1303. std::unique_ptr<door::Panel> p = make_month(current_month(month));
  1304. p->set(3, 3);
  1305. s->addPanel(std::move(p));
  1306. p = make_weekdays();
  1307. p->set(8, 3);
  1308. s->addPanel(std::move(p));
  1309. p = make_calendar_panel();
  1310. p->set(8, 6);
  1311. s->addPanel(std::move(p));
  1312. /*
  1313. const int W = 72;
  1314. p->setStyle(door::BorderStyle::NONE);
  1315. // Ok, that is working. I'm getting the first day of the month. So...
  1316. store the time_t for the date.
  1317. store the day in the column it needs to be in.
  1318. store any hands played (pull data from the db).
  1319. seems like this should be its own class, there's a lot of data that is
  1320. specific just to it.
  1321. */
  1322. /*
  1323. door::ANSIColor statusColor(door::COLOR::WHITE, door::COLOR::BLUE,
  1324. door::ATTR::BOLD);
  1325. door::ANSIColor valueColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  1326. door::ATTR::BOLD);
  1327. door::renderFunction svRender = statusValue(statusColor, valueColor);
  1328. // or use renderStatus as defined above.
  1329. // We'll stick with these for now.
  1330. {
  1331. std::string userString = "Name: ";
  1332. userString += door.username;
  1333. door::Line username(userString, W);
  1334. username.setRender(svRender);
  1335. p->addLine(std::make_unique<door::Line>(username));
  1336. }
  1337. {
  1338. door::updateFunction scoreUpdate = [this](void) -> std::string {
  1339. std::string text = "Score: ";
  1340. text.append(std::to_string(score));
  1341. return text;
  1342. };
  1343. std::string scoreString = scoreUpdate();
  1344. door::Line scoreline(scoreString, W);
  1345. scoreline.setRender(svRender);
  1346. scoreline.setUpdater(scoreUpdate);
  1347. p->addLine(std::make_unique<door::Line>(scoreline));
  1348. }
  1349. {
  1350. door::updateFunction timeUpdate = [this](void) -> std::string {
  1351. std::stringstream ss;
  1352. std::string text;
  1353. ss << "Time used: " << setw(3) << door.time_used << " / " << setw(3)
  1354. << door.time_left;
  1355. text = ss.str();
  1356. return text;
  1357. };
  1358. std::string timeString = timeUpdate();
  1359. door::Line time(timeString, W);
  1360. time.setRender(svRender);
  1361. time.setUpdater(timeUpdate);
  1362. p->addLine(std::make_unique<door::Line>(time));
  1363. }
  1364. {
  1365. door::updateFunction handUpdate = [this](void) -> std::string {
  1366. std::string text = "Playing Hand ";
  1367. text.append(std::to_string(hand));
  1368. text.append(" of ");
  1369. text.append(std::to_string(total_hands));
  1370. return text;
  1371. };
  1372. std::string handString = handUpdate();
  1373. door::Line hands(handString, W);
  1374. hands.setRender(svRender);
  1375. hands.setUpdater(handUpdate);
  1376. p->addLine(std::make_unique<door::Line>(hands));
  1377. }
  1378. */
  1379. return s;
  1380. }