game2006/server/gameserver/matchmgr.cc
aozhiwei c144c7a803 1
2022-12-17 15:25:05 +08:00

167 lines
4.1 KiB
C++

#include "precompile.h"
#include <f8/timer.h>
#include "cs_proto.pb.h"
#include "matchmgr.h"
#include "GGListener.h"
#include "matchteam.h"
#include "f8/utils.h"
void MatchMgr::Init()
{
f8::Timer::Instance()->SetIntervalEx
(1000,
[] (int event, const a8::Args* args)
{
if (a8::TIMER_EXEC_EVENT == event) {
MatchMgr::Instance()->Output();
}
},
&timer_attacher
);
}
void MatchMgr::UnInit()
{
}
void MatchMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg)
{
#ifdef DEBUG
{
std::string data = f8::PbToJson(&msg);
a8::XPrintf("CMJoin socket_handle:%d members_size:%d %s\n", {hdr.socket_handle, msg.team_members().size(), data});
}
#endif
if (!NeedMatch(msg)) {
return;
}
{
cs::SMShowTeamUI notifymsg;
GGListener::Instance()->SendToClient(hdr.socket_handle, 0, notifymsg);
}
{
cs::SMJoinedNotify notifymsg;
notifymsg.set_error_code(0);
GGListener::Instance()->SendToClient(hdr.socket_handle, 0, notifymsg);
}
{
MatchTeam* team = GetTeam(msg.team_uuid());
if (!team) {
team = new MatchTeam();
team->Init(hdr, msg);
team_hash_[msg.team_uuid()] = team;
team->TryCombineTeam();
#ifdef DEBUG
a8::XPrintf("newteam %s\n", {msg.team_uuid()});
#endif
} else {
if (!team->IsValidMember(msg)) {
return;
}
if (team->IsRawMember(msg.account_id())) {
return;
}
team->AddRawMember(hdr, msg);
}
socket_hash_[hdr.socket_handle] = std::make_tuple(msg.account_id(), team);
}
}
bool MatchMgr::NeedMatch(const cs::CMJoin& msg)
{
bool need = !msg.team_uuid().empty() &&
msg.team_mode() == 1 &&
msg.auto_fill() &&
msg.team_slot_num() > 1 &&
msg.team_slot_num() != 3 &&
msg.team_slot_num() <= MAX_TEAM_NUM &&
msg.team_members().size() <= msg.team_slot_num();
if (need) {
bool found = false;
for (int i = 0; i < msg.team_members().size(); ++i) {
if (msg.account_id() == msg.team_members(i).account_id()) {
found = true;
break;
}
}
if (found) {
MatchTeam* team = GetTeam(msg.team_uuid());
if (team && !team->IsValidMember(msg)) {
need = false;
}
} else {
need = false;
}
#ifdef DEBUG1
a8::XPrintf("needMatch found:%d\n", {found});
#endif
}
#ifdef DEBUG1
{
std::string data = f8::PbToJson(&msg);
a8::XPrintf("CMJoin need:%d members_size:%d %s\n", {need, msg.team_members().size(), data});
}
#endif
return need;
}
MatchTeam* MatchMgr::GetTeam(const std::string& team_uuid)
{
auto itr = team_hash_.find(team_uuid);
return itr != team_hash_.end() ? itr->second : nullptr;
}
void MatchMgr::TraverseTeam(std::function<void (MatchTeam*, bool&)> func)
{
if (!func) {
return;
}
bool stop = false;
for (auto& pair : team_hash_) {
if (pair.second) {
func(pair.second, stop);
if (stop){
return;
}
}
}
}
std::tuple<std::string, MatchTeam*>* MatchMgr::GetMatchInfo(int socket_handle)
{
auto itr = socket_hash_.find(socket_handle);
return itr != socket_hash_.end() ? &itr->second : nullptr;
}
void MatchMgr::RemoveTeam(const std::string& team_uuid)
{
auto itr = team_hash_.find(team_uuid);
if (itr != team_hash_.end()) {
delete itr->second;
team_hash_.erase(itr);
}
}
void MatchMgr::RemoveSocket(int socket_handle)
{
auto itr = socket_hash_.find(socket_handle);
if (itr != socket_hash_.end()) {
socket_hash_.erase(itr);
}
}
void MatchMgr::Output()
{
#ifdef DEBUG1
a8::XPrintf(">>>>>>>>>>>>>>>>>>>>>>>>>>\n", {});
for (auto& pair : team_hash_) {
a8::XPrintf("team:%s \n", {pair.first});
}
a8::XPrintf("<<<<<<<<<<<<<<<<<<<<<<<<<<\n", {});
#endif
}