12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166 |
- #include "door.h"
- #include "space.h"
- #include <chrono> // chrono::system_clock
- #include <ctime> // localtime
- #include <iomanip> // put_time
- #include <iostream>
- #include <random>
- #include <string>
- #include "db.h"
- #include "deck.h"
- #include "version.h"
- #include <algorithm> // transform
- void string_toupper(std::string &str) {
- std::transform(str.begin(), str.end(), str.begin(), ::toupper);
- }
- bool replace(std::string &str, const std::string &from, const std::string &to) {
- size_t start_pos = str.find(from);
- if (start_pos == std::string::npos)
- return false;
- str.replace(start_pos, from.length(), to);
- return true;
- }
- bool replace(std::string &str, const char *from, const char *to) {
- size_t start_pos = str.find(from);
- if (start_pos == std::string::npos)
- return false;
- str.replace(start_pos, strlen(from), to);
- return true;
- }
- door::ANSIColor from_string(std::string colorCode);
- std::function<std::ofstream &(void)> get_logger;
- /*
- Cards:
- 4 layers deep.
- https://en.wikipedia.org/wiki/Code_page_437
- using: \xb0, 0xb1, 0xb2, 0xdb
- OR: \u2591, \u2592, \u2593, \u2588
- Like so:
- ##### #####
- ##### #####
- ##### #####
- Cards: (Black on White, or Red on White)
- 8D### TH###
- ##D## ##H##
- ###D8 ###HT
- D, H = Red, Clubs, Spades = Black.
- ^ Where D = Diamonds, H = Hearts
- ♥, ♦, ♣, ♠
- \x03, \x04, \x05, \x06
- \u2665, \u2666, \u2663, \u2660
- Card layout. Actual cards are 3 lines thick.
- ░░░░░ ░░░░░ ░░░░░
- ░░░░░ ░░░░░ ░░░░░
- ▒▒▒▒▒░▒▒▒▒▒ #####░##### #####░#####
- ▒▒▒▒▒ ▒▒▒▒▒ ##### ##### ##### #####
- ▓▓▓▓▓▒▓▓▓▓▓▒▓▓▓▓▓ #####=#####=##### #####=#####=#####
- ▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ##### ##### ##### ##### ##### #####
- █████▓█████▓█████▓#####=#####=#####=#####=#####=#####=#####
- █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
- █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
- #####
- Player Information ##### Time in: xx Time out: xx
- Name: ##### Playing Day: November 3rd
- Hand Score : Current Streak: N
- Todays Score : XX Cards Remaining Longest Streak: NN
- Monthly Score: Playing Hand X of X Most Won: xxx Lost: xxx
- [4] Lf [6] Rt [Space] Play Card [Enter] Draw [D]one [H]elp [R]edraw
- ►, ◄, ▲, ▼
- \x10, \x11, \x1e, \x1f
- \u25ba, \u25c4, \u25b2, \u25bc
- The Name is <- 6 to the left.
- # is back of card. = is upper card showing between.
- There's no fancy anything here. Cards overlap the last
- line of the previous line/card.
- */
- // The idea is that this would be defined elsewhere (maybe)
- int user_score = 0;
- // NOTE: When run as a door, we always detect CP437 (because that's what Enigma
- // is defaulting to)
- void adjust_score(int by) { user_score += by; }
- door::Panel make_timeout(int mx, int my) {
- door::ANSIColor yellowred =
- door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
- std::string line_text("Sorry, you've been inactive for too long.");
- int msgWidth = line_text.length() + (2 * 3); // + padding * 2
- door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
- // place.setTitle(std::make_unique<door::Line>(title), 1);
- timeout.setStyle(door::BorderStyle::DOUBLE);
- timeout.setColor(yellowred);
- door::Line base(line_text);
- base.setColor(yellowred);
- std::string pad1(3, ' ');
- /*
- std::string pad1(3, '\xb0');
- if (door::unicode) {
- std::string unicode;
- door::cp437toUnicode(pad1.c_str(), unicode);
- pad1 = unicode;
- }
- */
- base.setPadding(pad1, yellowred);
- // base.setColor(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLACK));
- std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
- timeout.addLine(std::make_unique<door::Line>(base));
- return timeout;
- }
- door::Panel make_notime(int mx, int my) {
- door::ANSIColor yellowred =
- door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
- std::string line_text("Sorry, you've used up all your time for today.");
- int msgWidth = line_text.length() + (2 * 3); // + padding * 2
- door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
- // place.setTitle(std::make_unique<door::Line>(title), 1);
- timeout.setStyle(door::BorderStyle::DOUBLE);
- timeout.setColor(yellowred);
- door::Line base(line_text);
- base.setColor(yellowred);
- std::string pad1(3, ' ');
- /*
- std::string pad1(3, '\xb0');
- if (door::unicode) {
- std::string unicode;
- door::cp437toUnicode(pad1.c_str(), unicode);
- pad1 = unicode;
- }
- */
- base.setPadding(pad1, yellowred);
- // base.setColor(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLACK));
- std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
- timeout.addLine(std::make_unique<door::Line>(base));
- return timeout;
- }
- door::Menu make_main_menu(void) {
- door::Menu m(5, 5, 25);
- door::Line mtitle(SPACEACE " Main Menu");
- door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
- door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
- door::ATTR::BOLD);
- m.setColor(border_color);
- mtitle.setColor(title_color);
- mtitle.setPadding(" ", title_color);
- m.setTitle(std::make_unique<door::Line>(mtitle), 1);
- // m.setColorizer(true,
- m.setRender(true, door::Menu::makeRender(
- door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
- // m.setColorizer(false,
- m.setRender(false, door::Menu::makeRender(
- door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
- door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
- door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
- door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
- door::ATTR::BOLD)));
- m.addSelection('P', "Play Cards");
- m.addSelection('S', "View Scores");
- m.addSelection('C', "Configure");
- m.addSelection('H', "Help");
- m.addSelection('A', "About this game");
- m.addSelection('Q', "Quit");
- return m;
- }
- door::renderFunction statusValue(door::ANSIColor status,
- door::ANSIColor value) {
- door::renderFunction rf = [status,
- value](const std::string &txt) -> door::Render {
- door::Render r(txt);
- door::ColorOutput co;
- co.pos = 0;
- co.len = 0;
- co.c = status;
- size_t pos = txt.find(':');
- if (pos == std::string::npos) {
- // failed to find - use entire string as status color.
- co.len = txt.length();
- r.outputs.push_back(co);
- } else {
- pos++; // Have : in status color
- co.len = pos;
- r.outputs.push_back(co);
- co.reset();
- co.pos = pos;
- co.c = value;
- co.len = txt.length() - pos;
- r.outputs.push_back(co);
- }
- return r;
- };
- return rf;
- }
- door::renderFunction rStatus = [](const std::string &txt) -> door::Render {
- door::Render r(txt);
- door::ColorOutput co;
- // default colors STATUS: value
- door::ANSIColor status(door::COLOR::BLUE, door::ATTR::BOLD);
- door::ANSIColor value(door::COLOR::YELLOW, door::ATTR::BOLD);
- co.pos = 0;
- co.len = 0;
- co.c = status;
- size_t pos = txt.find(':');
- if (pos == std::string::npos) {
- // failed to find - use entire string as status color.
- co.len = txt.length();
- r.outputs.push_back(co);
- } else {
- pos++; // Have : in status color
- co.len = pos;
- r.outputs.push_back(co);
- co.reset();
- co.pos = pos;
- co.c = value;
- co.len = txt.length() - pos;
- r.outputs.push_back(co);
- }
- return r;
- };
- std::string return_current_time_and_date() {
- auto now = std::chrono::system_clock::now();
- auto in_time_t = std::chrono::system_clock::to_time_t(now);
- std::stringstream ss;
- // ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
- ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %r");
- return ss.str();
- }
- int press_a_key(door::Door &door) {
- door << door::reset << "Press a key to continue...";
- int r = door.sleep_key(door.inactivity);
- door << door::nl;
- return r;
- }
- door::Menu make_config_menu(void) {
- door::Menu m(5, 5, 31);
- door::Line mtitle(SPACEACE " Configuration Menu");
- door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
- door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
- door::ATTR::BOLD);
- m.setColor(border_color);
- mtitle.setColor(title_color);
- mtitle.setPadding(" ", title_color);
- m.setTitle(std::make_unique<door::Line>(mtitle), 1);
- // m.setColorizer(true,
- m.setRender(true, door::Menu::makeRender(
- door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
- // m.setColorizer(false,
- m.setRender(false, door::Menu::makeRender(
- door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
- door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
- door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
- door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
- door::ATTR::BOLD)));
- m.addSelection('D', "Deck Colors");
- m.addSelection('Q', "Quit");
- return m;
- }
- vector<std::string> deck_colors = {std::string("All"), std::string("Blue"),
- std::string("Cyan"), std::string("Green"),
- std::string("Magenta"), std::string("Red")};
- /**
- * @brief menu render that sets the text color based on the color found in the
- * text itself.
- *
- * @param c1
- * @param c2
- * @param c3
- * @return door::renderFunction
- */
- door::renderFunction makeColorRender(door::ANSIColor c1, door::ANSIColor c2,
- door::ANSIColor c3) {
- door::renderFunction render = [c1, c2,
- c3](const std::string &txt) -> door::Render {
- door::Render r(txt);
- bool option = true;
- door::ColorOutput co;
- // I need this mutable
- door::ANSIColor textColor = c3;
- // Color update:
- {
- std::string found;
- for (auto &dc : deck_colors) {
- if (txt.find(dc) != string::npos) {
- found = dc;
- break;
- }
- }
- if (!found.empty()) {
- if (found == "All") {
- // handle this some other way.
- textColor.setFg(door::COLOR::WHITE);
- } else {
- door::ANSIColor c = from_string(found);
- textColor.setFg(c.getFg());
- }
- }
- }
- co.pos = 0;
- co.len = 0;
- co.c = c1;
- // d << blue;
- int tpos = 0;
- for (char const &c : txt) {
- if (option) {
- if (c == '[' or c == ']') {
- if (co.c != c1)
- if (co.len != 0) {
- r.outputs.push_back(co);
- co.reset();
- co.pos = tpos;
- }
- co.c = c1;
- if (c == ']')
- option = false;
- } else {
- if (co.c != c2)
- if (co.len != 0) {
- r.outputs.push_back(co);
- co.reset();
- co.pos = tpos;
- }
- co.c = c2;
- }
- } else {
- if (co.c != textColor)
- if (co.len != 0) {
- r.outputs.push_back(co);
- co.reset();
- co.pos = tpos;
- }
- co.c = textColor;
- }
- co.len++;
- tpos++;
- }
- if (co.len != 0) {
- r.outputs.push_back(co);
- }
- return r;
- };
- return render;
- }
- door::Menu make_deck_menu(void) {
- door::Menu m(5, 5, 31);
- door::Line mtitle(SPACEACE " Deck Menu");
- door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
- door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
- door::ATTR::BOLD);
- m.setColor(border_color);
- mtitle.setColor(title_color);
- mtitle.setPadding(" ", title_color);
- m.setTitle(std::make_unique<door::Line>(mtitle), 1);
- /*
- // m.setColorizer(true,
- m.setRender(true, door::Menu::makeRender(
- door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::BLUE,
- door::ATTR::BOLD)));
- // m.setColorizer(false,
- m.setRender(false, door::Menu::makeRender(
- door::ANSIColor(door::COLOR::YELLOW,
- door::COLOR::BLUE, door::ATTR::BOLD), door::ANSIColor(door::COLOR::WHITE,
- door::COLOR::BLUE, door::ATTR::BOLD), door::ANSIColor(door::COLOR::YELLOW,
- door::COLOR::BLUE, door::ATTR::BOLD), door::ANSIColor(door::COLOR::CYAN,
- door::COLOR::BLUE, door::ATTR::BOLD)));
- */
- m.setRender(true, makeColorRender(
- door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD)));
- m.setRender(false, makeColorRender(
- door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
- door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
- door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
- door::ATTR::BOLD)));
- for (auto iter = deck_colors.begin(); iter != deck_colors.end(); ++iter) {
- char c = (*iter)[0];
- m.addSelection(c, (*iter).c_str());
- }
- /*
- m.addSelection('A', "All");
- m.addSelection('B', "Blue");
- m.addSelection('C', "Cyan");
- m.addSelection('G', "Green");
- m.addSelection('M', "Magenta");
- m.addSelection('R', "Red");
- */
- return m;
- }
- bool iequals(const string &a, const string &b) {
- unsigned int sz = a.size();
- if (b.size() != sz)
- return false;
- for (unsigned int i = 0; i < sz; ++i)
- if (tolower(a[i]) != tolower(b[i]))
- return false;
- return true;
- }
- // convert a string to an option
- // an option to the string to store
- door::ANSIColor from_string(std::string colorCode) {
- std::map<std::string, door::ANSIColor> codeMap = {
- {std::string("BLUE"), door::ANSIColor(door::COLOR::BLUE)},
- {std::string("RED"), door::ANSIColor(door::COLOR::RED)},
- {std::string("CYAN"), door::ANSIColor(door::COLOR::CYAN)},
- {std::string("GREEN"), door::ANSIColor(door::COLOR::GREEN)},
- {std::string("MAGENTA"), door::ANSIColor(door::COLOR::MAGENTA)}};
- std::string code = colorCode;
- string_toupper(code);
- auto iter = codeMap.find(code);
- if (iter != codeMap.end()) {
- return iter->second;
- }
- // And if it doesn't match, and isn't ALL ... ?
- // if (code.compare("ALL") == 0) {
- std::random_device dev;
- std::mt19937_64 rng(dev());
- std::uniform_int_distribution<size_t> idDist(0, codeMap.size() - 1);
- iter = codeMap.begin();
- std::advance(iter, idDist(rng));
- return iter->second;
- // }
- }
- // This does not seem to be working. I keep getting zero.
- int opt_from_string(std::string colorCode) {
- for (std::size_t pos = 0; pos != deck_colors.size(); ++pos) {
- // if (caseInsensitiveStringCompare(colorCode, deck_colors[pos]) == 0) {
- if (iequals(colorCode, deck_colors[pos])) {
- return pos;
- }
- }
- return 0;
- }
- std::string from_color_option(int opt) { return deck_colors[opt]; }
- int configure(door::Door &door, DBData &db) {
- auto menu = make_config_menu();
- int r = 0;
- while (r >= 0) {
- r = menu.choose(door);
- if (r > 0) {
- door << door::reset << door::cls;
- char c = menu.which(r - 1);
- if (c == 'D') {
- // Ok, deck colors
- // get default
- std::string key("DeckColor");
- std::string currentDefault = db.getSetting(key, std::string("ALL"));
- int currentOpt = opt_from_string(currentDefault);
- door << door::reset << door::cls;
- auto deck = make_deck_menu();
- deck.defaultSelection(currentOpt);
- int newOpt = deck.choose(door);
- door << door::reset << door::cls;
- if (newOpt >= 0) {
- newOpt--;
- std::string newColor = from_color_option(newOpt);
- if (newOpt != currentOpt) {
- door.log() << key << " was " << currentDefault << ", " << currentOpt
- << ". Now " << newColor << ", " << newOpt << std::endl;
- db.setSetting(key, newColor);
- }
- }
- }
- if (c == 'Q') {
- return r;
- }
- }
- }
- return r;
- }
- int play_cards(door::Door &door, DBData &db, std::mt19937 &rng) {
- int mx = door.width;
- int my = door.height;
- // cards color --
- // configured by the player.
- door::ANSIColor deck_color;
- std::string key("DeckColor");
- std::string currentDefault = db.getSetting(key, std::string("ALL"));
- door.log() << key << " shows as " << currentDefault << std::endl;
- deck_color = from_string(currentDefault);
- /*
- // RED, BLUE, GREEN, MAGENTA, CYAN
- std::uniform_int_distribution<int> rand_color(0, 4);
- switch (rand_color(rng)) {
- case 0:
- deck_color = door::ANSIColor(door::COLOR::RED);
- break;
- case 1:
- deck_color = door::ANSIColor(door::COLOR::BLUE);
- break;
- case 2:
- deck_color = door::ANSIColor(door::COLOR::GREEN);
- break;
- case 3:
- deck_color = door::ANSIColor(door::COLOR::MAGENTA);
- break;
- case 4:
- deck_color = door::ANSIColor(door::COLOR::CYAN);
- break;
- default:
- deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
- break;
- }
- */
- int height = 3;
- Deck d(deck_color, height);
- door::Panel *c;
- door << door::reset << door::cls;
- // This displays the cards in the upper left corner.
- // We want them center, and down some.
- const int space = 3;
- // int cards_dealt_width = 59; int cards_dealt_height = 9;
- int game_width;
- int game_height = 13; // 9;
- {
- int cx, cy, level;
- cardgo(27, space, height, cx, cy, level);
- game_width = cx + 5; // card width
- }
- int off_x = (mx - game_width) / 2;
- int off_y = (my - game_height) / 2;
- // The idea is to see the cards with <<Something Unique to the card game>>,
- // Year, Month, Day, and game (like 1 of 3).
- // This will make the games the same/fair for everyone.
- std::string tripeaks(" " SPACEACE " - Tri-Peaks Solitaire v" SPACEACE_VERSION
- " ");
- int tp_off_x = (mx - tripeaks.size()) / 2;
- door::Panel spaceAceTriPeaks(tp_off_x, off_y, tripeaks.size());
- spaceAceTriPeaks.setStyle(door::BorderStyle::SINGLE);
- spaceAceTriPeaks.setColor(
- door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLACK));
- spaceAceTriPeaks.addLine(
- std::make_unique<door::Line>(tripeaks, tripeaks.size()));
- door << spaceAceTriPeaks;
- off_y += 3;
- std::seed_seq s1{2021, 2, 27, 1};
- cards deck1 = card_shuffle(s1, 1);
- cards state = card_states();
- // I tried setting the cursor before the delay and before displaying the
- // card. It is very hard to see / just about useless. Not worth the effort.
- for (int x = 0; x < 28; x++) {
- int cx, cy, level;
- cardgo(x, space, height, cx, cy, level);
- // This is hardly visible.
- // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
- std::this_thread::sleep_for(std::chrono::milliseconds(75));
- c = d.back(level);
- c->set(cx + off_x, cy + off_y);
- door << *c;
- }
- /*
- std::this_thread::sleep_for(
- std::chrono::seconds(1)); // 3 secs seemed too long!
- */
- for (int x = 18; x < 28; x++) {
- int cx, cy, level;
- // usleep(1000 * 20);
- state.at(x) = 1;
- cardgo(x, space, height, cx, cy, level);
- // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
- c = d.card(deck1.at(x));
- c->set(cx + off_x, cy + off_y);
- door << *c;
- }
- door << door::reset;
- door << door::nl << door::nl;
- int r = door.sleep_key(door.inactivity);
- return r;
- }
- door::Panel make_about(void) {
- door::Panel about(60);
- about.setStyle(door::BorderStyle::DOUBLE_SINGLE);
- about.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
- door::ATTR::BOLD));
- about.addLine(std::make_unique<door::Line>("About This Door", 60));
- about.addLine(std::make_unique<door::Line>(
- "---------------------------------", 60,
- door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE, door::ATTR::BOLD)));
- /*
- 123456789012345678901234567890123456789012345678901234567890
- This door was written by Bugz.
- It is written in c++, only supports Linux, and replaces
- opendoors.
- It's written in c++, and replaces the outdated opendoors
- library.
- */
- about.addLine(
- std::make_unique<door::Line>(SPACEACE " v" SPACEACE_VERSION, 60));
- std::string copyright = SPACEACE_COPYRIGHT;
- if (door::unicode) {
- /*
- std::string textcp = "(C)";
- std::string utf8cp = "\u00a9";
- replace(copyright, textcp, utf8cp);*/
- replace(copyright, "(C)", "\u00a9");
- }
- about.addLine(std::make_unique<door::Line>(copyright, 60));
- about.addLine(std::make_unique<door::Line>("", 60));
- about.addLine(
- std::make_unique<door::Line>("This door was written by Bugz.", 60));
- about.addLine(std::make_unique<door::Line>("", 60));
- about.addLine(std::make_unique<door::Line>(
- "It is written in c++, only support Linux, and replaces", 60));
- about.addLine(std::make_unique<door::Line>("opendoors.", 60));
- /*
- door::updateFunction updater = [](void) -> std::string {
- std::string text = "Currently: ";
- text.append(return_current_time_and_date());
- return text;
- };
- std::string current = updater();
- door::Line active(current, 60);
- active.setUpdater(updater);
- active.setRender(renderStatusValue(
- door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
- door::ATTR::BOLD), door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
- door::ATTR::BOLD)));
- about.addLine(std::make_unique<door::Line>(active));
- */
- /*
- about.addLine(std::make_unique<door::Line>(
- "Status: blue", 60,
- statusValue(door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD),
- door::ANSIColor(door::COLOR::MAGENTA,
- door::ATTR::BLINK)))); about.addLine(std::make_unique<door::Line>("Name:
- BUGZ", 60, rStatus)); about.addLine(std::make_unique<door::Line>( "Size:
- 10240", 60, statusValue(door::ANSIColor(door::COLOR::GREEN,
- door::COLOR::BLUE, door::ATTR::BOLD), door::ANSIColor(door::COLOR::YELLOW,
- door::COLOR::BLUE, door::ATTR::BOLD, door::ATTR::BLINK))));
- about.addLine(std::make_unique<door::Line>("Bugz is here.", 60, rStatus));
- */
- return about;
- }
- void display_starfield(door::Door &door, std::mt19937 &rng) {
- door << door::reset << door::cls;
- int mx = door.width;
- int my = door.height;
- // display starfield
- const char *stars[2];
- stars[0] = ".";
- if (door::unicode) {
- stars[1] = "\u2219"; // "\u00b7";
- } else {
- stars[1] = "\xf9"; // "\xfa";
- };
- {
- // Make uniform random distribution between 1 and MAX screen size X/Y
- std::uniform_int_distribution<int> uni_x(1, mx);
- std::uniform_int_distribution<int> uni_y(1, my);
- door::ANSIColor white(door::COLOR::WHITE);
- door::ANSIColor dark(door::COLOR::BLACK, door::ATTR::BRIGHT);
- // 10 is too many, 100 is too few. 40 looks ok.
- int MAX_STARS = ((mx * my) / 40);
- // door.log() << "Generating starmap using " << mx << "," << my << " : "
- // << MAX_STARS << " stars." << std::endl;
- for (int x = 0; x < MAX_STARS; x++) {
- door::Goto star_at(uni_x(rng), uni_y(rng));
- door << star_at;
- if (x % 5 < 2)
- door << dark;
- else
- door << white;
- if (x % 2 == 0)
- door << stars[0];
- else
- door << stars[1];
- }
- }
- }
- void display_space_ace(door::Door &door) {
- int mx = door.width;
- int my = door.height;
- // space_ace is 72 chars wide, 6 high
- int sa_x = (mx - 72) / 2;
- int sa_y = (my - 6) / 2;
- // output the SpaceAce logo -- centered!
- for (const auto s : space) {
- door::Goto sa_at(sa_x, sa_y);
- door << sa_at;
- if (door::unicode) {
- std::string unicode;
- door::cp437toUnicode(s, unicode);
- door << unicode; // << door::nl;
- } else
- door << s; // << door::nl;
- sa_y++;
- }
- // pause 5 seconds so they can enjoy our awesome logo -- if they want.
- door.sleep_key(5);
- }
- void display_starfield_space_ace(door::Door &door, std::mt19937 &rng) {
- // mx = door.width;
- // my = door.height;
- display_starfield(door, rng);
- display_space_ace(door);
- door << door::reset;
- }
- int main(int argc, char *argv[]) {
- door::Door door("space-ace", argc, argv);
- // door << door::reset << door::cls << door::nl;
- get_logger = [&door]() -> ofstream & { return door.log(); };
- DBData spacedb;
- spacedb.setUser(door.username);
- time_t last_call = std::stol(spacedb.getSetting("LastCall", "0"));
- time_t now =
- std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
- spacedb.setSetting("LastCall", std::to_string(now));
- if (last_call != 0) {
- door << "Welcome Back!" << door::nl;
- auto nowClock = std::chrono::system_clock::from_time_t(now);
- auto lastClock = std::chrono::system_clock::from_time_t(last_call);
- auto delta = nowClock - lastClock;
- // int days = chrono::duration_cast<chrono::days>(delta).count(); // c++ 20
- int hours = chrono::duration_cast<chrono::hours>(delta).count();
- int days = hours / 24;
- int minutes = chrono::duration_cast<chrono::minutes>(delta).count();
- if (days > 1) {
- door << "It's been " << days << " days since you last played."
- << door::nl;
- } else {
- if (hours > 1) {
- door << "It's been " << hours << " hours since you last played."
- << door::nl;
- } else {
- door << "It's been " << minutes << " minutes since you last played."
- << door::nl;
- }
- }
- press_a_key(door);
- }
- /*
- std::function<std::ofstream &(void)> get_logger;
- get_logger = [&door]() -> ofstream & { return door.log(); };
- if (get_logger) {
- get_logger() << "MEOW" << std::endl;
- get_logger() << "hey! It works!" << std::endl;
- }
- */
- // spacedb.init();
- /*
- // Example: How to read/set values in spacedb settings.
- std::string setting = "last_play";
- std::string user = door.username;
- std::string value;
- std::string blank = "<blank>";
- value = spacedb.getSetting(user, setting, blank);
- door << door::reset << "last_play: " << value << door::nl;
- std::this_thread::sleep_for(std::chrono::seconds(2));
- value = return_current_time_and_date();
- spacedb.setSetting(user, setting, value);
- */
- // https://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range
- std::random_device rd; // only used once to initialise (seed) engine
- std::mt19937 rng(rd());
- // random-number engine used (Mersenne-Twister in this case)
- // std::uniform_int_distribution<int> uni(min, max); // guaranteed unbiased
- int mx, my; // Max screen width/height
- if (door.width == 0) {
- // screen detect failed, use sensible defaults
- door.width = mx = 80;
- door.height = my = 23;
- } else {
- mx = door.width;
- my = door.height;
- }
- // We assume here that the width and height are something crazy like 10x15.
- // :P
- display_starfield_space_ace(door, rng);
- // for testing inactivity timeout
- // door.inactivity = 10;
- door::Panel timeout = make_timeout(mx, my);
- door::Menu m = make_main_menu();
- door::Panel about = make_about(); // 8 lines
- // center the about box
- about.set((mx - 60) / 2, (my - 9) / 2);
- int r = 0;
- while ((r >= 0) and (r != 6)) {
- // starfield + menu ?
- display_starfield(door, rng);
- r = m.choose(door);
- // need to reset the colors. (whoops!)
- door << door::reset << door::cls; // door::nl;
- // OK! The screen is blank at this point!
- switch (r) {
- case 1: // play game
- r = play_cards(door, spacedb, rng);
- break;
- case 2: // view scores
- door << "Show scores goes here!" << door::nl;
- r = press_a_key(door);
- break;
- case 3: // configure
- r = configure(door, spacedb);
- // r = press_a_key(door);
- break;
- case 4: // help
- door << "Help! Need some help here..." << door::nl;
- r = press_a_key(door);
- break;
- case 5: // about
- display_starfield(door, rng);
- door << about << door::nl;
- r = press_a_key(door);
- break;
- case 6: // quit
- break;
- }
- }
- if (r < 0) {
- TIMEOUT:
- if (r == -1) {
- door.log() << "TIMEOUT" << std::endl;
- door << timeout << door::reset << door::nl << door::nl;
- } else {
- if (r == -3) {
- door.log() << "OUTTA TIME" << std::endl;
- door::Panel notime = make_notime(mx, my);
- door << notime << door::reset << door::nl;
- }
- }
- return 0;
- }
- door << door::nl;
- /*
- // magic time!
- door << door::reset << door::nl << "Press another key...";
- int x;
- for (x = 0; x < 60; ++x) {
- r = door.sleep_key(1);
- if (r == -1) {
- // ok! Expected timeout!
- // PROBLEM: regular "local" terminal loses current attributes
- // when cursor is save / restored.
- door << door::SaveCursor;
- if (about.update(door)) {
- // ok I need to "fix" the cursor position.
- // it has moved.
- }
- door << door::RestoreCursor << door::reset;
- } else {
- if (r < 0)
- goto TIMEOUT;
- if (r >= 0)
- break;
- }
- }
- if (x == 60)
- goto TIMEOUT;
- */
- door << door::nl;
- #ifdef NNY
- // configured by the player.
- door::ANSIColor deck_color;
- // RED, BLUE, GREEN, MAGENTA, CYAN
- std::uniform_int_distribution<int> rand_color(0, 4);
- switch (rand_color(rng)) {
- case 0:
- deck_color = door::ANSIColor(door::COLOR::RED);
- break;
- case 1:
- deck_color = door::ANSIColor(door::COLOR::BLUE);
- break;
- case 2:
- deck_color = door::ANSIColor(door::COLOR::GREEN);
- break;
- case 3:
- deck_color = door::ANSIColor(door::COLOR::MAGENTA);
- break;
- case 4:
- deck_color = door::ANSIColor(door::COLOR::CYAN);
- break;
- default:
- deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
- break;
- }
- int height = 3;
- Deck d(deck_color, height);
- door::Panel *c;
- door << door::reset << door::cls;
- // This displays the cards in the upper left corner.
- // We want them center, and down some.
- int space = 3;
- // int cards_dealt_width = 59; int cards_dealt_height = 9;
- int game_width;
- {
- int cx, cy, level;
- cardgo(27, space, height, cx, cy, level);
- game_width = cx + 5; // card width
- }
- int off_x = (mx - game_width) / 2;
- int off_y = (my - 9) / 2;
- // The idea is to see the cards with <<Something Unique to the card game>>,
- // Year, Month, Day, and game (like 1 of 3).
- // This will make the games the same/fair for everyone.
- std::seed_seq s1{2021, 2, 27, 1};
- cards deck1 = card_shuffle(s1, 1);
- cards state = card_states();
- // I tried setting the cursor before the delay and before displaying the
- // card. It is very hard to see / just about useless. Not worth the effort.
- for (int x = 0; x < 28; x++) {
- int cx, cy, level;
- cardgo(x, space, height, cx, cy, level);
- // This is hardly visible.
- // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
- std::this_thread::sleep_for(std::chrono::milliseconds(75));
- c = d.back(level);
- c->set(cx + off_x, cy + off_y);
- door << *c;
- }
- /*
- std::this_thread::sleep_for(
- std::chrono::seconds(1)); // 3 secs seemed too long!
- */
- for (int x = 18; x < 28; x++) {
- int cx, cy, level;
- // usleep(1000 * 20);
- state.at(x) = 1;
- cardgo(x, space, height, cx, cy, level);
- // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
- c = d.card(deck1.at(x));
- c->set(cx + off_x, cy + off_y);
- door << *c;
- }
- door << door::reset;
- door << door::nl << door::nl;
- r = door.sleep_key(door.inactivity);
- if (r < 0)
- goto TIMEOUT;
- #endif
- /*
- door::Panel *p = d.back(2);
- p->set(10, 10);
- door << *p;
- door::Panel *d8 = d.card(8);
- d8->set(20, 8);
- door << *d8;
- r = door.sleep_key(door.inactivity);
- if (r < 0)
- goto TIMEOUT;
- */
- // door << door::reset << door::cls;
- display_starfield(door, rng);
- door << m << door::reset << door::nl;
- // Normal DOOR exit goes here...
- door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
- get_logger = nullptr;
- return 0;
- }
|