]> git.stg.codes - stg.git/blob - projects/sgconf/corps.cpp
Expanded more abbreviations.
[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.userName.data(),
87                     config.userPass.data());
88 return proto.GetCorporations(GetCorpsCallback, NULL) == STG::st_ok;
89 }
90
91 bool GetCorpFunction(const SGCONF::CONFIG & config,
92                      const std::string & arg,
93                      const std::map<std::string, std::string> & /*options*/)
94 {
95 STG::SERVCONF proto(config.server.data(),
96                     config.port.data(),
97                     config.userName.data(),
98                     config.userPass.data());
99 return proto.GetCorp(arg, GetCorpCallback, NULL) == STG::st_ok;
100 }
101
102 bool DelCorpFunction(const SGCONF::CONFIG & config,
103                      const std::string & arg,
104                      const std::map<std::string, std::string> & /*options*/)
105 {
106 STG::SERVCONF proto(config.server.data(),
107                     config.port.data(),
108                     config.userName.data(),
109                     config.userPass.data());
110 return proto.DelCorp(arg, SimpleCallback, NULL) == STG::st_ok;
111 }
112
113 bool AddCorpFunction(const SGCONF::CONFIG & config,
114                      const std::string & arg,
115                      const std::map<std::string, std::string> & options)
116 {
117 CORP_CONF_RES conf;
118 conf.name = arg;
119 SGCONF::MaybeSet(options, "cash", conf.cash);
120 STG::SERVCONF proto(config.server.data(),
121                     config.port.data(),
122                     config.userName.data(),
123                     config.userPass.data());
124 return proto.AddCorp(arg, conf, SimpleCallback, NULL) == STG::st_ok;
125 }
126
127 bool ChgCorpFunction(const SGCONF::CONFIG & config,
128                      const std::string & arg,
129                      const std::map<std::string, std::string> & options)
130 {
131 CORP_CONF_RES conf;
132 conf.name = arg;
133 SGCONF::MaybeSet(options, "cash", conf.cash);
134 STG::SERVCONF proto(config.server.data(),
135                     config.port.data(),
136                     config.userName.data(),
137                     config.userPass.data());
138 return proto.ChgCorp(conf, SimpleCallback, NULL) == STG::st_ok;
139 }
140
141 } // namespace anonymous
142
143 void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
144 {
145 std::vector<API_ACTION::PARAM> params(GetCorpParams());
146 blocks.Add("Corporation management options")
147       .Add("get-corps", SGCONF::MakeAPIAction(commands, GetCorpsFunction), "\tget corporation list")
148       .Add("get-corp", SGCONF::MakeAPIAction(commands, "<name>", GetCorpFunction), "get corporation")
149       .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", params, AddCorpFunction), "add corporation")
150       .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", DelCorpFunction), "delete corporation")
151       .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", params, ChgCorpFunction), "change corporation");
152 }