123 lines
4.2 KiB
Lua
123 lines
4.2 KiB
Lua
local MG_RATE = 10
|
|
|
|
local http = minetest.request_http_api()
|
|
if http == nil then
|
|
error("Please add g1_bridge to secure.http_mods")
|
|
end
|
|
|
|
local mod_storage = core.get_mod_storage()
|
|
if mod_storage == nil then
|
|
error("Cannot get mod storage")
|
|
end
|
|
|
|
function receive_debts()
|
|
local block = mod_storage:get_int("block")
|
|
http.fetch({url="http://127.0.0.1:30061/recv/"..block, method="GET"},
|
|
function(res)
|
|
if res.code == 200 then
|
|
local data = minetest.parse_json(res.data)
|
|
if data == nil or type(data.block) ~= "number" or data.block <= block or type(data.debts) ~= "table" then
|
|
return
|
|
end
|
|
for i, debt in ipairs(data.debts) do
|
|
if type(debt.name) == "string" and type(debt.amount) == "number" and core.player_exists(debt.name) then
|
|
local player_debt = mod_storage:get_int("debt_"..debt.name)
|
|
player_debt = player_debt + debt.amount
|
|
mod_storage:set_int("debt_"..debt.name, player_debt)
|
|
end
|
|
end
|
|
mod_storage:set_int("block", data.block + 1)
|
|
end
|
|
end
|
|
)
|
|
end
|
|
|
|
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)
|
|
local args = {}
|
|
for arg in string.gmatch(param,"%S+") do
|
|
if #args >= 2 then
|
|
break
|
|
end
|
|
table.insert(args, arg)
|
|
end
|
|
if #args == 2 and string.find(args[1],"^[%da-zA-Z]+$") and string.find(args[2],"^%d+$") then
|
|
local address = args[1]
|
|
local amount_mg = tonumber(args[2])
|
|
-- This version Lua does not have any way to check if a number is represented as an integer or a float,
|
|
-- because it explicitely doesn't care.
|
|
-- Hence, this program won't care if the player writes an invalid number,
|
|
-- as there is no need to ensure arithmetic rules are respected. (as Lua's manual pretends)
|
|
-- As the pattern excludes "." and nobody should have billions of items,
|
|
-- it should still be safe. I hope.
|
|
if type(amount_mg) ~= "number" then
|
|
return
|
|
end
|
|
local inv = core.get_inventory({type="player", name=name})
|
|
local wanted_stack = "currency:minegeld "..amount_mg
|
|
local balanced = inv:contains_item("main", ItemStack(wanted_stack))
|
|
if balanced then
|
|
local amount = amount_mg * MG_RATE
|
|
inv:remove_item("main", ItemStack(wanted_stack))
|
|
http.fetch({url="http://127.0.0.1:30061/send/"..address.."/"..amount, method="GET"},
|
|
function(res)
|
|
if res ~= nil and res.code == 200 then
|
|
minetest.log("action", "mt-g1 - "..name.." sent "..amount_mg.."MG to "..address)
|
|
core.chat_send_player(name, "Transfer sent")
|
|
else
|
|
core.chat_send_player(name, "Error during transfer, giving your money back")
|
|
inv:add_item("main", ItemStack(wanted_stack))
|
|
end
|
|
end
|
|
)
|
|
else
|
|
core.chat_send_player(name, "Not enough money (please ensure it is available in 1MG banknotes)")
|
|
end
|
|
end
|
|
end
|
|
})
|
|
|
|
minetest.register_chatcommand("g1_claim", {
|
|
description = "Claim MineGeld from G1 sent to the server",
|
|
func = function(name, param)
|
|
receive_debts()
|
|
local amount = mod_storage:get_int("debt_"..name)
|
|
local amount_mg = amount / MG_RATE
|
|
local inv = core.get_inventory({type="player", name=name})
|
|
local stack = "currency:minegeld "..amount_mg
|
|
if inv:room_for_item("main", ItemStack(stack)) then
|
|
minetest.log("action", "mt-g1 - "..name.." claimed "..amount_mg.."MG")
|
|
inv:add_item("main", ItemStack(stack))
|
|
core.chat_send_player(name, "Claimed "..amount_mg.."MG")
|
|
mod_storage:set_int("debt_"..name, 0)
|
|
else
|
|
core.chat_send_player(name, "No room in your inventory! Available: "..amount_mg.."MG")
|
|
end
|
|
end
|
|
})
|
|
|
|
minetest.register_chatcommand("g1_block", {
|
|
description = "Get latest block number known by the server",
|
|
func = function(name, param)
|
|
local block = mod_storage:get_int("block")
|
|
core.chat_send_player(name, "Current block: "..block)
|
|
end
|
|
})
|
|
|
|
minetest.register_chatcommand("g1_setblock", {
|
|
privs = {
|
|
server = true
|
|
},
|
|
params = "<block number>",
|
|
description = "(admin) Set latest block number",
|
|
func = function(name, param)
|
|
local new_block = tonumber(param)
|
|
if type(new_block) == "number" then
|
|
mod_storage:set_int("block", new_block)
|
|
else
|
|
core.chat_send_player("name", "Argument must be an integer.")
|
|
end
|
|
end
|
|
})
|