Skip to content

Commit

Permalink
[+] Http模块,具体参考docs.lua 及 http模块
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralu committed Apr 7, 2023
1 parent b92a03b commit 034495f
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 1 deletion.
1 change: 1 addition & 0 deletions ModuleConfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ loadModule('adminDamage')
--loadModule('noBattleInjury')
--loadModule('summonDemo')
loadModule('setupMagicAttr')
loadModule('http')
--loadModule('itembox') --黑白宝箱概率调整lua
--loadModule('petBPExtend')
--useModule('Welcome2')
Expand Down
115 changes: 115 additions & 0 deletions Modules/http.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---http模块
local Module = ModuleBase:createModule('http')

---@alias ParamType {string:string}
---@alias HttpApiFn {string:fun(params:ParamType, body:string):string}
---@alias HttpMethods 'get'|'post'|'put'|'delete'|'patch'

--- 加载模块钩子
function Module:onLoad()
self:logInfo('load')
local status = Http.GetStatus();
self:logInfo('Http.GetStatus', status);

if status == 0 then
Http.Init();
self:logInfo('Http.Init');
Http.AddMountPoint("/", "./lua/www/")
self:logInfo('Http.AddMountPoint');
status = 1;
end
if status == 1 then
Http.Start("0.0.0.0", 10086);
self:logInfo('Http.Start');
end
self._Apis = {} --[[@type {string: HttpApiFn}]];
self:regCallback('HttpRequestEvent', Func.bind(self.onHttpRequest, self));
self:regApi('post', "register", Func.bind(self.ApiRegister, self));
self:regApi('post', "doLua", Func.bind(self.doLua, self));
self:regApi('post', "reloadModule", Func.bind(self.reloadModule, self));
end

---http://127.0.0.1:10086/api/doLua
---@param params ParamType
---@param body string
---@return string
function Module:doLua(params, body)
self:logInfo("doLua", params['lua']);
local r, ret = pcall(dofile, params['lua']);
self:logDebug('result', r, ret);
return "true"
end

---http://127.0.0.1:10086/api/reloadModule
---@param params ParamType
---@param body string
---@return string
function Module:reloadModule(params, body)
self:logInfo("reloadModule", params['module']);
reloadModule(params['module']);
return "true"
end

---注册新用户 http://127.0.0.1:10086/api/register
---@param params ParamType
---@param body string
---@return string
function Module:ApiRegister(params, body)
local b, ret = pcall(JSON.decode, body);
if b ~= true or ret == nil then
return "false";
end
local account = ret.account;
local password = ret.password;
if (account or '') == '' or (password or '') == '' then
return "false";
end
self:logInfo("Register", account, password);
local user = SQL.QueryEx('select CdKey from tbl_user where CdKey = ?', account);
if #user.rows == 0 then
local seq = SQL.QueryEx('select max(SequenceNumber) + 1 as Max from tbl_user');
local sql = 'insert into tbl_user (CdKey, SequenceNumber, AccountID, AccountPassWord, '
.. ' EnableFlg, UseFlg, BadMsg, TrialFlg, DownFlg, ExpFlg) values ('
.. SQL.sqlValue(account) .. ', ' .. SQL.sqlValue(seq.rows[1].Max) .. ', '
.. SQL.sqlValue(account) .. ', '
.. SQL.sqlValue(password) .. ',1,1,0,8,0,0);'
local r = SQL.QueryEx(sql);
if r.effectRows == 1 then
return "true"
end
--print(r, sql);
end

return "false";
end

---http请求回调
---@param method string
---@param api string API名字
---@param params ParamType 参数
---@param body string body内容
---@return string body 返回内容
function Module:onHttpRequest(method, api, params, body)
if self._Apis[string.lower(method .. api)] then
self:logInfo(string.lower(method .. api), self._Apis[string.lower(method .. api)]);
return self._Apis[string.lower(method .. api)](params, body);
end
return "";
end

---@param method HttpMethods
---@param api string 对应http://127.0.0.1:10086/api/******
---@param fn HttpApiFn
function Module:regApi(method, api, fn)
self._Apis[string.lower(method .. api)] = fn;
end

--- 卸载模块钩子
function Module:onUnload()
self:logInfo('unload')
if Http.GetStatus() == 2 then
Http.Stop();
end
end

return Module;
40 changes: 40 additions & 0 deletions docs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,11 @@ function NL.RegItemTribeRateEvent(dofile, callback) end
---@return number 返回新的克制比率
function NL.ItemTribeRateEventCallback(a, b, rate) end

---Http请求事件
---@param callback string callback回调参数
---@see Http.HttpRequestEventCallback
function NL.RegHttpRequestEvent(dofile, callback) end

---@param sql string sql
---@vararg string|number 绑定参数,最多40
---@return {status:number, effectRows:number, rows: table} 返回查询内容
Expand Down Expand Up @@ -934,3 +939,38 @@ function Map.GetDungeonId(floor) end
---@param dungeonId integer dungeonId
---@return number mapType, number floor, number x, number y
function Map.FindDungeonEntry(dungeonId) end

Http = _G.Http or {}

---初始化Http服务器
function Http.Init() end

---开启Http服务
---@param addr string 监听IP,例如: "0.0.0.0"
---@param port integer 端口 建议10000以上
---@return integer ret @1:成功,其他为失败
function Http.Start(addr, port) end

---关闭Http服务器,需要注意,在请求中停止会导致请求响应502并且强制关闭所有未处理的请求
function Http.Stop() end

---获取Http服务器状态
---@return 0|1|2 status @0=未初始化 1=未启动 2=运行中
function Http.GetStatus() end

---绑定静态资源
---@param path string url地址
---@param dir string 本地目录
function Http.AddMountPoint(path, dir) end

---移除静态资源
---@param path string url地址
function Http.RemoveMountPoint(path) end

---http请求回调
---@param method string
---@param api string API名字
---@param params {string:string} 参数
---@param body string body内容
---@return string body 返回内容
function Http.HttpRequestEventCallback(method, api, params, body) end
2 changes: 1 addition & 1 deletion libs/Gmsv/BATTLE_PVE_PATCH.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local Pve = Battle.PVE
---@param DoFunc string
---@param EnemyIdAr number[]
---@param BaseLevelAr number[]
---@param RandLv number[]
---@param RandLv? number[]
Battle.PVE = function(CharIndex, CreatePtr, DoFunc, EnemyIdAr, BaseLevelAr, RandLv)
if #EnemyIdAr < 1 then
return -1;
Expand Down
1 change: 1 addition & 0 deletions libs/Gmsv/NL.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ NL.newEvent('CheckDummyDollEvent', 1);
-- function PreItemPickUpEventCallBack(CharIndex, ItemIndex) end
NL.newEvent('PreItemPickUpEvent', 0);
NL.newEvent('ItemBoxGenerateEvent', nil);
NL.newEvent('HttpRequestEvent', "");
--local hookOnCharaDeleted;
--hookOnCharaDeleted = ffi.hook.new('int (__cdecl*)(int a1)', function(queueIndex)
-- local queuePtr = Addresses.DBQueue + 0x58 * queueIndex
Expand Down
11 changes: 11 additions & 0 deletions types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ NPCPart = NPCPart or {};
---@return CharIndex
function NPCPart:NPC_createNormal(name, image, positionInfo) end

JSON = JSON or {};

---@generic T
---@param s string
---@return T
function JSON.decode(s) end

---@generic T
---@param s T
---@return string
function JSON.encode(s) end
49 changes: 49 additions & 0 deletions www/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>X</title>
<script src="http://cdn.staticfile.org/jquery/2.0.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@1.*/css/pico.min.css">
</head>

<body>
<div class="container">
<h1>注册账号</h1>
<form>
<div class="grid">
<label for="account">
账号
<input type="text" id="account" name="account" placeholder="账号" required>
</label>
<label for="password">
密码
<input type="text" id="password" name="password" placeholder="密码" required>
</label>
</div>
<button type="button" id="btnRegister" role="button">注册</button>
</form>
</div>
<script>
$('#btnRegister').on('click', function () {
var account = $('#account').val();
var password = $('#password').val();
$.ajax({
type: "POST",
url: "/api/register",
processData: false,
contentType: 'application/json',
data: JSON.stringify({ account: account, password: password }),
success: function (r) {
}
}).then(function (data) {
alert(data == 'true' ? '注册成功' : "注册失败");
})
});
</script>
</body>

</html>

0 comments on commit 034495f

Please sign in to comment.