lobby_service.cpp 11.3 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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
#include "lobby_service.h"
#include <proto_header.h>
#include "user.h"
#include "gateway.h"
#include <lobby_gateway.pb.h>
#include <gateway_msg.pb.h>
#include "dbproxy.h"
#include "rank.h"
#include <db.pb.h>
#include <share_msg.pb.h>
#include "btl_cnt_reward.h"
#include "game_cfg.h"


#include "sys.h"

lobby_service_t* g_lobby_service;


int lobby_service_t::send_msg( google::protobuf::Message* msg, USER_ID ack_uid,
						CMD_ID cmd)
{
	static proto_head_t proto_head;
	proto_head.cmd = cmd;
	proto_head.id = ack_uid;

	try{
		static void* sb = g_gateway->send_msg_buf.send_data + proto_head_t::PROTO_HEAD_LEN;
		static const int sb_len = sizeof(g_gateway->send_msg_buf.send_data) - proto_head_t::PROTO_HEAD_LEN;
		msg->SerializeToArray(sb, sb_len);

		uint32_t body_len = msg->ByteSize();

		TRACE_LOG("======>s2lobby msg [uid:%" PRIu64 ", cmd:0x%x, body_len:%u]", ack_uid, cmd, body_len);

		g_gateway->send_msg_buf.add_body_len(body_len);
		g_gateway->send_msg_buf.set_head(&proto_head);
		
		int ret = this->do_s2(g_gateway->send_msg_buf.send_data, body_len + proto_head_t::PROTO_HEAD_LEN);

		if (!msg->Utf8DebugString().empty()){
			TRACE_LOG("%s", msg->Utf8DebugString().c_str());
		}
		return ret;
	} catch (...){
		ERROR_LOG("[uid:%" PRIu64 ", cmd:%u, seq:%u]", ack_uid, (uint32_t)cmd, proto_head.seq);
	}
	return 0;
}

lobby_service_t::lobby_service_t()
{
#undef  BIND_PROTO_CMD
#undef  BIND_PROTO_CMD_NO_CB

#define BIND_PROTO_CMD(cmd, fun_name, proto_name)\
	{\
		if (this->msg_map.end() != this->msg_map.find(cmd)){\
			WARN_LOG("cmd inster err![0x%x]", cmd);\
			exit(0);\
		} else {\
			this->msg_map[cmd] = MSG_HANDLE_LOBBY_T(new lobby_gateway_msg::proto_name##_res(), &lobby_service_t::fun_name##_res, true);\
		}\
	}
#define BIND_PROTO_CMD_NO_CB(cmd, fun_name, proto_name) BIND_PROTO_CMD(cmd, fun_name, proto_name)

#include <lobby_gateway_cmd.h>
#undef  BIND_PROTO_CMD
#undef  BIND_PROTO_CMD_NO_CB

	if (NULL == this->peer){
		this->ip = g_bench_conf->get_strval("lobby", "ip");
		this->port = ::atoi(g_bench_conf->get_strval("lobby", "port").c_str());	

		if (SUCC != this->connect()){
			ERROR_LOG("connet to lobby fail %s:%u", this->ip.c_str(), this->port);
		}
	}
}

lobby_service_t::~lobby_service_t()
{
	FOREACH(this->msg_map, it){
		SAFE_DELETE(it->second.prototype);
	}
	this->msg_map.clear();

	this->offline();
}

int lobby_service_t::on_recv( el::lib_tcp_peer_info_t* peer_fd_info, CMD_ID cmd,
					   char* pdata, int len, USER_ID uid, uint32_t seq,
					   uint32_t ret )
{
	TRACE_LOG("<======recv lobby [uid:%" PRIu64 ", cmd_id:0x%x, body_len:%u, seq:%u]", 
		uid, cmd, len, seq);
	user_t* user = NULL;
	user = g_user_mgr->find(uid);
	if (NULL == user){
		WARN_LOG("");
		return ERR;
	}
	
//     if (SUCC != ret){
// 		ERROR_LOG("lobby back msg error [cmd:%#x, ret:%#x]", cmd, ret);
// 		if (NULL == user){
// 			return ret;
// 		}
// 		
// 		user->send_err(cmd, ret);
// 		return ret;
//     }
	auto it = this->msg_map.find(cmd);
	if(it == this->msg_map.end()){
// 		if (common_msg::E_PENETRATE_ROOM_MSG_BEGIN <= cmd
// 			&& cmd <= common_msg::E_PENETRATE_ROOM_MSG_END){
// 			if (NULL == user){
// 				return SUCC;
// 			}
// 			el_async::s2peer(user->fd_info, pdata-proto_head_t::PROTO_HEAD_LEN, len+proto_head_t::PROTO_HEAD_LEN);
// 			return SUCC;
// 		}
		WARN_LOG("[cmd:%#x]", cmd);
		return ERR;
	}

	try{
		if(!it->second.prototype->ParseFromArray(pdata, len)){
			WARN_LOG("[cmd:0x%#x]", cmd);
			return ERR;
		}
	}catch(...)
	{
		WARN_LOG("[cmd:0x%#x", cmd);
		return ERR;
	}

    if (!it->second.prototype->Utf8DebugString().empty()){
        TRACE_LOG("lobby msg \n%s ", it->second.prototype->Utf8DebugString().c_str());
    }
	return (this->*it->second.func)(peer_fd_info, it->second.prototype, uid, seq, ret, user);
}

void lobby_service_t::offline()
{
	this->do_disconnect();

	FOREACH(g_user_mgr->user_login_complete_map, it){
		user_t* user = it->second;
		if (user->lobby_mgr.lobby_service != this){
			continue;
		}
		user->lobby_mgr.out_lobby();
	}
}

//////////////////////////////////////////////////////////////////////////
//handle msg about lobby
int gateway_t::on_lobby_enter_msg(el::lib_tcp_peer_info_t* peer_fd_info, 
								  google::protobuf::Message* msg, user_t* user)
{
	if (user->lobby_mgr.is_in_lobby()){
		return share_msg::EC_USER_IN_LOBBY;
	}

	auto in = (gateway_msg::lobby_enter_msg*)msg;
	
	{
		if (4 != in->pet_id_size()){
			return 0;
		}
		FOREACH_PB(in->pet_id, idx){
			const uint32_t petid = in->pet_id(idx);
			if (NULL == user->pet_mgr.find(petid)){
				return 0;
			}
		}
	}

	user->lobby_mgr.enter_lobby(g_lobby_service);
	{
		//  给lobby发送消息. [2019/1/21 Administrator]
		lobby_gateway_msg::lobby_enter_msg out;
		out.mutable_user_show()->CopyFrom(user->user_show);
		{
			FOREACH_PB(in->pet_id, idx){
				const uint32_t petid = in->pet_id(idx);
				share_msg::pet_t* pet = user->pet_mgr.find(petid);

				share_msg::pet_btl_t* pet_btl = out.add_pet_btl();
				pet_btl->set_id(petid);
				uint32_t pet_lv = g_pet_cfg_mgr->gen_level(petid, pet->exp());
				pet_btl->set_lv(pet_lv);
				{
					pet_cfg_t* pc = g_pet_cfg_mgr->find(petid);
					auto it_pet_cfg = pc->pet_lv_data_map.find(pet_lv);
					if (pc->pet_lv_data_map.end() == it_pet_cfg){
						pet_btl->set_ack(1);
						pet_btl->set_hp(1);
						pet_btl->set_congming(1);
					} else {
						pet_lv_data_t& r = it_pet_cfg->second;
						pet_btl->set_ack(r.ack);
						pet_btl->set_hp(r.def);
						pet_btl->set_congming(r.ai);


						uint32_t exp_max = g_pet_cfg_mgr->find_exp_max(pet->id());
						if (exp_max == pet->exp()){
							pet_btl->set_congming(100);
						}
					}
				}

				std::string str_data = user->event_mgr.find_str_data(common_msg::FOREVER_EVENT_PET_SKILL, petid);
				{
					std::vector<uint32_t> data_vec;
					el::g_cat_string(data_vec, str_data);
					FOREACH(data_vec, it){
						pet_btl->set_skill_id(*it);
						break;
					}
				}
			}
		}
		user->lobby_mgr.lobby_service->send_msg(&out, user->uid(), lobby_gateway_lobby_enter_msg_cmd);
	}

	{
		gateway_msg::lobby_enter_msg_res out;
		user->send_msg_res(&out);
	}
	return 0;
}

int lobby_service_t::on_lobby_enter_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){
	return 0;
}

int gateway_t::on_lobby_out_msg(el::lib_tcp_peer_info_t* peer_fd_info, 
								google::protobuf::Message* msg, user_t* user)
{
	
	if (!user->lobby_mgr.is_in_lobby()){
		return share_msg::EC_USER_OUT_LOBBY;
	}

	user->lobby_mgr.out_lobby(true);

	{
		gateway_msg::lobby_out_msg_res out;
		user->send_msg_res(&out);
	}
	return 0;
}

int lobby_service_t::on_lobby_out_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)
{
	return 0;
}

int gateway_t::on_get_btl_team_msg(el::lib_tcp_peer_info_t* peer_fd_info, 
								  google::protobuf::Message* msg, user_t* user)
{
	gateway_msg::get_btl_team_msg_res out;
	{
		EVENT_DATA ed = user->event_mgr.find_data(common_msg::FOREVER_EVENT_BTL_TEAM_ID);
		out.set_team_id(ed);

		std::map<EVENT_ID, event_t>& events = user->event_mgr.find_events(common_msg::FOREVER_EVENT_BTL_TEAM);
		FOREACH(events, it){
			event_t& event = it->second;
			share_msg::pet_team_t* pt = out.add_pet_team();
			pt->set_id(event.id);
				
			std::vector<uint32_t> data_vec;
			el::g_cat_string(data_vec, event.str_data);
			FOREACH(data_vec, it){
				pt->add_pet_id(*it);
			}
		}
	}
	user->send_msg_res(&out);
	return 0;
}

int gateway_t::on_set_btl_team_msg(el::lib_tcp_peer_info_t* peer_fd_info, 
								   google::protobuf::Message* msg, user_t* user)
{
	auto in = (gateway_msg::set_btl_team_msg*)msg;

	uint32_t max_cnt = 3;
	if (user->is_vip_active()){
		max_cnt = g_game_cfg_mgr->vip_pet_btl_team_cnt;
	}
	share_msg::pet_team_t* pt = in->mutable_pet_team();
	if (pt->id() <= 0 || max_cnt < pt->id()){
		return 0;
	}

	std::string str_data;
	{
		if (4 != pt->pet_id_size()){
			return 0;
		}
		
		FOREACH_PB(pt->pet_id, idx){
			const uint32_t petid = pt->pet_id(idx);
			if (NULL == user->pet_mgr.find(petid)){
				return 0;
			}
			str_data += el::convert_to_string(petid);
			str_data += ",";
		}
		if (!str_data.empty()){
			str_data.pop_back();
		}
	}
	{
		user->event_mgr.update_event(user->uid(), common_msg::FOREVER_EVENT_BTL_TEAM, pt->id(), 0, 0, str_data);
	}

	{
		gateway_msg::set_btl_team_msg_res out;
		user->send_msg_res(&out);
	}
	return 0;
}

int gateway_t::on_set_btl_team_id_msg(el::lib_tcp_peer_info_t* peer_fd_info, 
								   google::protobuf::Message* msg, user_t* user)
{
	auto in = (gateway_msg::set_btl_team_id_msg*)msg;

	uint32_t max_cnt = 3;
	if (user->is_vip_active()){
		max_cnt = g_game_cfg_mgr->vip_pet_btl_team_cnt;
	}
	if (in->team_id() <= 0 || max_cnt < in->team_id()){
		return 0;
	}

	user->event_mgr.update_event(user->uid(), common_msg::FOREVER_EVENT_BTL_TEAM_ID, 0, in->team_id(), 0);
	{
		gateway_msg::set_btl_team_id_msg_res out;
		user->send_msg_res(&out);
	}
	return 0;
}

int lobby_service_t::on_notify_game_end_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)
{
	//todo
	//1.处理战斗结果	
	// 2.退出战斗服	
	//  [2019/1/30 Administrator]
	/*
	g_user_mgr->del_in_room_reconnect(uid);
	if (NULL == user){
		return 0;
	}
	
	auto in = (lobby_gateway_msg::notify_game_end_msg_res*)msg;
	
	{
		gateway_msg::notify_game_end_msg_res out;
		auto user_game_end = out.mutable_user_game_end();
		user_game_end->CopyFrom(in->user_game_end());
		auto xzdd = out.mutable_xzdd();
		xzdd->CopyFrom(in->xzdd());
		auto nn = out.mutable_nn();
		nn->CopyFrom(in->nn());
		auto ddz = out.mutable_ddz();
		ddz->CopyFrom(in->ddz());
		auto bj = out.mutable_bj();
		bj->CopyFrom(in->bj());
		user->send_msg(cli_notify_game_end_msg_cmd, &out, 0, user->uid());
	}

	uint32_t room_lv = in->room_lv();

	uint32_t game_id = in->game_id();
	FOREACH_PB(in->user_game_end, index){
		const share_msg::user_game_end_t& uge = in->user_game_end(index);
		if (user != g_user_mgr->find(uge.user_show().uid())){
			continue;
		}

		user->lobby_mgr.game_end();

		if (1 == uge.is_off_user()){
			continue;
		}

		if (share_msg::E_GAME_ID_GD == game_id
			|| share_msg::E_GAME_ID_DDZ == game_id
			|| share_msg::E_GAME_ID_NN == game_id
			|| share_msg::E_GAME_ID_XZDD == game_id
			|| share_msg::E_GAME_ID_XZDD_3 == game_id
			|| share_msg::E_GAME_ID_XZDD_3_2 == game_id
			|| share_msg::E_GAME_ID_XZDD_4 == game_id
			|| share_msg::E_GAME_ID_XLCH == game_id){
			g_shop_medal_mgr->gen_reward(user, room_lv);
		}

		if (share_msg::E_GAME_ID_DDZ == game_id){
			user->achievement_mgr.update(2, 1);
		}
		
		if (share_msg::E_GAME_ID_GD == game_id
			|| share_msg::E_GAME_ID_DDZ == game_id){
			g_btl_cnt_reward_cfg_mgr->add_btl_cnt(user, game_id, 1);
			user->task_mgr.listen_daily_task(game_id, share_msg::E_TASK_DAILY_TYPE_PLAY, 1);
			
			if (0 < uge.add_money()){
				user->task_mgr.listen_daily_task(game_id, share_msg::E_TASK_DAILY_TYPE_WIN_MONEY, uge.add_money());
				if (share_msg::E_GAME_ID_DDZ == game_id){
					user->task_mgr.listen_daily_task(game_id, share_msg::E_TASK_DAILY_TYPE_WIN, 1);
				}
			}
		}	

		if (0 < uge.change_score()){
			user->task_mgr.listen_daily_task(game_id, share_msg::E_TASK_DAILY_TYPE_GAME_LEVEL, in->mutable_room_data()->gd_level_point_win());
		}
	}
	*/
	return 0;
}