From 47e6a1448053ec55bbdf38a4ed461d635d8b9113 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 21 May 2020 13:21:48 +0800 Subject: [PATCH] remove error_code --- server/gameserver/error_code.h | 13 ----- server/gameserver/player.cc | 8 +++ server/gameserver/player.h | 5 +- server/gameserver/precompile.h | 1 - server/gameserver/room.cc | 13 +++++ server/gameserver/roommgr.cc | 100 ++++++++++++++++----------------- server/gameserver/roommgr.h | 4 ++ 7 files changed, 78 insertions(+), 66 deletions(-) delete mode 100755 server/gameserver/error_code.h diff --git a/server/gameserver/error_code.h b/server/gameserver/error_code.h deleted file mode 100755 index 35ec63e..0000000 --- a/server/gameserver/error_code.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -/* - 类似http的错误码设计 - 0:成功 - <400: 客户端的错 - >500: 服务器的错 -*/ -enum ErrorCode_e -{ - ERRNO_OK = 0, - -}; diff --git a/server/gameserver/player.cc b/server/gameserver/player.cc index 3dcd7ed..0eac118 100644 --- a/server/gameserver/player.cc +++ b/server/gameserver/player.cc @@ -869,6 +869,14 @@ void Player::ProcPrepareItems(const ::google::protobuf::RepeatedField< ::google: } } +void Player::ProcPrepareItems2(const ::google::protobuf::RepeatedPtrField< cs::MFPair >& + prepare_items) +{ + for (auto& pair : prepare_items) { + AddItem(pair.key(), pair.value()); + } +} + void Player::_CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg) { moving = false; diff --git a/server/gameserver/player.h b/server/gameserver/player.h index a3a5c5b..ef82265 100644 --- a/server/gameserver/player.h +++ b/server/gameserver/player.h @@ -82,7 +82,10 @@ class Player : public Human void ObstacleInteraction(Obstacle* entity); void LootInteraction(Loot* entity); void HumanInteraction(Human* hum); - void ProcPrepareItems(const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& prepare_items); + void ProcPrepareItems(const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >& + prepare_items); + void ProcPrepareItems2(const ::google::protobuf::RepeatedPtrField< cs::MFPair >& + prepare_items); void _CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg); void _CMEmote(f8::MsgHdr& hdr, const cs::CMEmote& msg); diff --git a/server/gameserver/precompile.h b/server/gameserver/precompile.h index b991782..726d0f7 100644 --- a/server/gameserver/precompile.h +++ b/server/gameserver/precompile.h @@ -9,7 +9,6 @@ #include "constant.h" #include "types.h" -#include "error_code.h" #include "global.h" namespace google diff --git a/server/gameserver/room.cc b/server/gameserver/room.cc index e960839..97113e5 100644 --- a/server/gameserver/room.cc +++ b/server/gameserver/room.cc @@ -165,6 +165,19 @@ void Room::AddPlayer(Player* hum) hum->room = this; hum->join_frameno = frame_no; hum->Initialize(); + { + cs::SMJoinedNotify notifymsg; + notifymsg.set_error_code(0); + FillSMJoinedNotify(hum, notifymsg); + GGListener::Instance()->SendToClient(hum->socket_handle, 0, notifymsg); + } + { + cs::SMMapInfo notifymsg; + notifymsg.set_map_id(map_meta->i->map_id()); + notifymsg.set_map_width(map_meta->i->map_width()); + notifymsg.set_map_height(map_meta->i->map_height()); + GGListener::Instance()->SendToClient(hum->socket_handle, 0, notifymsg); + } uniid_hash_[hum->entity_uniid] = hum; moveable_hash_[hum->entity_uniid] = hum; accountid_hash_[hum->account_id] = hum; diff --git a/server/gameserver/roommgr.cc b/server/gameserver/roommgr.cc index 37cc459..dd92572 100644 --- a/server/gameserver/roommgr.cc +++ b/server/gameserver/roommgr.cc @@ -53,6 +53,10 @@ void RoomMgr::Init() if (!map_meta_) { abort(); } + hum_meta_ = MetaMgr::Instance()->GetPlayer(40001); + if (!hum_meta_) { + abort(); + } map_service_ = new MapService(); grid_service_ = new GridService(); grid_service_->Init(map_meta_->i->map_width(), @@ -99,10 +103,7 @@ void RoomMgr::Update(int delta_time) void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg) { - MetaData::Player* hum_meta = MetaMgr::Instance()->GetPlayer(40001); - if (!hum_meta) { - abort(); - } + RoomType_e self_room_type = GetHumanRoomType(msg); if (IsLimitJoin()) { { cs::SMJoinedNotify notifymsg; @@ -110,13 +111,14 @@ void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg) GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg); } { - a8::Timer::Instance()->AddDeadLineTimer(1000 * 2, - a8::XParams() - .SetSender(hdr.socket_handle), - [] (const a8::XParams& param) - { - GGListener::Instance()->ForceCloseChildSocket(param.sender); - }); + a8::Timer::Instance()-> + AddDeadLineTimer(1000 * 2, + a8::XParams() + .SetSender(hdr.socket_handle), + [] (const a8::XParams& param) + { + GGListener::Instance()->ForceCloseChildSocket(param.sender); + }); } a8::UdpLog::Instance()->Warning("room is full! accountid:%s max_mainloop_rundelay:%d " "room_num:%d player_num:%d online_num:%d", @@ -129,49 +131,16 @@ void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg) }); return; } - RoomType_e self_room_type = GetHumanRoomType(msg); Room* room = GetJoinableRoom(msg, self_room_type); if (!room) { - room = new Room(); - room->room_type = self_room_type; - room->room_uuid = App::Instance()->NewUuid(); - room->room_idx = AllocRoomIdx(); - room->grid_service = grid_service_; - room->map_service = map_service_; - room->spawn_points = &spawn_points_; - room->loots = &loots_; - room->buildings = &buildings_; - if (GetRoomByUuid(room->room_uuid)) { - abort(); - } - room->map_meta = map_meta_; - room->Init(); - inactive_room_hash_[room->room_uuid] = room; - room_hash_[room->room_uuid] = room; } - Player* hum = PlayerMgr::Instance()->CreatePlayerByCMJoin(hdr.ip_saddr, hdr.socket_handle, msg); - hum->meta = hum_meta; - room->AddPlayer(hum); + Player* hum = PlayerMgr::Instance()->CreatePlayerByCMJoin(hdr.ip_saddr, + hdr.socket_handle, + msg); + hum->meta = hum_meta_; hum->ProcPrepareItems(msg.prepare_items()); - { - for (auto& pair : msg.prepare_items2()) { - hum->AddItem(pair.key(), pair.value()); - } - } - - { - cs::SMJoinedNotify notifymsg; - notifymsg.set_error_code(0); - room->FillSMJoinedNotify(hum, notifymsg); - GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg); - } - { - cs::SMMapInfo notifymsg; - notifymsg.set_map_id(map_meta_->i->map_id()); - notifymsg.set_map_width(map_meta_->i->map_width()); - notifymsg.set_map_height(map_meta_->i->map_height()); - GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg); - } + hum->ProcPrepareItems2(msg.prepare_items2()); + room->AddPlayer(hum); } int RoomMgr::RoomNum() @@ -221,7 +190,7 @@ Room* RoomMgr::GetJoinableRoom(const cs::CMJoin& msg, const RoomType_e self_room } } } - return nullptr; + return CreateRoom(self_room_type); } Room* RoomMgr::GetRoomByUuid(long long room_uuid) @@ -525,3 +494,32 @@ int RoomMgr::AllocRoomIdx() } while (GetRoomByIdx(++current_room_idx_)); return current_room_idx_; } + +Room* RoomMgr::CreateRoom(RoomType_e room_type) +{ + int room_idx = AllocRoomIdx(); + if (room_idx == -1) { + return nullptr; + } + Room* room = new Room(); + room->room_type = room_type; + room->room_uuid = App::Instance()->NewUuid(); + room->room_idx = AllocRoomIdx(); + room->grid_service = grid_service_; + room->map_service = map_service_; + room->spawn_points = &spawn_points_; + room->loots = &loots_; + room->buildings = &buildings_; + if (GetRoomByUuid(room->room_uuid)) { + abort(); + } + if (GetRoomByIdx(room->room_idx)) { + abort(); + } + room->map_meta = map_meta_; + room->Init(); + inactive_room_hash_[room->room_uuid] = room; + room_hash_[room->room_uuid] = room; + room_idx_hash_[room->room_idx] = room; + return room; +} diff --git a/server/gameserver/roommgr.h b/server/gameserver/roommgr.h index bce83a5..9cc0427 100644 --- a/server/gameserver/roommgr.h +++ b/server/gameserver/roommgr.h @@ -11,6 +11,7 @@ namespace MetaData { struct Map; struct MapTplThing; + struct Player; } namespace metatable @@ -61,6 +62,7 @@ class RoomMgr : public a8::Singleton Entity* GetEntityByUniId(int uniid); int AllocUniid(); int AllocRoomIdx(); + Room* CreateRoom(RoomType_e room_type); private: std::map inactive_room_hash_; @@ -78,4 +80,6 @@ class RoomMgr : public a8::Singleton std::vector spawn_points_; std::vector loots_; std::vector buildings_; + + MetaData::Player* hum_meta_; };