]> git.stg.codes - stg.git/blob - projects/sgconf/corps.cpp
b48a7c71352bc769a065945224448ec3109a7ffb
[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 void SimpleCallback(bool result,
33                     const std::string & reason,
34                     void * /*data*/)
35 {
36 if (!result)
37     {
38     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
39     return;
40     }
41 std::cout << "Success.\n";
42 }
43
44 void GetCorpsCallback(bool result,
45                       const std::string & reason,
46                       const std::vector<STG::GET_CORP::INFO> & info,
47                       void * /*data*/)
48 {
49 if (!result)
50     {
51     std::cerr << "Failed to get corp list. Reason: '" << reason << "'." << std::endl;
52     return;
53     }
54 std::cout << "Corps:\n";
55 for (size_t i = 0; i < info.size(); ++i)
56     PrintCorp(info[i], 1);
57 }
58
59 void GetCorpCallback(bool result,
60                      const std::string & reason,
61                      const STG::GET_CORP::INFO & info,
62                      void * /*data*/)
63 {
64 if (!result)
65     {
66     std::cerr << "Failed to get corp. Reason: '" << reason << "'." << std::endl;
67     return;
68     }
69 PrintCorp(info);
70 }
71
72 bool GetCorpsFunction(const SGCONF::CONFIG & config,
73                       const std::string & /*arg*/,
74                       const std::map<std::string, std::string> & /*options*/)
75 {
76 STG::SERVCONF proto(config.server.data(),
77                     config.port.data(),
78                     config.userName.data(),
79                     config.userPass.data());
80 return proto.GetCorporations(GetCorpsCallback, NULL) == STG::st_ok;
81 }
82
83 bool GetCorpFunction(const SGCONF::CONFIG & config,
84                      const std::string & arg,
85                      const std::map<std::string, std::string> & /*options*/)
86 {
87 STG::SERVCONF proto(config.server.data(),
88                     config.port.data(),
89                     config.userName.data(),
90                     config.userPass.data());
91 return proto.GetCorp(arg, GetCorpCallback, NULL) == STG::st_ok;
92 }
93
94 bool DelCorpFunction(const SGCONF::CONFIG & config,
95                      const std::string & arg,
96                      const std::map<std::string, std::string> & /*options*/)
97 {
98 STG::SERVCONF proto(config.server.data(),
99                     config.port.data(),
100                     config.userName.data(),
101                     config.userPass.data());
102 return proto.DelCorp(arg, SimpleCallback, NULL) == STG::st_ok;
103 }
104
105 bool AddCorpFunction(const SGCONF::CONFIG & config,
106                      const std::string & arg,
107                      const std::map<std::string, std::string> & /*options*/)
108 {
109 // TODO
110 std::cerr << "Unimplemented.\n";
111 return false;
112 }
113
114 bool ChgCorpFunction(const SGCONF::CONFIG & config,
115                      const std::string & arg,
116                      const std::map<std::string, std::string> & options)
117 {
118 // TODO
119 std::cerr << "Unimplemented.\n";
120 return false;
121 }
122
123 } // namespace anonymous
124
125 void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
126 {
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");
133 }