Files
wnmj/Classes/Global/GlobalJosn.cpp
2026-02-13 14:34:15 +08:00

262 lines
5.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "GlobalJosn.h"
#include "cocos2d.h"
#include "json/document.h"
#include "Utility.h"
#include "Platform.h"
#include "cryptHeader.h"
USING_NS_CC;
GlobalJosn* GlobalJosn::s_configInstance = nullptr;
void GlobalJosn::init()
{
std::vector<tagGameConfig>().swap(m_vecGameConfig);
m_vecGameConfig.reserve(10);
m_strLogonIp = LOGON_SERVER_ADDRESS;
m_iPort = LOGON_SERVER_PORT;
m_strAgentTip = "";
m_strShareTip = "";
m_IsNotice = false;
m_NoticeUrl = "";
m_strPayUrl = PAY_TO_PLAYER_HTTP_ADDRESS;
m_strDuiHuanUrl = DUI_HUAN_HTTP_ADDRESS;
m_strGongHuiUrl = GONG_HUI_HTTP_ADDRESS;
m_RefreshTime = DEFINE_REFRESH_TIME;
m_strShareUrl = SHARE_HTTP_ADDRESS;
#if (CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
m_IsOpenNN = true;
m_IsOpenZJH = true;
m_IsOpenSSZ = true;
#else
m_IsOpenNN = false;
m_IsOpenZJH = false;
m_IsOpenSSZ = false;
#endif
}
void GlobalJosn::clear()
{
std::vector<tagGameConfig>().swap(m_vecGameConfig);
}
void GlobalJosn::loadConfig()
{
//loadServerList();
loadShareList();
}
void GlobalJosn::loadServerList()
{
std::vector<tagGameConfig>().swap(m_vecGameConfig);
auto str = cocos2d::FileUtils::getInstance()->getStringFromFile("Platform/gamelist/GameConfig.json");
rapidjson::Document doc;
doc.Parse<0>(str.c_str());
if (doc.HasParseError() || !doc.IsArray())
{
return;
}
//读取游戏配置信息
for (rapidjson::SizeType i = 0; i < doc.Size(); ++i)
{
const rapidjson::Value &gameObject = doc[i];
tagGameConfig config = tagGameConfig();
//游戏名
if (gameObject.HasMember("name") && gameObject["name"].IsString())
{
config.strGamename = gameObject["name"].GetString();
}
//kind id
if (gameObject.HasMember("kind") && gameObject["kind"].IsInt())
{
config.nGameKindId = gameObject["kind"].GetInt();
}
//是否激活
if (gameObject.HasMember("active") && gameObject["active"].IsBool())
{
config.bGameActive = gameObject["active"].GetBool();
}
//是否添加到游戏列表
if (gameObject.HasMember("addtolist") && gameObject["addtolist"].IsBool())
{
config.bAddToList = gameObject["addtolist"].GetBool();
}
//游戏规则
if (gameObject.HasMember("GameRule") && gameObject["GameRule"].IsString())
{
config.strGameRule = gameObject["GameRule"].GetString();
}
//注释
if (gameObject.HasMember("comment") && gameObject["comment"].IsString())
{
config.strComment = gameObject["comment"].GetString();
cocos2d::log("game name:%s, kind:%d, comment:%s", config.strGamename.c_str(), config.nGameKindId, config.strGamename.c_str());
}
m_vecGameConfig.push_back(config);
}
std::string strCout = StringUtils::format("游戏总数: %d", (int)m_vecGameConfig.size());
cocos2d::log("%s", utility::a_u8(strCout).c_str());
}
void GlobalJosn::loadShareList()
{
std::vector<tagShareInfo>().swap(m_vecShareList);
auto str = cocos2d::FileUtils::getInstance()->getStringFromFile("Platform/gamelist/notice.json");
rapidjson::Document doc;
doc.Parse<0>(str.c_str());
if (doc.HasParseError() || !doc.IsArray())
{
return;
}
CC_ASSERT(!doc.HasParseError() && doc.IsArray());
for (rapidjson::SizeType i = 0; i < doc.Size(); i++)
{
const rapidjson::Value &gameObject = doc[i];
tagShareInfo config = tagShareInfo();
//kind id
if (gameObject.HasMember("kind") && gameObject["kind"].IsInt())
{
config.nKindID = gameObject["kind"].GetInt();
}
//内容
if (gameObject.HasMember("comment") && gameObject["comment"].IsString())
{
config.strComment = gameObject["comment"].GetString();
}
m_vecShareList.push_back(config);
}
}
int GlobalJosn::getGameCount()
{
return (int)m_vecGameConfig.size();
}
tagGameConfig GlobalJosn::getGameConfigByIdx(const int &nIdx, bool &bHave)
{
bHave = false;
if (nIdx >= 0 && nIdx < (int)m_vecGameConfig.size())
{
bHave = true;
return m_vecGameConfig[nIdx];
}
return tagGameConfig();
}
tagGameConfig GlobalJosn::getGameConfigByKind(const int &nKind, bool &bHave)
{
bHave = false;
for (int i = 0; i < (int)m_vecGameConfig.size(); ++i)
{
if (m_vecGameConfig[i].nGameKindId == nKind)
{
bHave = true;
return m_vecGameConfig[i];
}
}
return tagGameConfig();
}
bool GlobalJosn::isGameActive(const int &nKind)
{
for (int i = 0; i < (int)m_vecGameConfig.size(); ++i)
{
if (m_vecGameConfig[i].nGameKindId == nKind)
{
return m_vecGameConfig[i].bGameActive;
}
}
return false;
}
bool GlobalJosn::isGameInList(const int &nKind)
{
for (int i = 0; i < (int)m_vecGameConfig.size(); ++i)
{
if (m_vecGameConfig[i].nGameKindId == nKind)
{
return true;
}
}
return false;
}
// 文本格式本身就是UTF-8程序无需再转
std::string GlobalJosn::getShareInfoByKind(const int nKindID)
{
if (nKindID == SH_KIND_FRIEND_RUL || nKindID == SH_KIND_WEIXIN_URL)
{
return m_strShareUrl;
}
for (int i = 0; i < (int)m_vecShareList.size(); ++i)
{
if (m_vecShareList[i].nKindID == nKindID)
{
return m_vecShareList[i].strComment;
}
}
return "";
}
//设置IP和PORT(格式:192.168.1.123,10086)
void GlobalJosn::SetLoginIpAndPort(std::string strIpAndPortEncrypt)
{
if (strIpAndPortEncrypt.empty()) return;
// 设置解码KEY
m_strAesKey = "45404b08fbfa98823c6f3dbb5ffd8259";
coAesSetKey(m_strAesKey.c_str());
//cocos2d::log("AesKey = %s", m_strAesKey.c_str());
// 定义变量
std::string strBase64Decode = ""; // base64解码
std::string strAesDecode = ""; // aes解码
// BASE64解码
coBase64Decode(strBase64Decode, strIpAndPortEncrypt);
//cocos2d::log("Base64Decode = %s", strBase64Decode.c_str());
// AES解密
coAesDecript(strBase64Decode, strAesDecode);
//cocos2d::log("AesDecode = %s", strAesDecode.c_str());
// 格式解析
int __size = strAesDecode.length();
int __ind = strAesDecode.find(',');
if (std::string::npos == __ind) return;
std::string strPort = "";
m_strLogonIp = strAesDecode.substr(0, __ind);
strPort = strAesDecode.substr(__ind + 1);
//cocos2d::log("m_strLogonIp = %s, strPort = %s", m_strLogonIp.c_str(), strPort.c_str());
// 端口转换
char* lpStrEnd = NULL;
int iPort = (int)strtol(strPort.c_str(), &lpStrEnd, 10);
GlobalJosn::getInstance()->m_iPort = iPort;
}