170 lines
4.1 KiB
C++
170 lines
4.1 KiB
C++
#include "precompile.h"
|
|
|
|
#include <a8/timer.h>
|
|
|
|
#include "cs_proto.pb.h"
|
|
#include "matchmgr.h"
|
|
#include "GGListener.h"
|
|
#include "matchteam.h"
|
|
|
|
void MatchMgr::Init()
|
|
{
|
|
a8::Timer::Instance()->AddRepeatTimerAndAttach
|
|
(1000,
|
|
a8::XParams()
|
|
.SetSender(this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
MatchMgr::Instance()->Output();
|
|
},
|
|
&timer_attacher.timer_list_,
|
|
[] (const a8::XParams& param)
|
|
{
|
|
}
|
|
);
|
|
}
|
|
|
|
void MatchMgr::UnInit()
|
|
{
|
|
|
|
}
|
|
|
|
void MatchMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg)
|
|
{
|
|
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->IsRawMember(msg.account_id())) {
|
|
return;
|
|
}
|
|
team->AddRawMember(hdr, msg);
|
|
}
|
|
socket_hash_[hdr.socket_handle] = std::make_tuple(msg.account_id(), team);
|
|
}
|
|
}
|
|
|
|
void MatchMgr::_CMMatchCancel(f8::MsgHdr& hdr, const cs::CMMatchCancel& msg)
|
|
{
|
|
auto match_info = GetMatchInfo(hdr.socket_handle);
|
|
if (match_info) {
|
|
std::get<1>(*match_info)->_CMMatchCancel(hdr, msg);
|
|
}
|
|
}
|
|
|
|
void MatchMgr::_CMMatchChoose(f8::MsgHdr& hdr, const cs::CMMatchChoose& msg)
|
|
{
|
|
auto match_info = GetMatchInfo(hdr.socket_handle);
|
|
if (match_info) {
|
|
std::get<1>(*match_info)->_CMMatchChoose(hdr, msg);
|
|
}
|
|
}
|
|
|
|
void MatchMgr::_CMMatchStartGame(f8::MsgHdr& hdr, const cs::CMMatchStartGame& msg)
|
|
{
|
|
auto match_info = GetMatchInfo(hdr.socket_handle);
|
|
if (match_info) {
|
|
std::get<1>(*match_info)->_CMMatchStartGame(hdr, msg);
|
|
}
|
|
}
|
|
|
|
void MatchMgr::_CMMatchCancelStartGame(f8::MsgHdr& hdr, const cs::CMMatchCancelStartGame& msg)
|
|
{
|
|
auto match_info = GetMatchInfo(hdr.socket_handle);
|
|
if (match_info) {
|
|
std::get<1>(*match_info)->_CMMatchCancelStartGame(hdr, msg);
|
|
}
|
|
}
|
|
|
|
bool MatchMgr::NeedMatch(const cs::CMJoin& msg)
|
|
{
|
|
bool need = !msg.team_uuid().empty() &&
|
|
msg.show_team_ui() &&
|
|
msg.team_mode() == 1 &&
|
|
msg.auto_fill() &&
|
|
msg.team_members().size() > 0 &&
|
|
msg.team_members().size() < MAX_TEAM_NUM;
|
|
if (need) {
|
|
MatchTeam* team = GetTeam(msg.team_uuid());
|
|
if (team && !team->IsValidMember(msg)) {
|
|
need = false;
|
|
}
|
|
}
|
|
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
|
|
}
|