This commit is contained in:
aozhiwei 2022-12-18 13:30:58 +08:00
parent 4de95e5225
commit 79d4e61bcd
5 changed files with 120 additions and 165 deletions

View File

@ -61,15 +61,15 @@ void HttpProxy::UnInit()
}
std::string HttpProxy::HttpGet(a8::XParams param,
f8::AsyncHttpOnOkFunc on_ok,
f8::AsyncHttpOnErrorFunc on_error,
std::string HttpProxy::HttpGet(
f8::HttpProxyCb cb,
const char* url,
a8::XObject url_params
)
{
std::shared_ptr<HttpProxyRequest> request = std::make_shared<HttpProxyRequest>();
request->req_id = CreateRequestId();
#if 0
request->param = param;
request->on_ok = on_ok;
request->on_error = on_error;
@ -81,6 +81,7 @@ std::string HttpProxy::HttpGet(a8::XParams param,
}
request_hash_[request->req_id] = request;
auto proxy_url_params = a8::MutableXObject::CreateObject();
proxy_url_params->SetVal("seq_id", request->req_id);
proxy_url_params->SetVal("target_url", std::string(url));
@ -130,6 +131,7 @@ std::string HttpProxy::HttpGet(a8::XParams param,
*proxy_url_params,
rand() % MAX_SYS_HTTP_NUM
);
#endif
return request->req_id;
}

View File

@ -5,7 +5,10 @@
#include "f8/httpclientpool.h"
struct HttpProxyRequest;
typedef std::function<void(a8::XObject&)> HttpProxyCb;
namespace f8 {
struct HttpContext;
typedef std::function<void(bool, a8::XObject*, f8::HttpContext*)> HttpProxyCb;
}
class HttpProxy : public a8::Singleton<HttpProxy>
{
@ -18,9 +21,8 @@ class HttpProxy : public a8::Singleton<HttpProxy>
void Init();
void UnInit();
std::string HttpGet(a8::XParams param,
f8::AsyncHttpOnOkFunc on_ok,
f8::AsyncHttpOnErrorFunc on_error,
std::string HttpGet(
f8::HttpProxyCb cb,
const char* url,
a8::XObject url_params
);

View File

@ -2557,36 +2557,6 @@ void Human::SendBattleSettlement()
return;
}
Player* p = (Player*)this;
auto on_ok =
[] (a8::XParams& param, a8::XObject& data)
{
Room* room = RoomMgr::Instance()->GetRoomByUuid(param.sender);
if (!room) {
return;
}
Player* hum = room->GetPlayerByAccountId(param.param1.GetString());
if (!hum) {
return;
}
};
auto on_error =
[] (a8::XParams& param, const std::string& response)
{
f8::UdpLog::Instance()->Error("reportSettlement error params: %s response: %s",
{
param.param2,
response
});
Room* room = RoomMgr::Instance()->GetRoomByUuid(param.sender);
if (!room) {
return;
}
Player* hum = room->GetPlayerByAccountId(param.param1.GetString());
if (!hum) {
return;
}
};
std::shared_ptr<a8::MutableXObject> params = a8::MutableXObject::CreateObject();
{
params->SetVal("account_id", account_id);
@ -2611,14 +2581,30 @@ void Human::SendBattleSettlement()
}
}
#endif
long long room_uuid = room->GetRoomUuid();
std::string account_id = account_id;
HttpProxy::Instance()->HttpGet
(
a8::XParams()
.SetSender(room->GetRoomUuid())
.SetParam1(account_id)
.SetParam2(data),
on_ok,
on_error,
[room_uuid, account_id, data]
(bool ok, a8::XObject* rsp_obj, f8::HttpContext* ctx)
{
Room* room = RoomMgr::Instance()->GetRoomByUuid(room_uuid);
if (!room) {
return;
}
Player* hum = room->GetPlayerByAccountId(account_id);
if (!hum) {
return;
}
if (!ok) {
f8::UdpLog::Instance()->Error("reportSettlement error params: %s response: %s",
{
data,
""
});
}
},
url.c_str(),
*params.get()
);
@ -2628,41 +2614,6 @@ void Human::SendBattleReport()
{
std::shared_ptr<a8::MutableXObject> params = a8::MutableXObject::CreateObject();
GenBattleReportData(params.get());
auto on_ok =
[] (a8::XParams& param, a8::XObject& data)
{
Room* room = RoomMgr::Instance()->GetRoomByUuid(param.sender);
if (!room) {
return;
}
Player* hum = room->GetPlayerByAccountId(param.param1.GetString());
if (!hum) {
return;
}
hum->sending_battlereport_ = false;
hum->already_report_battle_ = true;
hum->stats.ParseReward(hum, data);
hum->SendGameOver();
hum->SendBattleSettlement();
};
auto on_error =
[] (a8::XParams& param, const std::string& response)
{
f8::UdpLog::Instance()->Error("battleReport http error params: %s response: %s",
{
param.param2,
response
});
Room* room = RoomMgr::Instance()->GetRoomByUuid(param.sender);
if (!room) {
return;
}
Player* hum = room->GetPlayerByAccountId(param.param1.GetString());
if (!hum) {
return;
}
hum->sending_battlereport_ = false;
};
std::string url;
JsonDataMgr::Instance()->GetApiUrl(url);
if (url.find('?') != std::string::npos) {
@ -2677,14 +2628,38 @@ void Human::SendBattleReport()
already_report_battle_ = true;
SendGameOver();
} else {
long long room_uuid = room->GetRoomUuid();
std::string account_id = account_id;
HttpProxy::Instance()->HttpGet
(
a8::XParams()
.SetSender(room->GetRoomUuid())
.SetParam1(account_id)
.SetParam2(data),
on_ok,
on_error,
[room_uuid, account_id, data]
(bool ok, a8::XObject* rsp_obj, f8::HttpContext* ctx)
{
if (!ok) {
f8::UdpLog::Instance()->Error("battleReport http error params: %s response: %s",
{
data,
""
});
}
Room* room = RoomMgr::Instance()->GetRoomByUuid(room_uuid);
if (!room) {
return;
}
Player* hum = room->GetPlayerByAccountId(account_id);
if (!hum) {
return;
}
if (ok) {
hum->sending_battlereport_ = false;
hum->already_report_battle_ = true;
hum->stats.ParseReward(hum, *rsp_obj);
hum->SendGameOver();
hum->SendBattleSettlement();
} else {
hum->sending_battlereport_ = false;
}
},
url.c_str(),
*params.get()
);

View File

@ -1176,53 +1176,45 @@ void Player::_CMRevive(f8::MsgHdr& hdr, const cs::CMRevive& msg)
url_params->SetVal("session_id", session_id);
url_params->SetVal("target_id", hum->account_id);
url_params->SetVal("num", 1);
long long room_uuid = room->GetRoomUuid();
std::string account_id = account_id;
auto target_uniid = msg.target_uniid();
HttpProxy::Instance()->HttpGet
(
a8::XParams()
.SetSender(room->GetRoomUuid())
.SetParam1(account_id)
.SetParam2(msg.target_uniid()),
[] (a8::XParams& param, a8::XObject& data)
[room_uuid, account_id, target_uniid]
(bool ok, a8::XObject* rsp_obj, f8::HttpContext* ctx)
{
Room* room = RoomMgr::Instance()->GetRoomByUuid(param.sender);
Room* room = RoomMgr::Instance()->GetRoomByUuid(room_uuid);
if (!room) {
return;
}
Player* hum = room->GetPlayerByAccountId(param.param1.GetString());
Player* hum = room->GetPlayerByAccountId(account_id);
if (!hum) {
return;
}
if (room->IsGameOver()) {
return;
if (ok) {
if (room->IsGameOver()) {
return;
}
if (rsp_obj->GetType() == a8::XOT_OBJECT &&
rsp_obj->Get("errcode").GetInt() == 0) {
int revive_coin = rsp_obj->Get("revive_coin");
room->xtimer.SetTimeoutEx
(
NEXT_FRAME_TIMER,
[hum, target_uniid, revive_coin] (int event, const a8::Args* args)
{
if (a8::TIMER_EXEC_EVENT == event) {
a8::UnSetBitFlag(hum->status, CS_Reviving);
hum->InternalRevive(target_uniid, revive_coin);
}
},
&hum->xtimer_attacher);
}
} else {
a8::UnSetBitFlag(hum->status, CS_Reviving);
}
if (data.GetType() == a8::XOT_OBJECT &&
data.Get("errcode").GetInt() == 0) {
int revive_coin = data.Get("revive_coin");
int target_uniid = param.param2.GetInt();
room->xtimer.SetTimeoutEx
(
NEXT_FRAME_TIMER,
[hum, target_uniid, revive_coin] (int event, const a8::Args* args)
{
if (a8::TIMER_EXEC_EVENT == event) {
a8::UnSetBitFlag(hum->status, CS_Reviving);
hum->InternalRevive(target_uniid, revive_coin);
}
},
&hum->xtimer_attacher);
}
},
[] (a8::XParams& param, const std::string& response)
{
Room* room = RoomMgr::Instance()->GetRoomByUuid(param.sender);
if (!room) {
return;
}
Player* hum = room->GetPlayerByAccountId(param.param1.GetString());
if (!hum) {
return;
}
a8::UnSetBitFlag(hum->status, CS_Reviving);
},
url.c_str(),
*url_params

View File

@ -802,57 +802,41 @@ void RoomMgr::SendGetBattleData(int mode,
}
HttpProxy::Instance()->HttpGet
(
a8::XParams()
.SetSender(new std::shared_ptr<std::vector<std::shared_ptr<BattleDataContext>>>(result))
.SetParam1(new std::function<
void(std::vector<std::shared_ptr<BattleDataContext>>&)>(cb)),
[] (a8::XParams& param, a8::XObject& data)
[result, cb]
(bool ok, a8::XObject* rsp_obj, f8::HttpContext* ctx)
{
f8::UdpLog::Instance()->Info
("GetBattleData ok %s",
{
data.ToJsonStr()
});
auto result = (std::shared_ptr<std::vector<std::shared_ptr<BattleDataContext>>>*)param.sender.GetUserData();
auto cb = (std::function<
void(std::vector<std::shared_ptr<BattleDataContext>>&)>*)param.param1.GetUserData();
if (ok) {
f8::UdpLog::Instance()->Info
("GetBattleData ok %s",
{
rsp_obj->ToJsonStr()
});
if (data.GetType() == a8::XOT_OBJECT) {
int match_mode = data.Get("match_mode");
RoomMgr::Instance()->SetMatchMode(match_mode ? 1 : 0);
if (data.HasKey("members")) {
auto members = data.At("members");
if (members->GetType() == a8::XOT_ARRAY && members->Size() == (*result->get()).size()) {
for (int i = 0; i < members->Size(); ++i) {
auto& ctx = (*result->get()).at(i);
auto member = members->At(i);
if (member->GetType() == a8::XOT_OBJECT) {
ctx->ParseResult(*member);
if (rsp_obj->GetType() == a8::XOT_OBJECT) {
int match_mode = rsp_obj->Get("match_mode");
RoomMgr::Instance()->SetMatchMode(match_mode ? 1 : 0);
if (rsp_obj->HasKey("members")) {
auto members = rsp_obj->At("members");
if (members->GetType() == a8::XOT_ARRAY && members->Size() == result->size()) {
for (int i = 0; i < members->Size(); ++i) {
auto& ctx = result->at(i);
auto member = members->At(i);
if (member->GetType() == a8::XOT_OBJECT) {
ctx->ParseResult(*member);
}
}
}
}
}
cb(*result);
} else {
f8::UdpLog::Instance()->Warning
("GetBattleData error %s",
{
""
});
cb(*result);
}
(*cb)(*result->get());
delete cb;
delete result;
},
[] (a8::XParams& param, const std::string& response)
{
f8::UdpLog::Instance()->Warning
("GetBattleData error %s",
{
response
});
auto result = (std::shared_ptr<std::vector<std::shared_ptr<BattleDataContext>>>*)param.sender.GetUserData();
auto cb = (std::function<
void(std::vector<std::shared_ptr<BattleDataContext>>&)>*)param.param1.GetUserData();
(*cb)(*result->get());
for (auto& context : *result->get()) {
}
delete cb;
delete result;
},
url.c_str(),
*url_params