Ajoute le retrait d'espèces depuis l'ATM

This commit is contained in:
Sys Quatre 2020-01-07 23:04:59 +01:00
parent 57deb8582e
commit c1a527eeea
1 changed files with 69 additions and 32 deletions

101
atm.lua
View File

@ -11,34 +11,60 @@ local unique = (function(unique_num)
end end
end)(0) end)(0)
local coins_convert = { local coins = {
["minercantile:copper_coin"]=1, ["minercantile:silver_coin"]=9, ["minercantile:gold_coin"]=81, [1] = "bitchange:minecoinblock", [2] = "bitchange:minecoin", [3] = "bitchange:mineninth",
["maptools:copper_coin"]=1, ["maptools:silver_coin"]=9, ["maptools:gold_coin"]=81, [4] = "maptools:gold_coin", [5] = "maptools:silver_coin", [6] = "maptools:copper_coin"
["bitchange:mineninth"]=729, ["bitchange:minecoin"]=6561, ["bitchange:minecoinblock"]=59049
} }
local function deposit_fs(fs, p_name) local coins_convert = {
local balance = exchange:get_balance(p_name) [coins[6]]=1, [coins[5]]=9, [coins[4]]=81, [coins[3]]=729, [coins[2]]=6561, [coins[1]]=59049,
local spos = atm_pos[p_name].x..","..atm_pos[p_name].y..","..atm_pos[p_name].z ["minercantile:copper_coin"]=1, ["minercantile:silver_coin"]=9, ["minercantile:gold_coin"]=81
}
fs:size(8,9) local function withdraw_fs(fs, p_name, amount)
local spos = atm_pos[p_name].x..","..atm_pos[p_name].y..","..atm_pos[p_name].z
if not balance then local inv = minetest.get_inventory({ type = "node", pos={x=atm_pos[p_name].x, y=atm_pos[p_name].y, z=atm_pos[p_name].z} })
fs:label(0.5,0.5, "You don't have an account.")
else local msg = ""
fs:label(0.5,0.5, "Balance: " .. balance) local w_amount = tonumber(amount)
fs:label(1,1,"Put your coins to credit your account") if w_amount and math.floor(w_amount) == w_amount and w_amount > 0 then
fs:list(3.5,2.5, 1,1, "nodemeta:"..spos, "main") local succ, err = exchange:give_credits(p_name, 0 - w_amount, "Cash withdrawal: (-"..w_amount..")")
fs:list(0,4, 8,4, "current_player", "main") if succ then
--fs("list[nodemeta:"..spos..";main;3.5,2.5;1,1;]".. local index = 1
-- "list[current_player;main;0,4.85;8,1;]".. repeat
-- "list[current_player;main;0,6.08;8,3;8]" .. local m = math.floor(w_amount / coins_convert[coins[index]])
fs("listring[nodemeta:"..spos..";main]".. if m > 0 then
"listring[current_player;main]" inv:add_item( "main", ItemStack({ name = coins[index], count = m }) )
) end
w_amount = w_amount - (coins_convert[coins[index]] * m)
index = index + 1
until ( w_amount == 0)
else
msg = err
end
elseif w_amount then
msg = "Invalid number ! Must be an Integer > 0"
end end
fs:button(1,2, 2,1, "logout", "Log Out") local balance = exchange:get_balance(p_name)
fs:size(8,10)
if not balance then
fs:label(0.5, 0.5, "You don't have an account.")
else
fs:label(3,8.9, "Balance: " .. balance)
fs:field(0.75, 1.25, 3.25, 1, "w_amount", "Desired amount:")
fs:button(4, 1, 3.25, 1, "withdraw", "Get !")
fs:label(1, 2.25, "Or deposit your coins to credit your account:")
fs:list(1,3,6,1, "nodemeta:"..spos, "main")
fs:list(0,4.25, 8,4, "current_player", "main")
fs("listring[current_player;main]"..
"listring[nodemeta:"..spos..";main]"
)
fs:label(0,9.75, msg)
end
fs:button(0,8.75, 2,1, "logout", "Log Out")
end end
local function info_fs(fs, p_name) local function info_fs(fs, p_name)
@ -121,11 +147,11 @@ end
local function main_menu_fs(fs, p_name) local function main_menu_fs(fs, p_name)
fs:size(6,2) fs:size(8,2)
fs:button(0,0.125, 2,1, "deposit", "Cash Deposit") fs:button(0,0.125, 4,1, "withdraw", "Cash deposit and withdrawal")
fs:button(2,0.125, 2,1, "info", "Account Info") fs:button(4,0.125, 2,1, "info", "Account Info")
fs:button(4,0.125, 2,1, "wire", "Wire Monies") fs:button(6,0.125, 2,1, "wire", "Wire Monies")
fs:button(0.50,1.125, 5.0,1, "transaction_log", "Transaction Log") fs:button(0.50, 1.125, 7, 1, "transaction_log", "Transaction Log")
end end
@ -159,12 +185,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
show_atm_form(info_fs, p_name) show_atm_form(info_fs, p_name)
elseif fields.wire then elseif fields.wire then
show_atm_form(wire_fs, p_name) show_atm_form(wire_fs, p_name)
elseif fields.withdraw then
show_atm_form(withdraw_fs, p_name, fields and fields.w_amount)
elseif fields.send then elseif fields.send then
show_atm_form(send_fs, p_name, fields.recipient, fields.amount) show_atm_form(send_fs, p_name, fields.recipient, fields.amount)
elseif fields.transaction_log then elseif fields.transaction_log then
show_atm_form(log_fs, p_name) show_atm_form(log_fs, p_name)
elseif fields.deposit then
show_atm_form(deposit_fs, p_name)
end end
return true return true
@ -242,6 +268,11 @@ minetest.register_node("global_exchange:atm_bottom", {
return itemstack return itemstack
end end
end, end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main")
end,
on_destruct = function(pos) on_destruct = function(pos)
local pos2 = {x = pos.x, y = pos.y + 1, z = pos.z} local pos2 = {x = pos.x, y = pos.y + 1, z = pos.z}
local n2 = minetest.get_node(pos2) local n2 = minetest.get_node(pos2)
@ -253,7 +284,7 @@ minetest.register_node("global_exchange:atm_bottom", {
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("infotext", "ATM") meta:set_string("infotext", "ATM")
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size("main", 1) inv:set_size("main", 6)
end, end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player) allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local itname = stack:get_name() local itname = stack:get_name()
@ -262,6 +293,9 @@ minetest.register_node("global_exchange:atm_bottom", {
end end
return 0 return 0
end, end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
return stack:get_count()
end,
on_metadata_inventory_put = function(pos, listname, index, stack, player) on_metadata_inventory_put = function(pos, listname, index, stack, player)
local itname = stack:get_name() local itname = stack:get_name()
if coins_convert[itname] ~= nil then if coins_convert[itname] ~= nil then
@ -274,13 +308,16 @@ minetest.register_node("global_exchange:atm_bottom", {
if succ then if succ then
inv:set_stack(listname, index, nil) inv:set_stack(listname, index, nil)
minetest.log("action", p_name.." put "..nb.." "..stack:get_name() .. " to ATM at " .. minetest.pos_to_string(pos)) minetest.log("action", p_name.." put "..nb.." "..stack:get_name() .. " to ATM at " .. minetest.pos_to_string(pos))
show_atm_form(deposit_fs, p_name) show_atm_form(withdraw_fs, p_name)
--minetest.show_formspec(p_name, atm_form, deposit_fs(p_name)) --minetest.show_formspec(p_name, atm_form, deposit_fs(p_name))
else else
minetest.log("error", p_name.." want to put "..nb.." "..stack:get_name().." to ATM at ".. minetest.pos_to_string(pos).." but: "..msg) minetest.log("error", p_name.." want to put "..nb.." "..stack:get_name().." to ATM at ".. minetest.pos_to_string(pos).." but: "..msg)
end end
end end
end, end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name().." take "..stack:get_count().." "..stack:get_name().." from ATM at "..minetest.pos_to_string(pos))
end,
on_rightclick = function(pos, _, clicker) on_rightclick = function(pos, _, clicker)
local p_name = clicker:get_player_name() local p_name = clicker:get_player_name()
atm_pos[p_name] = pos atm_pos[p_name] = pos