diff --git a/src/client.h b/src/client.h index 80ae67e..d54c8a4 100644 --- a/src/client.h +++ b/src/client.h @@ -2,13 +2,126 @@ #define H_CLIENT #include #include +#include +#include #include "drone.h" -#include "config.h" -PengoConfig conf; bool debug = false; std::random_device rd; std::mt19937 rng(rd()); +class PengoConfig { +public: + PengoConfig() { + std::ifstream mconf("conf/pengobot.conf"); + + std::string line; + while (std::getline(mconf, line)) { + // Check if line is a comment line. + if (line.rfind("#", 0) != 0) { + std::string name = line.substr(0, line.find("=")); + std::string value = line.substr(line.find("=")+1, line.length()); + if (debug) std::cout << name << ": \"" << value << "\"" << std::endl; + conf[name] = value; + } + } + + for (auto const& mc : conf) { + std::string msgName; + if ((mc.first.substr(0, mc.first.find("_"))) == "msg") { + msgName = mc.first.substr(mc.first.find("_")+1, mc.first.length()); + std::ifstream msglines(mc.second); + std::string line; + std::vector lines; + while (std::getline(msglines, line)) { + if (line.rfind("#", 0) != 0) { + lines.push_back(line); + if (debug) std::cout << msgName << "=" << line << std::endl; + } + } + messages[msgName] = lines; + } + } + + if ((conf["worldfile"]).length() > 0) { + std::ifstream wrlds(conf["worldfile"]); + std::string wline; + while (std::getline(wrlds, wline)) { + if (wline.rfind("#", 0) != 0) { + std::string name = wline.substr(0, wline.find("=")); + std::string value = wline.substr(wline.find("=")+1, wline.length()); + if (debug) std::cout << name << ": \"" << value << "\"" << std::endl; + worlds[name] = value; + } + } + } + + if ((conf["responsefile"]).length() > 0) { + std::ifstream rspn(conf["responsefile"]); + std::string rline; + while (std::getline(rspn, rline)) { + if (rline.rfind("#", 0) != 0) { + std::string name = rline.substr(0, rline.find("=")); + std::string value = rline.substr(rline.find("=")+1, rline.length()); + if (debug) std::cout << name << ": \"" << value << "\"" << std::endl; + responses[name] = value; + } + } + } + } + PengoConfig operator = (PengoConfig *pc) { return *pc; }; + + std::string getValue(std::string name, std::string def) { + if (((std::string)conf[name]).length() > 0) return conf[name]; + else return def; + } + + int getInt(std::string name, int def) { + if (((std::string)conf[name]).length() > 0) return std::stoi(conf[name]); + else return def; + } + + void setValue(std::string name, std::string value) { + conf[name] = value; + } + + void setInt(std::string name, int value) { + conf[name] = std::to_string(value); + } + + std::string getMessage(std::string type) { + std::uniform_int_distribution rno(0,(messages[type]).size()-1); + return (messages[type])[rno(rng)]; + } + + std::vector getMessages(std::string type) { + return messages[type]; + } + + std::string getWorld(std::string name) { + return worlds[name]; + } + + std::map getWorlds() { + return worlds; + } + + std::string getReply(std::string name) { + return responses[name]; + } + + std::map getResponses() { + return responses; + } + +private: + std::map conf; + std::map worlds; + std::map responses; + std::map> messages; +}; + +PengoConfig conf; + #define BUFFERSIZE 65535 bool autoOnline = false; diff --git a/src/config.h b/src/config.h deleted file mode 100644 index b5c9bf8..0000000 --- a/src/config.h +++ /dev/null @@ -1,118 +0,0 @@ -#ifndef CONFIG_H -#define CONFIG_H -#include -#include -#include - -class PengoConfig { -public: - PengoConfig() { - std::ifstream mconf("conf/pengobot.conf"); - - std::string line; - while (std::getline(mconf, line)) { - // Check if line is a comment line. - if (line.rfind("#", 0) != 0) { - std::string name = line.substr(0, line.find("=")); - std::string value = line.substr(line.find("=")+1, line.length()); - std::cout << name << ": \"" << value << "\"" << std::endl; - conf[name] = value; - } - } - - for (auto const& mc : conf) { - std::string msgName; - if ((mc.first.substr(0, mc.first.find("_"))) == "msg") { - msgName = mc.first.substr(mc.first.find("_")+1, mc.first.length()); - std::ifstream msglines(mc.second); - std::string line; - std::vector lines; - while (std::getline(msglines, line)) { - if (line.rfind("#", 0) != 0) { - lines.push_back(line); - std::cout << msgName << "=" << line << std::endl; - } - } - messages[msgName] = lines; - } - } - - if ((conf["worldfile"]).length() > 0) { - std::ifstream wrlds(conf["worldfile"]); - std::string wline; - while (std::getline(wrlds, wline)) { - if (wline.rfind("#", 0) != 0) { - std::string name = wline.substr(0, wline.find("=")); - std::string value = wline.substr(wline.find("=")+1, wline.length()); - std::cout << name << ": \"" << value << "\"" << std::endl; - worlds[name] = value; - } - } - } - - if ((conf["responsefile"]).length() > 0) { - std::ifstream rspn(conf["responsefile"]); - std::string rline; - while (std::getline(rspn, rline)) { - if (rline.rfind("#", 0) != 0) { - std::string name = rline.substr(0, rline.find("=")); - std::string value = rline.substr(rline.find("=")+1, rline.length()); - std::cout << name << ": \"" << value << "\"" << std::endl; - responses[name] = value; - } - } - } - } - PengoConfig operator = (PengoConfig *pc) { return *pc; }; - - std::string getValue(std::string name, std::string def) { - if (((std::string)conf[name]).length() > 0) return conf[name]; - else return def; - } - - int getInt(std::string name, int def) { - if (((std::string)conf[name]).length() > 0) return std::stoi(conf[name]); - else return def; - } - - void setValue(std::string name, std::string value) { - conf[name] = value; - } - - void setInt(std::string name, int value) { - conf[name] = std::to_string(value); - } - - std::string getMessage(std::string type) { - return (messages[type])[(int)(rand() % ((messages[type]).size()))]; - } - - std::vector getMessages(std::string type) { - return messages[type]; - } - - std::string getWorld(std::string name) { - return worlds[name]; - } - - std::map getWorlds() { - return worlds; - } - - std::string getReply(std::string name) { - return responses[name]; - } - - std::map getResponses() { - return responses; - } - -private: - std::map conf; - std::map worlds; - std::map responses; - std::map> messages; -}; - -#endif -