3 #include "api_action.h"
7 #include "stg/servconf.h"
8 #include "stg/servconf_types.h"
9 #include "stg/corp_conf.h"
10 #include "stg/common.h"
19 std::string Indent(size_t level, bool dash = false)
23 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
26 void PrintCorp(const STG::GET_CORP::INFO & info, size_t level = 0)
28 std::cout << Indent(level, true) << "name: " << info.name << "\n"
29 << Indent(level) << "cash: " << info.cash << "\n";
32 std::vector<SGCONF::API_ACTION::PARAM> GetCorpParams()
34 std::vector<SGCONF::API_ACTION::PARAM> params;
35 params.push_back({"cash", "<cash>", "\tcorporation's cash"});
39 void SimpleCallback(bool result,
40 const std::string & reason,
45 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
48 std::cout << "Success.\n";
51 void GetCorpsCallback(bool result,
52 const std::string & reason,
53 const std::vector<STG::GET_CORP::INFO> & info,
58 std::cerr << "Failed to get corp list. Reason: '" << reason << "'." << std::endl;
61 std::cout << "Corps:\n";
62 for (size_t i = 0; i < info.size(); ++i)
63 PrintCorp(info[i], 1);
66 void GetCorpCallback(bool result,
67 const std::string & reason,
68 const STG::GET_CORP::INFO & info,
73 std::cerr << "Failed to get corp. Reason: '" << reason << "'." << std::endl;
79 bool GetCorpsFunction(const SGCONF::CONFIG & config,
80 const std::string & /*arg*/,
81 const std::map<std::string, std::string> & /*options*/)
83 STG::SERVCONF proto(config.server.data(),
85 config.userName.data(),
86 config.userPass.data());
87 return proto.GetCorporations(GetCorpsCallback, NULL) == STG::st_ok;
90 bool GetCorpFunction(const SGCONF::CONFIG & config,
91 const std::string & arg,
92 const std::map<std::string, std::string> & /*options*/)
94 STG::SERVCONF proto(config.server.data(),
96 config.userName.data(),
97 config.userPass.data());
98 return proto.GetCorp(arg, GetCorpCallback, NULL) == STG::st_ok;
101 bool DelCorpFunction(const SGCONF::CONFIG & config,
102 const std::string & arg,
103 const std::map<std::string, std::string> & /*options*/)
105 STG::SERVCONF proto(config.server.data(),
107 config.userName.data(),
108 config.userPass.data());
109 return proto.DelCorp(arg, SimpleCallback, NULL) == STG::st_ok;
112 bool AddCorpFunction(const SGCONF::CONFIG & config,
113 const std::string & arg,
114 const std::map<std::string, std::string> & /*options*/)
117 std::cerr << "Unimplemented.\n";
121 bool ChgCorpFunction(const SGCONF::CONFIG & config,
122 const std::string & arg,
123 const std::map<std::string, std::string> & options)
126 std::cerr << "Unimplemented.\n";
130 } // namespace anonymous
132 void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
134 std::vector<API_ACTION::PARAM> params(GetCorpParams());
135 blocks.Add("Corporation management options")
136 .Add("get-corps", SGCONF::MakeAPIAction(commands, GetCorpsFunction), "\tget corporation list")
137 .Add("get-corp", SGCONF::MakeAPIAction(commands, "<name>", GetCorpFunction), "get corporation")
138 .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", params, AddCorpFunction), "add corporation")
139 .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", DelCorpFunction), "del corporation")
140 .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", params, ChgCorpFunction), "change corporation");