Only sync once every second. Also:
Fixed some indentations
This commit is contained in:
parent
5c04e733f5
commit
e1d58c0aef
106
init.lua
106
init.lua
@ -7,27 +7,27 @@ local modpath = minetest.get_modpath(minetest.get_current_modname())
|
|||||||
local ie, req_ie = _G, minetest.request_insecure_environment
|
local ie, req_ie = _G, minetest.request_insecure_environment
|
||||||
if req_ie then ie = req_ie() end
|
if req_ie then ie = req_ie() end
|
||||||
if not ie then
|
if not ie then
|
||||||
error("The Matrix mod requires access to insecure functions in order "..
|
error("The Matrix mod requires access to insecure functions in order "..
|
||||||
"to work. Please add the matrix mod to your secure.trusted_mods "..
|
"to work. Please add the matrix mod to your secure.trusted_mods "..
|
||||||
"setting or disable the matrix mod.")
|
"setting or disable the matrix mod.")
|
||||||
end
|
end
|
||||||
|
|
||||||
ie.package.path =
|
ie.package.path =
|
||||||
modpath.."/lua-matrix/?.lua;"
|
modpath.."/lua-matrix/?.lua;"
|
||||||
..ie.package.path
|
..ie.package.path
|
||||||
|
|
||||||
matrix = {
|
matrix = {
|
||||||
version = "0.0.1",
|
version = "0.0.1",
|
||||||
joined_players = {},
|
joined_players = {},
|
||||||
connected = false,
|
connected = false,
|
||||||
modpath = modpath,
|
modpath = modpath,
|
||||||
lib = lib,
|
lib = lib,
|
||||||
}
|
}
|
||||||
|
|
||||||
dofile(modpath.."/config.lua")
|
dofile(modpath.."/config.lua")
|
||||||
|
|
||||||
local function eprintf(fmt, ...)
|
local function eprintf(fmt, ...)
|
||||||
minetest.log("info", fmt:format(...))
|
minetest.log("info", fmt:format(...))
|
||||||
end
|
end
|
||||||
|
|
||||||
local client = require("matrix").client("https://"..matrix.config.server..":"..matrix.config.port)
|
local client = require("matrix").client("https://"..matrix.config.server..":"..matrix.config.port)
|
||||||
@ -41,11 +41,11 @@ client:hook("invite", function (client, room)
|
|||||||
client:join_room(room)
|
client:join_room(room)
|
||||||
end
|
end
|
||||||
end):hook("logged-in", function (client)
|
end):hook("logged-in", function (client)
|
||||||
matrix.connected = true
|
matrix.connected = true
|
||||||
eprintf("Logged in successfully\n")
|
eprintf("Logged in successfully\n")
|
||||||
end):hook("logged-out", function (client)
|
end):hook("logged-out", function (client)
|
||||||
eprintf("Logged out... bye!\n")
|
eprintf("Logged out... bye!\n")
|
||||||
matrix.connected = false
|
matrix.connected = false
|
||||||
end):hook("left", function (client, room)
|
end):hook("left", function (client, room)
|
||||||
eprintf("Left room %s, active rooms:\n", room)
|
eprintf("Left room %s, active rooms:\n", room)
|
||||||
for room_id, room in pairs(client.rooms) do
|
for room_id, room in pairs(client.rooms) do
|
||||||
@ -61,7 +61,7 @@ end):hook("joined", function (client, room)
|
|||||||
|
|
||||||
--room:send_text("Type “!bot quit” to make the bot exit")
|
--room:send_text("Type “!bot quit” to make the bot exit")
|
||||||
|
|
||||||
room:hook("message", function (room, sender, message, event)
|
room:hook("message", function (room, sender, message, event)
|
||||||
if event.origin_server_ts < start_ts then
|
if event.origin_server_ts < start_ts then
|
||||||
eprintf("%s: (Skipping message sent before bot startup)\n", room)
|
eprintf("%s: (Skipping message sent before bot startup)\n", room)
|
||||||
return
|
return
|
||||||
@ -77,9 +77,9 @@ end):hook("joined", function (client, room)
|
|||||||
|
|
||||||
eprintf("%s: <%s> %s\n", room, sender, message.body)
|
eprintf("%s: <%s> %s\n", room, sender, message.body)
|
||||||
|
|
||||||
if message.body == "!bot quit" then
|
if message.body == "!bot quit" then
|
||||||
for _, room in pairs(client.rooms) do
|
for _, room in pairs(client.rooms) do
|
||||||
room:send_text("(gracefully shutting down)")
|
room:send_text("(gracefully shutting down)")
|
||||||
end
|
end
|
||||||
client:logout()
|
client:logout()
|
||||||
matrix.connected = false
|
matrix.connected = false
|
||||||
@ -92,52 +92,54 @@ end)
|
|||||||
|
|
||||||
dofile(modpath.."/callback.lua")
|
dofile(modpath.."/callback.lua")
|
||||||
|
|
||||||
local stepnum = 0
|
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime) return matrix.step(dtime) end)
|
minetest.register_globalstep(function(dtime) return matrix.step(dtime) end)
|
||||||
|
|
||||||
function matrix.step()
|
local stepnum = 0
|
||||||
if stepnum == 3 then
|
local interval = 1
|
||||||
matrix.connect()
|
local counter = 0
|
||||||
end
|
|
||||||
stepnum = stepnum + 1
|
|
||||||
|
|
||||||
if not matrix.connected then return end
|
function matrix.step(dtime)
|
||||||
|
if stepnum == 3 then
|
||||||
-- Hooks will manage incoming messages and errors
|
matrix.connect()
|
||||||
local good, err = xpcall(function() client:_sync() end, debug.traceback)
|
end
|
||||||
if not good then
|
stepnum = stepnum + 1
|
||||||
print(err)
|
counter = counter + dtime
|
||||||
return
|
if counter >= interval and matrix.connected then
|
||||||
end
|
counter = counter - interval
|
||||||
|
local good, err = xpcall(function() client:_sync() end, debug.traceback)
|
||||||
|
if not good then
|
||||||
|
print(err)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function matrix.connect()
|
function matrix.connect()
|
||||||
if matrix.connected then
|
if matrix.connected then
|
||||||
minetest.log("error", "Matrix: already connected")
|
minetest.log("error", "Matrix: already connected")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
client:login_with_password(matrix.config.user, matrix.config.password, true)
|
client:login_with_password(matrix.config.user, matrix.config.password, true)
|
||||||
matrix.connected = true
|
matrix.connected = true
|
||||||
minetest.log("action", "Matrix: Connected!")
|
minetest.log("action", "Matrix: Connected!")
|
||||||
minetest.chat_send_all("Matrix: Connected!")
|
minetest.chat_send_all("Matrix: Connected!")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function matrix.disconnect(message)
|
function matrix.disconnect(message)
|
||||||
if matrix.connected then
|
if matrix.connected then
|
||||||
--The OnDisconnect hook will clear matrix.connected and print a disconnect message
|
--The OnDisconnect hook will clear matrix.connected and print a disconnect message
|
||||||
client:logout()
|
client:logout()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function matrix.say(to, message)
|
function matrix.say(to, message)
|
||||||
if not message then
|
if not message then
|
||||||
message = to
|
message = to
|
||||||
to = matrix.config.channel
|
to = matrix.config.channel
|
||||||
end
|
end
|
||||||
to = to or matrix.config.channel
|
to = to or matrix.config.channel
|
||||||
for room_id, room in pairs(client.rooms) do
|
for room_id, room in pairs(client.rooms) do
|
||||||
room:send_text(message)
|
room:send_text(message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user