merge web components

master
Thomas Rudin 2020-01-18 15:09:05 +01:00
parent cf85706ee6
commit 715d87753d
4 changed files with 125 additions and 1 deletions

View File

@ -2,6 +2,9 @@
--
-- Mod settings -- Change these to your liking
local http = minetest.request_http_api()
beerchat = {
-- The main channel is the one you send messages to when no channel is specified
main_channel_name = "main",
@ -27,7 +30,12 @@ beerchat = {
channels = {},
playersChannels = {},
currentPlayerChannel = {}
currentPlayerChannel = {},
-- web settings
url = minetest.settings:get("beerchat.url") or "http://127.0.0.1:8080",
http = http -- will be removed after init
}
local MP = minetest.get_modpath("beerchat")
@ -43,6 +51,16 @@ dofile(MP.."/whisper.lua")
dofile(MP.."/message.lua")
dofile(MP.."/chatcommands.lua")
if beerchat.web then
-- load web stuff
dofile(MP.."/web/executor.lua")
dofile(MP.."/web/tx.lua")
dofile(MP.."/web/rx.lua")
end
-- remove http ref
beerchat.http = nil
print("[OK] beerchat")

20
web/executor.lua Normal file
View File

@ -0,0 +1,20 @@
beerchat.executor = function(str, playername)
minetest.log("action", "[beerchat] executing: '" .. str .. "' as " .. playername)
local found, _, commandname, params = str:find("^([^%s]+)%s(.+)$")
if not found then
commandname = str
end
local command = minetest.chatcommands[commandname]
if not command then
return false, "Not a valid command: " .. commandname
end
if command.privs and not minetest.check_player_privs(playername, command.privs) then
return false, "Not enough privileges!"
end
return command.func(playername, (params or ""))
end

46
web/rx.lua Normal file
View File

@ -0,0 +1,46 @@
local http = beerchat.http
local recv_loop
function handle_data(data)
if not data or not data.channel or not data.username or not data.message then
return
end
if data.direct then
-- direct message to bot
local playername = data.username .. "@Remote"
-- TODO: /login command over irc and name mapping
local success, msg = beerchat.executor(data.message, playername)
-- TODO return values
return success, msg
end
local name = data.username .. "@Remote"
beerchat.send_on_channel(name, data.channel, data.message)
end
recv_loop = function()
http.fetch({
url = beerchat.url,
timeout = 30,
}, function(res)
if res.succeeded and res.code == 200 then
local data = minetest.parse_json(res.data)
handle_data(data)
minetest.after(0.5, recv_loop)
else
-- ignore errors
minetest.log("error", "[beerchat] http request to " ..
beerchat.url .. " failed with code " .. res.code)
minetest.after(5, recv_loop)
end
end)
end
-- start loop
recv_loop()

40
web/tx.lua Normal file
View File

@ -0,0 +1,40 @@
local http = beerchat.http
beerchat.on_channel_message = function(channel, playername, message)
local data = {
channel = channel,
playername = playername,
message = message
}
local json = minetest.write_json(data)
http.fetch({
url = beerchat.url,
extra_headers = { "Content-Type: application/json" },
timeout = 5,
post_data = json
}, function(res)
-- ignore errors
end)
end
minetest.register_on_joinplayer(function(player)
beerchat.on_channel_message(nil, nil, "Player " .. player:get_player_name() ..
" joined the game")
end)
minetest.register_on_leaveplayer(function(player, timed_out)
local msg = player:get_player_name() .. " left the game"
if timed_out then
msg = msg .. " (timed out)"
end
beerchat.on_channel_message(nil, nil, msg)
end)
beerchat.on_channel_message(nil, nil, "Minetest started!")