Receive
This commit is contained in:
parent
a54ac76523
commit
2bab960f23
9 changed files with 62500 additions and 911 deletions
65
init.lua
65
init.lua
|
|
@ -1,8 +1,37 @@
|
|||
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.",
|
||||
|
|
@ -14,35 +43,55 @@ minetest.register_chatcommand("g1_send", {
|
|||
end
|
||||
table.insert(args, arg)
|
||||
end
|
||||
if #args == 3 and string.find(args[1],"^[%da-zA-Z]+$") and string.find(args[2],"^%d+$") then
|
||||
if #args == 2 and string.find(args[1],"^[%da-zA-Z]+$") and string.find(args[2],"^%d+$") then
|
||||
local address = args[1]
|
||||
local amount = tonumber(args[2])
|
||||
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) ~= "number" then
|
||||
if type(amount_mg) ~= "number" then
|
||||
return
|
||||
end
|
||||
local inv = core.get_inventory({type="player", name=name})
|
||||
local wanted_stack = "currency:minegeld "..amount
|
||||
local wanted_stack = "currency:minegeld "..amount_mg
|
||||
local balanced = inv:contains_item("main", ItemStack(wanted_stack))
|
||||
if balanced then
|
||||
inv:remove_item("main", wanted_stack)
|
||||
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.code == 200 then
|
||||
if res ~= nil and res.code == 200 then
|
||||
core.chat_send_player(name, "Transfer sent")
|
||||
else
|
||||
core.chat_send_player(name, "Error during transfer")
|
||||
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 currency")
|
||||
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
|
||||
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
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue