This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/apps/scim/lua/dice.lua

96 lines
1.9 KiB
Lua
Raw Normal View History

2019-02-27 23:29:49 -06:00
local os = require "os"
local math = require "math"
math.randomseed(os.clock())
function table.tostring(self, indent, done)
local result = ""
done = done or {}
indent = indent or 0
if type(self) == "table" then
for key, value in pairs(self) do
result = result .. string.rep(" ", indent)
if type(value) == "table" and not done[value] then
done[value] = true
result = result ..
string.format(
"[%s] => table\n",
tostring(key)
) ..
"{\n" ..
table.tostring(value, indent + 2, done) ..
"}\n"
else
result = result ..
string.format("[%s] => %s\n",
tostring(key),
tostring(value)
)
end
end
else
result = result .. self .. "\n"
end
return result
end
function log(s)
s = "[timestamp: " .. tostring(os.time()) .. "]\n" .. s
local out = s:gsub("\n", "\n ")
local file = io.open("/tmp/sc-im-dice.log", "a+")
file:write("\n")
file:write(out)
io.close(file)
end
local dstr_matcher = "(%d*)d(%d+)"
function dstr(n, s, fl)
n = tonumber(n) or 1
local sum = 0
for di = 1, n do
sum = sum + math.random(1, tonumber(s))
end
return sum
end
function rolls(s)
local pieces = {}
for nd, ds in s:gmatch(dstr_matcher) do
local sum = dstr(tonumber(nd), tonumber(ds))
pieces[#pieces+1] = {
tostring(nd) .. "d" .. tostring(ds),
tostring(sum)
}
end
return pieces
end
function replacerolls(s, rolls)
local result = s
for k, n in ipairs(rolls) do
result = result:gsub(n[1], n[2], 1)
end
return result
end
function evalreturn(s)
local f = loadstring("return " .. s)
return f()
end
2019-02-27 23:37:09 -06:00
function sc_parserollstr(s)
local r = rolls(s)
log(table.tostring(r))
return evalreturn(replacerolls(s, r))
2019-02-27 23:29:49 -06:00
end
function sc_rollres(c, r)
2019-02-27 23:37:09 -06:00
local os = require "os"
local seed = (os.time() + os.clock()) * 10000000
math.randomseed(seed)
log("Setting Seed: " .. tostring(seed))
local sum = sc_parserollstr(sc.lgetstr(c, r))
2019-03-28 17:00:56 -05:00
sc.lsetnum(c, r, sum)
2019-02-27 23:29:49 -06:00
end