3 #include "api_action.h"
9 #include "stg/servconf.h"
10 #include "stg/servconf_types.h"
11 #include "stg/corp_conf.h"
12 #include "stg/common.h"
21 std::string Indent(size_t level, bool dash = false)
25 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
28 void PrintCorp(const STG::GetCorp::Info & info, size_t level = 0)
30 std::cout << Indent(level, true) << "name: " << info.name << "\n"
31 << Indent(level) << "cash: " << info.cash << "\n";
34 std::vector<SGCONF::API_ACTION::PARAM> GetCorpParams()
36 std::vector<SGCONF::API_ACTION::PARAM> params;
37 params.push_back(SGCONF::API_ACTION::PARAM("cash", "<cash>", "\tcorporation's cash"));
41 void SimpleCallback(bool result,
42 const std::string & reason,
47 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
50 std::cout << "Success.\n";
53 void GetCorpsCallback(bool result,
54 const std::string & reason,
55 const std::vector<STG::GetCorp::Info> & info,
60 std::cerr << "Failed to get corp list. Reason: '" << reason << "'." << std::endl;
63 std::cout << "Corps:\n";
64 for (size_t i = 0; i < info.size(); ++i)
65 PrintCorp(info[i], 1);
68 void GetCorpCallback(bool result,
69 const std::string & reason,
70 const STG::GetCorp::Info & info,
75 std::cerr << "Failed to get corp. Reason: '" << reason << "'." << std::endl;
81 bool GetCorpsFunction(const SGCONF::CONFIG & config,
82 const std::string & /*arg*/,
83 const std::map<std::string, std::string> & /*options*/)
85 return makeProto(config).GetCorporations(GetCorpsCallback, NULL) == STG::st_ok;
88 bool GetCorpFunction(const SGCONF::CONFIG & config,
89 const std::string & arg,
90 const std::map<std::string, std::string> & /*options*/)
92 return makeProto(config).GetCorp(arg, GetCorpCallback, NULL) == STG::st_ok;
95 bool DelCorpFunction(const SGCONF::CONFIG & config,
96 const std::string & arg,
97 const std::map<std::string, std::string> & /*options*/)
99 return makeProto(config).DelCorp(arg, SimpleCallback, NULL) == STG::st_ok;
102 bool AddCorpFunction(const SGCONF::CONFIG & config,
103 const std::string & arg,
104 const std::map<std::string, std::string> & options)
106 STG::CorpConfOpt conf;
108 SGCONF::MaybeSet(options, "cash", conf.cash);
109 return makeProto(config).AddCorp(arg, conf, SimpleCallback, NULL) == STG::st_ok;
112 bool ChgCorpFunction(const SGCONF::CONFIG & config,
113 const std::string & arg,
114 const std::map<std::string, std::string> & options)
116 STG::CorpConfOpt conf;
118 SGCONF::MaybeSet(options, "cash", conf.cash);
119 return makeProto(config).ChgCorp(conf, SimpleCallback, NULL) == STG::st_ok;
122 } // namespace anonymous
124 void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
126 std::vector<API_ACTION::PARAM> params(GetCorpParams());
127 blocks.Add("Corporation management options")
128 .Add("get-corps", SGCONF::MakeAPIAction(commands, GetCorpsFunction), "\tget corporation list")
129 .Add("get-corp", SGCONF::MakeAPIAction(commands, "<name>", GetCorpFunction), "get corporation")
130 .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", params, AddCorpFunction), "add corporation")
131 .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", DelCorpFunction), "delete corporation")
132 .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", params, ChgCorpFunction), "change corporation");