play.cpp 54 KB

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