118 lines
2.7 KiB
C++
118 lines
2.7 KiB
C++
#include "precompile.h"
|
|
|
|
#include <f8/udplog.h>
|
|
|
|
#include "playermgr.h"
|
|
#include "player.h"
|
|
|
|
#include <f8/utils.h>
|
|
#include <f8/msgqueue.h>
|
|
|
|
void PlayerMgr::Init()
|
|
{
|
|
f8::MsgQueue::Instance()->RegisterCallBack
|
|
(
|
|
IM_ClientSocketDisconnect,
|
|
[this] (const a8::Args& args)
|
|
{
|
|
int gg_socket = args.Get<int>(0);
|
|
OnClientDisconnect(gg_socket);
|
|
});
|
|
}
|
|
|
|
void PlayerMgr::UnInit()
|
|
{
|
|
}
|
|
|
|
int PlayerMgr::OnlineNum()
|
|
{
|
|
return socket_hash_.size();
|
|
}
|
|
|
|
std::shared_ptr<Player> PlayerMgr::GetPlayerBySocket(int socket)
|
|
{
|
|
auto itr = socket_hash_.find(socket);
|
|
return itr != socket_hash_.end() ? itr->second : nullptr;
|
|
}
|
|
|
|
void PlayerMgr::OnClientDisconnect(int gg_socket)
|
|
{
|
|
|
|
std::vector<int> socket_list;
|
|
for (auto& pair : socket_hash_) {
|
|
unsigned short parent_socket_handle = (pair.first >> 16) & 0xFFFF;
|
|
if (parent_socket_handle == gg_socket) {
|
|
socket_list.push_back(pair.first);
|
|
}
|
|
}
|
|
for (int socket_handle : socket_list) {
|
|
std::shared_ptr<Player> hum = GetPlayerBySocket(socket_handle);
|
|
if (hum) {
|
|
RemovePlayerBySocket(socket_handle);
|
|
}
|
|
}
|
|
}
|
|
|
|
void PlayerMgr::RemovePlayerBySocket(int socket_handle)
|
|
{
|
|
auto itr = socket_hash_.find(socket_handle);
|
|
if (itr != socket_hash_.end()) {
|
|
itr->second->socket_handle = 0;
|
|
socket_hash_.erase(itr);
|
|
}
|
|
}
|
|
|
|
void PlayerMgr::IncAccountNum(const std::string& account_id)
|
|
{
|
|
auto itr = account_num_hash_.find(account_id);
|
|
if (itr != account_num_hash_.end()) {
|
|
++(itr->second);
|
|
if (itr->second > 3) {
|
|
f8::UdpLog::Instance()->Warning
|
|
(
|
|
"IncAccountNum account_id:%s num:%d > 3",
|
|
{
|
|
account_id,
|
|
itr->second
|
|
}
|
|
);
|
|
}
|
|
} else {
|
|
account_num_hash_[account_id] = 1;
|
|
}
|
|
}
|
|
|
|
void PlayerMgr::DecAccountNum(const std::string& account_id)
|
|
{
|
|
auto itr = account_num_hash_.find(account_id);
|
|
if (itr != account_num_hash_.end()) {
|
|
--(itr->second);
|
|
if (itr->second < 0) {
|
|
f8::UdpLog::Instance()->Warning
|
|
(
|
|
"DecAccountNum account_id:%s num:%d < 0",
|
|
{
|
|
account_id,
|
|
itr->second
|
|
}
|
|
);
|
|
}
|
|
if (itr->second <= 0) {
|
|
account_num_hash_.erase(itr);
|
|
}
|
|
} else {
|
|
f8::UdpLog::Instance()->Warning
|
|
(
|
|
"DecAccountNum account_id:%s not exits",
|
|
{
|
|
account_id
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
void PlayerMgr::ReBindSocket(std::shared_ptr<Player> hum)
|
|
{
|
|
socket_hash_[hum->socket_handle] = hum;
|
|
}
|