nick.cpp 3.12 KB
#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;
}