From: Maxim Mamontov Date: Sat, 13 Aug 2011 16:08:32 +0000 (+0300) Subject: Basic corporations implementation added X-Git-Tag: 2.408-alpha~31 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/d25555ce9c6e09344356868780e678c75bbbd164?ds=sidebyside Basic corporations implementation added --- diff --git a/include/stg/corp_conf.h b/include/stg/corp_conf.h index e29a5dcf..d6cc2ebb 100644 --- a/include/stg/corp_conf.h +++ b/include/stg/corp_conf.h @@ -5,6 +5,7 @@ struct CORP_CONF { +CORP_CONF() : name(), cash(0) {} CORP_CONF(const std::string & n) : name(n), cash(0) {} CORP_CONF(const std::string & n, double c) : name(n), cash(c) {} @@ -12,4 +13,10 @@ std::string name; double cash; }; +inline +bool operator==(const CORP_CONF & a, const CORP_CONF & b) +{ +return a.name == b.name; +} + #endif //CORP_CONF_H diff --git a/include/stg/corporations.h b/include/stg/corporations.h index cc147fc6..ad23967e 100644 --- a/include/stg/corporations.h +++ b/include/stg/corporations.h @@ -33,6 +33,7 @@ public: virtual bool FindCorp(const std::string & name, CORP_CONF & corp) = 0; virtual bool CorpExists(const std::string & name) const = 0; virtual const std::string & GetStrError() const = 0; + virtual size_t Count() const = 0; virtual int OpenSearch() const = 0; virtual int SearchNext(int, CORP_CONF * corp) const = 0; diff --git a/projects/stargazer/corps_impl.cpp b/projects/stargazer/corps_impl.cpp new file mode 100644 index 00000000..456523dc --- /dev/null +++ b/projects/stargazer/corps_impl.cpp @@ -0,0 +1,258 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#include +#include +#include + +#include "stg/admin.h" +#include "stg/common.h" +#include "corps_impl.h" + +//----------------------------------------------------------------------------- +CORPORATIONS_IMPL::CORPORATIONS_IMPL(STORE * st) + : CORPORATIONS(), + data(), + store(st), + WriteServLog(GetStgLogger()), + searchDescriptors(), + handle(0) +{ +pthread_mutex_init(&mutex, NULL); +Read(); +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::Add(const CORP_CONF & corp, const ADMIN * admin) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +const PRIV * priv = admin->GetPriv(); + +if (!priv->corpChg) + { + string s = admin->GetLogStr() + " Add corporation \'" + corp.name + "\'. Access denied."; + strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; + } + +crp_iter si(find(data.begin(), data.end(), corp)); + +if (si != data.end()) + { + strError = "Corporation \'" + corp.name + "\' cannot not be added. Corporation already exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + + return -1; + } + +data.push_back(corp); + +if (store->AddCorp(corp.name) == 0) + { + WriteServLog("%s Corporation \'%s\' added.", + admin->GetLogStr().c_str(), corp.name.c_str()); + return 0; + } + +strError = "Corporation \'" + corp.name + "\' was not added. Error: " + store->GetStrError(); +WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + +return -1; +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::Del(const string & name, const ADMIN * admin) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +const PRIV * priv = admin->GetPriv(); + +if (!priv->corpChg) + { + string s = admin->GetLogStr() + " Delete corporation \'" + name + "\'. Access denied."; + strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; + } + +crp_iter si(find(data.begin(), data.end(), CORP_CONF(name))); + +if (si == data.end()) + { + strError = "Corporation \'" + name + "\' cannot be deleted. Corporation does not exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + return -1; + } + +map::iterator csi; +csi = searchDescriptors.begin(); +while (csi != searchDescriptors.end()) + { + if (csi->second == si) + (csi->second)++; + csi++; + } + +data.remove(*si); +if (store->DelCorp(name) < 0) + { + strError = "Corporation \'" + name + "\' was not deleted. Error: " + store->GetStrError(); + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + + return -1; + } + +WriteServLog("%s Corporation \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str()); +return 0; +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::Change(const CORP_CONF & corp, const ADMIN * admin) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +const PRIV * priv = admin->GetPriv(); + +if (!priv->corpChg) + { + string s = admin->GetLogStr() + " Change corporation \'" + corp.name + "\'. Access denied."; + strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; + } + +crp_iter si(find(data.begin(), data.end(), corp)); + +if (si == data.end()) + { + strError = "Corporation \'" + corp.name + "\' cannot be changed " + ". Corporation does not exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + return -1; + } + +*si = corp; +if (store->SaveCorp(corp)) + { + WriteServLog("Cannot write corporation %s.", corp.name.c_str()); + WriteServLog("%s", store->GetStrError().c_str()); + return -1; + } + +WriteServLog("%s Corporation \'%s\' changed.", + admin->GetLogStr().c_str(), corp.name.c_str()); + +return 0; +} +//----------------------------------------------------------------------------- +bool CORPORATIONS_IMPL::Read() +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +vector corpsList; +if (store->GetCorpsList(&corpsList) < 0) + { + WriteServLog(store->GetStrError().c_str()); + return true; + } + +for (size_t i = 0; i < corpsList.size(); i++) + { + CORP_CONF corp; + + if (store->RestoreCorp(&corp, corpsList[i])) + { + WriteServLog(store->GetStrError().c_str()); + return true; + } + + data.push_back(corp); + } +return false; +} +//----------------------------------------------------------------------------- +bool CORPORATIONS_IMPL::Find(const string & name, CORP_CONF * corp) +{ +assert(corp != NULL && "Pointer to corporation is not null"); + +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (data.empty()) + return false; + +crp_iter si(find(data.begin(), data.end(), CORP_CONF(name))); + +if (si != data.end()) + { + *corp = *si; + return false; + } + +return true; +} +//----------------------------------------------------------------------------- +bool CORPORATIONS_IMPL::Exists(const string & name) const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (data.empty()) + { + printfd(__FILE__, "no admin in system!\n"); + return true; + } + +const_crp_iter si(find(data.begin(), data.end(), CORP_CONF(name))); + +if (si != data.end()) + return true; + +return false; +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::OpenSearch() const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +handle++; +searchDescriptors[handle] = data.begin(); +return handle; +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::SearchNext(int h, CORP_CONF * corp) const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (searchDescriptors.find(h) == searchDescriptors.end()) + { + WriteServLog("CORPORATIONS. Incorrect search handle."); + return -1; + } + +if (searchDescriptors[h] == data.end()) + return -1; + +*corp = *searchDescriptors[h]++; + +return 0; +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::CloseSearch(int h) const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (searchDescriptors.find(h) != searchDescriptors.end()) + { + searchDescriptors.erase(searchDescriptors.find(h)); + return 0; + } + +WriteServLog("CORPORATIONS. Incorrect search handle."); +return -1; +} +//----------------------------------------------------------------------------- diff --git a/projects/stargazer/corps_impl.h b/projects/stargazer/corps_impl.h new file mode 100644 index 00000000..ad705c54 --- /dev/null +++ b/projects/stargazer/corps_impl.h @@ -0,0 +1,70 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef CORPORATIONS_IMPL_H +#define CORPORATIONS_IMPL_H + +#include + +#include +#include +#include + +#include "stg/corporations.h" +#include "stg/corp_conf.h" +#include "stg/locker.h" +#include "stg/store.h" +#include "stg/noncopyable.h" +#include "stg/logger.h" + +class ADMIN; + +class CORPORATIONS_IMPL : private NONCOPYABLE, public CORPORATIONS { +public: + CORPORATIONS_IMPL(STORE * st); + virtual ~CORPORATIONS_IMPL() {} + + int Add(const CORP_CONF & corp, const ADMIN * admin); + int Del(const std::string & name, const ADMIN * admin); + int Change(const CORP_CONF & corp, const ADMIN * admin); + bool Find(const std::string & name, CORP_CONF * corp); + bool Exists(const std::string & name) const; + const std::string & GetStrError() const { return strError; } + + int OpenSearch() const; + int SearchNext(int, CORP_CONF * corp) const; + int CloseSearch(int) const; + +private: + typedef list::iterator crp_iter; + typedef list::const_iterator const_crp_iter; + + bool Read(); + + std::list data; + STORE * store; + STG_LOGGER & WriteServLog; + mutable std::map searchDescriptors; + mutable unsigned int handle; + mutable pthread_mutex_t mutex; + std::string strError; +}; + +#endif