main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 << door::Goto(1, 1);
  97. door << door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLACK)
  98. << "Game Settings - SysOp Configurable" << door::reset << door::nl
  99. << door::nl;
  100. for (auto cfg : config) {
  101. std::string key = cfg.first.as<std::string>();
  102. if (key[0] == '_')
  103. continue;
  104. // TODO: replace _ with ' ' in string.
  105. while (replace(key, "_", " ")) {
  106. };
  107. std::string value = cfg.second.as<std::string>();
  108. door << door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLACK,
  109. door::ATTR::BOLD)
  110. << std::setw(20) << key
  111. << door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLACK,
  112. door::ATTR::BOLD)
  113. << " : "
  114. << door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLACK,
  115. door::ATTR::BOLD)
  116. << value << door::reset << door::nl;
  117. }
  118. r = press_a_key(door);
  119. if (r < 0)
  120. return r;
  121. door << door::reset << door::cls;
  122. }
  123. if (c == 'Q') {
  124. return r;
  125. }
  126. }
  127. }
  128. return r;
  129. }
  130. int main(int argc, char *argv[]) {
  131. door::Door door("space-ace", argc, argv);
  132. // store the door log so we can easily access it.
  133. get_logger = [&door]() -> ofstream & { return door.log(); };
  134. std::random_device rd;
  135. std::mt19937 rng(rd());
  136. cls_display_starfield = [&door, &rng]() -> void {
  137. display_starfield(door, rng);
  138. };
  139. DBData spacedb;
  140. spacedb.setUser(door.username);
  141. if (file_exists("space-ace.yaml")) {
  142. config = YAML::LoadFile("space-ace.yaml");
  143. }
  144. bool update_config = false;
  145. // populate with "good" defaults
  146. if (!config["hands_per_day"]) {
  147. config["hands_per_day"] = 3;
  148. update_config = true;
  149. }
  150. if (!config["date_format"]) {
  151. config["date_format"] = "%B %d"; // Month day or "%b %d,%Y" Mon,d YYYY
  152. update_config = true;
  153. }
  154. if (!config["date_score"]) {
  155. config["date_score"] = "%m/%d/%Y"; // or "%Y/%0m/%0d";
  156. update_config = true;
  157. }
  158. if (!config["makeup_per_day"]) {
  159. config["makeup_per_day"] = 5;
  160. update_config = true;
  161. }
  162. if (!config["play_days_ahead"]) {
  163. config["play_days_ahead"] = 2;
  164. update_config = true;
  165. }
  166. if (!config["date_monthly"]) {
  167. config["date_monthly"] = "%B %Y";
  168. update_config = true;
  169. }
  170. // save configuration -- something was updated
  171. if (update_config) {
  172. std::ofstream fout("space-ace.yaml");
  173. fout << config << std::endl;
  174. }
  175. // retrieve lastcall
  176. time_t last_call = std::stol(spacedb.getSetting("LastCall", "0"));
  177. std::chrono::_V2::system_clock::time_point now =
  178. std::chrono::system_clock::now();
  179. // store now as lastcall
  180. time_t now_t = std::chrono::system_clock::to_time_t(now);
  181. spacedb.setSetting("LastCall", std::to_string(now_t));
  182. // run maint
  183. {
  184. std::chrono::_V2::system_clock::time_point maint_date = now;
  185. firstOfMonthDate(maint_date);
  186. if (spacedb.expireScores(
  187. std::chrono::system_clock::to_time_t(maint_date))) {
  188. if (get_logger)
  189. get_logger() << "maint completed" << std::endl;
  190. door << "Thanks for waiting..." << door::nl;
  191. }
  192. }
  193. // Have they used this door before?
  194. if (last_call != 0) {
  195. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)
  196. << "Welcome Back!" << door::nl;
  197. auto nowClock = std::chrono::system_clock::from_time_t(now_t);
  198. auto lastClock = std::chrono::system_clock::from_time_t(last_call);
  199. auto delta = nowClock - lastClock;
  200. // int days = chrono::duration_cast<chrono::days>(delta).count(); //
  201. // c++ 20
  202. int hours = chrono::duration_cast<chrono::hours>(delta).count();
  203. int days = hours / 24;
  204. int minutes = chrono::duration_cast<chrono::minutes>(delta).count();
  205. int secs = chrono::duration_cast<chrono::seconds>(delta).count();
  206. if (days > 1) {
  207. door << door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD)
  208. << "It's been " << days << " days since you last played."
  209. << door::nl;
  210. } else {
  211. if (hours > 1) {
  212. door << door::ANSIColor(door::COLOR::CYAN) << "It's been " << hours
  213. << " hours since you last played." << door::nl;
  214. } else {
  215. if (minutes > 1) {
  216. door << door::ANSIColor(door::COLOR::CYAN) << "It's been " << minutes
  217. << " minutes since you last played." << door::nl;
  218. } else {
  219. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)
  220. << "It's been " << secs << " seconds since you last played."
  221. << door::nl;
  222. door << "It's like you never left." << door::nl;
  223. }
  224. }
  225. }
  226. door << door::reset;
  227. press_a_key(door);
  228. }
  229. int mx, my; // Max screen width/height
  230. if (door.width == 0) {
  231. // screen detect failed, use sensible defaults
  232. door.width = mx = 80;
  233. door.height = my = 23;
  234. } else {
  235. mx = door.width;
  236. my = door.height;
  237. }
  238. // We assume here that the width and height aren't something crazy like
  239. // 10x15. :P (or 24x923!)
  240. display_starfield_space_ace(door, rng);
  241. // for testing inactivity timeout
  242. // door.inactivity = 10;
  243. door::Panel timeout = make_timeout(mx, my);
  244. door::Menu m = make_main_menu();
  245. door::Panel about = make_about(); // 8 lines
  246. door::Panel help = make_help();
  247. // center the about box
  248. about.set((mx - 60) / 2, (my - 9) / 2);
  249. // center the help box
  250. help.set((mx - 60) / 2, (my - 15) / 2);
  251. int r = 0;
  252. while ((r >= 0) and (r != 6)) {
  253. // starfield + menu ?
  254. display_starfield(door, rng);
  255. r = m.choose(door);
  256. // need to reset the colors. (whoops!)
  257. door << door::reset << door::cls; // door::nl;
  258. // OK! The screen is blank at this point!
  259. switch (r) {
  260. case 1: // play game
  261. {
  262. PlayCards pc(door, spacedb, rng);
  263. r = pc.play();
  264. // r = play_cards(door, spacedb, rng);
  265. }; break;
  266. case 2: // view scores
  267. {
  268. // door << door::cls;
  269. Scores score(door, spacedb);
  270. score.display_scores(door);
  271. r = press_a_key(door);
  272. break;
  273. display_starfield(door, rng);
  274. door << door::Goto(1, 2);
  275. auto monthly_scores = spacedb.getMonthlyScores(10);
  276. if (!monthly_scores.empty()) {
  277. door << "The TOP monthly Scores:" << door::nl;
  278. }
  279. for (auto it : monthly_scores) {
  280. time_t date = it.date;
  281. std::string nice_date = convertDateToMonthlyFormat(date);
  282. door << nice_date << " " << std::setw(18) << it.user << " " << it.score
  283. << door::nl;
  284. }
  285. door << door::nl;
  286. // I probably want JUST the top 10 here!
  287. auto all_scores = spacedb.getScores();
  288. if (!all_scores.empty()) {
  289. door << "The Top Scores for this Month:" << door::nl;
  290. }
  291. for (auto it : all_scores) {
  292. std::string nice_date = convertDateToDateScoreFormat(it.date);
  293. door << " *** " << nice_date << setw(15) << it.user << " " << it.won
  294. << " " << it.score << door::nl;
  295. }
  296. door << "====================" << door::nl;
  297. }
  298. r = press_a_key(door);
  299. break;
  300. case 3: // configure
  301. r = configure(door, spacedb);
  302. // r = press_a_key(door);
  303. break;
  304. case 4: // help
  305. display_starfield(door, rng);
  306. door << help << door::nl;
  307. r = press_a_key(door);
  308. break;
  309. case 5: // about
  310. display_starfield(door, rng);
  311. door << about << door::nl;
  312. r = press_a_key(door);
  313. break;
  314. case 6: // quit
  315. break;
  316. }
  317. }
  318. if (r < 0) {
  319. // TIMEOUT:
  320. if (r == -1) {
  321. door.log() << "TIMEOUT" << std::endl;
  322. door << timeout << door::reset << door::nl << door::nl;
  323. } else {
  324. if (r == -3) {
  325. door.log() << "OUTTA TIME" << std::endl;
  326. door::Panel notime = make_notime(mx, my);
  327. door << notime << door::reset << door::nl;
  328. }
  329. }
  330. return 0;
  331. }
  332. door << door::nl;
  333. // door << door::reset << door::cls;
  334. display_starfield(door, rng);
  335. door << m << door::reset << door::nl;
  336. // Normal DOOR exit goes here...
  337. door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
  338. get_logger = nullptr;
  339. cls_display_starfield = nullptr;
  340. return 0;
  341. }