]> git.stg.codes - stg.git/blob - projects/sgconf/corps.cpp
Fixed tests.
[stg.git] / projects / sgconf / corps.cpp
1 #include "corps.h"
2
3 #include "api_action.h"
4 #include "options.h"
5 #include "config.h"
6 #include "utils.h"
7
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/corp_conf.h"
11 #include "stg/common.h"
12
13 #include <iostream>
14 #include <string>
15 #include <map>
16
17 namespace
18 {
19
20 std::string Indent(size_t level, bool dash = false)
21 {
22 if (level == 0)
23     return "";
24 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
25 }
26
27 void PrintCorp(const STG::GET_CORP::INFO & info, size_t level = 0)
28 {
29 std::cout << Indent(level, true) << "name: " << info.name << "\n"
30           << Indent(level)       << "cash: " << info.cash << "\n";
31 }
32
33 std::vector<SGCONF::API_ACTION::PARAM> GetCorpParams()
34 {
35 std::vector<SGCONF::API_ACTION::PARAM> params;
36 params.push_back(SGCONF::API_ACTION::PARAM("cash", "<cash>", "\tcorporation's cash"));
37 return params;
38 }
39
40 void SimpleCallback(bool result,
41                     const std::string & reason,
42                     void * /*data*/)
43 {
44 if (!result)
45     {
46     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
47     return;
48     }
49 std::cout << "Success.\n";
50 }
51
52 void GetCorpsCallback(bool result,
53                       const std::string & reason,
54                       const std::vector<STG::GET_CORP::INFO> & info,
55                       void * /*data*/)
56 {
57 if (!result)
58     {
59     std::cerr << "Failed to get corp list. Reason: '" << reason << "'." << std::endl;
60     return;
61     }
62 std::cout << "Corps:\n";
63 for (size_t i = 0; i < info.size(); ++i)
64     PrintCorp(info[i], 1);
65 }
66
67 void GetCorpCallback(bool result,
68                      const std::string & reason,
69                      const STG::GET_CORP::INFO & info,
70                      void * /*data*/)
71 {
72 if (!result)
73     {
74     std::cerr << "Failed to get corp. Reason: '" << reason << "'." << std::endl;
75     return;
76     }
77 PrintCorp(info);
78 }
79
80 bool GetCorpsFunction(const SGCONF::CONFIG & config,
81                       const std::string & /*arg*/,
82                       const std::map<std::string, std::string> & /*options*/)
83 {
84 STG::SERVCONF proto(config.server.data(),
85                     config.port.data(),
86                     config.localAddress.data(),
87                     config.localPort.data(),
88                     config.userName.data(),
89                     config.userPass.data());
90 return proto.GetCorporations(GetCorpsCallback, NULL) == STG::st_ok;
91 }
92
93 bool GetCorpFunction(const SGCONF::CONFIG & config,
94                      const std::string & arg,
95                      const std::map<std::string, std::string> & /*options*/)
96 {
97 STG::SERVCONF proto(config.server.data(),
98                     config.port.data(),
99                     config.localAddress.data(),
100                     config.localPort.data(),
101                     config.userName.data(),
102                     config.userPass.data());
103 return proto.GetCorp(arg, GetCorpCallback, NULL) == STG::st_ok;
104 }
105
106 bool DelCorpFunction(const SGCONF::CONFIG & config,
107                      const std::string & arg,
108                      const std::map<std::string, std::string> & /*options*/)
109 {
110 STG::SERVCONF proto(config.server.data(),
111                     config.port.data(),
112                     config.localAddress.data(),
113                     config.localPort.data(),
114                     config.userName.data(),
115                     config.userPass.data());
116 return proto.DelCorp(arg, SimpleCallback, NULL) == STG::st_ok;
117 }
118
119 bool AddCorpFunction(const SGCONF::CONFIG & config,
120                      const std::string & arg,
121                      const std::map<std::string, std::string> & options)
122 {
123 CORP_CONF_RES conf;
124 conf.name = arg;
125 SGCONF::MaybeSet(options, "cash", conf.cash);
126 STG::SERVCONF proto(config.server.data(),
127                     config.port.data(),
128                     config.localAddress.data(),
129                     config.localPort.data(),
130                     config.userName.data(),
131                     config.userPass.data());
132 return proto.AddCorp(arg, conf, SimpleCallback, NULL) == STG::st_ok;
133 }
134
135 bool ChgCorpFunction(const SGCONF::CONFIG & config,
136                      const std::string & arg,
137                      const std::map<std::string, std::string> & options)
138 {
139 CORP_CONF_RES conf;
140 conf.name = arg;
141 SGCONF::MaybeSet(options, "cash", conf.cash);
142 STG::SERVCONF proto(config.server.data(),
143                     config.port.data(),
144                     config.localAddress.data(),
145                     config.localPort.data(),
146                     config.userName.data(),
147                     config.userPass.data());
148 return proto.ChgCorp(conf, SimpleCallback, NULL) == STG::st_ok;
149 }
150
151 } // namespace anonymous
152
153 void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
154 {
155 std::vector<API_ACTION::PARAM> params(GetCorpParams());
156 blocks.Add("Corporation management options")
157       .Add("get-corps", SGCONF::MakeAPIAction(commands, GetCorpsFunction), "\tget corporation list")
158       .Add("get-corp", SGCONF::MakeAPIAction(commands, "<name>", GetCorpFunction), "get corporation")
159       .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", params, AddCorpFunction), "add corporation")
160       .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", DelCorpFunction), "delete corporation")
161       .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", params, ChgCorpFunction), "change corporation");
162 }