play.cpp 53 KB

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