scores.cpp 7.1 KB

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