role_reward.cpp
5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "role_reward.h"
#include "gateway.h"
#include <gateway_msg.pb.h>
#include "user.h"
#include <lib_file_tab_txt.h>
role_reward_cfg_mgr_t* g_role_reward_cfg_mgr;
create_reward_cfg_mgr_t* g_create_reward_cfg_mgr;
//////////////////////////////////////////////////////////////////////////
//role_reward config
//////////////////////////////////////////////////////////////////////////
role_reward_element_t* role_reward_cfg_mgr_t::find( uint32_t id )
{
auto it = this->element_map.find(id);
if (this->element_map.end() == it){
return NULL;
}
return &it->second;
}
void role_reward_cfg_mgr_t::init()
{
this->element_map.clear();
}
bool role_reward_cfg_mgr_t::load_cfg()
{
const std::string cfg_path = "./cfg/role_reward.txt";
el::lib_file_tab_txt_t file_tab_txt;
int ret = file_tab_txt.load(cfg_path.c_str());
if (0 != ret){
ALERT_LOG("open");
return false;
}
this->init();
{
FOREACH(file_tab_txt.content_vector, it_content){
std::vector<std::string>& r = *it_content;
uint32_t id = file_tab_txt.get_val_def("id", r, 0);
if (0 == id){
ALERT_LOG("[id:%u]", id);
return false;
}
role_reward_element_t element;
std::string str_reward;
file_tab_txt.get_val(str_reward, "reward", r);
{
std::set<uint32_t> has_set;
std::vector<std::vector<uint32_t>> reward_vec_vec;
reward_vec_vec = el::g_cat_string<uint32_t>(str_reward, ';', ',');
FOREACH(reward_vec_vec, it_vec){
std::vector<uint32_t>& r_vec = *it_vec;
if (2 != r_vec.size()){
ALERT_LOG("[id:%u]", id);
return false;
}
if (NULL == g_item_cfg_mgr->find(r_vec[0])){
ALERT_LOG("[id:%u]", id);
return false;
}
share_msg::item_t item;
item.set_id(r_vec[0]);
if (!has_set.insert(r_vec[0]).second){
ALERT_LOG("[id:%u]", id);
return false;
}
item.set_cnt(r_vec[1]);
element.reward_vec.push_back(item);
}
}
if (!this->element_map.insert(std::make_pair(id, element)).second) {
ALERT_LOG("[id:%u]", id);
return false;
}
}
}
return true;
}
void create_reward_cfg_mgr_t::init()
{
this->reward_map.clear();
}
bool create_reward_cfg_mgr_t::load_cfg()
{
const std::string cfg_path = "./cfg/create_reward.xml";
el::lib_xmlparser_t xml;
int ret = xml.open(cfg_path.c_str());
if (SUCC != ret){
ALERT_LOG("open");
return false;
}
this->init();
xml.move2children_node();
xmlNodePtr cur = xml.node_ptr;
while (cur) {
if (!xmlStrcmp(cur->name, (const xmlChar *)"id")) {
{
std::string str_def;
std::string str_item;
xml.get_xml_prop_def(cur, str_item, "item", str_def);
std::vector<uint_pair> uint_pair_val;
str_split_uint(str_item, uint_pair_val);
FOREACH(uint_pair_val, it) {
if (NULL == g_item_cfg_mgr->find(it->key)){
ALERT_LOG("[item_id:%u]", it->key);
return false;
}
if (!this->reward_map.insert(std::make_pair(it->key, it->val)).second) {
ALERT_LOG("[item_id:%u]", it->key);
return false;
}
}
}
{// pet
// std::string str_def;
// std::string str_pet;
// xml.get_xml_prop_def(cur, str_pet, "pet", str_def);
// std::vector<uint32_t> u_vec;
// el::g_cat_string(u_vec, str_pet);
// if (2 != u_vec.size()){
// ALERT_LOG("[item_id]");
// return false;
// }
// this->pet_id = u_vec[0];
// if (0 != this->pet_id){
// pet_cfg_t* pc = g_pet_cfg_mgr->find(this->pet_id);
// if (NULL == pc){
// return false;
// }
// this->pet_idx = u_vec[1];
// if (this->pet_idx < 0 || 5 < this->pet_idx){
// return false;
// }
// }
// //////////////////////////////////////////////////////////////////////////
std::string str_def;
std::string str_item;
xml.get_xml_prop_def(cur, str_item, "pet", str_def);
std::vector<uint_pair> uint_pair_val;
str_split_uint(str_item, uint_pair_val);
FOREACH(uint_pair_val, it) {
pet_cfg_t* pc = g_pet_cfg_mgr->find(it->key);
if (NULL == pc){
return false;
}
if (it->val < 0 || 5 < it->val){
return false;
}
share_msg::key_val_t k;
k.set_key(it->key);
k.set_val1(it->val);
this->pet_id_idx.push_back(k);
}
}
{
std::string str_def;
std::string str_item;
xml.get_xml_prop_def(cur, str_item, "wxLogin", str_def);
std::vector<uint_pair> uint_pair_val;
str_split_uint(str_item, uint_pair_val);
FOREACH(uint_pair_val, it) {
if (NULL == g_item_cfg_mgr->find(it->key)){
ALERT_LOG("[item_id:%u]", it->key);
return false;
}
if (!this->wx_reward_map.insert(std::make_pair(it->key, it->val)).second) {
ALERT_LOG("[item_id:%u]", it->key);
return false;
}
}
}
}
cur = cur->next;
}//while
return true;
}
//////////////////////////////////////////////////////////////////////////
int gateway_t::on_get_role_reward_msg(el::lib_tcp_peer_info_t* peer_fd_info,
google::protobuf::Message* msg,
user_t* user)
{
uint32_t next_role_reward_id = 0;
uint32_t last_time_second = 0;
event_t* event = user->event_mgr.find(common_msg::FOREVER_EVENT_ROLE_REWARD);
if (NULL == event){
next_role_reward_id = 1;
} else {
next_role_reward_id = (uint32_t)event->data + 1;
last_time_second = event->time;
}
role_reward_element_t* shop_element = g_role_reward_cfg_mgr->find(next_role_reward_id);
if (NULL == shop_element){
return share_msg::EC_VALUE_INVALID;
}
if (el::lib_time_t::time_is_today(last_time_second)){
return share_msg::EC_VALUE_INVALID;
}
user->event_mgr.update_event(user->uid(), common_msg::FOREVER_EVENT_ROLE_REWARD, 0, next_role_reward_id, el_async::get_now_sec());
FOREACH(shop_element->reward_vec, it){
share_msg::item_t& buy = *it;
user->item_mgr.add(buy.id(), buy.cnt());
}
{
gateway_msg::get_role_reward_msg_res out;
user->send_msg_res(&out);
}
return 0;
}