game2006/server/robotserver/android_agent.cc
aozhiwei 827e95aa8d 1
2024-08-15 16:46:21 +08:00

276 lines
8.9 KiB
C++

#include "precompile.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#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"
#include "glmhelper.h"
#include "perf.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 true;
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)
{
#if 0
a8::XPrintf("data:%s\n", {rsp_obj->ToJsonStr()});
#endif
if (ok) {
++Perf::Instance()->login_ok_times;
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)
{
#if 0
a8::XPrintf("data:%s\n", {rsp_obj->ToJsonStr()});
#endif
if (ok) {
++Perf::Instance()->enter_ok_times;
context->login_ok = true;
} else {
context->login_ok = false;
++Perf::Instance()->enter_fail_times;
}
},
"https://game2006api-test.kingsome.cn/webapp/index.php",
url_params
);
} else {
context->login_ok = false;
++Perf::Instance()->login_fail_times;
}
},
"https://login-test.kingsome.cn/webapp/index.php",
url_params
);
}
co->runing_cb =
[this, context] (BtCoroutine* co)
{
if (context->login_ok == std::nullopt) {
return behaviac::BT_RUNNING;
}
owner_->SetLoginSucess();
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*, int)> old_on_disconnect;
bool pending = true;
bool connect_ok = false;
int socket_id = 0;
);
context->old_on_error = owner_->GetWebSocket()->on_error;
context->old_on_connect = owner_->GetWebSocket()->on_connect;
context->old_on_disconnect = owner_->GetWebSocket()->on_disconnect;
context->socket_id = owner_->GetSocketId();
owner_->GetWebSocket()->on_error =
[this, context] (a8::WebSocketClient*, int err_code)
{
a8::XPrintf("id:%d WebSocketClient on_error:%d\n", {context->socket_id, err_code});
context->pending = false;
context->connect_ok = false;
};
owner_->GetWebSocket()->on_connect =
[this, context] (a8::WebSocketClient* )
{
//a8::XPrintf("id:%d WebSocketClient on_connect\n", {context->socket_id});
context->pending = false;
context->connect_ok = true;
};
owner_->GetWebSocket()->on_disconnect =
[this, context] (a8::WebSocketClient*, int err_code)
{
a8::XPrintf("id:%d WebSocketClient on_disconnect:%d\n", {context->socket_id, err_code});
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;
}
if (context->connect_ok) {
++Perf::Instance()->connect_ok_times;
} else {
++Perf::Instance()->connect_fail_times;
}
return context->connect_ok ? behaviac::BT_SUCCESS : behaviac::BT_FAILURE;
};
return StartCoroutine(co);
}
behaviac::EBTStatus AndroidAgent::CoJoin()
{
PRE_ENTER_COROUTINE();
cs::CMJoin msg;
msg.set_server_id(6);
msg.set_team_uuid("");
msg.set_account_id(owner_->GetAccountId());
msg.set_proto_version(cs::ProtoVersion);
//weapons
msg.set_session_id(owner_->GetSessionId());
owner_->SendMsg(msg);
//a8::XPrintf("CoJoin %d\n", {owner_->GetSocketId()});
auto context = MAKE_BTCONTEXT
(
);
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoJoin");
co->runing_cb =
[this, context] (BtCoroutine* co)
{
if (!owner_->IsJoinOk()) {
return behaviac::BT_RUNNING;
}
++Perf::Instance()->join_ok_times;
return behaviac::BT_SUCCESS;
};
return StartCoroutine(co);
}
static void ToPb(const glm::vec3& v, cs::MFVec3* pb_obj)
{
pb_obj->set_x(v.x);
pb_obj->set_y(v.y);
pb_obj->set_z(v.z);
}
behaviac::EBTStatus AndroidAgent::CoUpdateGame()
{
#if 1
++Perf::Instance()->update_times;
if (owner_->GetWebSocket()->Connected()) {
std::shared_ptr<glm::vec3> sand_table_target_pos;
if (a8::XGetTickCount() - last_dir_tick_ > 3000) {
curr_dir_ = glm::vec3(0.0f, 0.0f, 1.0f);
GlmHelper::RotateY(curr_dir_, glm::radians((float(rand() % 360))));
last_dir_tick_ = a8::XGetTickCount();
++Perf::Instance()->chg_dir_times;
sand_table_target_pos = std::make_shared<glm::vec3>(0.0f, 0.0f, 0.0f);
sand_table_target_pos->x = a8::RandEx(100, 2000);
sand_table_target_pos->z = a8::RandEx(100, 2000);
}
if (a8::XGetTickCount() - last_send_tick_ > 80) {
last_send_tick_ = a8::XGetTickCount();
++Perf::Instance()->send_move_times;
{
cs::CMMove msg;
ToPb(curr_dir_, msg.mutable_move_dir());
ToPb(curr_dir_, msg.mutable_attack_dir());
msg.set_emote(200001);
msg.set_shot_start(1);
msg.set_shot_hold(1);
if (sand_table_target_pos) {
ToPb(*sand_table_target_pos.get(), msg.mutable_sand_table_target_pos());
}
owner_->SendMsg(msg);
}
{
cs::CMPing msg;
owner_->SendMsg(msg);
}
}
} else {
//a8::XPrintf("disconnect\n", {});
}
#endif
return behaviac::BT_SUCCESS;
}
bool AndroidAgent::IsGameOver()
{
return false;
}