play.cpp 43 KB

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