play.cpp 53 KB

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