Skip to content

Commit

Permalink
优化NIAHttpBOT
Browse files Browse the repository at this point in the history
  • Loading branch information
NIANIANKNIA committed Jul 19, 2023
1 parent 0d747f2 commit f342ccf
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 114 deletions.
48 changes: 10 additions & 38 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,25 @@

**版本说明:本次script-api出现部分变动,与老版本不相兼容,请及时更新本版本!**

## 适配

BDS && Minecraft BedRock-1.20.10.02

Liteloader 2.14.1

## 新增

1.全新的NIAHttpBOT!基于c++编写,相比旧方案内存占用更小,性能更好!(特别感谢[**@jiansyuan**](https://github.com/jiansyuan)在编写中给予的帮助!)
1.NIAHttpBOT新增部分API(具体使用方法请前往**NIA服务器官方文档站**查看)

2.玩家交易市场部分功能(制作中,预计将在v1.4.0发布正式版)
- `/CheckFile`
- `/RunCmd`
- `/WriteLineToFile`
- `/OverwriteFile`
- `/CheckDir`

已完成模块:
2.NIAHttpBOT新增配置文件

- 玩家上线市场逻辑(服务器本地)
- 玩家上线物品依赖服务器存储数据
- 玩家可以初步查看市场商品
3.行为包现在可以自动检查是否为最新版本了

## 优化

1.由于qq机器人暂时不稳定,在找到稳定的解决方案前暂时停止qq机器人的使用

2.自本版本之后,**停止对NIA-Server-BOT的更新与维护**,转而使用全新的**NIAHttpBOT**实现对原有功能的替代。

3.飞行系统授权码算法优化 [**@jiansyuan**](https://github.com/jiansyuan)

4.优化商城id生成逻辑

## 修复

1.商店系统&&回收系统无法正常购买&&回收的bug

2.传送系统无法正常同意的bug

3.转账系统无法正常使用的bug

4.飞行系统无法正常验证的bug

5.玩家上架物品时不填写详细项目仍可以正常上架的bug

6.玩家上架物品不为物品最大数量时显示仍为最大数量的bug

7.在部分情况下无法正常上架商品的bug

8.每晚12点更新时传送指令的错误
1.NIAHttpBOT部分情况下报错信息更加详细

9.(llse)扫地机插件无法正常恢复被扫物品的bug
2.正式移除`NIA-Server-BOT`项目及其所有文件

**配置说明:您可以前往[NIA服务器官方文档站](https://docs.mcnia.top/zh-CN/deploy.html)查看具体部署过程!**

41 changes: 9 additions & 32 deletions NIAHttpBOT/src/NIAHttpBOT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int main() {
UseCmd = par.getBool("UseCmd");
INFO("已成功读取配置文件!");
}

INFO("NIAHttpBOT 已在 " + IPAddress + ":" + std::to_string(PORT) + " 上成功启动!");
INFO("项目地址:https://github.com/NIANIANKNIA/NIASERVER-V4/tree/main/NIAHttpBOT");
INFO("项目作者:@NIANIANKNIA @jiansyuan");
Expand Down Expand Up @@ -134,22 +134,20 @@ int main() {
//创建新的文件
svr.Post("/CreateNewFile", [](const httplib::Request& req, httplib::Response& res) {
INFO("接收到创建文件的请求! ");
res.status = 200;
//解析字符串并创建一个json对象
rapidjson::Document NewFileData;
NewFileData.Parse(req.body.c_str());
//判断是否解析成功
if (NewFileData.HasParseError()) {
res.status = 200;
res.set_content("Data parsing failed", "text/plain");
return ;
}
if(!NewFileData.HasMember("fileName")){
res.status = 200;
res.set_content("The fileName key for the json object was not found! Please recheck and send again.", "text/plain");
return ;
}
if(!NewFileData.HasMember("fileContent")){
res.status = 200;
res.set_content("The fileContent key for the json object was not found! Please recheck and send again.", "text/plain");
return ;
}
Expand All @@ -163,15 +161,13 @@ int main() {
// 检测${fileName}文件是否存在,如果不存在,就创建一个新的文件,并写入内容
std::ifstream file(fileName);
if(file){
res.status = 200;
res.set_content("The target file already exists and cannot be regenerated.", "text/plain");
return ;
}
//打开指定文件开始写入数据
std::ofstream outFile(fileName);
outFile << fileContent;
outFile.close();
res.status = 200;
res.set_content("success", "text/plain");
file.close();
});
Expand All @@ -180,22 +176,20 @@ int main() {
//创建新的json文件
svr.Post("/CreateNewJsonFile", [](const httplib::Request& req, httplib::Response& res) {
INFO("接收到创建json文件的请求! ");
res.status = 200;
//解析字符串并创建一个json对象
rapidjson::Document NewFileData;
NewFileData.Parse(req.body.c_str());
//判断是否解析成功
if (NewFileData.HasParseError()) {
res.status = 200;
res.set_content("Data parsing failed", "text/plain");
return ;
}
if(!NewFileData.HasMember("fileName")){
res.status = 200;
res.set_content("The fileName key for the json object was not found! Please recheck and send again.", "text/plain");
return ;
}
if(!NewFileData.HasMember("fileContent")){
res.status = 200;
res.set_content("The fileContent key for the json object was not found! Please recheck and send again.", "text/plain");
return ;
}
Expand All @@ -214,29 +208,27 @@ int main() {
// 检测${fileName}文件是否存在,如果不存在,就创建一个新的文件,并写入内容
std::ifstream file(fileName);
if(file){
res.status = 200;
res.set_content("The target file already exists and cannot be regenerated.", "text/plain");
return ;
}
file.close();
std::ofstream out(fileName);
out << buffer.GetString();
out.close();
res.status = 200;
res.set_content("success", "text/plain");
file.close();
});

//获取json文件数据
svr.Post("/GetJsonFileData", [](const httplib::Request& req, httplib::Response& res) {
INFO("接收到获取文件数据的请求,请求获取的文件名称为: " << req.body);
res.status = 200;
//初始化文件名称
std::string fileName = req.body;
//判断文件存不存在
std::ifstream file(fileName);
if(!file) {
//文件打开失败
res.status = 200;
res.set_content("The target file does not exist", "text/plain");
return ;
}
Expand All @@ -251,30 +243,27 @@ int main() {
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
std::string jsonStr = buffer.GetString();
res.status = 200;
res.set_content(jsonStr, "text/plain");
file.close();
});

//覆盖json文件内容
svr.Post("/OverwriteJsonFile", [](const httplib::Request& req, httplib::Response& res) {
INFO("接收到覆写json文件的请求!");
res.status = 200;
//解析字符串并创建一个json对象
rapidjson::Document overWriteFileData;
overWriteFileData.Parse(req.body.c_str());
//判断是否解析成功
if (overWriteFileData.HasParseError()) {
res.status = 200;
res.set_content("Data parsing failed", "text/plain");
return ;
}
if(!overWriteFileData.HasMember("fileName")){
res.status = 200;
res.set_content("The fileName key for the json object was not found! Please recheck and send again.", "text/plain");
return ;
}
if(!overWriteFileData.HasMember("fileData")){
res.status = 200;
res.set_content("The fileData key for the json object was not found! Please recheck and send again.", "text/plain");
return ;
}
Expand All @@ -293,54 +282,49 @@ int main() {
// 检测${fileName}文件是否存在,如果不存在,就创建一个新的文件,并写入内容
std::ifstream file(fileName);
if(!file) {
res.status = 200;
res.set_content("The target file does not exist.", "text/plain");
return ;
}
std::ofstream out(fileName);
out << buffer.GetString();
out.close();
res.status = 200;
res.set_content("success", "text/plain");
file.close();

});

//执行cmd命令
svr.Post("/RunCmd", [](const httplib::Request& req, httplib::Response& res) {
res.status = 200;
//首先判断配置文件是否启用
if (!UseCmd) {
WARN("执行DOS命令的功能暂未启用!请在启用后使用!");
res.status = 200;
res.set_content("feature not enabled!", "text/plain");
return ;
}
std::string cmd = req.body;
WARN("收到一条执行DOS命令的请求:" + cmd);
system(cmd.c_str());
res.status = 200;
res.set_content("success", "text/plain");
});

//向目标文件写入一行内容
svr.Post("/WriteLineToFile", [](const httplib::Request& req, httplib::Response& res) {
INFO("接收到向目标文件写入一行内容的请求!");
res.status = 200;
//解析字符串并创建一个json对象
rapidjson::Document WriteLineData;
WriteLineData.Parse(req.body.c_str());
//判断是否解析成功
if (WriteLineData.HasParseError()) {
res.status = 200;
res.set_content("Data parsing failed", "text/plain");
return ;
}
if(!WriteLineData.HasMember("fileName")){
res.status = 200;
res.set_content("The fileName key for the json object was not found! Please recheck and send again.", "text/plain");
return ;
}
if(!WriteLineData.HasMember("content")){
res.status = 200;
res.set_content("The content key for the json object was not found! Please recheck and send again.", "text/plain");
return ;
}
Expand All @@ -353,38 +337,34 @@ int main() {
// 检测${fileName}文件是否存在,如果不存在,就创建一个新的文件,并写入内容
std::ifstream file(fileName);
if(!file) {
res.status = 200;
res.set_content("The target file does not exist.", "text/plain");
return ;
}
//打开指定文件开始写入数据
std::ofstream outFile(fileName, std::ios_base::app);
outFile << fileContent;
outFile.close();
res.status = 200;
res.set_content("success", "text/plain");
file.close();
});

//向目标文件覆盖内容
svr.Post("/OverwriteFile", [](const httplib::Request& req, httplib::Response& res) {
INFO("接收到覆写文件的请求!");
res.status = 200;
//解析字符串并创建一个json对象
rapidjson::Document OverwriteFileData;
OverwriteFileData.Parse(req.body.c_str());
//判断是否解析成功
if (OverwriteFileData.HasParseError()) {
res.status = 200;
res.set_content("Data parsing failed", "text/plain");
return ;
}
if(!OverwriteFileData.HasMember("fileName")){
res.status = 200;
res.set_content("The fileName key for the json object was not found! Please recheck and send again.", "text/plain");
return ;
}
if(!OverwriteFileData.HasMember("content")){
res.status = 200;
res.set_content("The content key for the json object was not found! Please recheck and send again.", "text/plain");
return ;
}
Expand All @@ -397,27 +377,24 @@ int main() {
// 检测${fileName}文件是否存在
std::ifstream file(fileName);
if(!file) {
res.status = 200;
res.set_content("The target file does not exist.", "text/plain");
return ;
}
//打开指定文件开始写入数据
std::ofstream outFile(fileName);
outFile << fileContent;
outFile.close();
res.status = 200;
res.set_content("success", "text/plain");
file.close();
});

//检测指定文件夹是否存在
svr.Post("/CheckDir", [](const httplib::Request& req, httplib::Response& res) {
res.status = 200;
std::filesystem::path p{req.body};
if (std::filesystem::exists(p)) {
res.status = 200;
res.set_content("true", "text/plain");
} else {
res.status = 200;
res.set_content("false", "text/plain");
}
});
Expand Down
Empty file.
8 changes: 5 additions & 3 deletions development_behavior_packs/NIA_V4.0_BP/scripts/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//这里是配置文件,你可以根据自己的需求更改相应的配置项目
//配置文件每一项的详细作用前往 https://docs.mcnia.top/zh-CN/deploy.html 查看
//当前配置文件版本号;v1.0.0

const config = {
"ServerMode": "Island",
Expand All @@ -16,7 +13,12 @@ const config = {
"CX": 402,
"CY": 100,
"CZ": 547
},
"HttpCfg": {
"IPAddress": "http://127.0.0.1",
"Port": 10086
}
}


export const cfg = config
18 changes: 1 addition & 17 deletions development_behavior_packs/NIA_V4.0_BP/scripts/customFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function AddScoreboard(scoreboardName,showName) {
export function GetTime() {
//创建一个Date对象
let nowTime = new Date()
//这里是或得北京时间
let addedTime = new Date(nowTime.getTime() + 28800000);
//获取年份
let year = addedTime.getFullYear ();
Expand Down Expand Up @@ -99,31 +100,14 @@ export function GetScore(scoreboardName,targets) {
}


// function getNumberInNormalDistribution(mean,std_dev){
// return mean+(uniform2NormalDistribution()*std_dev);
// }

// function uniform2NormalDistribution(){
// var sum=0.0;
// for(var i=0; i<12; i++){
// sum=sum+Math.random();
// }
// return sum-6.0;
// }

function randomNormalDistribution(){
var u=0.0, v=0.0, w=0.0, c=0.0;
do{
//获得两个(-1,1)的独立随机变量
u=Math.random()*2-1.0;
v=Math.random()*2-1.0;
w=u*u+v*v;
}while(w==0.0||w>=1.0)
//这里就是 Box-Muller转换
c=Math.sqrt((-2*Math.log(w))/w);
//返回2个标准正态分布的随机数,封装进一个数组返回
//当然,因为这个函数运行较快,也可以扔掉一个
//return [u*c,v*c];
return u*c;
}

Expand Down
Loading

0 comments on commit f342ccf

Please sign in to comment.