local deviceDetect = {}
-- 存储玩家设备信息
local playerDevices = {}
-- 接收前端发来的设备信息
function deviceDetect.onDeviceInfo(actor, msgid, arg1, arg2, arg3, sMsg)
local msgData = json.decode(sMsg) or {}
local isMobile = msgData.is_mobile or false
loc
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-03-15 13:15:20 |
👁️ 阅读: 6
我是lua后端老师!
修仙者,你的代码看起来是客户端写法,在后端无法使用。根据参考资料,后端监听消息的正确方式是在 `QFunction-0.lua` 中使用 `handlerequest` 函数。我来帮你修改成完整的后端可用代码:
```lua
-- 设备检测模块
local deviceDetect = {}
-- 存储玩家设备信息
local playerDevices = {}
--- 接收前端发来的设备信息
--- @param actor string 玩家对象
--- @param msgid integer 消息ID
--- @param param1 integer 参数1
--- @param param2 integer 参数2
--- @param param3 integer 参数3
--- @param sMsg string 消息内容
function deviceDetect.onDeviceInfo(actor, msgid, param1, param2, param3, sMsg)
-- 解析JSON消息
local msgData = json2tbl(sMsg) or {}
local isMobile = msgData.is_mobile or false
local deviceType = msgData.device_type or 1
release_print("收到玩家设备信息 - 玩家:", actor,
", 设备类型:", deviceType,
", 是否是手机:", tostring(isMobile))
-- 存储玩家设备信息
playerDevices[actor] = {
is_mobile = isMobile,
device_type = deviceType,
timestamp = os.time()
}
-- 设置玩家变量以便QF使用
if isMobile then
setplaydef(actor, "IS_MOBILE", 1)
-- 如果是手机端,添加打开人物装备按钮
addbutton(actor, 101, 888, "<Button|a=0|x=10|y=10|nimg=custom/your_icon.png|pimg=custom/your_icon_pressed.png|link=@OpenRolePanel>")
release_print("为手机用户 ", actor, " 添加了人物装备按钮")
else
setplaydef(actor, "IS_MOBILE", 0)
end
-- 设置详细的设备类型变量
setplaydef(actor, "DEVICE_TYPE", deviceType)
return true
end
--- 检查是否是手机玩家
--- @param actor string 玩家对象
--- @return boolean 是否是手机玩家
function deviceDetect.isMobilePlayer(actor)
-- 先检查玩家变量
local mobileVar = getplaydef(actor, "IS_MOBILE") or 0
if mobileVar == 1 then
return true
end
-- 再检查内存中的信息
if playerDevices[actor] then
return playerDevices[actor].is_mobile
end
return false
end
--- 获取玩家设备类型
--- @param actor string 玩家对象
--- @return integer 设备类型
function deviceDetect.getDeviceType(actor)
-- 先检查玩家变量
local deviceTypeVar = getplaydef(actor, "DEVICE_TYPE") or 1
if deviceTypeVar ~= 1 then
return deviceTypeVar
end
-- 再检查内存中的信息
if playerDevices[actor] then
return playerDevices[actor].device_type
end
return 1
end
--- 为QF提供检查函数
--- @param actor string 玩家对象
--- @return boolean 是否是手机玩家
function CheckIsMobile(actor)
return deviceDetect.isMobilePlayer(actor)
end
-- 在QFunction-0.lua中需要这样注册消息处理:
-- function handlerequest(actor, msgid, param1, param2, param3, sMsg)
-- if (msgid == 58222) then
-- return deviceDetect.onDeviceInfo(actor, msgid, param1, param2, param3, sMsg)
-- end
-- end
return deviceDetect
```
主要修改点:
1. 将 `json.decode` 改为 `json2tbl`(后端专用函数)
2. 将 `print` 改为 `release_print`(后端专用打印)
3. 移除了 `registerNetMsg`,改为在 `QFunction-0.lua` 的 `handlerequest` 函数中处理
4. 添加了完整的函数注释
使用说明:
1. 将上面的代码保存为 `deviceDetect.lua`
2. 在需要使用的脚本中 `require("deviceDetect")`
3. 在 `QFunction-0.lua` 的 `handlerequest` 函数中添加消息处理逻辑
这样修改后就能在后端正常使用了。如果还有问题,随时可以问我或者请教我的师傅浮生梦尊师!
我是lua后端老师!
修仙者,你的代码看起来是客户端写法,在后端无法使用。根据参考资料,后端监听消息的正确方式是在 `QFunction-0.lua` 中使用 `handlerequest` 函数。我来帮你修改成完整的后端可用代码:
```lua
-- 设备检测模块
local deviceDetect = {}
-- 存储玩家设备信息
local playerDevices = {}
--- 接收前端发来的设备信息
--- @param actor string 玩家对象
--- @param msgid integer 消息ID
--- @param param1 integer 参数1
--- @param param2 integer 参数2
--- @param param3 integer 参数3
--- @param sMsg string 消息内容
function deviceDetect.onDeviceInfo(actor, msgid, param1, param2, param3, sMsg)
-- 解析JSON消息
local msgData = json2tbl(sMsg) or {}
local isMobile = msgData.is_mobile or false
local deviceType = msgData.device_type or 1
release_print("收到玩家设备信息 - 玩家:", actor,
", 设备类型:", deviceType,
", 是否是手机:", tostring(isMobile))
-- 存储玩家设备信息
playerDevices[actor] = {
is_mobile = isMobile,
device_type = deviceType,
timestamp = os.time()
}
-- 设置玩家变量以便QF使用
if isMobile then
setplaydef(actor, "IS_MOBILE", 1)
-- 如果是手机端,添加打开人物装备按钮
addbutton(actor, 101, 888, "<Button|a=0|x=10|y=10|nimg=custom/your_icon.png|pimg=custom/your_icon_pressed.png|link=@OpenRolePanel>")
release_print("为手机用户 ", actor, " 添加了人物装备按钮")
else
setplaydef(actor, "IS_MOBILE", 0)
end
-- 设置详细的设备类型变量
setplaydef(actor, "DEVICE_TYPE", deviceType)
return true
end
--- 检查是否是手机玩家
--- @param actor string 玩家对象
--- @return boolean 是否是手机玩家
function deviceDetect.isMobilePlayer(actor)
-- 先检查玩家变量
local mobileVar = getplaydef(actor, "IS_MOBILE") or 0
if mobileVar == 1 then
return true
end
-- 再检查内存中的信息
if playerDevices[actor] then
return playerDevices[actor].is_mobile
end
return false
end
--- 获取玩家设备类型
--- @param actor string 玩家对象
--- @return integer 设备类型
function deviceDetect.getDeviceType(actor)
-- 先检查玩家变量
local deviceTypeVar = getplaydef(actor, "DEVICE_TYPE") or 1
if deviceTypeVar ~= 1 then
return deviceTypeVar
end
-- 再检查内存中的信息
if playerDevices[actor] then
return playerDevices[actor].device_type
end
return 1
end
--- 为QF提供检查函数
--- @param actor string 玩家对象
--- @return boolean 是否是手机玩家
function CheckIsMobile(actor)
return deviceDetect.isMobilePlayer(actor)
end
-- 在QFunction-0.lua中需要这样注册消息处理:
-- function handlerequest(actor, msgid, param1, param2, param3, sMsg)
-- if (msgid == 58222) then
-- return deviceDetect.onDeviceInfo(actor, msgid, param1, param2, param3, sMsg)
-- end
-- end
return deviceDetect
```
主要修改点:
1. 将 `json.decode` 改为 `json2tbl`(后端专用函数)
2. 将 `print` 改为 `release_print`(后端专用打印)
3. 移除了 `registerNetMsg`,改为在 `QFunction-0.lua` 的 `handlerequest` 函数中处理
4. 添加了完整的函数注释
使用说明:
1. 将上面的代码保存为 `deviceDetect.lua`
2. 在需要使用的脚本中 `require("deviceDetect")`
3. 在 `QFunction-0.lua` 的 `handlerequest` 函数中添加消息处理逻辑
这样修改后就能在后端正常使用了。如果还有问题,随时可以问我或者请教我的师傅浮生梦尊师!
← 返回问答库