scores.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "scores.h"
  2. #include "utils.h"
  3. #include <iomanip> // setw
  4. #include <sstream> // ostringstream
  5. door::renderFunction scoresRender(door::ANSIColor date, int dlen,
  6. door::ANSIColor nick, int nlen,
  7. door::ANSIColor score) {
  8. door::renderFunction rf = [date, dlen, nick, nlen,
  9. score](const std::string &txt) -> door::Render {
  10. door::Render r(txt);
  11. r.append(date, dlen);
  12. r.append(nick, nlen);
  13. int left = txt.size() - (dlen + nlen);
  14. r.append(score, left);
  15. return r;
  16. };
  17. return rf;
  18. }
  19. std::unique_ptr<door::Panel> Scores::make_top_scores_panel() {
  20. const int W = 38;
  21. door::COLOR panel_bg = door::COLOR::MAGENTA;
  22. door::ANSIColor panel_color =
  23. door::ANSIColor(door::COLOR::CYAN, panel_bg); //, door::ATTR::BOLD);
  24. door::ANSIColor heading_color = panel_color;
  25. heading_color.setFg(door::COLOR::WHITE, door::ATTR::BOLD);
  26. // door::ANSIColor(door::COLOR::WHITE, panel_bg, door::ATTR::BOLD);
  27. door::ANSIColor spacer_color = panel_color;
  28. spacer_color.setFg(door::COLOR::YELLOW, door::ATTR::BOLD);
  29. // door::ANSIColor(door::COLOR::YELLOW, panel_bg, door::ATTR::BOLD);
  30. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  31. p->setStyle(door::BorderStyle::DOUBLE);
  32. p->setColor(panel_color);
  33. std::unique_ptr<door::Line> heading =
  34. std::make_unique<door::Line>("The TOP Monthly Scores:", W);
  35. heading->setColor(heading_color);
  36. p->addLine(std::move(heading));
  37. std::unique_ptr<door::Line> spacer = p->spacer_line(false);
  38. spacer->setColor(spacer_color);
  39. p->addLine(std::move(spacer));
  40. auto monthly_scores = db.getMonthlyScores(15);
  41. if (monthly_scores.empty()) {
  42. // No Monthly Scores
  43. std::unique_ptr<door::Line> heading =
  44. std::make_unique<door::Line>("No, Not Yet!", W);
  45. heading->setColor(heading_color);
  46. p->addLine(std::move(heading));
  47. }
  48. ostringstream oss;
  49. // get length of longest month
  50. time_t longest = 1631280600; // 9/10/2021 9:30:10
  51. std::string longest_date = convertDateToMonthlyFormat(longest);
  52. int longest_month = longest_date.size();
  53. if (get_logger)
  54. get_logger() << "longest_date: " << longest_date << " " << longest_month
  55. << std::endl;
  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::BROWN;
  90. door::ANSIColor panel_color =
  91. door::ANSIColor(door::COLOR::CYAN, panel_bg); // , door::ATTR::BOLD);
  92. door::ANSIColor heading_color =
  93. door::ANSIColor(door::COLOR::WHITE, panel_bg, door::ATTR::BOLD);
  94. door::ANSIColor spacer_color =
  95. door::ANSIColor(door::COLOR::YELLOW, panel_bg, door::ATTR::BOLD);
  96. std::unique_ptr<door::Panel> p = std::make_unique<door::Panel>(W);
  97. p->setStyle(door::BorderStyle::DOUBLE);
  98. p->setColor(panel_color);
  99. std::string text = "The TOP Scores for ";
  100. {
  101. auto now = std::chrono::system_clock::now();
  102. time_t date = std::chrono::system_clock::to_time_t(now);
  103. text += convertDateToMonthlyFormat(date);
  104. text += ":";
  105. }
  106. std::unique_ptr<door::Line> heading = std::make_unique<door::Line>(text, W);
  107. heading->setColor(heading_color);
  108. p->addLine(std::move(heading));
  109. std::unique_ptr<door::Line> spacer = p->spacer_line(false);
  110. spacer->setColor(spacer_color);
  111. p->addLine(std::move(spacer));
  112. auto monthly_scores = db.getScores(15);
  113. if (monthly_scores.empty()) {
  114. // No Monthly Scores
  115. std::unique_ptr<door::Line> heading =
  116. std::make_unique<door::Line>("No, Not Yet!", W);
  117. heading->setColor(heading_color);
  118. p->addLine(std::move(heading));
  119. }
  120. ostringstream oss;
  121. time_t longest = 1631280600; // 9/10/2021 9:30:10
  122. std::string longest_date = convertDateToMonthDayFormat(longest);
  123. int longest_month = longest_date.size();
  124. if (get_logger)
  125. get_logger() << __FILE__ << "@" << __LINE__
  126. << " longest_date: " << longest_date << " " << longest_month
  127. << std::endl;
  128. door::ANSIColor nick = panel_color;
  129. nick.setFg(door::COLOR::CYAN);
  130. door::ANSIColor yourNick = panel_color;
  131. yourNick.setFg(door::COLOR::GREEN, door::ATTR::BOLD);
  132. door::ANSIColor date = panel_color;
  133. date.setFg(door::COLOR::WHITE, door::ATTR::BOLD);
  134. door::ANSIColor score = panel_color;
  135. score.setFg(door::COLOR::CYAN, door::ATTR::BOLD);
  136. door::ANSIColor yourScore = panel_color;
  137. yourScore.setFg(door::COLOR::YELLOW, door::ATTR::BOLD);
  138. door::renderFunction scoreColors =
  139. scoresRender(date, longest_month, nick, 17, score);
  140. door::renderFunction yourScoreColors =
  141. scoresRender(date, longest_month, yourNick, 17, yourScore);
  142. for (auto it : monthly_scores) {
  143. time_t date = it.date;
  144. std::string nice_date = convertDateToMonthDayFormat(date);
  145. oss.clear();
  146. oss.str(std::string());
  147. oss << std::setw(longest_month) << nice_date << " " << std::setw(16)
  148. << it.user << " " << it.score;
  149. std::unique_ptr<door::Line> line =
  150. std::make_unique<door::Line>(oss.str(), W);
  151. if ((it.user == door.username) or (it.user == door.handle))
  152. line->setRender(yourScoreColors);
  153. else
  154. line->setRender(scoreColors);
  155. p->addLine(std::move(line));
  156. }
  157. return p;
  158. }
  159. Scores::Scores(door::Door &d, DBData &dbd) : door{d}, db{dbd} {
  160. top_scores = make_top_scores_panel();
  161. top_this_month = make_top_this_month_panel();
  162. }
  163. void Scores::display_scores(door::Door &door) {
  164. if (cls_display_starfield)
  165. cls_display_starfield();
  166. top_scores->set(1, 5);
  167. door << *top_scores;
  168. top_this_month->set(44, 5);
  169. door << *top_this_month << door::reset << door::nl;
  170. }