item.cpp 9.41 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
#include "item.h"
#include "user.h"
#include "gateway.h"
#include "dbproxy.h"
#include <gateway_msg.pb.h>
#include <lib_file_tab_txt.h>
#include <db.pb.h>
#include "level.h"

item_cfg_mgr_t* g_item_cfg_mgr;

share_msg::item_t* item_mgr_t::find(ITEM_ID id){
	auto it = this->item_map.find(id);
	if(it == this->item_map.end()){
		return NULL;
	}
	return &it->second;
}

void item_mgr_t::update( ITEM_ID id, ITEM_CNT cnt )
{	
	if (0 == cnt){
		this->del(id);
	} else {
		share_msg::item_t* item = this->find(id);
		if (NULL == item){
			share_msg::item_t new_item;
			new_item.set_id(id);
			new_item.set_cnt(cnt);
			this->item_map[id] = new_item;
		} else {
			item->set_cnt(cnt);
		}
		
		{
			db_msg::update_item_msg out;
			share_msg::item_t* item = out.mutable_item();
			item->set_id(id);
			item->set_cnt(cnt);
			g_dbproxy->send_msg(&out, this->user->uid(), db_update_item_msg_cmd, NULL);
		}

		this->notify_user(id, cnt);
	}
}

void item_mgr_t::del( ITEM_ID id )
{
	this->item_map.erase(id);

	{
		db_msg::del_item_msg out;
		share_msg::item_t* item = out.mutable_item();
		item->set_id(id);
		g_dbproxy->send_msg(&out, this->user->uid(), db_del_item_msg_cmd, NULL);
	}

	this->notify_user(id, 0);
}

void item_mgr_t::load_from_db( const share_msg::item_t& r )
{
	this->item_map[r.id()] = r;
}

void item_mgr_t::notify_user( ITEM_ID id, ITEM_CNT cnt )
{
	gateway_msg::notify_item_msg_res out;
	share_msg::item_t* item = out.add_items();
	item->set_id(id);
	item->set_cnt(cnt);
	this->user->send_msg(cli_notify_item_msg_cmd, &out);
}

ITEM_CNT item_mgr_t::get_item_cnt( ITEM_ID id )
{
	share_msg::item_t* item = this->find(id);
	if (NULL == item){
		return 0;
	}
	return item->cnt();
}

void item_mgr_t::reduce( ITEM_ID id, ITEM_CNT cnt )
{
	if (0 == id || 0 == cnt){
		return;
	}
	
	ITEM_CNT has_cnt = this->get_item_cnt(id);
	if (has_cnt <= cnt){
		this->update(id, 0);
	} else {
		this->update(id, has_cnt-cnt);
	}
}

void item_mgr_t::add( ITEM_ID id, ITEM_CNT cnt )
{
	if (0 == cnt || 0 == id){
		return;
	}
	share_msg::item_t* item = this->find(id);
	switch (id){
	case share_msg::E_ITEM_ID_MONEY:
		user->achievement_mgr.update(4, cnt);
		break;
	case share_msg::E_ITEM_ID_CASH:
		user->achievement_mgr.update(5, cnt);
		break;
	case share_msg::E_ITEM_ID_VIP_EXP:
		{
			if (NULL == item){
				vip_level_cfg_t* vlc = g_vip_level_cfg_mgr->find(cnt);
				user->achievement_mgr.update(10, vlc->level);
			} else {
				vip_level_cfg_t* old_vlc = g_vip_level_cfg_mgr->find(item->cnt());
				vip_level_cfg_t* new_val = g_vip_level_cfg_mgr->find(item->cnt() + cnt);
				if (old_vlc->level < new_val->level){
					user->achievement_mgr.update(10, new_val->level - old_vlc->level);
				}
			}
		}
		break;
	default:
		break;
	}
	
	if (g_item_cfg_mgr->is_furniture(id)){
		db_msg::update_furniture_msg out;
		share_msg::furniture_t* f = out.mutable_furniture();
		f->set_id(id);
		g_dbproxy->send_msg(&out, this->user->uid(), db_update_furniture_msg_cmd, NULL);

		this->user->furniture_mgr.furniture_map[id] = *f;

		//this->notify_user(id, 1);
		this->user->furniture_mgr.notify_user(id);

		user->achievement_mgr.update(8, 1);
	} else {
		if (NULL == item){
			if (g_item_cfg_mgr->is_clothes_wing(id)){
				user->achievement_mgr.update(7, 1);
			} else if (g_item_cfg_mgr->is_clothes(id)){
				user->achievement_mgr.update(6, 1);
			}
			
			else if (g_item_cfg_mgr->is_wall(id)){
				user->achievement_mgr.update(9, 1);
			}
			this->update(id, cnt);
		} else {
			uint64_t all_cnt = item->cnt() + cnt;
			ITEM_CNT new_cnt = (ITEM_CNT)(min(all_cnt, (uint64_t)ITEM_CNT_MAX));
			this->update(id, new_cnt);
		}
	}
}

bool item_mgr_t::is_has_all( ITEM_ID id, ITEM_CNT cnt )
{
	if (0 == cnt){
		return true;
	}
	
	return this->get_item_cnt(id) >= cnt;
}

item_mgr_t::item_mgr_t()
{
	this->user = NULL;
}

// ITEM_CNT item_mgr_t::game_add( ITEM_ID id, ITEM_CNT cnt )
// {
// 	if (share_msg::E_ITEM_ID_MONEY == id){
// 		static uint32_t game_add_money_max_cnt = 30000000;
// 		EVENT_DATA data = this->user->event_mgr.find_data(common_msg::DAILY_EVENT_GAME_MONEY_CNT_MAX);
// 		if (game_add_money_max_cnt < data){
// 			{
// 				gateway_msg::notify_item_cnt_max_msg_res out;
// 				auto item = out.add_item();
// 				item->set_id(share_msg::E_ITEM_ID_MONEY);
// 				item->set_cnt(game_add_money_max_cnt);
// 				this->user->send_msg(cli_notify_item_cnt_max_msg_cmd, &out);
// 			}
// 			return 0;
// 		}
// 		this->user->event_mgr.update_event(this->user->uid(), common_msg::DAILY_EVENT_GAME_MONEY_CNT_MAX, 0, data+cnt, el::lib_time_t::tomorrow_start_time() - 1);
// 	}
// 	this->add(id, cnt);
// 	return cnt;
// }

void item_mgr_t::clothes_off( uint32_t body_type )
{
	this->user->event_mgr.del_event(this->user->uid(), common_msg::FOREVER_EVENT_ROLE_CLOTHES, body_type);
}

void item_mgr_t::clothes_on( ITEM_ID item_id )
{
	common_msg::item_cfg_t* item_cfg = g_item_cfg_mgr->find(item_id);
	this->user->event_mgr.update_event(this->user->uid(), common_msg::FOREVER_EVENT_ROLE_CLOTHES, item_cfg->body_type(), item_id, 0);
}

void item_mgr_t::wall_on( ITEM_ID item_id )
{
	this->user->event_mgr.update_event(this->user->uid(), common_msg::FOREVER_EVENT_ROLE_WALL, 0, item_id, 0);
}





//////////////////////////////////////////////////////////////////////////
//item config
//////////////////////////////////////////////////////////////////////////

common_msg::item_cfg_t* item_cfg_mgr_t::find( ITEM_ID id )
{
	auto it = this->item_cfg_map.find(id);
	if (this->item_cfg_map.end() == it){
		return NULL;
	}
	return &it->second;
}

void item_cfg_mgr_t::init()
{
	this->item_cfg_map.clear();
	this->pet_star_item_map.clear();
}

bool item_cfg_mgr_t::load_cfg()
{
	const std::string cfg_path = "./cfg/item.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;
			std::string str_def;
			common_msg::item_cfg_t item_cfg;

			uint32_t item_id = file_tab_txt.get_val_def("id", r, 0);
			if (0 == item_id){
				ALERT_LOG("[item_id:%u]", item_id);
				return false;
			}
			item_cfg.set_id(item_id);

			uint32_t item_stock = file_tab_txt.get_val_def("amount", r, 0);
			item_cfg.set_stock(item_stock);

			{
				uint32_t item_body_type = file_tab_txt.get_val_def("type", r, 0);
				item_cfg.set_body_type(item_body_type);

				if (0 != item_body_type){
					if (item_body_type < share_msg::E_USER_BODY_TYPE_MIN || share_msg::E_USER_BODY_TYPE_MAX < item_body_type){
						ALERT_LOG("[item_id:%u]", item_id);
						return false;
					}
				} else {
					if (g_item_cfg_mgr->is_clothes(item_id)){
						ALERT_LOG("[item_id:%u]", item_id);
						return false;
					}
				}
			}
			{
				uint32_t add_pet_exp = file_tab_txt.get_val_def("add_pet_exp", r, 0);
				item_cfg.set_add_pet_exp(add_pet_exp);

				if (0 != add_pet_exp){
					if (!g_item_cfg_mgr->is_pet_eat(item_id)){
						ALERT_LOG("[item_id:%u]", item_id);
						return false;
					}
				} else {
					if (g_item_cfg_mgr->is_pet_eat(item_id)){
						ALERT_LOG("[item_id:%u]", item_id);
						return false;
					}
				}
			}
			uint32_t pet_star = 0;
			{
				pet_star = file_tab_txt.get_val_def("pet_star", r, 0);
				item_cfg.set_pet_star(pet_star);

				if (0 != pet_star){
					if (!g_item_cfg_mgr->is_pet_key(item_id) && !g_item_cfg_mgr->is_pet_skill_crystal(item_id)){
						ALERT_LOG("[item_id:%u]", item_id);
						return false;
					}
				} else {
					if (g_item_cfg_mgr->is_pet_key(item_id)){
						ALERT_LOG("[item_id:%u]", item_id);
						return false;
					}
				}
			}

			{
				uint32_t vip = file_tab_txt.get_val_def("vip", r, 0);
				item_cfg.set_vip(vip);
			}

			if (!this->item_cfg_map.insert(std::make_pair(item_cfg.id(), item_cfg)).second) {
				ALERT_LOG("[item_id:%u]", item_id);
				return false;
			}

			common_msg::item_cfg_t* ic = this->find(item_cfg.id());
			if (0 != pet_star){
				this->pet_star_item_map.insert(std::make_pair(pet_star, ic));
			}
		}
	}

	return true;
}

common_msg::item_cfg_t* item_cfg_mgr_t::find_key_by_star( uint32_t star )
{
	FOREACH(this->item_cfg_map, it){
		common_msg::item_cfg_t& ic = it->second;
		if (!this->is_pet_key(ic.id())){
			continue;
		}
		
		if (star == ic.pet_star()){
			return &ic;
		}
	}
	return NULL;
}

common_msg::item_cfg_t* item_cfg_mgr_t::find_shuijing_by_star( uint32_t star )
{
	FOREACH(this->item_cfg_map, it){
		common_msg::item_cfg_t& ic = it->second;
		if (!this->is_shuijing(ic.id())){
			continue;
		}

		if (star == ic.pet_star()){
			return &ic;
		}
	}
	return NULL;
}

//////////////////////////////////////////////////////////////////////////
int gateway_t::on_use_item_msg(el::lib_tcp_peer_info_t* peer_fd_info,
								  							   google::protobuf::Message* msg,
								  							   user_t* user)
{
	return 1;
}

furniture_mgr_t::furniture_mgr_t()
{
	this->user = NULL;
}

share_msg::furniture_t* furniture_mgr_t::find( uint32_t furniture_id )
{
	auto it = this->furniture_map.find(furniture_id);
	if (this->furniture_map.end() == it){
		return NULL;
	}
	return &it->second;
}

void furniture_mgr_t::update( share_msg::furniture_t* furniture )
{
	{
		db_msg::update_furniture_msg out;
		
		share_msg::furniture_t* f = out.mutable_furniture();
		f->CopyFrom(*furniture);
		g_dbproxy->send_msg(&out, this->user->uid(), db_update_furniture_msg_cmd, NULL);
	}
}

void furniture_mgr_t::notify_user( ITEM_ID furniture_id )
{
	gateway_msg::furniture_pos_msg_res out;
	out.mutable_furniture()->set_id(furniture_id);
	this->user->send_msg(cli_furniture_pos_msg_cmd, &out);
}