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