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.userName.data(),
87 config.userPass.data());
88 return proto.GetCorporations(GetCorpsCallback, NULL) == STG::st_ok;
91 bool GetCorpFunction(const SGCONF::CONFIG & config,
92 const std::string & arg,
93 const std::map<std::string, std::string> & /*options*/)
95 STG::SERVCONF proto(config.server.data(),
97 config.userName.data(),
98 config.userPass.data());
99 return proto.GetCorp(arg, GetCorpCallback, NULL) == STG::st_ok;
102 bool DelCorpFunction(const SGCONF::CONFIG & config,
103 const std::string & arg,
104 const std::map<std::string, std::string> & /*options*/)
106 STG::SERVCONF proto(config.server.data(),
108 config.userName.data(),
109 config.userPass.data());
110 return proto.DelCorp(arg, SimpleCallback, NULL) == STG::st_ok;
113 bool AddCorpFunction(const SGCONF::CONFIG & config,
114 const std::string & arg,
115 const std::map<std::string, std::string> & options)
119 SGCONF::MaybeSet(options, "cash", conf.cash);
120 STG::SERVCONF proto(config.server.data(),
122 config.userName.data(),
123 config.userPass.data());
124 return proto.AddCorp(arg, conf, SimpleCallback, NULL) == STG::st_ok;
127 bool ChgCorpFunction(const SGCONF::CONFIG & config,
128 const std::string & arg,
129 const std::map<std::string, std::string> & options)
133 SGCONF::MaybeSet(options, "cash", conf.cash);
134 STG::SERVCONF proto(config.server.data(),
136 config.userName.data(),
137 config.userPass.data());
138 return proto.ChgCorp(conf, SimpleCallback, NULL) == STG::st_ok;
141 } // namespace anonymous
143 void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
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");