126 lines
3.8 KiB
C++
126 lines
3.8 KiB
C++
#include "precompile.h"
|
|
|
|
#include "roommgr.h"
|
|
#include "room.h"
|
|
#include "cs_proto.pb.h"
|
|
#include "GGListener.h"
|
|
#include "player.h"
|
|
#include "playermgr.h"
|
|
#include "app.h"
|
|
#include "metamgr.h"
|
|
|
|
void RoomMgr::Init()
|
|
{
|
|
}
|
|
|
|
void RoomMgr::UnInit()
|
|
{
|
|
}
|
|
|
|
void RoomMgr::Update(int delta_time)
|
|
{
|
|
for (auto& pair : room_hash_) {
|
|
pair.second->Update(delta_time);
|
|
}
|
|
}
|
|
|
|
void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg)
|
|
{
|
|
MetaData::Player* hum_meta = MetaMgr::Instance()->GetPlayer(40001);
|
|
assert(hum_meta);
|
|
if (!hum_meta) {
|
|
abort();
|
|
}
|
|
if (App::Instance()->is_test_mode) {
|
|
for (int i = 0; i < App::Instance()->test_param; ++i) {
|
|
Room* room = GetJoinableRoom(msg.account_id());
|
|
if (!room) {
|
|
room = new Room();
|
|
room->room_uuid = App::Instance()->NewUuid();
|
|
assert(!GetRoomByUuid(room->room_uuid));
|
|
if (GetRoomByUuid(room->room_uuid)) {
|
|
abort();
|
|
}
|
|
room->map_meta = MetaMgr::Instance()->GetMap(1001);
|
|
room->Init();
|
|
inactive_room_hash_[room->room_uuid] = room;
|
|
room_hash_[room->room_uuid] = room;
|
|
}
|
|
Player* hum = PlayerMgr::Instance()->CreatePlayerByCMJoin(hdr.socket_handle + i * 10, msg);
|
|
hum->meta = hum_meta;
|
|
room->AddPlayer(hum);
|
|
|
|
if (i == 0) {
|
|
{
|
|
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(room->map_meta->i->map_id());
|
|
room->FillSMMapInfo(notifymsg);
|
|
GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
Room* room = GetJoinableRoom(msg.account_id());
|
|
if (!room) {
|
|
room = new Room();
|
|
room->room_uuid = App::Instance()->NewUuid();
|
|
assert(!GetRoomByUuid(room->room_uuid));
|
|
if (GetRoomByUuid(room->room_uuid)) {
|
|
abort();
|
|
}
|
|
room->map_meta = MetaMgr::Instance()->GetMap(1001);
|
|
room->Init();
|
|
inactive_room_hash_[room->room_uuid] = room;
|
|
room_hash_[room->room_uuid] = room;
|
|
}
|
|
Player* hum = PlayerMgr::Instance()->CreatePlayerByCMJoin(hdr.socket_handle, msg);
|
|
hum->meta = hum_meta;
|
|
room->AddPlayer(hum);
|
|
|
|
{
|
|
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(room->map_meta->i->map_id());
|
|
room->FillSMMapInfo(notifymsg);
|
|
GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg);
|
|
}
|
|
}
|
|
}
|
|
|
|
int RoomMgr::RoomNum()
|
|
{
|
|
return room_hash_.size();
|
|
}
|
|
|
|
Room* RoomMgr::GetJoinableRoom(const std::string& account_id)
|
|
{
|
|
for (auto& pair : inactive_room_hash_) {
|
|
if (!pair.second->IsFull() && !pair.second->GetPlayerByAccountId(account_id)) {
|
|
return pair.second;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
Room* RoomMgr::GetRoomByUuid(long long uuid)
|
|
{
|
|
auto itr = room_hash_.find(uuid);
|
|
return itr != room_hash_.end() ? itr->second : nullptr;
|
|
}
|
|
|
|
void RoomMgr::RemoveFromInactiveRoomHash(long long room_uuid)
|
|
{
|
|
inactive_room_hash_.erase(room_uuid);
|
|
}
|