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 void SimpleCallback(bool result,
33 const std::string & reason,
38 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
41 std::cout << "Success.\n";
44 void GetCorpsCallback(bool result,
45 const std::string & reason,
46 const std::vector<STG::GET_CORP::INFO> & info,
51 std::cerr << "Failed to get corp list. Reason: '" << reason << "'." << std::endl;
54 std::cout << "Corps:\n";
55 for (size_t i = 0; i < info.size(); ++i)
56 PrintCorp(info[i], 1);
59 void GetCorpCallback(bool result,
60 const std::string & reason,
61 const STG::GET_CORP::INFO & info,
66 std::cerr << "Failed to get corp. Reason: '" << reason << "'." << std::endl;
72 bool GetCorpsFunction(const SGCONF::CONFIG & config,
73 const std::string & /*arg*/,
74 const std::map<std::string, std::string> & /*options*/)
76 STG::SERVCONF proto(config.server.data(),
78 config.userName.data(),
79 config.userPass.data());
80 return proto.GetCorporations(GetCorpsCallback, NULL) == STG::st_ok;
83 bool GetCorpFunction(const SGCONF::CONFIG & config,
84 const std::string & arg,
85 const std::map<std::string, std::string> & /*options*/)
87 STG::SERVCONF proto(config.server.data(),
89 config.userName.data(),
90 config.userPass.data());
91 return proto.GetCorp(arg, GetCorpCallback, NULL) == STG::st_ok;
94 bool DelCorpFunction(const SGCONF::CONFIG & config,
95 const std::string & arg,
96 const std::map<std::string, std::string> & /*options*/)
98 STG::SERVCONF proto(config.server.data(),
100 config.userName.data(),
101 config.userPass.data());
102 return proto.DelCorp(arg, SimpleCallback, NULL) == STG::st_ok;
105 bool AddCorpFunction(const SGCONF::CONFIG & config,
106 const std::string & arg,
107 const std::map<std::string, std::string> & /*options*/)
110 std::cerr << "Unimplemented.\n";
114 bool ChgCorpFunction(const SGCONF::CONFIG & config,
115 const std::string & arg,
116 const std::map<std::string, std::string> & options)
119 std::cerr << "Unimplemented.\n";
123 } // namespace anonymous
125 void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
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>", true, GetCorpFunction), "get corporation")
130 .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", true, AddCorpFunction), "add corporation")
131 .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", true, DelCorpFunction), "del corporation")
132 .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", true, ChgCorpFunction), "change corporation");