send command: simplify, description

This commit is contained in:
Pascal Engélibert 2025-06-13 22:04:48 +02:00
commit a54ac76523
2 changed files with 35 additions and 33 deletions

View file

@ -3,7 +3,7 @@
Luanti mod to exchange in-game [currency](https://github.com/mt-mods/currency) with [libre currency](https://duniter.org) (Ğ1 blockchain). Luanti mod to exchange in-game [currency](https://github.com/mt-mods/currency) with [libre currency](https://duniter.org) (Ğ1 blockchain).
Luanti server has a dedicated account. Luanti server has a dedicated account.
Player transfer money on it and the server gives them a corresponding amount of in-game banknotes. Players transfer money on it and the server gives them a corresponding amount of in-game banknotes.
They can then do whatever they want with the banknotes (trade, decorate walls, burn...). They can then do whatever they want with the banknotes (trade, decorate walls, burn...).
Players can ask the server to transfer their banknotes to an account of their choice using a chat command. Players can ask the server to transfer their banknotes to an account of their choice using a chat command.
@ -13,6 +13,8 @@ It works with Ğ1v2 which is still in development/testing phase.
Put this repo in the `mods` directory. Put this repo in the `mods` directory.
Add `g1_bridge` to `secure.http_mods`.
Have a local Duniter node. Have a local Duniter node.
```bash ```bash
@ -31,6 +33,7 @@ Have a local Duniter node.
* Ensure all transfers are failproof (any communication, storage medium, server or whatever should be able to fail or rollback to a previous state, without causing double spend or money loss) * Ensure all transfers are failproof (any communication, storage medium, server or whatever should be able to fail or rollback to a previous state, without causing double spend or money loss)
* Force reception account to exist * Force reception account to exist
* Nice ATM-like interface * Nice ATM-like interface
* Accept mixed values (other banknotes values than 1)
## Workflow ## Workflow

View file

@ -3,46 +3,45 @@ if http == nil then
error("Please add g1_bridge to secure.http_mods") error("Please add g1_bridge to secure.http_mods")
end end
minetest.register_chatcommand("g1", { minetest.register_chatcommand("g1_send", {
params = "<address> <amount>",
description = "Send MineGeld from your inventory to a G1 address. Fixed rate: 1G1=10MG.",
func = function(name, param) func = function(name, param)
local args = {} local args = {}
for arg in string.gmatch(param,"%S+") do for arg in string.gmatch(param,"%S+") do
if #args >= 3 then if #args >= 2 then
break break
end end
table.insert(args, arg) table.insert(args, arg)
end end
if #args >= 1 then if #args == 3 and string.find(args[1],"^[%da-zA-Z]+$") and string.find(args[2],"^%d+$") then
core.chat_send_player(name, "args >= 1") local address = args[1]
if args[1] == "send" and #args == 3 and string.find(args[2],"^[%da-zA-Z]+$") and string.find(args[3],"^%d+$") then local amount = tonumber(args[2])
local address = args[2] -- This version Lua does not have any way to check if a number is represented as an integer or a float,
local amount = tonumber(args[3]) -- because it explicitely doesn't care.
-- This version Lua does not have any way to check if a number is represented as an integer or a float, -- Hence, this program won't care if the player writes an invalid number,
-- because it explicitely doesn't care. -- as there is no need to ensure arithmetic rules are respected. (as Lua's manual pretends)
-- Hence, this program won't care if the player writes an invalid number, -- As the pattern excludes "." and nobody should have billions of items,
-- as there is no need to ensure arithmetic rules are respected. (as Lua's manual pretends) -- it should still be safe. I hope.
-- As the pattern excludes "." and nobody should have billions of items, if type(amount) ~= "number" then
-- it should still be safe. I hope. return
if type(amount) ~= "number" then end
return local inv = core.get_inventory({type="player", name=name})
end local wanted_stack = "currency:minegeld "..amount
local inv = core.get_inventory({type="player", name=name}) local balanced = inv:contains_item("main", ItemStack(wanted_stack))
local wanted_stack = "currency:minegeld "..amount if balanced then
local balanced = inv:contains_item("main", ItemStack(wanted_stack)) inv:remove_item("main", wanted_stack)
if balanced then http.fetch({url="http://127.0.0.1:30061/send/"..address.."/"..amount, method="GET"},
inv:remove_item("main", wanted_stack) function(res)
http.fetch({url="http://127.0.0.1:30061/send/"..address.."/"..amount, method="GET"}, if res.code == 200 then
function(res) core.chat_send_player(name, "Transfer sent")
if res.code == 200 then else
core.chat_send_player(name, "Transfer sent") core.chat_send_player(name, "Error during transfer")
else
core.chat_send_player(name, "Error during transfer")
end
end end
) end
else )
core.chat_send_player(name, "Not enough currency") else
end core.chat_send_player(name, "Not enough currency")
end end
end end
end end