commit 33037d4f40eee70a05241fbbca860930d162a77a Author: HimbeerserverDE Date: Sat Jan 23 17:49:04 2021 +0100 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..16efe79 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 HimbeerserverDE + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e02492d --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +# multiserver API +A multiserver RPC API for Minetest Servers +## Usage +Install this mod on your minetest servers and enable mod channels on +each server. You can now use the API in your own mods. +**None of the methods will work if no players are connected to the +minetest server. A fix is planned.** +## Planned features +* Fix dependence on connected player +* Add register_on_* methods +* Low priority: Stop using callback functions if a response is expected +## Methods +> `multiserver.alert(msg)` +``` +msg: Message to send +Description: Sends msg to all clients that are connected to the proxy +``` +> `multiserver.get_default_server(callback)` +``` +callback: Callback function (params: default_server) +Description: Gets the default server and passes it to the callback +function +``` +> `multiserver.get_player_count(callback)` +``` +callback: Callback function (params: player_count) +Description: Gets the count of players that are connected to the proxy +and passes it to the callback function +``` +> `multiserver.is_online(name, callback)` +``` +name: Player name to use for check +callback: Callback function (params: is_online) +Description: Reports to the callback function if a player is online +``` +> `multiserver.check_privs(name, privs, callback)` +``` +name: Player name to use for check +privs: Privileges the player has to have +callback: Callback function (params: has_privs) +Description: Reports to the callback function if a player has the +specified privileges +``` +> `multiserver.get_server_name(name, callback)` +``` +name: Player name to perform lookup on +callback: Callback function (params: server_name) +Description: Gets the name of the server a player is currently +connected to and passes it to the callback function +``` +> `multiserver.redirect(name, tosrv)` +``` +name: Name of player to redirect +tosrv: Name of server to connect player to +Description: Redirects a player to the specified server +``` +> `multiserver.get_address(name, callback)` +``` +name: Player name to perform lookup on +callback: Callback function (params: address) +Description: Gets the address of a player (format: IP_ADDRESS:PORT) and +passes it to the callback function; This is needed because minetest +servers only see the address of the proxy +``` diff --git a/functions.lua b/functions.lua new file mode 100644 index 0000000..abaf596 --- /dev/null +++ b/functions.lua @@ -0,0 +1,31 @@ +multiserver.alert = function(msg) + multiserver.do_rpc("<-ALERT " .. msg, nil) +end + +multiserver.get_default_server = function(cb) + multiserver.do_rpc("<-GETDEFSRV", cb) +end + +multiserver.get_player_count = function(cb) + multiserver.do_rpc("<-GETPEERCNT", cb) +end + +multiserver.is_online = function(name, cb) + multiserver.do_rpc("<-ISONLINE " .. name, cb) +end + +multiserver.check_privs = function(name, privs, cb) + multiserver.do_rpc("<-CHECKPRIVS " .. name .. " " .. minetest.privs_to_string(privs):gsub(",", "|"), cb) +end + +multiserver.get_server_name = function(name, cb) + multiserver.do_rpc("<-GETSRV " .. name, cb) +end + +multiserver.redirect = function(name, tosrv) + multiserver.do_rpc("<-REDIRECT " .. name .. " " .. tosrv, nil) +end + +multiserver.get_address = function(name, cb) + multiserver.do_rpc("<-GETADDR " .. name, cb) +end diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..5a0abee --- /dev/null +++ b/init.lua @@ -0,0 +1,15 @@ +multiserver = {} + +local mp = minetest.get_modpath(minetest.get_current_modname()) + +dofile(mp .. "/typeconv.lua") +dofile(mp .. "/rpc.lua") +dofile(mp .. "/functions.lua") + +minetest.register_on_joinplayer(function(player) + minetest.after(1, function(name) + multiserver.get_address(name, function(addr) + minetest.chat_send_all(addr) + end) + end, player:get_player_name()) +end) diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..9d9dd60 --- /dev/null +++ b/mod.conf @@ -0,0 +1,3 @@ +author = HimbeerserverDE +name = multiserver +description = The multiserver mod channel RPC API client diff --git a/rpc.lua b/rpc.lua new file mode 100644 index 0000000..0092819 --- /dev/null +++ b/rpc.lua @@ -0,0 +1,32 @@ +local ch = minetest.mod_channel_join("multiserver") +local rq = 0 +local cb = {} + +multiserver.do_rpc = function(msg, cbf) + local data = multiserver.tohex(rq) .. " " .. msg + rq = rq + 1 + ch:send_all(data) + cb[rq] = cbf +end + +minetest.register_on_modchannel_message(function(channel_name, sender, msg) + local rrq = multiserver.fromhex(msg:split(" ")[1]) + if cb[rrq] == nil then return end + local cmd = msg:split(" ")[2] + local p + if cmd == "->DEFSRV" then + p = msg:split(" ")[3] + elseif cmd == "->PEERCNT" then + p = tonumber(msg:split(" ")[3]) + elseif cmd == "->ISONLINE" then + p = multiserver.tobool(msg:split(" ")[3]) + elseif cmd == "->HASPRIVS" then + p = multiserver.tobool(msg:split(" ")[3]) + elseif cmd == "->SRV" then + p = msg:split(" ")[3] + elseif cmd == "->ADDR" then + p = msg:split(" ")[3] + end + cb[rrq](p) + cb[rrq] = nil +end) diff --git a/typeconv.lua b/typeconv.lua new file mode 100644 index 0000000..4d1d320 --- /dev/null +++ b/typeconv.lua @@ -0,0 +1,17 @@ +multiserver.tohex = function(n) + local r = string.format("%x", n) + if #r < 2 then r = "0" .. r end + return r +end + +multiserver.fromhex = function(s) + return tonumber(s, 16) +end + +multiserver.frombool = function(b) + if b then return "true" else return "false" end +end + +multiserver.tobool = function(s) + if s == "true" then return true else return false end +end