Files
dnf-utils/ace_test/aServer/NetbarInterface.cpp
T
2025-12-06 16:42:21 +08:00

120 lines
3.7 KiB
C++

#include "stdafx.h"
#include "NetbarInterface.h"
#include "GameServer.h"
#include "GameProto.pb.h"
#include "CurlHelper.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
std::string buildRequestJson(GameClient* client, Game::NetbaraStatusReq* request)
{
rapidjson::Document root;
root.SetObject();
rapidjson::Document::AllocatorType& allocator = root.GetAllocator();
//openid
rapidjson::Value openIDValue(client->gameAccoundID.c_str(), static_cast<rapidjson::SizeType>(client->gameAccoundID.length()), allocator);
root.AddMember("openid", openIDValue, allocator);
//ip
std::string ipStr = client->connection->get_peer_addr().get_ip();
rapidjson::Value ipValue(ipStr.c_str(), static_cast<rapidjson::SizeType>(ipStr.length()), allocator);
root.AddMember("ip", ipValue, allocator);
//mac
rapidjson::Value macArray(rapidjson::kArrayType);
std::stringstream ss(request->macs().c_str());
std::string mac;
while (std::getline(ss, mac, ';'))
{
rapidjson::Value macValue(mac.c_str(), static_cast<rapidjson::SizeType>(mac.length()), allocator);
macArray.PushBack(macValue, allocator);
};
root.AddMember("macs", macArray, allocator);
//netbar token
rapidjson::Value tokenValue(request->token().c_str(), static_cast<rapidjson::SizeType>(request->token().length()), allocator);
root.AddMember("netbar_token", tokenValue, allocator);
//game id
root.AddMember("game_id", 1, allocator);
//game zone
std::string strZoneID = std::to_string(client->gameWorldID);
rapidjson::Value zoneIDValue(strZoneID.c_str(), static_cast<rapidjson::SizeType>(strZoneID.length()), allocator);
root.AddMember("zone", zoneIDValue, allocator);
//request no
std::string strRequestTime = std::to_string(static_cast<uint32_t>(time(nullptr)));
rapidjson::Value requestNumber(strRequestTime.c_str(), static_cast<rapidjson::SizeType>(strRequestTime.length()), allocator);
root.AddMember("reqno", requestNumber, allocator);
rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
root.Accept(writer);
std::string outputJson = sb.GetString();
return outputJson;
}
std::string buildRequestUrl()
{
return "http://api-test.netbar.itrpc.woa.com/netbar.cafe.rewards.rewards/Rewards";
}
bool NetbarInterface::onReceiveNetbarReportRequest(GameClient* client, Game::NetbaraStatusReq* request)
{
std::string requestBody = buildRequestJson(client, request);
std::string requestUrl = buildRequestUrl();
std::string result;
int32_t retCode = curlRequest(requestUrl, requestBody, result, true);
int32_t errorCode = 0;
std::string errorInfo;
int32_t netbarLevel = 0;
if(retCode != 0)
{
CY_LOG(cyclone::L_ERROR, "NetbarInterface::onReceiveNetbarReportRequest curlRequest failed, retCode=%d", retCode);
char szTemp[256] = { 0 };
snprintf(szTemp, 256, "Curl request failed, retCode=%d", retCode);
errorInfo = szTemp;
}
else
{
rapidjson::Document document;
rapidjson::ParseResult prserResult = document.Parse(result.c_str());
if(document.HasMember("errcode") && document["errcode"].IsInt())
{
errorCode = document["errcode"].GetInt();
}
if(document.HasMember("errinfo") && document["errinfo"].IsString())
{
errorInfo = document["errinfo"].GetString();
}
if(document.HasMember("rids") && document["rids"].IsInt())
{
netbarLevel = document["rids"].GetInt();
}
}
////send netbar status reply
Game::NetbarStatusRes netbarResponse;
netbarResponse.set_error_code(errorCode);
netbarResponse.set_error_message(errorInfo);
netbarResponse.set_netbar_level(netbarLevel);
cyclone::Packet outputPacket;
serializeProtoMessage(GAME_PROTO_NETBAR_RES, &netbarResponse, outputPacket);
client->connection->send(outputPacket.get_memory_buf(), outputPacket.get_memory_size());
return true;
}