scores.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "scores.h"
  2. #include "utils.h"
  3. #include <algorithm> // max
  4. #include <iomanip> // setw
  5. #include <sstream> // ostringstream
  6. door::renderFunction scoresRender(door::ANSIColor date, int dlen,
  7. door::ANSIColor nick, int nlen,
  8. door::ANSIColor score) {
  9. door::renderFunction rf = [date, dlen, nick, nlen,
  10. score](const std::string &txt) -> door::Render {
  11. door::Render r(txt);
  12. r.append(date, dlen);
  13. r.append(nick, nlen);
  14. int left = txt.size() - (dlen + nlen);
  15. r.append(score, left);
  16. return r;
  17. };
  18. return rf;
  19. }
  20. std::unique_ptr<door::Panel> Scores::make_top_scores_panel() {
  21. const int W = 38;
  22. door::COLOR panel_bg = door::COLOR::BLUE;
  23. door::ANSIColor panel_color =
  24. door::ANSIColor(door::COLOR::CYAN, panel_bg); //, door::ATTR::BOLD);
  25. door::ANSIColor heading_color = panel_color;
  26. heading_color.setFg(door::COLOR::GREEN, door::ATTR::BOLD);
  27. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  28. p->setStyle(door::BorderStyle::DOUBLE);
  29. p->setColor(panel_color);
  30. std::unique_ptr<door::Line> heading =
  31. std::make_unique<door::Line>("The TOP Monthly Scores:", W, heading_color);
  32. // heading->setColor(heading_color);
  33. p->addLine(std::move(heading));
  34. std::unique_ptr<door::Line> spacer = p->spacer_line(false);
  35. p->addLine(std::move(spacer));
  36. auto monthly_scores = db.getMonthlyScores(15);
  37. if (monthly_scores.empty()) {
  38. // No Monthly Scores
  39. door::ANSIColor nny_color =
  40. door::ANSIColor(door::COLOR::YELLOW, panel_bg, door::ATTR::BOLD);
  41. std::unique_ptr<door::Line> heading =
  42. std::make_unique<door::Line>("No, Not Yet!", W, nny_color);
  43. // heading->setColor(heading_color);
  44. p->addLine(std::move(heading));
  45. }
  46. ostringstream oss;
  47. // get length of longest month
  48. time_t longest = 1631280600; // 9/10/2021 9:30:10
  49. std::string longest_date = convertDateToMonthlyFormat(longest);
  50. int longest_month = longest_date.size();
  51. #ifdef DEBUG_OUTPUT
  52. if (get_logger)
  53. get_logger() << "longest_date: " << longest_date << " " << longest_month
  54. << std::endl;
  55. #endif
  56. door::ANSIColor nick = panel_color;
  57. nick.setFg(door::COLOR::CYAN);
  58. door::ANSIColor yourNick = panel_color;
  59. yourNick.setFg(door::COLOR::GREEN, door::ATTR::BOLD);
  60. door::ANSIColor date = panel_color;
  61. date.setFg(door::COLOR::WHITE, door::ATTR::BOLD);
  62. door::ANSIColor score = panel_color;
  63. score.setFg(door::COLOR::CYAN, door::ATTR::BOLD);
  64. door::ANSIColor yourScore = panel_color;
  65. yourScore.setFg(door::COLOR::YELLOW, door::ATTR::BOLD);
  66. door::renderFunction scoreColors =
  67. scoresRender(date, longest_month, nick, 17, score);
  68. door::renderFunction yourScoreColors =
  69. scoresRender(date, longest_month, yourNick, 17, yourScore);
  70. for (auto it : monthly_scores) {
  71. time_t date = it.date;
  72. std::string nice_date = convertDateToMonthlyFormat(date);
  73. oss.clear();
  74. oss.str(std::string());
  75. oss << std::setw(longest_month) << nice_date << " " << std::setw(16)
  76. << it.user << " " << it.score;
  77. std::unique_ptr<door::Line> line =
  78. std::make_unique<door::Line>(oss.str(), W);
  79. if ((it.user == door.username) or (it.user == door.handle))
  80. line->setRender(yourScoreColors);
  81. else
  82. line->setRender(scoreColors);
  83. p->addLine(std::move(line));
  84. }
  85. return p;
  86. }
  87. std::unique_ptr<door::Panel> Scores::make_top_this_month_panel() {
  88. const int W = 30;
  89. door::COLOR panel_bg = door::COLOR::BLUE;
  90. door::ANSIColor panel_color = door::ANSIColor(door::COLOR::CYAN, panel_bg);
  91. door::ANSIColor heading_color =
  92. door::ANSIColor(door::COLOR::GREEN, panel_bg, door::ATTR::BOLD);
  93. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  94. p->setStyle(door::BorderStyle::DOUBLE);
  95. p->setColor(panel_color);
  96. std::string text = "The TOP Scores for ";
  97. {
  98. auto now = std::chrono::system_clock::now();
  99. time_t date = std::chrono::system_clock::to_time_t(now);
  100. text += convertDateToMonthlyFormat(date);
  101. text += ":";
  102. }
  103. std::unique_ptr<door::Line> heading =
  104. std::make_unique<door::Line>(text, W, heading_color);
  105. // heading->setColor(heading_color);
  106. p->addLine(std::move(heading));
  107. std::unique_ptr<door::Line> spacer = p->spacer_line(false);
  108. p->addLine(std::move(spacer));
  109. auto monthly_scores = db.getScores(15);
  110. if (monthly_scores.empty()) {
  111. // No Monthly Scores
  112. door::ANSIColor nny_color =
  113. door::ANSIColor(door::COLOR::YELLOW, panel_bg, door::ATTR::BOLD);
  114. std::unique_ptr<door::Line> heading =
  115. std::make_unique<door::Line>("No, Not Yet!", W, nny_color);
  116. // heading->setColor(heading_color);
  117. p->addLine(std::move(heading));
  118. }
  119. ostringstream oss;
  120. time_t longest = 1631280600; // 9/10/2021 9:30:10
  121. std::string longest_date = convertDateToMonthDayFormat(longest);
  122. int longest_month = longest_date.size();
  123. #ifdef DEBUG_OUTPUT
  124. if (get_logger)
  125. get_logger() << __FILE__ << "@" << __LINE__
  126. << " longest_date: " << longest_date << " " << longest_month
  127. << std::endl;
  128. #endif
  129. door::ANSIColor nick = panel_color;
  130. nick.setFg(door::COLOR::CYAN);
  131. door::ANSIColor yourNick = panel_color;
  132. yourNick.setFg(door::COLOR::GREEN, door::ATTR::BOLD);
  133. door::ANSIColor date = panel_color;
  134. date.setFg(door::COLOR::WHITE, door::ATTR::BOLD);
  135. door::ANSIColor score = panel_color;
  136. score.setFg(door::COLOR::CYAN, door::ATTR::BOLD);
  137. door::ANSIColor yourScore = panel_color;
  138. yourScore.setFg(door::COLOR::YELLOW, door::ATTR::BOLD);
  139. door::renderFunction scoreColors =
  140. scoresRender(date, longest_month, nick, 17, score);
  141. door::renderFunction yourScoreColors =
  142. scoresRender(date, longest_month, yourNick, 17, yourScore);
  143. for (auto it : monthly_scores) {
  144. time_t date = it.date;
  145. std::string nice_date = convertDateToMonthDayFormat(date);
  146. oss.clear();
  147. oss.str(std::string());
  148. oss << std::setw(longest_month) << nice_date << " " << std::setw(16)
  149. << it.user << " " << it.score;
  150. std::unique_ptr<door::Line> line =
  151. std::make_unique<door::Line>(oss.str(), W);
  152. if ((it.user == door.username) or (it.user == door.handle))
  153. line->setRender(yourScoreColors);
  154. else
  155. line->setRender(scoreColors);
  156. p->addLine(std::move(line));
  157. }
  158. return p;
  159. }
  160. Scores::Scores(door::Door &d, DBData &dbd, Starfield &sf)
  161. : door{d}, db{dbd}, stars{sf} {
  162. top_scores = make_top_scores_panel();
  163. top_this_month = make_top_this_month_panel();
  164. }
  165. void Scores::display_scores(door::Door &door) {
  166. if (cls_display_starfield)
  167. cls_display_starfield();
  168. int mx = door.width;
  169. int my = door.height;
  170. int h = std::max(top_scores->getHeight(), top_this_month->getHeight());
  171. int padx = (mx - (top_scores->getWidth() + top_this_month->getWidth())) / 3;
  172. int pady = (my - h) / 2;
  173. top_scores->set(padx, pady);
  174. // display top_this_month, then top_scores.
  175. // This gives the press a key prompt a good place to be.
  176. top_this_month->set(padx * 2 + top_scores->getWidth(), pady);
  177. door << *top_this_month << *top_scores << door::reset << door::nl;
  178. }