main.cpp 10 KB

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