play.cpp 54 KB

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