diff --git a/atm.lua b/atm.lua index a1c4944..588613f 100644 --- a/atm.lua +++ b/atm.lua @@ -41,33 +41,33 @@ local function unique() end function cash_fs(p_name) - local balance = exchange:get_balance(p_name) - local formspec = - 'size[8,9]'.. - 'label[0,0;' .. p_name .. '\'s account]' .. - 'label[0,1;Balance: ' .. balance .. ']' .. - --money - 'list[detached:global_exchange;money;0,2;3,1;]'.. - --player inventory - 'list[current_player;main;0,4;8,4;]'.. - --back button - back(0,8) ---print(formspec) - return formspec + local balance = exchange:get_balance(p_name) + local formspec = + 'size[8,9]'.. + 'label[0,0;' .. p_name .. '\'s account]' .. + 'label[0,1;Balance: ' .. balance .. ']' .. + --money + 'list[detached:global_exchange;money;0,2;3,1;]'.. + --player inventory + 'list[current_player;main;0,4;8,4;]'.. + --back button + back(0,8) + --print(formspec) + return formspec end function bills2balance(stack, p_name) - local bal = exchange:get_balance(p_name) - local name = stack:get_name() - local count = stack:get_count() - if name == 'currency:minegeld' then - bal = bal + count - elseif name == 'currency:minegeld_5' then - bal = bal + count * 5 - elseif name == 'currency:minegeld_10' then - bal = bal + count * 10 - end - return bal + local bal = exchange:get_balance(p_name) + local name = stack:get_name() + local count = stack:get_count() + if name == 'currency:minegeld' then + bal = bal + count + elseif name == 'currency:minegeld_5' then + bal = bal + count * 5 + elseif name == 'currency:minegeld_10' then + bal = bal + count * 10 + end + return bal end local function wire_fs(p_name) diff --git a/exchange_machine.lua b/exchange_machine.lua index 9fe8e9d..99284b2 100644 --- a/exchange_machine.lua +++ b/exchange_machine.lua @@ -198,66 +198,66 @@ minetest.after(0, function() inv:add_item("main", item_name) show_main(p_name, item_name) end, - on_put = function(inv, listname, index, stack, player) - local p_name = player:get_player_name() - if listname == 'money' and minetest.get_item_group(stack:get_name(), 'minegeld') then - local old_balance = exchange:get_balance(p_name) - local balance = bills2balance(stack, p_name) - exchange:change_balance(p_name, balance - old_balance) - minetest.sound_play("atm_cash", {to_player = p_name}) - local stacks = inv:get_list('money') - local tens = math.floor(balance/10) - if tens > 0 then - inv:set_stack('money', 1, 'currency:minegeld_10 ' .. tens) - balance = balance - tens * 10 - else - inv:set_stack('money', 1, '') - end - local fives = math.floor(balance/5) - if fives > 0 then - inv:set_stack('money', 2, 'currency:minegeld_5 ' .. fives) - balance = balance - fives * 5 - else - inv:set_stack('money', 2, '') - end - local ones = math.floor(balance) - if ones > 0 then - inv:set_stack('money', 3, 'currency:minegeld ' .. ones) - balance = balance - ones - else - inv:set_stack('money', 3, '') - end - end - minetest.show_formspec(p_name, "global_exchange:atm_form", cash_fs(p_name)) --redraw with new balance - end, - allow_put = function(inv, listname, index, stack, player) - local name = stack:get_name() - if listname == 'money' and minetest.get_item_group(name, 'minegeld') > 0 then - return stack:get_count() - end - return 0 - end, - on_take = function(inv, listname, index, stack, player) - local p_name = player:get_player_name() - if listname == 'money' and minetest.get_item_group(stack:get_name(), 'minegeld') then - local name = stack:get_name() - local count = stack:get_count() - local delta - if name == 'currency:minegeld' then - delta = count - elseif name == 'currency:minegeld_5' then - delta = count * 5 - elseif name == 'currency:minegeld_10' then - delta = count * 10 - end - exchange:change_balance(p_name, -delta) --let's hope it always returns success - minetest.sound_play("atm_cash", {to_player = p_name}) - end - minetest.show_formspec(p_name, "global_exchange:atm_form", cash_fs(p_name)) --redraw with new balance - end, - allow_take = function(inv, listname, index, stack, player) - return stack:get_count() - end, + on_put = function(inv, listname, index, stack, player) + local p_name = player:get_player_name() + if listname == 'money' and minetest.get_item_group(stack:get_name(), 'minegeld') then + local old_balance = exchange:get_balance(p_name) + local balance = bills2balance(stack, p_name) + exchange:change_balance(p_name, balance - old_balance) + minetest.sound_play("atm_cash", {to_player = p_name}) + local stacks = inv:get_list('money') + local tens = math.floor(balance/10) + if tens > 0 then + inv:set_stack('money', 1, 'currency:minegeld_10 ' .. tens) + balance = balance - tens * 10 + else + inv:set_stack('money', 1, '') + end + local fives = math.floor(balance/5) + if fives > 0 then + inv:set_stack('money', 2, 'currency:minegeld_5 ' .. fives) + balance = balance - fives * 5 + else + inv:set_stack('money', 2, '') + end + local ones = math.floor(balance) + if ones > 0 then + inv:set_stack('money', 3, 'currency:minegeld ' .. ones) + balance = balance - ones + else + inv:set_stack('money', 3, '') + end + end + minetest.show_formspec(p_name, "global_exchange:atm_form", cash_fs(p_name)) --redraw with new balance + end, + allow_put = function(inv, listname, index, stack, player) + local name = stack:get_name() + if listname == 'money' and minetest.get_item_group(name, 'minegeld') > 0 then + return stack:get_count() + end + return 0 + end, + on_take = function(inv, listname, index, stack, player) + local p_name = player:get_player_name() + if listname == 'money' and minetest.get_item_group(stack:get_name(), 'minegeld') then + local name = stack:get_name() + local count = stack:get_count() + local delta + if name == 'currency:minegeld' then + delta = count + elseif name == 'currency:minegeld_5' then + delta = count * 5 + elseif name == 'currency:minegeld_10' then + delta = count * 10 + end + exchange:change_balance(p_name, -delta) --let's hope it always returns success + minetest.sound_play("atm_cash", {to_player = p_name}) + end + minetest.show_formspec(p_name, "global_exchange:atm_form", cash_fs(p_name)) --redraw with new balance + end, + allow_take = function(inv, listname, index, stack, player) + return stack:get_count() + end, }) local selectable_list,n = {},1