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