play.cpp 52 KB

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