nick.cpp
3.12 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
#include "dbproxy.h"
#include "gateway.h"
#include "user.h"
#include <gateway_msg.pb.h>
#include "nick.h"
#include <db.pb.h>
rand_nick_mgr* g_rand_nick_mgr;
bool rand_nick_mgr::load_cfg(void)
{
const std::string cfg_path = "./cfg/nick.xml";
el::lib_xmlparser_t xml;
int ret = xml.open(cfg_path.c_str());
if (SUCC != ret){
ALERT_LOG("open");
return false;
}
this->mem_first_names.clear();
this->mem_middle_names.clear();
this->mem_last_names.clear();
xml.move2children_node();
xmlNodePtr cur = xml.node_ptr;
while (cur) {
std::string str_def;
std::string str_data;
if (!xmlStrcmp(cur->name, (const xmlChar *)"first")) {
xml.get_xml_prop_def(cur, str_data, "str", str_def);
el::g_cat_string(this->mem_first_names, str_data, ',');
} else if (!xmlStrcmp(cur->name, (const xmlChar *)"middle")) {
xml.get_xml_prop_def(cur, str_data, "str", str_def);
el::g_cat_string(this->mem_middle_names, str_data, ',');
} else if (!xmlStrcmp(cur->name, (const xmlChar *)"last")) {
xml.get_xml_prop_def(cur, str_data, "str", str_def);
el::g_cat_string(this->mem_last_names, str_data, ',');
}
cur = cur->next;
}
if (mem_first_names.empty() || mem_middle_names.empty() || mem_last_names.empty()) {
ALERT_LOG("");
return false;
}
return true;
}
std::string rand_nick_mgr::get_rand_name(void)
{
std::string new_name;
int index_first = rand() % mem_first_names.size();
int index_middle = rand() % mem_middle_names.size();
int index_last = rand() % mem_last_names.size();
new_name = this->mem_first_names[index_first];
new_name += this->mem_middle_names[index_middle];
new_name += this->mem_last_names[index_last];
return new_name;
}
//////////////////////////////////////////////////////////////////////////
int gateway_t::on_change_nick_msg(el::lib_tcp_peer_info_t* peer_fd_info, google::protobuf::Message* msg, user_t* user)
{
if (!user->wx_unionid.empty()){
return 0;
}
auto in = (gateway_msg::change_nick_msg*)msg;
auto id1 = in->id1();
auto id2 = in->id2();
auto id3 = in->id3();
if (g_rand_nick_mgr->mem_first_names.size() < id1-1){
ERROR_LOG("");
return share_msg::EC_VALUE_INVALID;
}
if (g_rand_nick_mgr->mem_middle_names.size() < id2-1){
ERROR_LOG("");
return share_msg::EC_VALUE_INVALID;
}
if (g_rand_nick_mgr->mem_last_names.size() < id3-1){
ERROR_LOG("");
return share_msg::EC_VALUE_INVALID;
}
auto str_nick = g_rand_nick_mgr->mem_first_names[id1-1] + g_rand_nick_mgr->mem_middle_names[id2-1] + g_rand_nick_mgr->mem_last_names[id3-1];
db_msg::change_nick_msg out;
out.set_nick(str_nick);
g_dbproxy->send_msg(&out, user->uid(), db_change_nick_msg_cmd, user);
user->user_show.set_nick(str_nick);
return 0;
}
int dbproxy_t::on_change_nick_msg_res(el::lib_tcp_peer_info_t* peer_fd_info, google::protobuf::Message* msg, USER_ID uid, uint32_t seq, uint32_t ret, user_t* user)
{
auto in = (db_msg::change_nick_msg_res*)msg;
uint32_t res = in->res();
gateway_msg::change_nick_msg_res out;
out.set_res(res);
if (0 == res) {
std::string new_nick = in->nick();
user->user_show.set_nick(new_nick);
out.set_nick(new_nick);
}
user->send_msg(cli_change_nick_msg_cmd, &out);
return SUCC;
}