btl_cnt_reward.cpp 1.87 KB
#include "btl_cnt_reward.h"
#include "item.h"
#include "gateway.h"
#include "user.h"

btl_cnt_reward_cfg_mgr_t* g_btl_cnt_reward_cfg_mgr;

//////////////////////////////////////////////////////////////////////////
//btl_cnt_reward config
//////////////////////////////////////////////////////////////////////////

void btl_cnt_reward_cfg_mgr_t::init()
{
	this->reward_map.clear();
}

bool btl_cnt_reward_cfg_mgr_t::load_cfg()
{
	const std::string cfg_path = "./cfg/btl_cnt_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 *)"reward")) {
			uint32_t id = 0;
			xml.get_xml_prop(cur, id, "id");
			btl_cnt_reward_element_t bcre;
			xml.get_xml_prop(cur, bcre.pr, "pr");

			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;
				}
				share_msg::item_t item;
				item.set_id(it->key);
				item.set_cnt(it->val);
				bcre.reward_vec.push_back(item);
			}
			this->reward_map.insert(std::make_pair(id, bcre));
		}
		cur = cur->next;
	}//while

	uint32_t all_pr = 0;
	FOREACH(this->reward_map, it){
		btl_cnt_reward_element_t& bcre = it->second;
		if (0 == bcre.pr){
			ALERT_LOG("[pr:0]");
			return false;
		}
		all_pr += bcre.pr;
	}
	if (1000 != all_pr){
		ALERT_LOG("[1000 != all_pr]");
		return false;
	}
	
 	return true;
}

btl_cnt_reward_element_t* btl_cnt_reward_cfg_mgr_t::find( uint32_t id )
{
	auto it = this->reward_map.find(id);
	if (this->reward_map.end() == it){
		return NULL;
	}
	return &it->second;
}