From a993d31910b63a1f897e470842934e6ffefad32c Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 13 Nov 2009 22:25:09 +0000 Subject: added core::InfoType, refactored game::ShipModel as core::Info subclass, introduced core::Label --- src/core/info.cc | 285 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 224 insertions(+), 61 deletions(-) (limited to 'src/core/info.cc') diff --git a/src/core/info.cc b/src/core/info.cc index 4e5399a..8f843c4 100644 --- a/src/core/info.cc +++ b/src/core/info.cc @@ -9,78 +9,121 @@ #include "sys/sys.h" #include "core/info.h" +#include + namespace core { -/* ---- class Info ------------------------------------------------- */ +/* ---- class InfoType --------------------------------------------- */ -// default constructor -Info::Info() +// server-side constructor, assigns id +InfoType::InfoType(const char *label) { - static_info_id_counter++; - info_id = static_info_id_counter; - - info_model = 0; - info_timestamp = 0; + infotype_id_counter++; + infotype_id = infotype_id_counter; + if (label) + set_label(label); + + infotype_registry.push_back(this); +} + +// client-side constructor, receives id +InfoType::InfoType(const unsigned int id) +{ + infotype_id = id; + infotype_registry.push_back(this); +} + +InfoType::~InfoType() +{ + infotype_id = 0; +} - add(this); +/* ---- static InfoType registry ----------------------------------- */ + +InfoType::Registry InfoType::infotype_registry; +unsigned int InfoType::infotype_id_counter = 0; + +void InfoType::clear() +{ + for (Registry::iterator it = infotype_registry.begin(); it != infotype_registry.end(); it++) { + InfoType *infotype = (*it); + delete infotype; + } + infotype_registry.clear(); + infotype_id_counter = 0; + +} + +InfoType *InfoType::find(const std::string & label) +{ + if (!label.size()) + return 0; + + for (Registry::iterator it = infotype_registry.begin(); it != infotype_registry.end(); it++) { + InfoType *infotype = (*it); + if (infotype->label().compare(label) == 0) { + return infotype; + } + } + return 0; +} + +InfoType *InfoType::find(const unsigned int id) +{ + if (!id) + return 0; + + for (Registry::iterator it = infotype_registry.begin(); it != infotype_registry.end(); it++) { + InfoType *infotype = (*it); + if (infotype->id() == id) { + return infotype; + } + } + return 0; } -Info::Info(const std::string & label) +/* ---- class Info ------------------------------------------------- */ + +// server-side constructor, assigns id +Info::Info() { - static_info_id_counter++; - info_id = static_info_id_counter; + info_id_counter++; + info_id = info_id_counter; + info_type = 0; - info_timestamp = 0; info_model = 0; - - info_label.assign(label); - aux::to_lowercase(info_label); - aux::strip_quotes(info_label); - - add(this); + info_timestamp = 0; + info_price = 0; + + info_registry.push_back(this); } +// client-side constructor, receives id Info::Info(const unsigned int id) { info_id = id; + info_type = 0; info_timestamp = 0; info_model = 0; + info_price = 0; - add(this); + info_registry.push_back(this); } Info::~Info() { info_text.clear(); - info_label.clear(); info_modelname.clear(); info_text.clear(); info_timestamp = 0; info_model = 0; } -void Info::set_label(const std::string & label) +void Info::set_type(const InfoType *type) { - info_label.assign(label); - aux::to_label(info_label); -} - -void Info::set_label(const char *label) -{ - info_label.assign(label); - aux::to_label(info_label); -} - -void Info::set_name(const std::string & name) -{ - info_name.assign(name); -} - -void Info::set_name(const char *name) -{ - info_name.assign(name); + info_type = type; } void Info::set_modelname(const std::string & modelname) @@ -98,6 +141,11 @@ void Info::set_model(const model::Model *model) info_model = model; } +void Info::set_price(const long price) +{ + info_price = price; +} + void Info::set_timestamp(const unsigned long timestamp) { info_timestamp = timestamp; @@ -127,7 +175,7 @@ void Info::clear_text() void Info::serialize_server_update(std::ostream & os) const { - os << '"' << label() << "\" \"" << name() << "\" \"" << modelname() << "\" " << info_text.size() << " "; + os << " \"" << label() << "\" \"" << name() << "\" \"" << modelname() << "\" " << info_text.size() << " "; for (Text::const_iterator it = info_text.begin(); it != info_text.end(); it++) { if (it != info_text.begin()) @@ -146,13 +194,13 @@ void Info::receive_server_update(std::istream &is) while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) n += c; - info_label.assign(n); + set_label(n); // read name while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) n += c; - info_name.assign(n); + set_name(n); // read model name n.clear(); @@ -160,11 +208,11 @@ void Info::receive_server_update(std::istream &is) while ((is.get(c)) && (c != '"')) n += c; - info_modelname.assign(n); + set_modelname(n); // read info text - size_t s; - info_text.clear(); + clear_text(); + size_t s; is >> s; for (size_t i = 0; (i < s) && is.good(); i++) { n.clear(); @@ -172,29 +220,25 @@ void Info::receive_server_update(std::istream &is) while ((is.get(c)) && (c != '"')) n += c; - info_text.push_back(n); + add_text(n); } } void Info::print() const { - con_print << "label: ^B" << label() << " ^Nname: ^B" << name() << " ^Nmodel: ^B" << modelname() << "^N" << std::endl; - - for (Text::const_iterator it = info_text.begin(); it != info_text.end(); it++) { - con_print << " " << (*it) << std::endl; + if (info_text.size()) { + for (Text::const_iterator it = info_text.begin(); it != info_text.end(); it++) { + con_print << " " << (*it) << std::endl; + } + } else { + con_print << " label: ^B" << label() << " ^Nname: ^B" << name() << " ^Nmodel: ^B" << modelname() << "^N" << std::endl; } } - /* ---- static info registry --------------------------------------- */ Info::Registry Info::info_registry; -unsigned int Info::static_info_id_counter = 0; - -void Info::add(Info *info) -{ - info_registry.push_back(info); -} +unsigned int Info::info_id_counter = 0; Info *Info::find(const char *label) { @@ -238,6 +282,104 @@ Info *Info::find(const unsigned int id) return 0; } +Info *Info::search(const std::string & searchstr) +{ + if (!searchstr.size()) + return 0; + + std::string strsearchkey(aux::lowercase(searchstr)); + if (strsearchkey.size() < 3) { + return 0; + } + + for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { + Info *info = *(it); + + std::string labelstr(info->label()); + if (labelstr.size() && (labelstr.find(strsearchkey) != std::string::npos)) { + return info; + } + + std::string namestr(aux::lowercase(info->name())); + if (namestr.size() && (namestr.find(strsearchkey) != std::string::npos)) { + return info; + } + } + + return 0; +} + +Info *Info::find(const InfoType *type, const char *label) +{ + if (!label) + return 0; + + for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { + Info *info = (*it); + if ((info->type() == type) && (info->label().compare(label) == 0)) { + return info; + } + } + return 0; +} + +Info *Info::find(const InfoType *type, const std::string & label) +{ + if (!label.size()) + return 0; + + for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { + Info *info = (*it); + if ((info->type() == type) && (info->label().compare(label) == 0)) { + return info; + } + } + return 0; +} + +Info *Info::find(const InfoType *type, const unsigned int id) +{ + if (!id) + return 0; + + for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { + Info *info = (*it); + if ((info->type() == type) && (info->id() == id)) { + return info; + } + } + return 0; +} + +Info *Info::search(const InfoType *type, const std::string & searchstr) +{ + if (!searchstr.size()) + return 0; + + std::string strsearchkey(aux::lowercase(searchstr)); + if (strsearchkey.size() < 3) { + return 0; + } + + for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { + Info *info = *(it); + + if (info->type() == type) { + std::string labelstr(info->label()); + if (labelstr.size() && (labelstr.find(strsearchkey) != std::string::npos)) { + return info; + } + + std::string namestr(aux::lowercase(info->name())); + if (namestr.size() && (namestr.find(strsearchkey) != std::string::npos)) { + return info; + } + } + } + + return 0; +} + void Info::clear() { for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { @@ -245,16 +387,37 @@ void Info::clear() delete info; } info_registry.clear(); - static_info_id_counter = 0; + info_id_counter = 0; + + InfoType::clear(); } void Info::list() { for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { Info *info = (*it); - con_print << info->label() << std::endl; + con_print << " " + << "^B" << aux::pad_right(info->type()->label(), 8) << "^N " + << "^B" << aux::pad_right(info->label(), 12) << "^N " + << info->name() << "^N" << std::endl; + } + con_print << info_registry.size() << " info " << aux::plural("record", info_registry.size()) << std::endl; +} + +void Info::list_class(const InfoType *type) +{ + size_t count = 0; + for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { + core::Info *info = (*it); + if (info->type() == type) { + count++; + + con_print << " " + << "^B" << aux::pad_right(info->label(), 12) << "^N " + << info->name() << "^N" << std::endl; + } } - con_print << info_registry.size() << " registered info " << aux::plural("record", info_registry.size()) << std::endl; + con_print << count << " info " << aux::plural("record", count) << std::endl; } } -- cgit v1.2.3