123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644 |
- #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"
- int user_score = 0;
- 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);
- door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
-
- timeout.setStyle(door::BorderStyle::DOUBLE);
- timeout.setColor(yellowred);
- door::Line base(line_text);
- base.setColor(yellowred);
- std::string pad1(3, ' ');
-
- base.setPadding(pad1, yellowred);
-
- 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);
- door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
-
- timeout.setStyle(door::BorderStyle::DOUBLE);
- timeout.setColor(yellowred);
- door::Line base(line_text);
- base.setColor(yellowred);
- std::string pad1(3, ' ');
-
- base.setPadding(pad1, yellowred);
-
- 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("Space-Ace 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.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.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('1', "Play Cards");
- m.addSelection('2', "View Scores");
- m.addSelection('3', "Drop to DOS");
- m.addSelection('4', "Chat with BUGZ");
- 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) {
-
- co.len = txt.length();
- r.outputs.push_back(co);
- } else {
- pos++;
- 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;
-
- 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) {
-
- co.len = txt.length();
- r.outputs.push_back(co);
- } else {
- pos++;
- 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 %r");
- return ss.str();
- }
- door::Panel make_about(void) {
- door::Panel about(2, 2, 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)));
-
- 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));
-
- return about;
- }
- void display_starfield(int mx, int my, door::Door &door, std::mt19937 &rng) {
- door << door::reset << door::cls;
-
- const char *stars[2];
- stars[0] = ".";
- if (door::unicode) {
- stars[1] = "\u2219";
- } else {
- stars[1] = "\xf9";
- };
- {
-
- 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);
- for (int x = 0; x < (mx * my / 100); 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(int mx, int my, door::Door &door) {
-
- int sa_x = (mx - 72) / 2;
- int sa_y = (my - 6) / 2;
-
- 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;
- } else
- door << s;
- sa_y++;
- }
-
- door.sleep_key(5);
- }
- void display_starfield_space_ace(int mx, int my, door::Door &door,
- std::mt19937 &rng) {
- display_starfield(mx, my, door, rng);
- display_space_ace(mx, my, door);
- door << door::reset;
- }
- int main(int argc, char *argv[]) {
-
- door::Door door("space-ace", argc, argv);
-
-
-
-
- DBData spacedb;
-
- 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);
-
- std::random_device rd;
- std::mt19937 rng(
- rd());
-
- int mx, my;
- if (door.width == 0) {
-
- mx = 80;
- my = 23;
- } else {
- mx = door.width;
- my = door.height;
- }
-
- display_starfield_space_ace(mx, my, door, rng);
-
-
- door::Panel timeout = make_timeout(mx, my);
- door::Menu m = make_main_menu();
- int r = m.choose(door);
-
- door << door::reset << door::nl;
- if (r < 0) {
- TIMEOUT:
- if (r == -1) {
- door.log("TIMEOUT");
- door << timeout << door::reset << door::nl << door::nl;
- } else {
- if (r == -3) {
- door.log("OUTTA TIME");
- door::Panel notime = make_notime(mx, my);
- door << notime << door::reset << door::nl;
- }
- }
- return 0;
- }
-
- door << door::nl;
- door::Panel about = make_about();
- about.set((mx - 60) / 2, (my - 5) / 2);
- door << about;
-
- door << door::reset << door::nl << "Press another key...";
- int x;
- for (x = 0; x < 60; ++x) {
- r = door.sleep_key(1);
- if (r == -1) {
-
-
-
- door << door::SaveCursor;
- if (about.update(door)) {
-
-
- }
- door << door::RestoreCursor << door::reset;
- } else {
- if (r < 0)
- goto TIMEOUT;
- if (r >= 0)
- break;
- }
- }
- if (x == 60)
- goto TIMEOUT;
- door << door::nl;
- door::ANSIColor deck_color;
-
- 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;
-
-
- int space = 3;
-
- int game_width;
- {
- int cx, cy, level;
- cardgo(27, space, height, cx, cy, level);
- game_width = cx + 5;
- }
- int off_x = (mx - game_width) / 2;
- int off_y = (my - 9) / 2;
- std::seed_seq s1{2021, 2, 27, 1};
- cards deck1 = card_shuffle(s1, 1);
- cards state = card_states();
-
-
- for (int x = 0; x < 28; x++) {
- int cx, cy, level;
- cardgo(x, space, height, cx, cy, level);
-
-
- std::this_thread::sleep_for(std::chrono::milliseconds(75));
- c = d.back(level);
- c->set(cx + off_x, cy + off_y);
- door << *c;
- }
-
- for (int x = 18; x < 28; x++) {
- int cx, cy, level;
-
- state.at(x) = 1;
- cardgo(x, space, height, cx, cy, level);
-
- 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;
-
-
- display_starfield(mx, my, door, rng);
- door << m << door::reset << door::nl << "This is what the menu looked liked!"
- << door::nl;
-
- door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
- return 0;
- }
|