diff --git a/README.md b/README.md index c072bea..51502bd 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ The syntax is this: The `` bit is optional (default 1), the `d` letter is optional too if `times` = 1 and `bonus` = 0. Example: + /roll 1d4 /roll 2d5-2 /roll d20+3 @@ -22,6 +23,6 @@ This module uses Mersenne-Twister random-number library for better random number You can get the newest library version [here.](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html) -The library uses BSD license so it's included here (for Lua 5.2). Just compile the `random.so` and put it in plugin directory. +The library uses BSD license so it's included here (for Lua 5.2). Just compile the `random.so` and put it in `/usr/lib/prosody` or `/usr/lib/lua/5.1` directory. Then, drop the module to Prosody dir (usually `/usr/lib/prosody/modules`) and turn it on in your config. diff --git a/mod_muc_rpg.lua b/mod_muc_rpg.lua index d4f85c7..cbe47e4 100644 --- a/mod_muc_rpg.lua +++ b/mod_muc_rpg.lua @@ -3,10 +3,9 @@ -- based on mod_muc_intercom by Kim Alvefur local host_session = prosody.hosts[module.host]; -local st_msg = require "util.stanza".message; local jid = require "util.jid"; -local random=require"random" -r=random.new(os.time()) +--local random = require"random" +--r=random.new(os.time()) local function get_room_by_jid(mod_muc, jid) if mod_muc.get_room_by_jid then @@ -16,13 +15,13 @@ local function get_room_by_jid(mod_muc, jid) end end -function roll (times, sides, bonus) +local function roll (times, sides, bonus) local result = {} result.dice = {} bonus = bonus or 0 for i = 1, times do - result.dice[#result.dice + 1] = r(sides) + result.dice[#result.dice + 1] = math.random(sides) end result.bonus = bonus @@ -30,7 +29,7 @@ function roll (times, sides, bonus) return result end -function parseroll(message) +local function parseroll(message) local result = {} result.times = 1 result.sides = 20 @@ -60,29 +59,35 @@ function parseroll(message) end end -function check_message(data) - local origin, stanza = data.origin, data.stanza; - local mod_muc = host_session.muc; +local function check_message(data) + local stanza = data.stanza; + local body = stanza:get_child("body"); + + if not body then return; end -- No body, like topic changes + if not (stanza.name == "message" and tostring(stanza.attr.type) == "groupchat") then return; end if not mod_muc then return; end - local this_room = get_room_by_jid(mod_muc, stanza.attr.to); - if not this_room then return; end -- no such room - - local from_room_jid = this_room._jid_nick[stanza.attr.from]; - if not from_room_jid then return; end -- no such nick - - local from_room, from_host, from_nick = jid.split(from_room_jid); - - local body = stanza:get_child("body"); - if not body then return; end -- No body, like topic changes + local mod_muc = host_session.muc; + data.stanza.body = "hello" body = body and body:get_text(); if not string.match(body, '^%s*/roll%s') then return; end -- No command local message = body:match("^@([^:]+):(.*)"); if not message then return; end - local forward_stanza = st_msg({from = sender, to = this_room, type = "groupchat"}, message); + stanza.body = body .. " rolled: " + local result = roll(parseroll(message)) - this_room:broadcast_message(forward_stanza); + local total = 0 + for i = 1, #result.dice do + if i == 1 then + stanza.body = stanza.body .. result.dice[i] + else + stanza.body = stanza.body .. "," .. result.dice[i] + end + total = total + result.dice[i] + end + total = total + result.bonus + data.stanza.body = stanza.body .. ", total: " .. total end module:hook("message/bare", check_message, 10); diff --git a/random/Makefile b/random/Makefile index 6b41121..64c21ba 100644 --- a/random/Makefile +++ b/random/Makefile @@ -8,8 +8,8 @@ # these will probably work if Lua has been installed globally LUA= /usr -LUAINC= $(LUA)/include -LUALIB= $(LUA)/lib +LUAINC= $(LUA)/include/lua5.1 +LUALIB= $(LUA)/lib/lua5.1 LUABIN= $(LUA)/bin # probably no need to change anything below here