role_reward.cpp 5.79 KB
#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;
}