main.cpp 11 KB

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