user_head.cpp 3.85 KB
#include "user_head.h"
#include "gateway.h"
#include <gateway_msg.pb.h>
#include "user.h"
#include <db.pb.h>
#include "dbproxy.h"

#include "level.h"

head_cfg_mgr_t* g_head_cfg_mgr;

//////////////////////////////////////////////////////////////////////////
//head config
//////////////////////////////////////////////////////////////////////////

head_cfg_t* head_cfg_mgr_t::find( USER_HEAD_ID id )
{
	auto it = this->head_cfg_map.find(id);
	if (this->head_cfg_map.end() == it){
		return NULL;
	}
	return &it->second;
}

void head_cfg_mgr_t::init()
{
	this->head_cfg_map.clear();
}

bool head_cfg_mgr_t::load_cfg()
{
	const std::string cfg_path = "./cfg/head.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 *)"head")) {
			head_cfg_t head_cfg;
			uint32_t need = 0;
			xml.get_xml_prop(cur, head_cfg.id, "id");
			xml.get_xml_prop(cur, need, "needCard");
			head_cfg.need_card = (0 == need ? false : true);

			if (!this->head_cfg_map.insert(std::make_pair(head_cfg.id, head_cfg)).second) {
				ALERT_LOG("[id:%u]", head_cfg.id);
				return false;
			}
		}
		if (!xmlStrcmp(cur->name, (const xmlChar *)"headFrame")) {
			head_frame_cfg_t cfg;
			xml.get_xml_prop(cur, cfg.id, "id");
			xml.get_xml_prop_def(cur, cfg.need_score, "needScore", 0);
			xml.get_xml_prop_def(cur, cfg.need_charm, "needCharm", 0);
			if (0 < cfg.need_score){
			}

			if (0 < cfg.need_charm){

			}
			
			if (NULL == g_item_cfg_mgr->find(cfg.id)){
				ALERT_LOG("[id:%u]", cfg.id);
				return false;
			}
// 			if (!g_gd_score_cfg_mgr->has_level(cfg.need_score_level)){
// 				ALERT_LOG("[id:%u]", cfg.id);
// 				return false;
// 			}
			
			if (!this->head_frame_cfg_map.insert(std::make_pair(cfg.id, cfg)).second) {
				ALERT_LOG("[id:%u]", cfg.id);
				return false;
			}
		}
		
		cur = cur->next;
	}//while
	return true;
}

head_frame_cfg_t* head_cfg_mgr_t::find_head_frame( USER_HEAD_FRAME_ID id )
{
	auto it = this->head_frame_cfg_map.find(id);
	if (this->head_frame_cfg_map.end() == it){
		return NULL;
	}
	return &it->second;
}


head_cfg_t::head_cfg_t()
{
	this->id = 0;
	this->need_card = false;
}

//////////////////////////////////////////////////////////////////////////
int gateway_t::on_change_head_msg(el::lib_tcp_peer_info_t* peer_fd_info, google::protobuf::Message* msg, user_t* user)
{
	if (!user->wx_unionid.empty()){
		ERROR_LOG("");
		return share_msg::EC_VALUE_INVALID;
	}
	
	auto in = (gateway_msg::change_head_msg*)msg;
	auto head = in->head();

	head_cfg_t* head_cfg = g_head_cfg_mgr->find(head);
	if (NULL == head_cfg){
		return share_msg::EC_VALUE_INVALID;
	}

	if (head_cfg->need_card){// need delete card

	}

	{//send to db
		db_msg::change_head_msg out;
		out.set_head(head);
		g_dbproxy->send_msg(&out, user->uid(), db_change_head_msg_cmd, NULL);
	}
	{//send to user
		gateway_msg::change_head_msg_res out;
		out.set_head(head);
		user->send_msg_res(&out);
	}
	user->user_show.set_head(head);

	return 0;
}

int gateway_t::on_change_head_frame_msg(el::lib_tcp_peer_info_t* peer_fd_info, google::protobuf::Message* msg, user_t* user)
{
	auto in = (gateway_msg::change_head_frame_msg*)msg;
	auto head_frame = in->head_frame();

	auto* cfg = g_head_cfg_mgr->find_head_frame(head_frame);
	if (NULL == cfg){
		return share_msg::EC_VALUE_INVALID;
	}
	if (user->item_mgr.get_item_cnt(head_frame) < 1){
		return share_msg::EC_INEXISTENT_ITEM;
	}
	
	if (0 != cfg->need_score){
	}
	if (0 != cfg->need_charm){

	}
	
	{//send to db
		user->event_mgr.update_event(user->uid(), common_msg::FOREVER_EVENT_HEAD_FRAME, 0, head_frame, 0);
	}
	{//send to user
		gateway_msg::change_head_frame_msg_res out;
		out.set_head_frame(head_frame);
		user->send_msg_res(&out);
	}
	user->user_show.set_head_frame(head_frame);
	
	return 0;
}