Skip to content

Commit

Permalink
添加 规则数据库
Browse files Browse the repository at this point in the history
重写 昵称部分
  • Loading branch information
w4123 committed Jul 22, 2018
1 parent b81eeb8 commit 6a40b54
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
/Dice/x64/Debug
/Release
/Dice/x64/Release
/packages
Binary file modified Dice/Dice.cpp
Binary file not shown.
13 changes: 13 additions & 0 deletions Dice/Dice.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,38 @@
<ItemGroup>
<ClCompile Include="Dice.cpp" />
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="GetRule.cpp" />
<ClCompile Include="InitList.cpp" />
<ClCompile Include="NameStorage.cpp" />
<ClCompile Include="RD.cpp" />
<ClCompile Include="StorageBase.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="APPINFO.h" />
<ClInclude Include="GetRule.h" />
<ClInclude Include="InitList.h" />
<ClInclude Include="NameStorage.h" />
<ClInclude Include="RD.h" />
<ClInclude Include="RDConstant.h" />
<ClInclude Include="StorageBase.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\AWSSDKCPP-Core.redist.1.4.89\build\native\AWSSDKCPP-Core.redist.targets" Condition="Exists('..\packages\AWSSDKCPP-Core.redist.1.4.89\build\native\AWSSDKCPP-Core.redist.targets')" />
<Import Project="..\packages\AWSSDKCPP-Core.1.4.89\build\native\AWSSDKCPP-Core.targets" Condition="Exists('..\packages\AWSSDKCPP-Core.1.4.89\build\native\AWSSDKCPP-Core.targets')" />
<Import Project="..\packages\AWSSDKCPP-DynamoDB.redist.1.4.20120810.89\build\native\AWSSDKCPP-DynamoDB.redist.targets" Condition="Exists('..\packages\AWSSDKCPP-DynamoDB.redist.1.4.20120810.89\build\native\AWSSDKCPP-DynamoDB.redist.targets')" />
<Import Project="..\packages\AWSSDKCPP-DynamoDB.1.4.20120810.89\build\native\AWSSDKCPP-DynamoDB.targets" Condition="Exists('..\packages\AWSSDKCPP-DynamoDB.1.4.20120810.89\build\native\AWSSDKCPP-DynamoDB.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\AWSSDKCPP-Core.redist.1.4.89\build\native\AWSSDKCPP-Core.redist.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\AWSSDKCPP-Core.redist.1.4.89\build\native\AWSSDKCPP-Core.redist.targets'))" />
<Error Condition="!Exists('..\packages\AWSSDKCPP-Core.1.4.89\build\native\AWSSDKCPP-Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\AWSSDKCPP-Core.1.4.89\build\native\AWSSDKCPP-Core.targets'))" />
<Error Condition="!Exists('..\packages\AWSSDKCPP-DynamoDB.redist.1.4.20120810.89\build\native\AWSSDKCPP-DynamoDB.redist.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\AWSSDKCPP-DynamoDB.redist.1.4.20120810.89\build\native\AWSSDKCPP-DynamoDB.redist.targets'))" />
<Error Condition="!Exists('..\packages\AWSSDKCPP-DynamoDB.1.4.20120810.89\build\native\AWSSDKCPP-DynamoDB.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\AWSSDKCPP-DynamoDB.1.4.20120810.89\build\native\AWSSDKCPP-DynamoDB.targets'))" />
</Target>
</Project>
9 changes: 9 additions & 0 deletions Dice/Dice.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<ClCompile Include="StorageBase.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="GetRule.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="APPINFO.h">
Expand All @@ -53,5 +56,11 @@
<ClInclude Include="StorageBase.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="GetRule.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>
168 changes: 168 additions & 0 deletions Dice/GetRule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#include "GetRule.h"
#include <string>
#include <aws/core/Aws.h>
#include <aws/core/utils/Outcome.h>
#include <aws/dynamodb/DynamoDBClient.h>
#include <aws/dynamodb/model/GetItemRequest.h>
#include <aws/core/auth/AWSAuthSigner.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <CQLogger.h>
#include <Windows.h>
#include <fstream>
#include "RDConstant.h"
extern CQ::logger logger;
using namespace std;
using namespace Aws;
using namespace DynamoDB;

std::string GetRule::GBKtoUTF8(const std::string & strGBK)
{
int len = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, nullptr, 0);
wchar_t * str1 = new wchar_t[len + 1];
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, len);
len = WideCharToMultiByte(CP_UTF8, 0, str1, -1, nullptr, 0, nullptr, nullptr);
char * str2 = new char[len + 1];
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, len, nullptr, nullptr);
string strOutUTF8(str2);
delete[] str1;
delete[] str2;
return strOutUTF8;
}

std::string GetRule::UTF8toGBK(const std::string & strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, nullptr, 0);
wchar_t * wszGBK = new wchar_t[len + 1];
MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, nullptr, 0, nullptr, nullptr);
char *szGBK = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, nullptr, nullptr);
string strTemp(szGBK);
delete[] szGBK;
delete[] wszGBK;
return strTemp;
}

/*
* 避免Windows.h中的GetMessage宏与Aws SDK中的GetMessage函数冲突
*/
#undef GetMessage

GetRule::GetRule()
{
InitAPI(options);
Client::ClientConfiguration config;
config.region = Region::AP_SOUTHEAST_1;
client = make_unique<DynamoDBClient>(Auth::AWSCredentials(ACCESS_KEY, SECRET_ACCESS_KEY), config);

Model::GetItemRequest req;
const Model::AttributeValue haskKey("Rules"), sortKey("List");
req.AddKey("Type", haskKey).AddKey("Name", sortKey).SetTableName("DiceDB");
string ErrMsg;
for (auto i = 0; i != 3; ++i)
{
const Model::GetItemOutcome& result = client->GetItem(req);

if (result.IsSuccess())
{
const auto& item = result.GetResult().GetItem();
if (!item.empty() && item.count("RulesList"))
{
auto list = item.at("RulesList").GetSS();
for (const auto& rule : list)
{
rules.push_back(rule);
}
}
return;
}
ErrMsg += result.GetError().GetMessage();
Sleep(200);
}
ShutdownAPI(options);
failed = true;
ErrMsg = "尝试连接规则服务器失败!Rules功能将不可用! 具体信息:" + ErrMsg;
logger.Warning(ErrMsg);

}


GetRule::~GetRule()
{
if(!failed)
ShutdownAPI(options);
}

bool GetRule::analyse(string & rawstr, string& des)
{
if (failed)
{
des += strRulesFailedErr;
return false;
}
for (auto &chr : rawstr)chr = toupper(chr);

if (rawstr.find(':')!=string::npos)
{
const string name = rawstr.substr(rawstr.find(':') + 1);
if(name.empty())
{
des += strRulesFormatErr;
return false;
}
string rule = rawstr.substr(0, rawstr.find(':'));
if (ruleNameReplace.count(rule))rule = ruleNameReplace.at(rule);
return get(rule, name, des);

}
if (rawstr.empty())
{
des += strRulesFormatErr;
return false;
}
for (const auto& rule:rules)
{
des = "";
if (get(rule, rawstr, des))
{
return true;
}
}
return false;
}

bool GetRule::get(const std::string & rule, const std::string & name, std::string & des,bool isUTF8) const
{
Model::GetItemRequest req;
const string ruleName = isUTF8 ? rule : GBKtoUTF8(rule);
const string itemName = isUTF8 ? name : GBKtoUTF8(name);
const Model::AttributeValue haskKey("Rules-" + ruleName),sortKey(itemName);
req.AddKey("Type", haskKey).AddKey("Name", sortKey).SetTableName("DiceDB");

const Model::GetItemOutcome& result = client->GetItem(req);

if(result.IsSuccess())
{
const auto& item = result.GetResult().GetItem();
if (!item.empty())
{
if (item.count("Content"))
{
des += UTF8toGBK(item.at("Content").GetS());
return true;
}
if (item.count("Redirect"))
{
return get(ruleName, item.at("Redirect").GetS(), des, true);
}
des += strRuleNotFound;
return false;

}
des += strRuleNotFound;
return false;
}
logger.Warning(("获取规则数据失败! 详细信息:\n" + result.GetError().GetMessage()).data());
des += result.GetError().GetMessage();
return false;
}
31 changes: 31 additions & 0 deletions Dice/GetRule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include <string>
#include <vector>
#include <aws/dynamodb/DynamoDBClient.h>
#include <aws/core/Aws.h>

class GetRule
{
bool failed = false;
Aws::SDKOptions options;
std::unique_ptr<Aws::DynamoDB::DynamoDBClient> client{};
std::string ACCESS_KEY = "AKIAI5QK6ZFNW62URGFA";
std::string SECRET_ACCESS_KEY = "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";
std::vector<std::string> rules{};
const std::map<std::string, std::string> ruleNameReplace
{
std::make_pair("5E","DND5E"),
std::make_pair("3R","DND3R"),
std::make_pair("DND","DND3R"),
std::make_pair("D&D","DND3R"),
std::make_pair("COC", "COC7")
};
public:
GetRule();
~GetRule();
bool analyse(std::string& rawstr, std::string& des);
bool get(const std::string& rule, const std::string& name, std::string& des,bool isUTF8 = false) const;
static std::string GBKtoUTF8(const std::string& strGBK);
static std::string UTF8toGBK(const std::string& strUTF8);
};

Binary file modified Dice/NameStorage.cpp
Binary file not shown.
Binary file modified Dice/NameStorage.h
Binary file not shown.
Binary file modified Dice/RDConstant.h
Binary file not shown.
7 changes: 7 additions & 0 deletions Dice/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AWSSDKCPP-Core" version="1.4.89" targetFramework="native" />
<package id="AWSSDKCPP-Core.redist" version="1.4.89" targetFramework="native" />
<package id="AWSSDKCPP-DynamoDB" version="1.4.20120810.89" targetFramework="native" />
<package id="AWSSDKCPP-DynamoDB.redist" version="1.4.20120810.89" targetFramework="native" />
</packages>

0 comments on commit 6a40b54

Please sign in to comment.