some refactor

master
root 2021-08-04 11:16:39 +02:00
parent eaaa5bea41
commit 805163008e
2 changed files with 33 additions and 31 deletions

View File

@ -60,12 +60,12 @@ minetest.register_node("elez:piggy_bank", {
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = { fixed = {
{-0.25, -0.375, -0.25, 0.25, -0.0625, 0.25}, -- NodeBox1 {-0.25, -0.375, -0.25, 0.25, -0.0625, 0.25},
{-0.1875, -0.5, -0.1875, -0.125, -0.375, -0.125}, -- NodeBox2 {-0.1875, -0.5, -0.1875, -0.125, -0.375, -0.125},
{0.125, -0.5, -0.1875, 0.1875, -0.375, -0.125}, -- NodeBox3 {0.125, -0.5, -0.1875, 0.1875, -0.375, -0.125},
{0.125, -0.5, 0.125, 0.1875, -0.375, 0.1875}, -- NodeBox4 {0.125, -0.5, 0.125, 0.1875, -0.375, 0.1875},
{-0.1875, -0.5, 0.125, -0.125, -0.375, 0.1875}, -- NodeBox5 {-0.1875, -0.5, 0.125, -0.125, -0.375, 0.1875},
{-0.125, -0.3125, -0.3125, 0.125, -0.1875, -0.25}, -- NodeBox6 {-0.125, -0.3125, -0.3125, 0.125, -0.1875, -0.25},
} }
}, },
groups = {crumbly=2}, groups = {crumbly=2},
@ -93,7 +93,7 @@ minetest.register_node("elez:teller_machine", {
paramtype2 = "facedir", paramtype2 = "facedir",
param2 = 5, param2 = 5,
physical = true, physical = true,
groups = {crumbly=2}, groups = {cracky = 3},
on_rightclick = function(pos, node, player, itemstack, pointed_thing) on_rightclick = function(pos, node, player, itemstack, pointed_thing)
elez.electrumpay(player, "", nil, true) elez.electrumpay(player, "", nil, true)
end, end,
@ -119,6 +119,19 @@ end
--Basic Money functions --Basic Money functions
local function check_amount(amount)
if not amount then
return false, S("Error: You have to specify an amount of money.")
end
if not is_numeric(amount) then
return false, S("Error: The amount has to be numeric.")
end
if amount == 0 then
return false, S("Error: Type an amount greater than 0.")
end
return true
end
function elez.add_money(player, amount) function elez.add_money(player, amount)
if amount < -32768 then if amount < -32768 then
amount = -32768 amount = -32768
@ -154,17 +167,13 @@ function elez.save_money(player)
end end
function elez.transfer_money(src_name,dst_name,amount) function elez.transfer_money(src_name,dst_name,amount)
if not amount then local ok, msg = check_amount(amount)
return false, S("Error: You have to specify an amount of money.") if not ok then
end return ok, msg
if not is_numeric(amount) then
return false, S("Error: The amount has to be a number.")
end end
amount = math.abs(amount) amount = math.abs(amount)
if amount > 32767 then if amount > 32767 then
amount = 32767 amount = 32767
elseif amount == 0 then
return false, S("Error: Type an amount greater than 0.")
end end
local src = minetest.get_player_by_name(src_name) local src = minetest.get_player_by_name(src_name)
local dst = minetest.get_player_by_name(dst_name) local dst = minetest.get_player_by_name(dst_name)
@ -187,11 +196,9 @@ function elez.transfer_money(src_name,dst_name,amount)
end end
function elez.withdraw_money(player, amount) function elez.withdraw_money(player, amount)
if not amount then local ok, msg = check_amount(amount)
return false, S("Error: You have to specify an amount of money.") if not ok then
end return ok, msg
if not is_numeric(amount) then
return false, S("Error: The amount has to be a number.")
end end
amount = math.abs(amount) amount = math.abs(amount)
if amount > withdraw_max then if amount > withdraw_max then
@ -200,9 +207,6 @@ function elez.withdraw_money(player, amount)
if amount > elez.get_money(player) then if amount > elez.get_money(player) then
return false, S("Error: You has not").." "..tostring(amount).." "..S("of money to withdraw.") return false, S("Error: You has not").." "..tostring(amount).." "..S("of money to withdraw.")
end end
if amount == 0 then
return false, S("Error: Type an amount greater than 0.")
end
local inv = player:get_inventory() local inv = player:get_inventory()
local money_stack = ItemStack(coin_name.." "..tostring(amount)) local money_stack = ItemStack(coin_name.." "..tostring(amount))
if not inv:room_for_item("main", money_stack) then if not inv:room_for_item("main", money_stack) then
@ -255,9 +259,9 @@ end
local function get_default_fields(fields, success) local function get_default_fields(fields, success)
if success then if success then
return {name="",amount="",withdraw=""} return {name="", amount="", withdraw=""}
else else
return {name=fields.fld_name,amount=fields.fld_amount,withdraw=fields.fld_withdraw} return {name=fields.fld_name, amount=fields.fld_amount, withdraw=fields.fld_withdraw}
end end
end end
@ -273,7 +277,7 @@ function elez.electrumpay(user, msg, default_fields, withdraw)
title = S("ElectrumPay Card") title = S("ElectrumPay Card")
end end
if not default_fields then if not default_fields then
default_fields = {name="",amount="",withdraw=""} default_fields = {name = "", amount = "", withdraw = ""}
end end
minetest.show_formspec(user_name, "elez:electrumpay", compose_formspec(user, title, msg, default_fields, withdraw)) minetest.show_formspec(user_name, "elez:electrumpay", compose_formspec(user, title, msg, default_fields, withdraw))
end end
@ -305,11 +309,9 @@ minetest.register_chatcommand("add_money", {
return true, S("Error: You have to specify a player and an amount of money.") return true, S("Error: You have to specify a player and an amount of money.")
end end
local player_name, amount = string.match(param, "([%a%d_-]+) ([%a%d_-]+)") local player_name, amount = string.match(param, "([%a%d_-]+) ([%a%d_-]+)")
if not amount then local ok, msg = check_amount(amount)
return true, S("Error: You have to specify an amount of money.") if not ok then
end return true, msg
if not is_numeric(amount) then
return true, S("Error: The amount has to be a number.")
end end
local player = minetest.get_player_by_name(player_name) local player = minetest.get_player_by_name(player_name)
if not player then if not player then

View File

@ -12,7 +12,7 @@ Close=Cerrar
Add an amount of money (+ or -)=Añade una cantidad de dinero (+ o -) Add an amount of money (+ or -)=Añade una cantidad de dinero (+ o -)
Error: You have to specify a player and an amount of money.=Error: Tienes que especificar un jugador y una cantidad de dinero Error: You have to specify a player and an amount of money.=Error: Tienes que especificar un jugador y una cantidad de dinero
Error: You have to specify an amount of money.=Error: Tienes que especificar una cantidad de dinero. Error: You have to specify an amount of money.=Error: Tienes que especificar una cantidad de dinero.
Error: The amount has to be a number.=Error: La cantidad tiene que ser un número. Error: The amount has to be a number.=Error: La cantidad tiene que ser numérica.
Error: The player does not exist or not online.=El jugador no existe o no está en línea Error: The player does not exist or not online.=El jugador no existe o no está en línea
Transfer successfully completed.=Tranferencia realizada con éxito. Transfer successfully completed.=Tranferencia realizada con éxito.
You've added=Has añadido You've added=Has añadido