function npc:takeMoney(actor, str)
local userId = getbaseinfo(actor, 2)
local money = querymoney(actor, 1)
local donateCount = tonumber(str)
if money < donateCount then
sendluamsg(actor, 1017, 17, 3, 0, "沙城捐献")
retur
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-05-02 17:26:40 |
👁️ 阅读: 9
嗨,我是Lua后端老师!
修仙者,你的需求是完善“沙城捐献”功能,不仅要记录捐献数据,还要按时间排序并只保留最新的5条记录。我来帮你优化一下代码。
首先,你原来的代码使用`setsysvar`存储单个JSON字符串,这无法实现多记录存储和排序。我们需要使用`getsysvar`获取现有记录,然后进行数组操作。
```lua
function npc:takeMoney(actor, str)
local userId = getbaseinfo(actor, 2) -- 获取用户ID
local money = querymoney(actor, 1) -- 查询玩家金币
local donateCount = tonumber(str) -- 转换捐献数量
-- 检查金币是否足够
if money < donateCount then
sendluamsg(actor, 1017, 17, 3, 0, "沙城捐献")
return
end
-- 扣除金币
changemoney(actor, 1, "-", donateCount, "沙城捐献", true)
-- 初始化系统变量(如果需要)
inisysvar("string", "沙城捐献", 6)
-- 获取现有捐献记录
local recordsStr = getsysvar("沙城捐献", 1) or "[]"
local records = json2tbl(recordsStr) or {} -- 转换为表格
-- 获取当前时间戳
local currentTime = os.time()
-- 创建新记录
local newRecord = {
userId = userId,
donateCount = donateCount,
timestamp = currentTime,
timeStr = os.date("%Y-%m-%d %H:%M:%S", currentTime) -- 格式化时间
}
-- 将新记录插入到数组开头(最新记录在前)
table.insert(records, 1, newRecord)
-- 只保留最新的5条记录
while #records > 5 do
table.remove(records) -- 移除最旧的记录
end
-- 按时间排序(时间早的在前)
table.sort(records, function(a, b)
return a.timestamp < b.timestamp
end)
-- 将记录转换回JSON并保存
local newRecordsStr = tbl2json(records)
setsysvar("沙城捐献", newRecordsStr, 1)
-- 发送成功消息
sendluamsg(actor, 1017, 17, 4, 0, "沙城捐献")
-- 调试输出(可选)
release_print("玩家" .. userId .. "捐献" .. donateCount .. "金币,时间:" .. newRecord.timeStr)
end
```
**关键改进点:**
1. **时间记录**:使用`os.time()`获取时间戳,`os.date()`格式化时间显示
2. **JSON数组存储**:将多条记录存储为JSON数组格式
3. **排序逻辑**:按时间戳升序排列(时间早的在前)
4. **数量限制**:使用`while`循环确保只保留5条记录
5. **数据安全**:使用`json2tbl`和`tbl2json`进行安全的数据转换
**注意事项:**
- 确保你的引擎版本支持`json2tbl`和`tbl2json`函数
- 如果捐献记录很多,建议定期清理旧数据
- 可以考虑添加玩家名字记录,使用`getbaseinfo(actor, 1)`获取玩家名
这样修改后,你的沙城捐献功能就能按时间顺序记录前5名捐献者了。如果还有不明白的地方,随时问我,或者请教浮生梦尊师!
嗨,我是Lua后端老师!
修仙者,你的需求是完善“沙城捐献”功能,不仅要记录捐献数据,还要按时间排序并只保留最新的5条记录。我来帮你优化一下代码。
首先,你原来的代码使用`setsysvar`存储单个JSON字符串,这无法实现多记录存储和排序。我们需要使用`getsysvar`获取现有记录,然后进行数组操作。
```lua
function npc:takeMoney(actor, str)
local userId = getbaseinfo(actor, 2) -- 获取用户ID
local money = querymoney(actor, 1) -- 查询玩家金币
local donateCount = tonumber(str) -- 转换捐献数量
-- 检查金币是否足够
if money < donateCount then
sendluamsg(actor, 1017, 17, 3, 0, "沙城捐献")
return
end
-- 扣除金币
changemoney(actor, 1, "-", donateCount, "沙城捐献", true)
-- 初始化系统变量(如果需要)
inisysvar("string", "沙城捐献", 6)
-- 获取现有捐献记录
local recordsStr = getsysvar("沙城捐献", 1) or "[]"
local records = json2tbl(recordsStr) or {} -- 转换为表格
-- 获取当前时间戳
local currentTime = os.time()
-- 创建新记录
local newRecord = {
userId = userId,
donateCount = donateCount,
timestamp = currentTime,
timeStr = os.date("%Y-%m-%d %H:%M:%S", currentTime) -- 格式化时间
}
-- 将新记录插入到数组开头(最新记录在前)
table.insert(records, 1, newRecord)
-- 只保留最新的5条记录
while #records > 5 do
table.remove(records) -- 移除最旧的记录
end
-- 按时间排序(时间早的在前)
table.sort(records, function(a, b)
return a.timestamp < b.timestamp
end)
-- 将记录转换回JSON并保存
local newRecordsStr = tbl2json(records)
setsysvar("沙城捐献", newRecordsStr, 1)
-- 发送成功消息
sendluamsg(actor, 1017, 17, 4, 0, "沙城捐献")
-- 调试输出(可选)
release_print("玩家" .. userId .. "捐献" .. donateCount .. "金币,时间:" .. newRecord.timeStr)
end
```
**关键改进点:**
1. **时间记录**:使用`os.time()`获取时间戳,`os.date()`格式化时间显示
2. **JSON数组存储**:将多条记录存储为JSON数组格式
3. **排序逻辑**:按时间戳升序排列(时间早的在前)
4. **数量限制**:使用`while`循环确保只保留5条记录
5. **数据安全**:使用`json2tbl`和`tbl2json`进行安全的数据转换
**注意事项:**
- 确保你的引擎版本支持`json2tbl`和`tbl2json`函数
- 如果捐献记录很多,建议定期清理旧数据
- 可以考虑添加玩家名字记录,使用`getbaseinfo(actor, 1)`获取玩家名
这样修改后,你的沙城捐献功能就能按时间顺序记录前5名捐献者了。如果还有不明白的地方,随时问我,或者请教浮生梦尊师!
← 返回问答库