game2006/server/robotserver/playermgr.cc
aozhiwei e23fe79136 1
2023-11-24 21:57:36 +08:00

133 lines
3.6 KiB
C++

#include "precompile.h"
#include <a8/websocketclient.h>
#include <a8/mutable_xobject.h>
#include <f8/udplog.h>
#include <f8/utils.h>
#include <f8/msgqueue.h>
#include <f8/comgr.h>
#include <f8/coroutine.h>
#include "playermgr.h"
#include "player.h"
#include "httpproxy.h"
#include "iomgr.h"
#include "app.h"
void PlayerMgr::Init()
{
#if 0
for (int i = 1; i <= App::Instance()->robot_num; ++i) {
f8::CoMgr::Instance()->CreateCo
(
[this, i] (f8::Coroutine* co)
{
CoWebLogin(i, co);
});
}
#endif
}
void PlayerMgr::UnInit()
{
}
void PlayerMgr::Update()
{
for (auto& pair : account_id_hash_) {
pair.second->Update();
}
}
#if 0
void PlayerMgr::CoWebLogin(int idx, f8::Coroutine* co)
{
std::string account_id;
std::string session_id;
{
bool ret = false;
auto url_params = a8::MutableXObject::CreateObject();
url_params->SetVal("c", "Login");
url_params->SetVal("a", "auth");
url_params->SetVal("gameid", 2006);
url_params->SetVal("channel", 6513);
url_params->SetVal("openid", idx);
std::shared_ptr<a8::XObject> rsp;
co->CoAwait
(HttpProxy::Instance()->CoHttpGet
(
"https://login-test.kingsome.cn/webapp/index.php",
*url_params,
&ret,
&rsp
)
);
if (ret) {
auto rsp_obj = a8::XObject();
rsp_obj.ReadFromJsonString(rsp->ToJsonStr());
account_id = rsp_obj.At("account_id")->AsXValue().GetString();
session_id = rsp_obj.At("session_id")->AsXValue().GetString();
a8::XPrintf("rsp:%s\n", {rsp->ToJsonStr()});
} else {
abort();
}
}
{
bool ret = false;
auto url_params = a8::MutableXObject::CreateObject();
url_params->SetVal("c", "User");
url_params->SetVal("a", "login");
url_params->SetVal("account_id", account_id);
url_params->SetVal("session_id", session_id);
std::shared_ptr<a8::XObject> rsp;
co->CoAwait
(HttpProxy::Instance()->CoHttpGet
(
"https://game2006api-test.kingsome.cn/webapp/index.php",
*url_params,
&ret,
&rsp
)
);
if (ret) {
auto rsp_obj = a8::XObject();
rsp_obj.ReadFromJsonString(rsp->ToJsonStr());
a8::XPrintf("rsp:%s\n", {rsp->ToJsonStr()});
} else {
abort();
}
}
CoCreatePlayer(co, idx, account_id, session_id);
}
void PlayerMgr::CoCreatePlayer(f8::Coroutine* co,
int idx,
const std::string& account_id,
const std::string& session_id)
{
auto socket = std::make_shared<a8::WebSocketClient>
(
IoMgr::Instance()->GetIoContext(0),
"192.168.100.21",
7601
);
auto hum = std::make_shared<Player>();
hum->Init(idx, account_id, session_id, socket);
account_id_hash_[hum->GetAccountId()] = hum;
socket_id_hash_[hum->GetSocketId()] = hum;
}
#endif
std::shared_ptr<Player> PlayerMgr::GetPlayerByAccountId(const std::string& account_id)
{
auto itr = account_id_hash_.find(account_id);
return itr != account_id_hash_.end() ? itr->second : nullptr;
}
std::shared_ptr<Player> PlayerMgr::GetPlayerBySocketHandle(int socket_handle)
{
auto itr = socket_id_hash_.find(socket_handle);
return itr != socket_id_hash_.end() ? itr->second : nullptr;
}