]> git.stg.codes - stg.git/blob - projects/sgconf/corps.cpp
Implemented --add-admin.
[stg.git] / projects / sgconf / corps.cpp
1 #include "corps.h"
2
3 #include "api_action.h"
4 #include "options.h"
5 #include "config.h"
6
7 #include "stg/servconf.h"
8 #include "stg/servconf_types.h"
9 #include "stg/corp_conf.h"
10 #include "stg/common.h"
11
12 #include <iostream>
13 #include <string>
14 #include <map>
15
16 namespace
17 {
18
19 std::string Indent(size_t level, bool dash = false)
20 {
21 if (level == 0)
22     return "";
23 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
24 }
25
26 void PrintCorp(const STG::GET_CORP::INFO & info, size_t level = 0)
27 {
28 std::cout << Indent(level, true) << "name: " << info.name << "\n"
29           << Indent(level)       << "cash: " << info.cash << "\n";
30 }
31
32 std::vector<SGCONF::API_ACTION::PARAM> GetCorpParams()
33 {
34 std::vector<SGCONF::API_ACTION::PARAM> params;
35 params.push_back(SGCONF::API_ACTION::PARAM("cash", "<cash>", "\tcorporation's cash"));
36 return params;
37 }
38
39 void SimpleCallback(bool result,
40                     const std::string & reason,
41                     void * /*data*/)
42 {
43 if (!result)
44     {
45     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
46     return;
47     }
48 std::cout << "Success.\n";
49 }
50
51 void GetCorpsCallback(bool result,
52                       const std::string & reason,
53                       const std::vector<STG::GET_CORP::INFO> & info,
54                       void * /*data*/)
55 {
56 if (!result)
57     {
58     std::cerr << "Failed to get corp list. Reason: '" << reason << "'." << std::endl;
59     return;
60     }
61 std::cout << "Corps:\n";
62 for (size_t i = 0; i < info.size(); ++i)
63     PrintCorp(info[i], 1);
64 }
65
66 void GetCorpCallback(bool result,
67                      const std::string & reason,
68                      const STG::GET_CORP::INFO & info,
69                      void * /*data*/)
70 {
71 if (!result)
72     {
73     std::cerr << "Failed to get corp. Reason: '" << reason << "'." << std::endl;
74     return;
75     }
76 PrintCorp(info);
77 }
78
79 bool GetCorpsFunction(const SGCONF::CONFIG & config,
80                       const std::string & /*arg*/,
81                       const std::map<std::string, std::string> & /*options*/)
82 {
83 STG::SERVCONF proto(config.server.data(),
84                     config.port.data(),
85                     config.userName.data(),
86                     config.userPass.data());
87 return proto.GetCorporations(GetCorpsCallback, NULL) == STG::st_ok;
88 }
89
90 bool GetCorpFunction(const SGCONF::CONFIG & config,
91                      const std::string & arg,
92                      const std::map<std::string, std::string> & /*options*/)
93 {
94 STG::SERVCONF proto(config.server.data(),
95                     config.port.data(),
96                     config.userName.data(),
97                     config.userPass.data());
98 return proto.GetCorp(arg, GetCorpCallback, NULL) == STG::st_ok;
99 }
100
101 bool DelCorpFunction(const SGCONF::CONFIG & config,
102                      const std::string & arg,
103                      const std::map<std::string, std::string> & /*options*/)
104 {
105 STG::SERVCONF proto(config.server.data(),
106                     config.port.data(),
107                     config.userName.data(),
108                     config.userPass.data());
109 return proto.DelCorp(arg, SimpleCallback, NULL) == STG::st_ok;
110 }
111
112 bool AddCorpFunction(const SGCONF::CONFIG & config,
113                      const std::string & arg,
114                      const std::map<std::string, std::string> & /*options*/)
115 {
116 // TODO
117 std::cerr << "Unimplemented.\n";
118 return false;
119 }
120
121 bool ChgCorpFunction(const SGCONF::CONFIG & config,
122                      const std::string & arg,
123                      const std::map<std::string, std::string> & options)
124 {
125 // TODO
126 std::cerr << "Unimplemented.\n";
127 return false;
128 }
129
130 } // namespace anonymous
131
132 void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
133 {
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");
141 }