game2006/server/robotserver/android_agent.cc
aozhiwei d27dfdb922 1
2023-12-06 11:36:55 +08:00

204 lines
6.3 KiB
C++

#include "precompile.h"
#include <a8/mutable_xobject.h>
#include <a8/websocketclient.h>
#include <f8/btmgr.h>
#include <f8/udplog.h>
#include "android_agent.h"
#include "player.h"
#include "playermgr.h"
#include "httpproxy.h"
AndroidAgent::AndroidAgent()
{
}
AndroidAgent::~AndroidAgent()
{
}
bool AndroidAgent::IsLoginSuccess()
{
return owner_->IsLoginSucess();
}
behaviac::EBTStatus AndroidAgent::SearchEnemy(int range)
{
return behaviac::BT_SUCCESS;
}
bool AndroidAgent::NetIsConnected()
{
return owner_->GetWebSocket()->Connected() != 0;
}
behaviac::EBTStatus AndroidAgent::CoIdle(int min_val, int max_val)
{
PRE_ENTER_COROUTINE();
auto context = MAKE_BTCONTEXT
(
int time = 0;
);
context->time = a8::RandEx(min_val, max_val);
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoIdle");
co->runing_cb =
[this, context] (BtCoroutine* co)
{
return behaviac::BT_SUCCESS;
};
return StartCoroutine(co);
}
behaviac::EBTStatus AndroidAgent::CoLogin()
{
PRE_ENTER_COROUTINE();
auto context = MAKE_BTCONTEXT
(
std::string account_id;
std::optional<bool> login_ok;
);
context->account_id = owner_->GetAccountId();
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoLogin");
{
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", owner_->GetIdx());
HttpProxy::Instance()->HttpGet
(
[context] (bool ok, a8::XObject* rsp_obj, f8::HttpContext* ctx)
{
a8::XPrintf("data:%s\n", {rsp_obj->ToJsonStr()});
if (ok) {
std::string account_id = rsp_obj->At("account_id")->AsXValue().GetString();
std::string session_id = rsp_obj->At("session_id")->AsXValue().GetString();
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);
auto hum = PlayerMgr::Instance()->GetPlayerByAccountId(account_id);
if (hum) {
hum->SetSessionId(session_id);
}
HttpProxy::Instance()->HttpGet
(
[context] (bool ok, a8::XObject* rsp_obj, f8::HttpContext* ctx)
{
a8::XPrintf("data:%s\n", {rsp_obj->ToJsonStr()});
if (ok) {
context->login_ok = true;
} else {
context->login_ok = false;
}
},
"https://game2006api-test.kingsome.cn/webapp/index.php",
*url_params
);
} else {
context->login_ok = false;
}
},
"https://login-test.kingsome.cn/webapp/index.php",
*url_params
);
}
co->runing_cb =
[this, context] (BtCoroutine* co)
{
if (context->login_ok == std::nullopt) {
owner_->SetLoginSucess();
return behaviac::BT_RUNNING;
}
return behaviac::BT_SUCCESS;
};
return StartCoroutine(co);
}
behaviac::EBTStatus AndroidAgent::CoConnectBattleServer()
{
PRE_ENTER_COROUTINE();
auto context = MAKE_BTCONTEXT
(
std::function<void (a8::WebSocketClient*, int)> old_on_error;
std::function<void (a8::WebSocketClient*)> old_on_connect;
std::function<void (a8::WebSocketClient*)> old_on_disconnect;
bool pending = true;
bool connect_ok = false;
);
context->old_on_error = owner_->GetWebSocket()->on_error;
context->old_on_connect = owner_->GetWebSocket()->on_connect;
context->old_on_disconnect = owner_->GetWebSocket()->on_disconnect;
owner_->GetWebSocket()->on_error =
[this, context] (a8::WebSocketClient*, int err_code)
{
a8::XPrintf("WebSocketClient on_error:%d\n", {err_code});
context->pending = false;
context->connect_ok = false;
};
owner_->GetWebSocket()->on_connect =
[this, context] (a8::WebSocketClient* )
{
a8::XPrintf("WebSocketClient on_connect\n", {});
context->pending = false;
context->connect_ok = true;
};
owner_->GetWebSocket()->on_disconnect =
[this, context] (a8::WebSocketClient* )
{
a8::XPrintf("WebSocketClient on_disconnect\n", {});
context->pending = false;
context->connect_ok = false;
};
if (owner_->GetWebSocket()->IsActive()) {
owner_->GetWebSocket()->Close();
}
owner_->GetWebSocket()->Open();
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoConnectBattleServer");
co->runing_cb =
[this, context] (BtCoroutine* co)
{
if (context->pending) {
return behaviac::BT_RUNNING;
}
return context->connect_ok ? behaviac::BT_SUCCESS : behaviac::BT_FAILURE;
};
return StartCoroutine(co);
}
behaviac::EBTStatus AndroidAgent::CoJoin()
{
cs::CMJoin msg;
msg.set_server_id(6);
msg.set_team_uuid("");
msg.set_account_id(owner_->GetAccountId());
msg.set_team_mode(2);
msg.set_proto_version(cs::ProtoVersion);
msg.set_auto_fill(1);
msg.set_name("");
msg.set_avatar_url("");
msg.set_mapid(2007);
msg.set_hero_id(30800);
//weapons
msg.set_session_id(owner_->GetSessionId());
owner_->SendMsg(msg);
a8::XPrintf("CoJoin\n", {});
return behaviac::BT_SUCCESS;
}
behaviac::EBTStatus AndroidAgent::CoUpdateGame()
{
//a8::XPrintf("CoUpdateGame\n", {});
return behaviac::BT_SUCCESS;
}
bool AndroidAgent::IsGameOver()
{
return false;
}