From c1a527eeea492636be3ebce42abc2c0648ee47ef Mon Sep 17 00:00:00 2001 From: sys4 Date: Tue, 7 Jan 2020 23:04:59 +0100 Subject: [PATCH] =?UTF-8?q?Ajoute=20le=20retrait=20d'esp=C3=A8ces=20depuis?= =?UTF-8?q?=20l'ATM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- atm.lua | 101 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 32 deletions(-) diff --git a/atm.lua b/atm.lua index 3e131f9..abab9c1 100644 --- a/atm.lua +++ b/atm.lua @@ -11,34 +11,60 @@ local unique = (function(unique_num) end end)(0) -local coins_convert = { - ["minercantile:copper_coin"]=1, ["minercantile:silver_coin"]=9, ["minercantile:gold_coin"]=81, - ["maptools:copper_coin"]=1, ["maptools:silver_coin"]=9, ["maptools:gold_coin"]=81, - ["bitchange:mineninth"]=729, ["bitchange:minecoin"]=6561, ["bitchange:minecoinblock"]=59049 +local coins = { + [1] = "bitchange:minecoinblock", [2] = "bitchange:minecoin", [3] = "bitchange:mineninth", + [4] = "maptools:gold_coin", [5] = "maptools:silver_coin", [6] = "maptools:copper_coin" } -local function deposit_fs(fs, p_name) - local balance = exchange:get_balance(p_name) - local spos = atm_pos[p_name].x..","..atm_pos[p_name].y..","..atm_pos[p_name].z +local coins_convert = { + [coins[6]]=1, [coins[5]]=9, [coins[4]]=81, [coins[3]]=729, [coins[2]]=6561, [coins[1]]=59049, + ["minercantile:copper_coin"]=1, ["minercantile:silver_coin"]=9, ["minercantile:gold_coin"]=81 +} - fs:size(8,9) - - if not balance then - fs:label(0.5,0.5, "You don't have an account.") - else - fs:label(0.5,0.5, "Balance: " .. balance) - fs:label(1,1,"Put your coins to credit your account") - fs:list(3.5,2.5, 1,1, "nodemeta:"..spos, "main") - fs:list(0,4, 8,4, "current_player", "main") - --fs("list[nodemeta:"..spos..";main;3.5,2.5;1,1;]".. - -- "list[current_player;main;0,4.85;8,1;]".. - -- "list[current_player;main;0,6.08;8,3;8]" .. - fs("listring[nodemeta:"..spos..";main]".. - "listring[current_player;main]" - ) +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 + 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} }) + + local msg = "" + local w_amount = tonumber(amount) + if w_amount and math.floor(w_amount) == w_amount and w_amount > 0 then + local succ, err = exchange:give_credits(p_name, 0 - w_amount, "Cash withdrawal: (-"..w_amount..")") + if succ then + local index = 1 + repeat + local m = math.floor(w_amount / coins_convert[coins[index]]) + if m > 0 then + 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 - 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 local function info_fs(fs, p_name) @@ -121,11 +147,11 @@ end local function main_menu_fs(fs, p_name) - fs:size(6,2) - fs:button(0,0.125, 2,1, "deposit", "Cash Deposit") - fs:button(2,0.125, 2,1, "info", "Account Info") - fs:button(4,0.125, 2,1, "wire", "Wire Monies") - fs:button(0.50,1.125, 5.0,1, "transaction_log", "Transaction Log") + fs:size(8,2) + fs:button(0,0.125, 4,1, "withdraw", "Cash deposit and withdrawal") + fs:button(4,0.125, 2,1, "info", "Account Info") + fs:button(6,0.125, 2,1, "wire", "Wire Monies") + fs:button(0.50, 1.125, 7, 1, "transaction_log", "Transaction Log") end @@ -159,12 +185,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) show_atm_form(info_fs, p_name) elseif fields.wire then 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 show_atm_form(send_fs, p_name, fields.recipient, fields.amount) elseif fields.transaction_log then show_atm_form(log_fs, p_name) - elseif fields.deposit then - show_atm_form(deposit_fs, p_name) end return true @@ -242,6 +268,11 @@ minetest.register_node("global_exchange:atm_bottom", { return itemstack 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) local pos2 = {x = pos.x, y = pos.y + 1, z = pos.z} local n2 = minetest.get_node(pos2) @@ -253,7 +284,7 @@ minetest.register_node("global_exchange:atm_bottom", { local meta = minetest.get_meta(pos) meta:set_string("infotext", "ATM") local inv = meta:get_inventory() - inv:set_size("main", 1) + inv:set_size("main", 6) end, allow_metadata_inventory_put = function(pos, listname, index, stack, player) local itname = stack:get_name() @@ -262,6 +293,9 @@ minetest.register_node("global_exchange:atm_bottom", { end return 0 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) local itname = stack:get_name() if coins_convert[itname] ~= nil then @@ -274,13 +308,16 @@ minetest.register_node("global_exchange:atm_bottom", { if succ then inv:set_stack(listname, index, nil) 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)) else 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, + 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) local p_name = clicker:get_player_name() atm_pos[p_name] = pos