123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562 |
- #include "director.h"
- #include <boost/format.hpp>
- #include "boxes.h"
- #include "galaxy.h"
- #include "logging.h"
- #include "utils.h"
- Director::Director() {
- BUGZ_LOG(warning) << "Director::Director()";
-
- game = 0;
- galaxy.reset();
-
-
- active = false;
-
- talk_direct = true;
- show_client = true;
- count = 0;
-
- SF_cimline = [this](const std::string &s) { this->SL_cimline(s); };
- SF_sectorline = [this](const std::string &s) { this->SL_sectorline(s); };
- SF_portline = [this](const std::string &s) { this->SL_portline(s); };
- SF_warpline = [this](const std::string &s) { this->SL_warpline(s); };
- build_menu();
- }
- Director::~Director() { BUGZ_LOG(warning) << "Director::~Director()"; }
- void Director::client_input(const std::string &input) {
-
- if (chain) {
- chain->client_input(input);
- return;
- }
- if (active) {
- if (input == "Q" || input == "q") proxy_deactivate();
- return;
- } else if (input == "\x1b" || input == "~") {
- std::string &prompt = current_prompt;
- BUGZ_LOG(trace) << "CI: ACTIVATE prompt shows: [" << prompt << "]";
- if (prompt == "Selection (? for menu): ") {
- to_client(
- "\n\rThere's not much we can do here. Activate in-game at a "
- "Command prompt.\n\r");
- to_client(current_raw_prompt);
- return;
- }
-
- if (prompt == "Enter your choice: ") {
- to_client(
- "\n\r\x1b[1;36mI'd choose \x1b[1;37m`T`\x1b[1;36m, but "
- "that's how I was coded.\n\r");
- to_client(current_raw_prompt);
- return;
- }
-
- if (prompt == "[Pause]") {
- to_client(" \x1b[1;36mPAWS\x1b[0m\n\r");
- to_client(current_raw_prompt);
- return;
- }
- if (prompt == "Planet command (?=help) [D] ") {
-
- return;
- }
-
-
-
-
-
- if (startswith(prompt, "Command [")) {
-
-
- if (endswith(prompt, "] (?=Help)? : ")) {
-
- proxy_activate();
- return;
- }
- }
- }
-
- if (talk_direct) to_server(input);
-
- }
- void Director::server_line(const std::string &line,
- const std::string &raw_line) {
-
- if (line.find("TradeWars Game Server ") != std::string::npos) {
- to_client("\rTradeWars Proxy v2++ READY (~ or ESC to activate)\n\r");
-
-
- }
- if (line.find("Selection (? for menu): ") != std::string::npos) {
- char ch = line[line.length() - 1];
- if (ch >= 'A' && ch < 'Q') {
- if ((game) && (game != ch)) {
- galaxy.save();
- }
- game = ch;
- BUGZ_LOG(warning) << "GAME " << game << " activated!";
-
- galaxy.game = game;
- galaxy.username = username;
- galaxy.load();
- }
-
- if (ch == 'Q') {
- if (game) {
-
- galaxy.save();
- }
- game = 0;
- galaxy.reset();
- }
- }
- if (game) {
-
-
- if (startswith(line, " Items Status Trading % of max OnBoard"))
- SL_parser = SF_portline;
- if (startswith(line, "Sector : ")) SL_parser = SF_sectorline;
- if (line == ": ") SL_parser = SF_cimline;
- }
- if (SL_parser) {
- SL_parser(line);
- }
-
- if (chain) {
- chain->server_line(line, raw_line);
- }
- }
- void Director::server_prompt(const std::string &prompt,
- const std::string &raw_prompt) {
- current_prompt = prompt;
- current_raw_prompt = raw_prompt;
- if (game) {
- if (prompt == "Selection (? for menu): ") {
- galaxy.save();
- game = 0;
- galaxy.reset();
- }
-
- if (startswith(prompt, "Command [") && endswith(prompt, "] (?=Help)? : ")) {
- std::string sector_text;
- size_t before, after;
- before = prompt.find("]:[") + 3;
- after = prompt.find("] (?=Help)");
- sector_text = prompt.substr(before, after - before);
- current_sector = stoi(sector_text);
- BUGZ_LOG(fatal) << "Sector: " << sector_text;
- }
- }
-
- if (chain) chain->server_prompt(prompt);
- }
- void Director::build_menu(void) {
- main_menu = std::make_shared<MenuDispatch>(*this);
- MenuDispatch *md = static_cast<MenuDispatch *>(&(*main_menu));
- md->menu_box_color = "\x1b[1;33;44m";
- md->menu_text_color = "\x1b[1;37;44m";
- md->menu_title = "Proxy Menu";
- md->menu_options_color = "\x1b[1;36;40m";
- md->menu_prompt =
- "\x1b[0;31;40m\xdb\xb2\xb1\xb0 \x1b[31;40mRED "
- "\x1b[32;40mGREEN\x1b[30;42m\xdb\xb2\xb1\xb0 \x1b[0m : ";
- md->lazy = true;
- md->menu = {{"C", "Configure"},
- {"D", "Display Report"},
- {"E", "Export Data/Save"},
- {"I", "Information"},
- {"P", "Port CIM"},
- {"W", "Warp CIM"},
- {"T", "Trading Report"},
- {"S", "Scripts"},
- {"X", "eXit"}};
- md->setNotify([this]() { this->menu_choice(); });
- cim = std::make_shared<CIMDispatch>(*this);
- cim->setNotify([this]() { this->cim_done(); });
- }
- void Director::proxy_activate(void) {
- active = true;
- to_server(" ");
-
- talk_direct = false;
- show_client = false;
-
- old_prompt = current_prompt;
- old_raw_prompt = current_raw_prompt;
- to_client("\x1b[0m\n\r");
-
- Boxes box(30, 1, true);
- box.boxcolor = "\x1b[1;33;44m";
- box.textcolor = "\x1b[1;33;44m";
- to_client(box.top());
- std::string output = " TradeWars Proxy \x1b[5mActive\x1b[0;1;33;44m ";
- to_client(box.row(output));
- to_client(box.bottom());
- chain = main_menu;
- main_menu->activate();
-
- }
- void Director::menu_choice(void) {
- MenuDispatch *md = dynamic_cast<MenuDispatch *>(&(*chain));
- if (md) {
- if (md->input.empty()) {
- to_client("Menu aborted.\n\r");
- proxy_deactivate();
- return;
- } else {
- switch (md->input[0]) {
- case 'C':
- break;
- case 'D':
- break;
- case 'E':
- to_client("Saving...");
- galaxy.save();
- to_client("\rSaved....\n\r");
- break;
- case 'I':
- information();
- break;
- case 'P':
-
-
- chain = cim;
- to_server("^RQ");
- to_client("Port CIM Report\n\r");
- chain->activate();
- return;
- break;
- case 'W':
- chain = cim;
- to_server("^IQ");
- to_client("Warp CIM Report\n\r");
- chain->activate();
- return;
- break;
- case 'T':
- break;
- case 'S':
- break;
- case 'X':
- proxy_deactivate();
- return;
- }
-
- md->activate();
- }
- }
- }
- void Director::have_input(void) {
- ++count;
- InputDispatch *id = dynamic_cast<InputDispatch *>(&(*chain));
- if (id) {
- std::string output =
- str(boost::format("Your Input (%2%): [%1%]\n\r") % id->input % count);
- to_client("\x1b[0m");
- to_client(output);
- } else {
- proxy_deactivate();
- return;
- }
- if (count > 3) {
- proxy_deactivate();
- } else {
- chain->activate();
- }
- }
- void Director::cim_done(void) {
- BUGZ_LOG(info) << "CIM done";
- chain = main_menu;
- main_menu->activate();
- }
- void Director::information(void) {
- std::string output;
- to_client("I currently know the following:\n\r");
- output = str(
- boost::format("Ports: %1%, Sectors: %2%, Config: %3%, Meta: %4%\n\r") %
- galaxy.ports.size() % galaxy.warps.size() % galaxy.config.size() %
- galaxy.meta.size());
- to_client(output);
- }
- void Director::proxy_deactivate(void) {
- active = false;
-
- talk_direct = true;
- show_client = true;
- chain.reset();
- to_client("\n\r");
- to_client(current_raw_prompt);
- }
- void Director::SL_cimline(const std::string &line) {
- if (line == ": ENDINTERROG") {
- SL_parser = nullptr;
- return;
- }
- if (line == ": ") {
-
-
-
-
- return;
- }
- if (line.empty()) {
- SL_parser = nullptr;
- return;
- }
-
-
-
-
- if (in(line, "%")) {
-
- struct port p = parse_portcim(line);
- if (p.sector == 0)
- BUGZ_LOG(fatal) << "portcim: FAIL [" << line << "]";
- else
- BUGZ_LOG(trace) << "portcim: " << p;
- galaxy.add_port(p);
- } else {
-
-
- auto warps = split(line);
- sector_warps sw;
- for (auto const &w : warps) {
- if (sw.sector == 0) {
- sw.sector = stoi(w);
- } else {
- sw.add(stoi(w));
- }
- }
- BUGZ_LOG(trace) << "warpcim: " << sw;
- galaxy.add_warp(sw);
- }
- }
- void Director::SL_thiefline(const std::string &line) {
- size_t pos = line.find("Suddenly you're Busted!");
- bool busted = pos != line.npos;
- if (busted) {
- BUGZ_LOG(fatal) << "set bust";
- SL_parser = nullptr;
- } else {
- pos = line.find("(You realize the guards saw you last time!)");
- if (pos != line.npos) SL_parser = nullptr;
- }
-
- }
- void Director::SL_sectorline(const std::string &line) {
- BUGZ_LOG(fatal) << "sectorline: [" << line << "]";
- if (line.empty()) {
- SL_parser = nullptr;
- } else {
-
- if (in(line, "Sector :")) {
- current_sector = stoi(line.substr(10));
- BUGZ_LOG(warning) << "SECTOR: " << current_sector;
- }
- if (in(line, "Ports :")) {
- std::string port_class;
- size_t pos = line.find(", Class ");
- if (pos != std::string::npos) {
- pos += 8;
- int class_ = stoi(line.substr(pos));
- BUGZ_LOG(fatal) << "PORT: " << class_;
- galaxy.add_port(current_sector, class_);
- }
- }
- if (in(line, "Warps to Sector(s) :")) {
- std::string temp = line.substr(20);
- replace(temp, " - ", " ");
-
-
- replace(temp, "(", "");
- replace(temp, ")", "");
- sector_warps sw;
- auto warps = split(temp);
- sw.sector = current_sector;
- for (auto const &w : warps) {
- sw.add(stoi(w));
- }
- BUGZ_LOG(fatal) << "WARPS: " << sw;
- galaxy.add_warp(sw);
- }
- }
- }
- void Director::SL_portline(const std::string &line) {
- if (line.empty()) {
- SL_parser = nullptr;
- return;
- }
-
- BUGZ_LOG(info) << "portline : " << line;
- if (in(line, "%")) {
-
-
-
- std::string work = line;
- replace(work, "Fuel Ore", "Fuel");
- auto parts = split(work);
- BUGZ_LOG(fatal) << "portline split:";
- for (auto const p : parts) {
- BUGZ_LOG(fatal) << p;
- }
-
- }
- }
- void Director::SL_warpline(const std::string &line) {
- if (line.empty()) {
- SL_parser = nullptr;
- return;
- }
-
- BUGZ_LOG(fatal) << "warpline: [" << line << "]";
- }
|