3 #include "api_action.h"
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/corp_conf.h"
11 #include "stg/common.h"
20 std::string Indent(size_t level, bool dash = false)
24 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
27 void PrintCorp(const STG::GET_CORP::INFO & info, size_t level = 0)
29 std::cout << Indent(level, true) << "name: " << info.name << "\n"
30 << Indent(level) << "cash: " << info.cash << "\n";
33 std::vector<SGCONF::API_ACTION::PARAM> GetCorpParams()
35 std::vector<SGCONF::API_ACTION::PARAM> params;
36 params.push_back(SGCONF::API_ACTION::PARAM("cash", "<cash>", "\tcorporation's cash"));
40 void SimpleCallback(bool result,
41 const std::string & reason,
46 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
49 std::cout << "Success.\n";
52 void GetCorpsCallback(bool result,
53 const std::string & reason,
54 const std::vector<STG::GET_CORP::INFO> & info,
59 std::cerr << "Failed to get corp list. Reason: '" << reason << "'." << std::endl;
62 std::cout << "Corps:\n";
63 for (size_t i = 0; i < info.size(); ++i)
64 PrintCorp(info[i], 1);
67 void GetCorpCallback(bool result,
68 const std::string & reason,
69 const STG::GET_CORP::INFO & info,
74 std::cerr << "Failed to get corp. Reason: '" << reason << "'." << std::endl;
80 bool GetCorpsFunction(const SGCONF::CONFIG & config,
81 const std::string & /*arg*/,
82 const std::map<std::string, std::string> & /*options*/)
84 STG::SERVCONF proto(config.server.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;
93 bool GetCorpFunction(const SGCONF::CONFIG & config,
94 const std::string & arg,
95 const std::map<std::string, std::string> & /*options*/)
97 STG::SERVCONF proto(config.server.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;
106 bool DelCorpFunction(const SGCONF::CONFIG & config,
107 const std::string & arg,
108 const std::map<std::string, std::string> & /*options*/)
110 STG::SERVCONF proto(config.server.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;
119 bool AddCorpFunction(const SGCONF::CONFIG & config,
120 const std::string & arg,
121 const std::map<std::string, std::string> & options)
125 SGCONF::MaybeSet(options, "cash", conf.cash);
126 STG::SERVCONF proto(config.server.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;
135 bool ChgCorpFunction(const SGCONF::CONFIG & config,
136 const std::string & arg,
137 const std::map<std::string, std::string> & options)
141 SGCONF::MaybeSet(options, "cash", conf.cash);
142 STG::SERVCONF proto(config.server.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;
151 } // namespace anonymous
153 void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
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");