]> git.stg.codes - stg.git/blob - stargazer/corps_impl.cpp
c0da9a67f7bd27ab2c0db32ef8eb72ed0e3f3d8b
[stg.git] / stargazer / corps_impl.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "corps_impl.h"
22
23 #include "stg/admin.h"
24 #include "stg/admin_conf.h"
25 #include "stg/store.h"
26 #include "stg/common.h"
27
28 #include <algorithm>
29 #include <cassert>
30
31 using STG::CorporationsImpl;
32
33 //-----------------------------------------------------------------------------
34 CorporationsImpl::CorporationsImpl(Store * st)
35     : store(st),
36       WriteServLog(Logger::get()),
37       handle(0)
38 {
39 Read();
40 }
41 //-----------------------------------------------------------------------------
42 int CorporationsImpl::Add(const CorpConf & corp, const Admin * admin)
43 {
44 std::lock_guard<std::mutex> lock(mutex);
45 const auto priv = admin->GetPriv();
46
47 if (!priv->corpChg)
48     {
49     std::string s = admin->GetLogStr() + " Add corporation \'" + corp.name + "\'. Access denied.";
50     strError = "Access denied.";
51     WriteServLog(s.c_str());
52     return -1;
53     }
54
55 crp_iter si(find(data.begin(), data.end(), corp));
56
57 if (si != data.end())
58     {
59     strError = "Corporation \'" + corp.name + "\' cannot not be added. Corporation already exist.";
60     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
61
62     return -1;
63     }
64
65 data.push_back(corp);
66
67 if (store->AddCorp(corp.name) == 0)
68     {
69     WriteServLog("%s Corporation \'%s\' added.",
70                  admin->GetLogStr().c_str(), corp.name.c_str());
71     return 0;
72     }
73
74 strError = "Corporation \'" + corp.name + "\' was not added. Error: " + store->GetStrError();
75 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
76
77 return -1;
78 }
79 //-----------------------------------------------------------------------------
80 int CorporationsImpl::Del(const std::string & name, const Admin * admin)
81 {
82 std::lock_guard<std::mutex> lock(mutex);
83 const auto priv = admin->GetPriv();
84
85 if (!priv->corpChg)
86     {
87     std::string s = admin->GetLogStr() + " Delete corporation \'" + name + "\'. Access denied.";
88     strError = "Access denied.";
89     WriteServLog(s.c_str());
90     return -1;
91     }
92
93 crp_iter si(find(data.begin(), data.end(), CorpConf(name)));
94
95 if (si == data.end())
96     {
97     strError = "Corporation \'" + name + "\' cannot be deleted. Corporation does not exist.";
98     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
99     return -1;
100     }
101
102 std::map<int, const_crp_iter>::iterator csi;
103 csi = searchDescriptors.begin();
104 while (csi != searchDescriptors.end())
105     {
106     if (csi->second == si)
107         (csi->second)++;
108     ++csi;
109     }
110
111 data.erase(si);
112 if (store->DelCorp(name) < 0)
113     {
114     strError = "Corporation \'" + name + "\' was not deleted. Error: " + store->GetStrError();
115     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
116
117     return -1;
118     }
119
120 WriteServLog("%s Corporation \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str());
121 return 0;
122 }
123 //-----------------------------------------------------------------------------
124 int CorporationsImpl::Change(const CorpConf & corp, const Admin * admin)
125 {
126 std::lock_guard<std::mutex> lock(mutex);
127 const auto priv = admin->GetPriv();
128
129 if (!priv->corpChg)
130     {
131     std::string s = admin->GetLogStr() + " Change corporation \'" + corp.name + "\'. Access denied.";
132     strError = "Access denied.";
133     WriteServLog(s.c_str());
134     return -1;
135     }
136
137 crp_iter si(find(data.begin(), data.end(), corp));
138
139 if (si == data.end())
140     {
141     strError = "Corporation \'" + corp.name + "\' cannot be changed " + ". Corporation does not exist.";
142     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
143     return -1;
144     }
145
146 *si = corp;
147 if (store->SaveCorp(corp))
148     {
149     WriteServLog("Cannot write corporation %s.", corp.name.c_str());
150     WriteServLog("%s", store->GetStrError().c_str());
151     return -1;
152     }
153
154 WriteServLog("%s Corporation \'%s\' changed.",
155              admin->GetLogStr().c_str(), corp.name.c_str());
156
157 return 0;
158 }
159 //-----------------------------------------------------------------------------
160 bool CorporationsImpl::Read()
161 {
162 std::lock_guard<std::mutex> lock(mutex);
163 std::vector<std::string> corpsList;
164 if (store->GetCorpsList(&corpsList) < 0)
165     {
166     WriteServLog(store->GetStrError().c_str());
167     return true;
168     }
169
170 for (size_t i = 0; i < corpsList.size(); i++)
171     {
172     CorpConf corp;
173
174     if (store->RestoreCorp(&corp, corpsList[i]))
175         {
176         WriteServLog(store->GetStrError().c_str());
177         return true;
178         }
179
180     data.push_back(corp);
181     }
182 return false;
183 }
184 //-----------------------------------------------------------------------------
185 bool CorporationsImpl::Find(const std::string & name, CorpConf * corp)
186 {
187 assert(corp != NULL && "Pointer to corporation is not null");
188
189 std::lock_guard<std::mutex> lock(mutex);
190 if (data.empty())
191     return false;
192
193 crp_iter si(find(data.begin(), data.end(), CorpConf(name)));
194
195 if (si != data.end())
196     {
197     *corp = *si;
198     return false;
199     }
200
201 return true;
202 }
203 //-----------------------------------------------------------------------------
204 bool CorporationsImpl::Exists(const std::string & name) const
205 {
206 std::lock_guard<std::mutex> lock(mutex);
207 if (data.empty())
208     {
209     printfd(__FILE__, "no admin in system!\n");
210     return true;
211     }
212
213 const_crp_iter si(find(data.begin(), data.end(), CorpConf(name)));
214
215 if (si != data.end())
216     return true;
217
218 return false;
219 }
220 //-----------------------------------------------------------------------------
221 int CorporationsImpl::OpenSearch() const
222 {
223 std::lock_guard<std::mutex> lock(mutex);
224 handle++;
225 searchDescriptors[handle] = data.begin();
226 return handle;
227 }
228 //-----------------------------------------------------------------------------
229 int CorporationsImpl::SearchNext(int h, CorpConf * corp) const
230 {
231 std::lock_guard<std::mutex> lock(mutex);
232 if (searchDescriptors.find(h) == searchDescriptors.end())
233     {
234     WriteServLog("CORPORATIONS. Incorrect search handle.");
235     return -1;
236     }
237
238 if (searchDescriptors[h] == data.end())
239     return -1;
240
241 *corp = *searchDescriptors[h]++;
242
243 return 0;
244 }
245 //-----------------------------------------------------------------------------
246 int CorporationsImpl::CloseSearch(int h) const
247 {
248 std::lock_guard<std::mutex> lock(mutex);
249 if (searchDescriptors.find(h) != searchDescriptors.end())
250     {
251     searchDescriptors.erase(searchDescriptors.find(h));
252     return 0;
253     }
254
255 WriteServLog("CORPORATIONS. Incorrect search handle.");
256 return -1;
257 }
258 //-----------------------------------------------------------------------------