play.cpp 53 KB

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