Skip to content

Commit 6a40b54

Browse files
committed
添加 规则数据库
重写 昵称部分
1 parent b81eeb8 commit 6a40b54

File tree

10 files changed

+229
-0
lines changed

10 files changed

+229
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@
3737
/Dice/x64/Debug
3838
/Release
3939
/Dice/x64/Release
40+
/packages

Dice/Dice.cpp

-7.11 KB
Binary file not shown.

Dice/Dice.vcxproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,25 +186,38 @@
186186
<ItemGroup>
187187
<ClCompile Include="Dice.cpp" />
188188
<ClCompile Include="dllmain.cpp" />
189+
<ClCompile Include="GetRule.cpp" />
189190
<ClCompile Include="InitList.cpp" />
190191
<ClCompile Include="NameStorage.cpp" />
191192
<ClCompile Include="RD.cpp" />
192193
<ClCompile Include="StorageBase.cpp" />
193194
</ItemGroup>
194195
<ItemGroup>
195196
<ClInclude Include="APPINFO.h" />
197+
<ClInclude Include="GetRule.h" />
196198
<ClInclude Include="InitList.h" />
197199
<ClInclude Include="NameStorage.h" />
198200
<ClInclude Include="RD.h" />
199201
<ClInclude Include="RDConstant.h" />
200202
<ClInclude Include="StorageBase.h" />
201203
</ItemGroup>
204+
<ItemGroup>
205+
<None Include="packages.config" />
206+
</ItemGroup>
202207
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
203208
<ImportGroup Label="ExtensionTargets">
209+
<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')" />
210+
<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')" />
211+
<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')" />
212+
<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')" />
204213
</ImportGroup>
205214
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
206215
<PropertyGroup>
207216
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
208217
</PropertyGroup>
218+
<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'))" />
219+
<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'))" />
220+
<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'))" />
221+
<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'))" />
209222
</Target>
210223
</Project>

Dice/Dice.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<ClCompile Include="StorageBase.cpp">
3434
<Filter>源文件</Filter>
3535
</ClCompile>
36+
<ClCompile Include="GetRule.cpp">
37+
<Filter>源文件</Filter>
38+
</ClCompile>
3639
</ItemGroup>
3740
<ItemGroup>
3841
<ClInclude Include="APPINFO.h">
@@ -53,5 +56,11 @@
5356
<ClInclude Include="StorageBase.h">
5457
<Filter>头文件</Filter>
5558
</ClInclude>
59+
<ClInclude Include="GetRule.h">
60+
<Filter>头文件</Filter>
61+
</ClInclude>
62+
</ItemGroup>
63+
<ItemGroup>
64+
<None Include="packages.config" />
5665
</ItemGroup>
5766
</Project>

Dice/GetRule.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#include "GetRule.h"
2+
#include <string>
3+
#include <aws/core/Aws.h>
4+
#include <aws/core/utils/Outcome.h>
5+
#include <aws/dynamodb/DynamoDBClient.h>
6+
#include <aws/dynamodb/model/GetItemRequest.h>
7+
#include <aws/core/auth/AWSAuthSigner.h>
8+
#include <aws/core/auth/AWSCredentialsProvider.h>
9+
#include <CQLogger.h>
10+
#include <Windows.h>
11+
#include <fstream>
12+
#include "RDConstant.h"
13+
extern CQ::logger logger;
14+
using namespace std;
15+
using namespace Aws;
16+
using namespace DynamoDB;
17+
18+
std::string GetRule::GBKtoUTF8(const std::string & strGBK)
19+
{
20+
int len = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, nullptr, 0);
21+
wchar_t * str1 = new wchar_t[len + 1];
22+
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, len);
23+
len = WideCharToMultiByte(CP_UTF8, 0, str1, -1, nullptr, 0, nullptr, nullptr);
24+
char * str2 = new char[len + 1];
25+
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, len, nullptr, nullptr);
26+
string strOutUTF8(str2);
27+
delete[] str1;
28+
delete[] str2;
29+
return strOutUTF8;
30+
}
31+
32+
std::string GetRule::UTF8toGBK(const std::string & strUTF8)
33+
{
34+
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, nullptr, 0);
35+
wchar_t * wszGBK = new wchar_t[len + 1];
36+
MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, wszGBK, len);
37+
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, nullptr, 0, nullptr, nullptr);
38+
char *szGBK = new char[len + 1];
39+
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, nullptr, nullptr);
40+
string strTemp(szGBK);
41+
delete[] szGBK;
42+
delete[] wszGBK;
43+
return strTemp;
44+
}
45+
46+
/*
47+
* 避免Windows.h中的GetMessage宏与Aws SDK中的GetMessage函数冲突
48+
*/
49+
#undef GetMessage
50+
51+
GetRule::GetRule()
52+
{
53+
InitAPI(options);
54+
Client::ClientConfiguration config;
55+
config.region = Region::AP_SOUTHEAST_1;
56+
client = make_unique<DynamoDBClient>(Auth::AWSCredentials(ACCESS_KEY, SECRET_ACCESS_KEY), config);
57+
58+
Model::GetItemRequest req;
59+
const Model::AttributeValue haskKey("Rules"), sortKey("List");
60+
req.AddKey("Type", haskKey).AddKey("Name", sortKey).SetTableName("DiceDB");
61+
string ErrMsg;
62+
for (auto i = 0; i != 3; ++i)
63+
{
64+
const Model::GetItemOutcome& result = client->GetItem(req);
65+
66+
if (result.IsSuccess())
67+
{
68+
const auto& item = result.GetResult().GetItem();
69+
if (!item.empty() && item.count("RulesList"))
70+
{
71+
auto list = item.at("RulesList").GetSS();
72+
for (const auto& rule : list)
73+
{
74+
rules.push_back(rule);
75+
}
76+
}
77+
return;
78+
}
79+
ErrMsg += result.GetError().GetMessage();
80+
Sleep(200);
81+
}
82+
ShutdownAPI(options);
83+
failed = true;
84+
ErrMsg = "尝试连接规则服务器失败!Rules功能将不可用! 具体信息:" + ErrMsg;
85+
logger.Warning(ErrMsg);
86+
87+
}
88+
89+
90+
GetRule::~GetRule()
91+
{
92+
if(!failed)
93+
ShutdownAPI(options);
94+
}
95+
96+
bool GetRule::analyse(string & rawstr, string& des)
97+
{
98+
if (failed)
99+
{
100+
des += strRulesFailedErr;
101+
return false;
102+
}
103+
for (auto &chr : rawstr)chr = toupper(chr);
104+
105+
if (rawstr.find(':')!=string::npos)
106+
{
107+
const string name = rawstr.substr(rawstr.find(':') + 1);
108+
if(name.empty())
109+
{
110+
des += strRulesFormatErr;
111+
return false;
112+
}
113+
string rule = rawstr.substr(0, rawstr.find(':'));
114+
if (ruleNameReplace.count(rule))rule = ruleNameReplace.at(rule);
115+
return get(rule, name, des);
116+
117+
}
118+
if (rawstr.empty())
119+
{
120+
des += strRulesFormatErr;
121+
return false;
122+
}
123+
for (const auto& rule:rules)
124+
{
125+
des = "";
126+
if (get(rule, rawstr, des))
127+
{
128+
return true;
129+
}
130+
}
131+
return false;
132+
}
133+
134+
bool GetRule::get(const std::string & rule, const std::string & name, std::string & des,bool isUTF8) const
135+
{
136+
Model::GetItemRequest req;
137+
const string ruleName = isUTF8 ? rule : GBKtoUTF8(rule);
138+
const string itemName = isUTF8 ? name : GBKtoUTF8(name);
139+
const Model::AttributeValue haskKey("Rules-" + ruleName),sortKey(itemName);
140+
req.AddKey("Type", haskKey).AddKey("Name", sortKey).SetTableName("DiceDB");
141+
142+
const Model::GetItemOutcome& result = client->GetItem(req);
143+
144+
if(result.IsSuccess())
145+
{
146+
const auto& item = result.GetResult().GetItem();
147+
if (!item.empty())
148+
{
149+
if (item.count("Content"))
150+
{
151+
des += UTF8toGBK(item.at("Content").GetS());
152+
return true;
153+
}
154+
if (item.count("Redirect"))
155+
{
156+
return get(ruleName, item.at("Redirect").GetS(), des, true);
157+
}
158+
des += strRuleNotFound;
159+
return false;
160+
161+
}
162+
des += strRuleNotFound;
163+
return false;
164+
}
165+
logger.Warning(("获取规则数据失败! 详细信息:\n" + result.GetError().GetMessage()).data());
166+
des += result.GetError().GetMessage();
167+
return false;
168+
}

Dice/GetRule.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
#include <string>
3+
#include <vector>
4+
#include <aws/dynamodb/DynamoDBClient.h>
5+
#include <aws/core/Aws.h>
6+
7+
class GetRule
8+
{
9+
bool failed = false;
10+
Aws::SDKOptions options;
11+
std::unique_ptr<Aws::DynamoDB::DynamoDBClient> client{};
12+
std::string ACCESS_KEY = "AKIAI5QK6ZFNW62URGFA";
13+
std::string SECRET_ACCESS_KEY = "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";
14+
std::vector<std::string> rules{};
15+
const std::map<std::string, std::string> ruleNameReplace
16+
{
17+
std::make_pair("5E","DND5E"),
18+
std::make_pair("3R","DND3R"),
19+
std::make_pair("DND","DND3R"),
20+
std::make_pair("D&D","DND3R"),
21+
std::make_pair("COC", "COC7")
22+
};
23+
public:
24+
GetRule();
25+
~GetRule();
26+
bool analyse(std::string& rawstr, std::string& des);
27+
bool get(const std::string& rule, const std::string& name, std::string& des,bool isUTF8 = false) const;
28+
static std::string GBKtoUTF8(const std::string& strGBK);
29+
static std::string UTF8toGBK(const std::string& strUTF8);
30+
};
31+

Dice/NameStorage.cpp

84 Bytes
Binary file not shown.

Dice/NameStorage.h

36 Bytes
Binary file not shown.

Dice/RDConstant.h

554 Bytes
Binary file not shown.

Dice/packages.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="AWSSDKCPP-Core" version="1.4.89" targetFramework="native" />
4+
<package id="AWSSDKCPP-Core.redist" version="1.4.89" targetFramework="native" />
5+
<package id="AWSSDKCPP-DynamoDB" version="1.4.20120810.89" targetFramework="native" />
6+
<package id="AWSSDKCPP-DynamoDB.redist" version="1.4.20120810.89" targetFramework="native" />
7+
</packages>

0 commit comments

Comments
 (0)