main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include "door.h"
  2. #include "yaml-cpp/yaml.h"
  3. #include <chrono> // chrono::system_clock
  4. #include <ctime> // localtime
  5. #include <iomanip> // put_time
  6. #include <iostream>
  7. #include <random>
  8. #include <string>
  9. #include "db.h"
  10. #include "deck.h"
  11. #include "play.h"
  12. #include "scores.h"
  13. #include "utils.h"
  14. #include "version.h"
  15. #include <algorithm> // transform
  16. // configuration here -- access via extern
  17. YAML::Node config;
  18. door::ANSIColor stringToANSIColor(std::string colorCode);
  19. std::function<std::ofstream &(void)> get_logger;
  20. std::function<void(void)> cls_display_starfield;
  21. std::string return_current_time_and_date() {
  22. auto now = std::chrono::system_clock::now();
  23. auto in_time_t = std::chrono::system_clock::to_time_t(now);
  24. std::stringstream ss;
  25. // ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
  26. ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %r");
  27. return ss.str();
  28. }
  29. int press_a_key(door::Door &door) {
  30. door << door::reset << "Press a key to continue...";
  31. int r = door.sleep_key(door.inactivity);
  32. door << door::nl;
  33. return r;
  34. }
  35. // This does not seem to be working. I keep getting zero.
  36. int opt_from_string(std::string colorCode) {
  37. for (std::size_t pos = 0; pos != deck_colors.size(); ++pos) {
  38. // if (caseInsensitiveStringCompare(colorCode, deck_colors[pos]) == 0) {
  39. if (iequals(colorCode, deck_colors[pos])) {
  40. return pos;
  41. }
  42. }
  43. return 0;
  44. }
  45. int configure(door::Door &door, DBData &db) {
  46. auto menu = make_config_menu();
  47. int r = 0;
  48. bool save_deckcolor = false;
  49. const char *deckcolor = "DeckColor";
  50. std::string newColor;
  51. while (r >= 0) {
  52. if (save_deckcolor) {
  53. door << menu;
  54. db.setSetting(deckcolor, newColor);
  55. save_deckcolor = false;
  56. }
  57. if (cls_display_starfield)
  58. cls_display_starfield();
  59. else
  60. door << door::reset << door::cls;
  61. r = menu.choose(door);
  62. if (r > 0) {
  63. door << door::reset << door::cls;
  64. char c = menu.which(r - 1);
  65. if (c == 'D') {
  66. // Ok, deck colors
  67. // get default
  68. std::string currentDefault = db.getSetting(deckcolor, "ALL");
  69. int currentOpt = opt_from_string(currentDefault);
  70. door << door::reset << door::cls;
  71. auto deck = make_deck_menu();
  72. deck.defaultSelection(currentOpt);
  73. if (cls_display_starfield)
  74. cls_display_starfield();
  75. else
  76. door << door::reset << door::cls;
  77. int newOpt = deck.choose(door);
  78. door << door::reset << door::cls;
  79. if (newOpt >= 0) {
  80. newOpt--;
  81. newColor = stringFromColorOptions(newOpt);
  82. if (newOpt != currentOpt) {
  83. door.log() << deckcolor << " was " << currentDefault << ", "
  84. << currentOpt << ". Now " << newColor << ", " << newOpt
  85. << std::endl;
  86. save_deckcolor = true;
  87. }
  88. }
  89. }
  90. if (c == 'V') {
  91. // view settings -- Sysop Configuration
  92. if (cls_display_starfield)
  93. cls_display_starfield();
  94. else
  95. door << door::reset << door::cls;
  96. door::Panel config_panel = make_sysop_config();
  97. // config_panel.set(1, 1);
  98. door << config_panel << door::reset << door::nl;
  99. r = press_a_key(door);
  100. if (r < 0)
  101. return r;
  102. door << door::reset << door::cls;
  103. }
  104. if (c == 'Q') {
  105. return r;
  106. }
  107. }
  108. }
  109. return r;
  110. }
  111. int main(int argc, char *argv[]) {
  112. door::Door door("space-ace", argc, argv);
  113. // store the door log so we can easily access it.
  114. get_logger = [&door]() -> ofstream & { return door.log(); };
  115. std::random_device rd;
  116. std::mt19937 rng(rd());
  117. cls_display_starfield = [&door, &rng]() -> void {
  118. display_starfield(door, rng);
  119. };
  120. DBData spacedb;
  121. spacedb.setUser(door.username);
  122. if (file_exists("space-ace.yaml")) {
  123. config = YAML::LoadFile("space-ace.yaml");
  124. }
  125. bool update_config = false;
  126. // populate with "good" defaults
  127. if (!config["hands_per_day"]) {
  128. config["hands_per_day"] = 3;
  129. update_config = true;
  130. }
  131. if (!config["date_format"]) {
  132. config["date_format"] = "%B %d"; // Month day or "%b %d,%Y" Mon,d YYYY
  133. update_config = true;
  134. }
  135. if (!config["date_score"]) {
  136. config["date_score"] = "%m/%d/%Y"; // or "%Y/%0m/%0d";
  137. update_config = true;
  138. }
  139. if (!config["makeup_per_day"]) {
  140. config["makeup_per_day"] = 5;
  141. update_config = true;
  142. }
  143. if (!config["play_days_ahead"]) {
  144. config["play_days_ahead"] = 2;
  145. update_config = true;
  146. }
  147. if (!config["date_monthly"]) {
  148. config["date_monthly"] = "%B %Y";
  149. update_config = true;
  150. }
  151. // save configuration -- something was updated
  152. if (update_config) {
  153. std::ofstream fout("space-ace.yaml");
  154. fout << config << std::endl;
  155. }
  156. // retrieve lastcall
  157. time_t last_call = std::stol(spacedb.getSetting("LastCall", "0"));
  158. std::chrono::_V2::system_clock::time_point now =
  159. std::chrono::system_clock::now();
  160. // store now as lastcall
  161. time_t now_t = std::chrono::system_clock::to_time_t(now);
  162. spacedb.setSetting("LastCall", std::to_string(now_t));
  163. // run maint
  164. {
  165. std::chrono::_V2::system_clock::time_point maint_date = now;
  166. firstOfMonthDate(maint_date);
  167. if (spacedb.expireScores(
  168. std::chrono::system_clock::to_time_t(maint_date))) {
  169. if (get_logger)
  170. get_logger() << "maint completed" << std::endl;
  171. door << "Thanks for waiting..." << door::nl;
  172. }
  173. }
  174. // Have they used this door before?
  175. if (last_call != 0) {
  176. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)
  177. << "Welcome Back!" << door::nl;
  178. auto nowClock = std::chrono::system_clock::from_time_t(now_t);
  179. auto lastClock = std::chrono::system_clock::from_time_t(last_call);
  180. auto delta = nowClock - lastClock;
  181. // int days = chrono::duration_cast<chrono::days>(delta).count(); //
  182. // c++ 20
  183. int hours = chrono::duration_cast<chrono::hours>(delta).count();
  184. int days = hours / 24;
  185. int minutes = chrono::duration_cast<chrono::minutes>(delta).count();
  186. int secs = chrono::duration_cast<chrono::seconds>(delta).count();
  187. if (days > 1) {
  188. door << door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD)
  189. << "It's been " << days << " days since you last played."
  190. << door::nl;
  191. } else {
  192. if (hours > 1) {
  193. door << door::ANSIColor(door::COLOR::CYAN) << "It's been " << hours
  194. << " hours since you last played." << door::nl;
  195. } else {
  196. if (minutes > 1) {
  197. door << door::ANSIColor(door::COLOR::CYAN) << "It's been " << minutes
  198. << " minutes since you last played." << door::nl;
  199. } else {
  200. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)
  201. << "It's been " << secs << " seconds since you last played."
  202. << door::nl;
  203. door << "It's like you never left." << door::nl;
  204. }
  205. }
  206. }
  207. door << door::reset;
  208. press_a_key(door);
  209. }
  210. int mx, my; // Max screen width/height
  211. if (door.width == 0) {
  212. // screen detect failed, use sensible defaults
  213. door.width = mx = 80;
  214. door.height = my = 23;
  215. } else {
  216. mx = door.width;
  217. my = door.height;
  218. }
  219. // We assume here that the width and height aren't something crazy like
  220. // 10x15. :P (or 24x923!)
  221. display_starfield_space_ace(door, rng);
  222. // for testing inactivity timeout
  223. // door.inactivity = 10;
  224. door::Panel timeout = make_timeout(mx, my);
  225. door::Menu m = make_main_menu();
  226. door::Panel about = make_about(); // 8 lines
  227. door::Panel help = make_help();
  228. // center the about box
  229. about.set((mx - 60) / 2, (my - 9) / 2);
  230. // center the help box
  231. help.set((mx - 60) / 2, (my - 15) / 2);
  232. int r = 0;
  233. while ((r >= 0) and (r != 6)) {
  234. // starfield + menu ?
  235. display_starfield(door, rng);
  236. r = m.choose(door);
  237. // need to reset the colors. (whoops!)
  238. door << door::reset << door::cls; // door::nl;
  239. // OK! The screen is blank at this point!
  240. switch (r) {
  241. case 1: // play game
  242. {
  243. PlayCards pc(door, spacedb, rng);
  244. r = pc.play();
  245. // r = play_cards(door, spacedb, rng);
  246. }; break;
  247. case 2: // view scores
  248. {
  249. // door << door::cls;
  250. Scores score(door, spacedb);
  251. score.display_scores(door);
  252. r = press_a_key(door);
  253. break;
  254. display_starfield(door, rng);
  255. door << door::Goto(1, 2);
  256. auto monthly_scores = spacedb.getMonthlyScores(10);
  257. if (!monthly_scores.empty()) {
  258. door << "The TOP monthly Scores:" << door::nl;
  259. }
  260. for (auto it : monthly_scores) {
  261. time_t date = it.date;
  262. std::string nice_date = convertDateToMonthlyFormat(date);
  263. door << nice_date << " " << std::setw(18) << it.user << " " << it.score
  264. << door::nl;
  265. }
  266. door << door::nl;
  267. // I probably want JUST the top 10 here!
  268. auto all_scores = spacedb.getScores();
  269. if (!all_scores.empty()) {
  270. door << "The Top Scores for this Month:" << door::nl;
  271. }
  272. for (auto it : all_scores) {
  273. std::string nice_date = convertDateToDateScoreFormat(it.date);
  274. door << " *** " << nice_date << setw(15) << it.user << " " << it.won
  275. << " " << it.score << door::nl;
  276. }
  277. door << "====================" << door::nl;
  278. }
  279. r = press_a_key(door);
  280. break;
  281. case 3: // configure
  282. r = configure(door, spacedb);
  283. // r = press_a_key(door);
  284. break;
  285. case 4: // help
  286. display_starfield(door, rng);
  287. door << help << door::nl;
  288. r = press_a_key(door);
  289. break;
  290. case 5: // about
  291. display_starfield(door, rng);
  292. door << about << door::nl;
  293. r = press_a_key(door);
  294. break;
  295. case 6: // quit
  296. break;
  297. }
  298. }
  299. if (r < 0) {
  300. // TIMEOUT:
  301. if (r == -1) {
  302. door.log() << "TIMEOUT" << std::endl;
  303. door << timeout << door::reset << door::nl << door::nl;
  304. } else {
  305. if (r == -3) {
  306. door.log() << "OUTTA TIME" << std::endl;
  307. door::Panel notime = make_notime(mx, my);
  308. door << notime << door::reset << door::nl;
  309. }
  310. }
  311. return 0;
  312. }
  313. door << door::nl;
  314. // door << door::reset << door::cls;
  315. display_starfield(door, rng);
  316. door << m << door::reset << door::nl;
  317. // Normal DOOR exit goes here...
  318. door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
  319. get_logger = nullptr;
  320. cls_display_starfield = nullptr;
  321. return 0;
  322. }