主要修改了计数阈值(原本是>7,现在可以自己修改,默认>6,变量名为COUNT_LIMIT
)
------------------------------------------#region
--- @author 简律纯
--- @date 2022-12-16
--- @changelog:
--- #2022-12-16, 实现 cc 指令,可以cc [属性] 或 [数字]。
--- #2023-10-16,实现多轮掷骰指令, 方法: .ccX#y
--- #2023-10-18, 末加值.cb指令
--- @description 一个lua实现的ww无限掷骰指令
------------------------------------------#endregion
msg_order = {
[".cc"] = "cc",
[".cb"] = "cb"
}
COUNT_LIMIT = 7
math.randomseed(os.time()) -- 设置随机数种子
function roll_dice(num)
local num = tonumber(num)
if num == 0 then num = 1 end
local dice_num = num
local outputs = {}
local count = 0
local ww_count = 0
while true do
local current_output = {}
local current_count = 0
local current_ww_count = 0
for i = 1, num do
local dice = math.random(1, 10) -- 掷骰子,随机数范围为 1-10
if dice >= COUNT_LIMIT then
if dice == 10 then current_ww_count = current_ww_count + 1 end
current_count = current_count + 1
count = count + 1
dice = '(' .. dice .. ')'
end
table.insert(current_output, dice) -- 将点数存入本次掷骰结果数组中
end
local output = "{" .. table.concat(current_output, ",") .. "}"
table.insert(outputs, output)
if current_ww_count == 0 then -- 如果本次没有加骰,则结束掷骰操作
break
else -- 否则,将加骰的情况记下来,重新进行掷骰
-- table.insert(outputs, "加骰:" .. ww_count .. " 个等于 10 的骰子")
--count = count + current_count
num = current_ww_count
end
end
-- 统一拼接输出结果
local result = table.concat(outputs, "+")
result = result .. "=" .. count
-- return "{nick}掷骰: " .. dice_num .. "a10=" .. result
return dice_num, result, count
end
function parseTable(tableStr)
local result = {}
for _, v in ipairs(tableStr) do
if tonumber(v) ~= nil then -- 数字
table.insert(result, tonumber(v))
else -- 文本
local attrValue = getPlayerCardAttr(msg.fromQQ, msg.fromGroup,
v:gsub("+", ""):gsub("-", ""):gsub("%d+", ""))
if attrValue == nil then -- 未找到属性,尝试寻找数字
local numValue = v:match("%d+")
if numValue == nil then -- 默认值为0
table.insert(result, 1)
else
table.insert(result, tonumber(numValue))
end
else -- 找到属性值
if v:match("%-") then
table.insert(result, -attrValue)
else
table.insert(result, attrValue)
end
end
end
end
return result
end
function cc(msg)
local isSuccess, result = pcall(function()
local str = msg.fromMsg:match("^.cc(.*)")
local result = {}
if str:match("#") then
-- 多轮掷骰
local single_result
local count = tonumber(str:match("(%d+)#.*"))
local rest_str = str:match(".*#(.*)")
for i = 1, count do
local times = {}
for s in rest_str:gmatch("[%+%-]?[%w\128-\255]+") do
table.insert(times, s)
end
_, _, single_result = roll_dice(load("return " .. table.concat(parseTable(times), "+"))())
table.insert(result, single_result)
end
return string.format("{nick}掷骰%s次={%s}", count, table.concat(result, ","))
else
local dice_num, results
for s in str:gmatch("[%+%-]?[%w\128-\255]+") do
table.insert(result, s)
end
dice_num, results, _ = roll_dice(load("return " .. table.concat(parseTable(result), "+"))())
return string.format("{nick}掷骰: %sa10=%s", dice_num, results)
end
end)
if isSuccess then
return result
else
return [[.cc(X[=轮数]#y[=属性值|数字])
// 可以使用+-表达式: .cc力量+3 (相当于.cc(力量+3))
// 多轮掷骰: .cc3#STR]]
end
end
function cb(msg)
local isSuccess, result = pcall(function()
local str = msg.fromMsg:match("^.cb(.*)")
local matched = str:match("([%+%-].*)$") or nil
local result_str = str:gsub("([%+%-].*)$", "")
local modified
local result = {}
local matches = {}
if matched == nil then
matched = "-0"
end
if result_str:match("#") then
-- 多轮掷骰
local single_result
local result = {}
local _result = {}
local count = tonumber(str:match("(%d+)#.*"))
local rest_str = str:match(".*#(.*)")
for i = 1, count do
local times = {}
for s in rest_str:gmatch("[%+%-]?[%w\128-\255]+") do
table.insert(times, s)
end
_, _, single_result = roll_dice(load("return " .. table.concat(parseTable(times), "+"))())
table.insert(result, single_result)
table.insert(_result, single_result)
end
for s in matched:gmatch("[%+%-]?[%w\128-\255]+") do
table.insert(matches, s)
end
modifi = tonumber(load("return " .. table.concat(parseTable(matches), "+"))())
-- 遍历 _result,将每个值都与 modifi 相加
for i = 1, #_result do
_result[i] = _result[i] + modifi
end
for i = 1, #result do
if result[i] <= 0 then result[i] = 0 end
end
for i = 1, #_result do
if _result[i] <= 0 then _result[i] = 0 end
end
-- msg:echo(string.format("result: {%s}, _result: {%s}", result, _result))
return string.format("{nick}掷骰%s次={%s}+%s={%s}", count, table.concat(result, ","), modifi,
table.concat(_result, ","))
else
for s in result_str:gmatch("[%+%-]?[%w\128-\255]+") do
table.insert(result, s)
end
dice_num, results, count = roll_dice(load("return " .. table.concat(parseTable(result), "+"))())
for s in matched:gmatch("[%+%-]?[%w\128-\255]+") do
table.insert(matches, s)
end
modifi = tonumber(load("return " .. table.concat(parseTable(matches), "+"))())
if modifi >= 0 then modifi = "+" .. modifi end
x = tonumber(count) + tonumber(modifi)
if x <= 0 then x = 0 end
return string.format("{nick}掷骰: %sa10=%s%s=%s", dice_num, results, modifi, x)
end
end)
if isSuccess then
return result
else
return [[.cb(X[=轮数]#y[=属性值|数字])
// 可以使用+-表达式: .cb力量+3 (相当于.cc(力量)+3)
// 多轮掷骰: .cb3#STR]]
end
end