diff --git a/atm.lua b/atm.lua new file mode 100644 index 0000000..b2f5ab3 --- /dev/null +++ b/atm.lua @@ -0,0 +1,103 @@ +local storage = minetest.get_mod_storage() + +function atm.create_account(name) + if not storage:contains("balance_" .. name) and minetest.player_exists(name) then + storage:set_int("balance_" .. name, atm.startbalance) + atm.balance[name] = atm.startbalance + end +end + +-- banking accounts storage +function atm.read_account(name) + if atm.balance[name] ~= nil then return end + if storage:contains("balance_" .. name) then + atm.balance[name] = storage:get_int("balance_" .. name) + else + atm.create_account(name) + end +end + +function atm.save_account(name) + if atm.balance[name] then + storage:set_int("balance_" .. name, atm.balance[name]) + return true + end + return false +end + +function atm.migrate_accounts() + local b = atm.balance + local file = io.open(atm.pth, "r") + if file then + repeat + local balance = file:read("*n") + if balance == nil then + break + end + local name = file:read("*l") + b[name:sub(2)] = balance + if b[name:sub(2)] ~= atm.startbalance then + atm.save_account(name:sub(2)) + end + until file:read(0) == nil + io.close(file) + end + os.remove(atm.pth) + minetest.log("action", "[atm] Migrated all accounts") +end + +-- wire transfer data storage +function atm.read_transaction(name) + if atm.completed_transactions[name] ~= nil then return end + local s = storage:get_string("transaction_" .. name) + if s == nil then return end + local t = minetest.deserialize(s) + if t == nil then return end + atm.completed_transactions[name] = t +end + +function atm.write_transaction(name) + if atm.completed_transactions[name] == nil then + storage:set_string("transaction_" .. name, "") + else + local s = minetest.serialize(atm.completed_transactions[name]) + if s == nil then return end + storage:set_string("transaction_" .. name, s) + end +end + +function atm.migrate_transactions() + local file = io.open(atm.pth_wt, "r") + if file then + local data = file:read("*all") + atm.completed_transactions = minetest.deserialize(data) + io.close(file) + end + for name, table in pairs(atm.completed_transactions) do + atm.write_transaction(name) + end + os.remove(atm.pth_wt) + minetest.log("action", "[atm] Migrated all transactions") +end + +minetest.register_on_leaveplayer(function(player) + if player then + local name = player:get_player_name() + atm.balance[name] = nil + atm.completed_transactions[name] = nil + atm.pending_transfers[name] = nil + end +end) + +minetest.register_on_mods_loaded(function() + local file = io.open(atm.pth, "r") + if file then + io.close(file) + atm.migrate_accounts() + end + file = io.open(atm.pth_wt, "r") + if file then + io.close(file) + atm.migrate_transactions() + end +end) \ No newline at end of file diff --git a/commands.lua b/commands.lua index a7a4e67..059764c 100644 --- a/commands.lua +++ b/commands.lua @@ -55,7 +55,7 @@ core.register_chatcommand("emeralds", { end if player and num then emeraldbank.add_emeralds(player, num) - atm.readaccounts() + atm.read_account(playername) minetest.chat_send_player(name, S("@1 has now @2 emeralds in bank account", playername, atm.balance[playername])) return true end diff --git a/forms.lua b/forms.lua new file mode 100644 index 0000000..9823301 --- /dev/null +++ b/forms.lua @@ -0,0 +1,100 @@ + + +local S = core.get_translator(core.get_current_modname()) + +local FS = function(...) return minetest.formspec_escape(S(...)) end + +-- abbreviation/symbol of the currency +local MONEY_SYMBOL = "$" + + +-- ATM +function atm.showform (player) + atm.read_account(player:get_player_name()) + local formspec = + "size[9,8.75]".. + "label[0.5,0;" .. FS("Your account balance: @1 @2", atm.balance[player:get_player_name()], MONEY_SYMBOL) .. "]" .. + "label[0.5,0.75;"..S("Deposit:").."]" .. + "label[7.5,0.75;"..S("Withdraw:").."]" .. + "item_image_button[0.5,1.25;1,1;".. "mcl_core:emerald" ..";i1;\n\n\b\b\b\b\b" .. "x1" .."]" .. + "item_image_button[7.5,1.25;1,1;".. "mcl_core:emerald" ..";i-1;\n\n\b\b\b\b\b" .. "x1" .."]" .. + "item_image_button[0.5,2.25;1,1;".. "mcl_core:emerald" ..";t10;\n\n\b\b\b\b" .. "x10" .."]" .. + "item_image_button[7.5,2.25;1,1;".. "mcl_core:emerald" ..";t-10;\n\n\b\b\b\b" .. "x10" .."]" .. + "item_image_button[0.5,3.25;1,1;".. "mcl_core:emerald" ..";c100;\n\n\b\b\b" .. "x100" .."]" .. + "item_image_button[7.5,3.25;1,1;".. "mcl_core:emerald" ..";c-100;\n\n\b\b\b" .. "x100" .."]" .. + "button[3.5,3;2,1;wt;"..S("Wire Transfer").."]".. + + "list[current_player;main;0,4.5;9,3;9]".. + mcl_formspec.get_itemslot_bg(0,4.5,9,3).. + "list[current_player;main;0,7.74;9,1;]".. + mcl_formspec.get_itemslot_bg(0,7.74,9,1).. + "listring[current_player;main]" + + minetest.after((0.1), function(gui) + return minetest.show_formspec(player:get_player_name(), "atm.form", gui) + end, formspec) +end + + + + + +-- wire transfer interface + +function atm.showform_wt (player) + atm.read_account(player:get_player_name()) + local formspec = + "size[8,6]".. + "button[5.75,0;2,1;transactions;" .. FS("Transactions >") .. "]" .. + "label[2.5,0;" .. FS("Wire Transfer Terminal") .. "]" .. + "label[2,0.5;" .. FS("Your account balance: @1 @2", atm.balance[player:get_player_name()], MONEY_SYMBOL) .. "]" .. + "field[0.5,1.5;5,1;dstn;" .. FS("Recipient:") .. ";]" .. + "field[6,1.5;2,1;amnt;" .. FS("Amount:") .. ";]" .. + "field[0.5,3;7.5,1;desc;" .. FS("Description:") .. ";]" .. + "button[4.7,5;3,1;pay;" .. FS("Complete the payment") .. "]" + minetest.after((0.1), function(gui) + return minetest.show_formspec(player:get_player_name(), "atm.form.wt", gui) + end, formspec) +end + +function atm.showform_wtconf(player, dstn, amnt, desc) + atm.read_account(player:get_player_name()) + local formspec = + "size[8,6]".. + "label[2.5,0;" .. FS("Wire Transfer Terminal") .. "]" .. + "label[2,0.5;" .. FS("Your account balance: @1 @2", atm.balance[player:get_player_name()], MONEY_SYMBOL) .. "]" .. + "label[2.5,1;" .. FS("TRANSACTION SUMMARY:") .. "]" .. + "label[0.5,1.5;" .. FS("Recipient: @1", dstn) .. "]" .. + "label[0.5,2;" .. FS("Amount: @1", amnt) .. "]" .. + "label[0.5,2.5;" .. FS("Description: @1", desc) .. "]" .. + "button[4.7,5;3,1;cnfrm;" .. FS("Confirm transfer") .. "]" + minetest.after((0.1), function(gui) + return minetest.show_formspec(player:get_player_name(), "atm.form.wtc", gui) + end, formspec) +end + +function atm.showform_wtlist(player, tlist) + atm.read_account(player:get_player_name()) + + local textlist = '' + + if not tlist then + textlist = FS("no transactions registered") .. "\n" + else + for _, entry in ipairs(tlist) do + textlist = textlist .. entry.date .. " " .. entry.sum .. " " .. MONEY_SYMBOL .. + " from " .. entry.from .. ": " .. entry.desc .. "\n" + end + end + + local formspec = + "size[8,6]".. + "button[5.75,0;2,1;transfer;" .. FS("< Transfer money") .. "]" .. + "label[2.5,0;" .. FS("Wire Transfer Terminal") .. "]" .. + "label[2,0.5;" .. FS("Your account balance: @1 @2", atm.balance[player:get_player_name()], MONEY_SYMBOL) .. "]" .. + "textarea[0.5,1.25;7.5,4;hst;" .. FS("Transaction list") .. ";" .. textlist .. "]" .. + "button[4.7,5;3,1;clr;" .. FS("Clear transactions") .. "]" + minetest.after((0.1), function(gui) + return minetest.show_formspec(player:get_player_name(), "atm.form.wtl", gui) + end, formspec) +end diff --git a/functions.lua b/functions.lua index f4675fd..d58426e 100644 --- a/functions.lua +++ b/functions.lua @@ -21,71 +21,11 @@ local S = core.get_translator(core.get_current_modname()) -function atm.ensure_init(name) - -- Ensure the atm account for the placer specified by name exists - atm.readaccounts() - if not atm.balance[name] then - atm.balance[name] = atm.startbalance - end -end - - --- banking accounts storage - -function atm.readaccounts () - local b = atm.balance - local file = io.open(atm.pth, "r") - if file then - repeat - local balance = file:read("*n") - if balance == nil then - break - end - local name = file:read("*l") - b[name:sub(2)] = balance - until file:read(0) == nil - io.close(file) - end -end - -function atm.saveaccounts() - if not atm.balance then - return - end - local data = {} - for k, v in pairs(atm.balance) do - table.insert(data, string.format("%d %s\n", v, k)) - end - - local output = io.open(atm.pth, "w") - output:write(table.concat(data)) - io.close(output) -end - -function emeraldbank.update_account() - atm.readaccounts() - for _, player in ipairs(core.get_connected_players()) do - if not player or player.is_fake_player then return end - local meta = player:get_meta() - local bankemeralds = meta:get_int("emeraldbank:emerald") - local name = player:get_player_name() - if atm.balance[name] then - atm.balance[name] = math.floor(atm.balance[name] + bankemeralds) - else - atm.balance[name] = bankemeralds - end - meta:set_int("emeraldbank:emerald", 0) - -- backup - meta:set_int("emeraldbank:emerald_backup", bankemeralds) - end - atm.saveaccounts() -end - function emeraldbank.add_emeralds(player, num) - atm.readaccounts() if not player then return false end local meta = player:get_meta() local name = player:get_player_name() + atm.read_account(name) if num then if atm.balance[name] then atm.balance[name] = math.floor(atm.balance[name] + num) @@ -93,12 +33,22 @@ function emeraldbank.add_emeralds(player, num) atm.balance[name] = num end mcl_title.set(player, "actionbar", {text=S("Emeralds in Bank: @1", atm.balance[name]), color="yellow"}) - atm.saveaccounts() + atm.save_account(name) return true end return false end +function emeraldbank.update_accounts() + for _, player in ipairs(minetest.get_connected_players()) do + if not player or player.is_fake_player then return end + local meta = player:get_meta() + local bankemeralds = meta:get_int("emeraldbank:emerald") + emeraldbank.add_emeralds(player, bankemeralds) + meta:set_int("emeraldbank:emerald", 0) + end +end + function emeraldbank.inv_emeralds_to_stonks(pos) local meta = core.get_meta(pos) local inv = meta:get_inventory() @@ -138,9 +88,9 @@ function emeraldbank.get_stonks(pos) end function emeraldbank.transfer_emeralds(player1, player2, num) - atm.readaccounts() local name = player1:get_player_name() local name2 = player2:get_player_name() + atm.readaccount(name) local bankemeralds1 = atm.balance[name] if num > 0 then if bankemeralds1 and bankemeralds1 >= num then @@ -168,28 +118,6 @@ function emeraldbank.transfer_emeralds(player1, player2, num) end end - --- wire transfer data storage - -function atm.read_transactions() - local file = io.open(atm.pth_wt, "r") - if file then - local data = file:read("*all") - atm.completed_transactions = minetest.deserialize(data) - end -end - -function atm.write_transactions() - if not atm.completed_transactions then - return - end - local file = io.open(atm.pth_wt, "w") - local data = minetest.serialize(atm.completed_transactions) - file:write(data) - io.close(file) -end - - function emeraldbank.upgrade_shop(pos) local oldnode = core.get_node(pos) local old_meta = core.get_meta(pos) @@ -242,316 +170,3 @@ function emeraldbank.upgrade_shop(pos) emeraldbank.refresh_vendor(pos) end - - -function atm.showform (player) - atm.ensure_init(player:get_player_name()) - local formspec = - "size[9,8.75]".. - "label[0.5,0;"..S("Your account balance: $@1", atm.balance[player:get_player_name()]).."]" .. - "label[0.5,0.75;"..S("Deposit:").."]" .. - -- "label[0.5,0.75;1s]" .. - -- "label[1.5,0.75;5s]" .. - -- "label[2.5,0.75;10s]" .. - -- "label[3.5,0.75;50s]" .. - -- "label[4.5,0.75;100s]" .. - "label[7.5,0.75;"..S("Withdraw:").."]" .. - -- "label[6.5,0.75;1s]" .. - -- "label[7.5,0.75;5s]" .. - -- "label[8.5,0.75;10s]" .. - -- "label[9.5,0.75;50s]" .. - -- "label[10.5,0.75;100s]" .. - "item_image_button[0.5,1.25;1,1;".. "mcl_core:emerald" ..";i1;\n\n\b\b\b\b\b" .. "x1" .."]" .. - -- "item_image_button[1.5,1.25;1,1;".. "currency:minegeld_5" ..";i5;\n\n\b\b\b\b\b" .. "x1" .."]" .. - -- "item_image_button[2.5,1.25;1,1;".. "currency:minegeld_10" ..";i10;\n\n\b\b\b\b\b" .. "x1" .."]" .. - -- "item_image_button[3.5,1.25;1,1;".. "currency:minegeld_50" ..";i50;\n\n\b\b\b\b\b" .. "x1" .."]" .. - -- "item_image_button[4.5,1.25;1,1;".. "currency:minegeld_100" ..";i100;\n\n\b\b\b\b\b" .. "x1" .."]" .. - "item_image_button[7.5,1.25;1,1;".. "mcl_core:emerald" ..";i-1;\n\n\b\b\b\b\b" .. "x1" .."]" .. - -- "item_image_button[7.5,1.25;1,1;".. "currency:minegeld_5" ..";i-5;\n\n\b\b\b\b\b" .. "x1" .."]" .. - -- "item_image_button[8.5,1.25;1,1;".. "currency:minegeld_10" ..";i-10;\n\n\b\b\b\b\b" .. "x1" .."]" .. - -- "item_image_button[9.5,1.25;1,1;".. "currency:minegeld_50" ..";i-50;\n\n\b\b\b\b\b" .. "x1" .."]" .. - -- "item_image_button[10.5,1.25;1,1;".. "currency:minegeld_100" ..";i-100;\n\n\b\b\b\b\b" .. "x1" .."]" .. - "item_image_button[0.5,2.25;1,1;".. "mcl_core:emerald" ..";t10;\n\n\b\b\b\b" .. "x10" .."]" .. - -- "item_image_button[1.5,2.25;1,1;".. "currency:minegeld_5" ..";t50;\n\n\b\b\b\b" .. "x10" .."]" .. - -- "item_image_button[2.5,2.25;1,1;".. "currency:minegeld_10" ..";t100;\n\n\b\b\b\b" .. "x10" .."]" .. - -- "item_image_button[3.5,2.25;1,1;".. "currency:minegeld_50" ..";t500;\n\n\b\b\b\b" .. "x10" .."]" .. - -- "item_image_button[4.5,2.25;1,1;".. "currency:minegeld_100" ..";t1000;\n\n\b\b\b\b" .. "x10" .."]" .. - "item_image_button[7.5,2.25;1,1;".. "mcl_core:emerald" ..";t-10;\n\n\b\b\b\b" .. "x10" .."]" .. - -- "item_image_button[7.5,2.25;1,1;".. "currency:minegeld_5" ..";t-50;\n\n\b\b\b\b" .. "x10" .."]" .. - -- "item_image_button[8.5,2.25;1,1;".. "currency:minegeld_10" ..";t-100;\n\n\b\b\b\b" .. "x10" .."]" .. - -- "item_image_button[9.5,2.25;1,1;".. "currency:minegeld_50" ..";t-500;\n\n\b\b\b\b" .. "x10" .."]" .. - -- "item_image_button[10.5,2.25;1,1;".. "currency:minegeld_100" ..";t-1000;\n\n\b\b\b\b" .. "x10" .."]" .. - "item_image_button[0.5,3.25;1,1;".. "mcl_core:emerald" ..";c100;\n\n\b\b\b" .. "x100" .."]" .. - -- "item_image_button[1.5,3.25;1,1;".. "currency:minegeld_5" ..";c500;\n\n\b\b\b" .. "x100" .."]" .. - -- "item_image_button[2.5,3.25;1,1;".. "currency:minegeld_10" ..";c1000;\n\n\b\b\b" .. "x100" .."]" .. - -- "item_image_button[3.5,3.25;1,1;".. "currency:minegeld_50" ..";c5000;\n\n\b\b\b" .. "x100" .."]" .. - -- "item_image_button[4.5,3.25;1,1;".. "currency:minegeld_100" ..";c10000;\n\n\b\b\b" .. "x100" .."]" .. - "item_image_button[7.5,3.25;1,1;".. "mcl_core:emerald" ..";c-100;\n\n\b\b\b" .. "x100" .."]" .. - -- "item_image_button[7.5,3.25;1,1;".. "currency:minegeld_5" ..";c-500;\n\n\b\b\b" .. "x100" .."]" .. - -- "item_image_button[8.5,3.25;1,1;".. "currency:minegeld_10" ..";c-1000;\n\n\b\b\b" .. "x100" .."]" .. - -- "item_image_button[9.5,3.25;1,1;".. "currency:minegeld_50" ..";c-5000;\n\n\b\b\b" .. "x100" .."]" .. - -- "item_image_button[10.5,3.25;1,1;".. "currency:minegeld_100" ..";c-10000;\n\n\b\b\b" .. "x100" .."]" .. - -- "button_exit[5.5,3;1,2;Quit;Quit]" .. - "button[3.5,3;2,1;wt;"..S("Wire Transfer").."]".. - - "list[current_player;main;0,4.5;9,3;9]".. - mcl_formspec.get_itemslot_bg(0,4.5,9,3).. - "list[current_player;main;0,7.74;9,1;]".. - mcl_formspec.get_itemslot_bg(0,7.74,9,1).. - "listring[current_player;main]" - - minetest.after((0.1), function(gui) - return minetest.show_formspec(player:get_player_name(), "atm.form", gui) - end, formspec) -end - - - --- wire transfer interface - -function atm.showform_wt (player) - atm.ensure_init(player:get_player_name()) - local formspec = - "size[8,6]".. - "button[5.75,0;2,1;transactions;"..S("Transactions >").."]" .. - "label[2.5,0;"..S("Wire Transfer System").."]" .. - "label[2,0.5;"..S("Your account balance: $@1", atm.balance[player:get_player_name()]).. "]" .. - "field[0.5,1.5;5,1;dstn;"..S("Recepient:")..";]".. - "field[6,1.5;2,1;amnt;"..S("Amount:")..";]".. - "field[0.5,3;7.5,1;desc;"..S("Description:")..";]".. - "button_exit[0.2,5;1,1;Quit;"..S("Quit").."]" .. - "button[4.7,5;3,1;pay;"..S("Complete the payment").."]" - minetest.after((0.1), function(gui) - return minetest.show_formspec(player:get_player_name(), "atm.form.wt", gui) - end, formspec) -end - -function atm.showform_wtconf (player, dstn, amnt, desc) - atm.ensure_init(player:get_player_name()) - local formspec = - "size[8,6]".. - "label[2.5,0;"..S("Wire Transfer System").."]" .. - "label[2,0.5;"..S("Your account balance: $@1", atm.balance[player:get_player_name()]).. "]" .. - "label[2.5,1;"..S("TRANSACTION SUMMARY:").."]".. - "label[0.5,1.5;"..S("Recepient:").." "..dstn.."]".. - "label[0.5,2;"..S("Amount:").." " .. amnt .. "]".. - "label[0.5,2.5;"..S("Description:").." " .. desc .. "]".. - "button_exit[0.2,5;1,1;Quit;"..S("Quit").."]" .. - "button[4.7,5;3,1;cnfrm;"..S("Confirm transfer").."]" - minetest.after((0.1), function(gui) - return minetest.show_formspec(player:get_player_name(), "atm.form.wtc", gui) - end, formspec) -end - -function atm.showform_wtlist (player, tlist) - atm.ensure_init(player:get_player_name()) - - local textlist = '' - - if not tlist then - textlist = S("no transactions registered").."\n" - else - for _, entry in ipairs(tlist) do - textlist = textlist .. entry.date .. " $" .. entry.sum .. " " .. S("from") .. " " .. entry.from .. ": " .. entry.desc .. "\n" - end - end - - local formspec = - "size[8,6]".. - "button[5.75,0;2,1;transfer;"..S("< Transfer money").."]" .. - "label[2.5,0;"..S("Wire Transfer System").."]" .. - "label[2,0.5;"..S("Your account balance: $@1", atm.balance[player:get_player_name()]).. "]" .. - "textarea[0.5,1.25;7.5,4;hst;"..S("Transaction list")..";" .. textlist .. "]" .. - "button_exit[0.2,5;1,1;Quit;"..S("Quit").."]" .. - "button[4.7,5;3,1;clr;"..S("Clear transactions").."]" - minetest.after((0.1), function(gui) - return minetest.show_formspec(player:get_player_name(), "atm.form.wtl", gui) - end, formspec) -end - - -function emeraldbank.bank_receive_fields(player, form, pressed) - - -- ATMs - if form == "atm.form" then - local n = player:get_player_name() - local transaction = { amount = 0, denomination = 0, count = 0 } - local pinv=player:get_inventory() - - -- single note transactions - for _,i in pairs({1, 5, 10, 50, 100, -1, -5, -10, -50, -100}) do - if pressed["i"..i] then - transaction.amount = i - transaction.denomination = '_' .. math.abs(i) - if transaction.denomination == '_1' then - transaction.denomination = '' - end - transaction.count = ' ' .. 1 - break - end - end - - -- 10x banknote transactions - for _,t in pairs({10, 50, 100, 500, 1000, -10, -50, -100, -500, -1000}) do - if pressed["t"..t] then - transaction.amount = t - transaction.denomination = '_' .. math.abs(t/10) - if transaction.denomination == '_1' then - transaction.denomination = '' - end - transaction.count = ' ' .. 10 - break - end - end - - -- 100x banknote transactions - for _,c in pairs({100, 500, 1000, 5000, 10000, -100, -500, -1000, -5000, -10000}) do - if pressed["c"..c] then - transaction.amount = c - transaction.denomination = '_' .. math.abs(c/100) - if transaction.denomination == '_1' then - transaction.denomination = '' - end - transaction.count = ' ' .. 100 - break - end - end - - -- someone hit exchange button - if pressed.wt then - atm.showform_wt(player) - end - - if (atm.balance[n] + transaction.amount) < 0 then - minetest.chat_send_player(n, S("Not enough money in your account")) - transaction.amount = 0 - end - - local item = "mcl_core:emerald" .. transaction.denomination .. transaction.count - - if transaction.amount < 0 then - if pinv:room_for_item("main", item) then - pinv:add_item("main", item) - atm.balance[n] = atm.balance[n] + transaction.amount - else - minetest.chat_send_player(n, S("Not enough room in your inventory")) - end - - elseif transaction.amount > 0 then - if pinv:contains_item("main", item) then - pinv:remove_item("main", item) - atm.balance[n] = atm.balance[n] + transaction.amount - else - minetest.chat_send_player(n, S("Not enough money in your inventory")) - end - end - - atm.saveaccounts() - - if not pressed.Quit and not pressed.quit then - atm.showform(player) - end - end - -end - - -function emeraldbank.wt_receive_fields(player, form, pressed) - - -- Wire transfer terminals - if form == "atm.form.wt" or form == "atm.form.wtc" or form == "atm.form.wtl" then - - local n = player:get_player_name() - - if not pressed.Quit and not pressed.quit then - if form == "atm.form.wt" and pressed.transactions then - -- transaction list (can be edited in the form, but than means nothing) - atm.read_transactions() - atm.showform_wtlist(player, atm.completed_transactions[n]) - elseif form == "atm.form.wtl" and pressed.transfer then - atm.showform_wt(player) - elseif form == "atm.form.wtl" and pressed.clr then - -- clear all transactions in the player's list - atm.read_transactions() - atm.completed_transactions[n] = nil - atm.write_transactions() - minetest.chat_send_player(n, S("Your transaction history has been cleared")) - atm.showform_wtlist(player, atm.completed_transactions[n]) - elseif form == "atm.form.wt" and pressed.pay then - - -- perform the checks of validity for wire transfer order - -- if passed, store the data in a temporary table and show confirmation window - if not atm.balance[pressed.dstn] then - minetest.chat_send_player(n, S("The recepient <@1> is not registered in the banking system, aborting", pressed.dstn)) - atm.showform_wt(player) - elseif not string.match(pressed.amnt, '^[0-9]+$') then - minetest.chat_send_player(n, S("Invalid amount <@1> : must be an integer number, aborting", pressed.amnt)) - atm.showform_wt(player) - elseif atm.balance[n] < tonumber(pressed.amnt) then - minetest.chat_send_player(n, S("Your account does not have enough funds to complete this transfer, aborting")) - atm.showform_wt(player) - else - atm.pending_transfers[n] = {to = pressed.dstn, sum = tonumber(pressed.amnt), desc = pressed.desc} - atm.showform_wtconf(player, pressed.dstn, pressed.amnt, pressed.desc) - end - - elseif form == "atm.form.wtc" then - -- transaction processing - atm.read_transactions() - local t = atm.pending_transfers[n] - if not t then - return - end - if not atm.completed_transactions[t.to] then - atm.completed_transactions[t.to] = {} - end - - if atm.balance[n] < t.sum then - -- you can never be too paranoid about the funds availaible - minetest.chat_send_player(n, S("Your account does not have enough funds to complete this transfer, aborting")) - if not t.extern then - atm.showform_wt(player) - else - minetest.close_formspec(n, "atm.form.wtc") - end - return - end - - table.insert(atm.completed_transactions[t.to], {date=os.date("%Y-%m-%d"), from=n, sum=t.sum, desc=t.desc}) - atm.balance[n] = atm.balance[n] - t.sum - atm.balance[t.to] = atm.balance[t.to] + t.sum - atm.write_transactions() - atm.saveaccounts() - if minetest.get_modpath("mail") and mail.send then - mail.send({ - from = "Emerald Bank", - to = t.to, - --cc = "carbon, copy", - --bcc = "blind, carbon, copy", - subject = S("Payment of @1 emeralds from @2", t.sum, n), - body = S("Good news!\n\n@1 has transferred @2 emeralds to your bank account using the Emerald Bank Transfer System.\n\nTransfer description:\n\n@3", n, t.sum, t.desc) - }) - end - minetest.chat_send_player(n, S("Payment of @1 to @2 completed", t.sum, t.to)) - minetest.chat_send_player(n, S("@1, thank you for choosing the Emerald Bank Transfer system", n)) - if t.callback then -- run callbacks from mods - t.callback(t) - end - if t.extern == true then -- Transfer was initiated by mod - atm.pending_transfers[n] = nil - minetest.close_formspec(n, "atm.form.wtc") - return - end - atm.pending_transfers[n] = nil - atm.showform_wt(player) - end - else - -- clear the pending transaction of the player, just in case - if atm.pending_transfers[n] then - atm.pending_transfers[n] = nil - end - end - - end - -end diff --git a/init.lua b/init.lua index 420e5fc..c1baab2 100644 --- a/init.lua +++ b/init.lua @@ -35,18 +35,18 @@ atm = { pth_wt = minetest.get_worldpath().."/atm_wt_transactions" } +dofile(modpath .. "/atm.lua") -- file common.lua from atm mod dofile(modpath .. "/functions.lua") +dofile(modpath .. "/forms.lua") -- file adapted from atm mod +dofile(modpath .. "/receive_fields.lua") -- file adapted from atm mod +dofile(modpath .. "/receive_fields_wt.lua") -- file from atm mod dofile(modpath .. "/bank.lua") dofile(modpath .. "/shop.lua") -dofile(modpath .. "/fancyshop.lua") +dofile(modpath .. "/fancyshop.lua") -- file adapted from fancy_vend mod. dofile(modpath .. "/commands.lua") if income_enabled then dofile(modpath .. "/income.lua") end -minetest.register_on_joinplayer(atm.readaccounts) -minetest.register_on_joinplayer(emeraldbank.update_account) - -minetest.register_on_player_receive_fields(emeraldbank.bank_receive_fields) -minetest.register_on_player_receive_fields(emeraldbank.wt_receive_fields) +minetest.register_on_joinplayer(emeraldbank.update_accounts) diff --git a/receive_fields.lua b/receive_fields.lua new file mode 100644 index 0000000..8efc443 --- /dev/null +++ b/receive_fields.lua @@ -0,0 +1,93 @@ + +-- Check the form +local S = core.get_translator(core.get_current_modname()) + +minetest.register_on_player_receive_fields(function(player, form, pressed) + + -- ATMs + if form == "atm.form" or form == "atm.form2" or form == "atm.form3" then + local n = player:get_player_name() + local transaction = { amount = 0, denomination = 0, count = 0 } + local pinv=player:get_inventory() + + -- single note transactions + for _,i in pairs({1, 5, 10, 50, 100, -1, -5, -10, -50, -100}) do + if pressed["i"..i] then + transaction.amount = i + transaction.denomination = '_' .. math.abs(i) + if transaction.denomination == '_1' then + transaction.denomination = '' + end + transaction.count = ' ' .. 1 + break + end + end + + -- 10x banknote transactions + for _,t in pairs({10, 50, 100, 500, 1000, -10, -50, -100, -500, -1000}) do + if pressed["t"..t] then + transaction.amount = t + transaction.denomination = '_' .. math.abs(t/10) + if transaction.denomination == '_1' then + transaction.denomination = '' + end + transaction.count = ' ' .. 10 + break + end + end + + -- 100x banknote transactions + for _,c in pairs({100, 500, 1000, 5000, 10000, -100, -500, -1000, -5000, -10000}) do + if pressed["c"..c] then + transaction.amount = c + transaction.denomination = '_' .. math.abs(c/100) + if transaction.denomination == '_1' then + transaction.denomination = '' + end + transaction.count = ' ' .. 100 + break + end + end + + if pressed.wt then + atm.showform_wt(player) + end + + if (atm.balance[n] + transaction.amount) < 0 then + minetest.chat_send_player(n, S("Not enough money in your account")) + transaction.amount = 0 + end + + local item = "mcl_core:emerald" .. transaction.denomination .. transaction.count + + if transaction.amount < 0 then + if pinv:room_for_item("main", item) then + pinv:add_item("main", item) + atm.balance[n] = atm.balance[n] + transaction.amount + else + minetest.chat_send_player(n, S("Not enough room in your inventory")) + end + + elseif transaction.amount > 0 then + if pinv:contains_item("main", item) then + pinv:remove_item("main", item) + atm.balance[n] = atm.balance[n] + transaction.amount + else + minetest.chat_send_player(n, S("Not enough money in your inventory")) + end + end + + atm.save_account(n) + + if not pressed.quit then + if form == "atm.form" then + atm.showform(player) + elseif form == "atm.form2" then + atm.showform2(player) + elseif form == "atm.form3" then + atm.showform3(player) + end + end + end + +end) diff --git a/receive_fields_wt.lua b/receive_fields_wt.lua new file mode 100644 index 0000000..b4693bc --- /dev/null +++ b/receive_fields_wt.lua @@ -0,0 +1,96 @@ + +local S = core.get_translator(core.get_current_modname()) + +-- Check the form + +minetest.register_on_player_receive_fields(function(player, form, pressed) + + -- Wire transfer terminals + if form == "atm.form.wt" or form == "atm.form.wtc" or form == "atm.form.wtl" then + + local n = player:get_player_name() + + if not pressed.quit then + if form == "atm.form.wt" and pressed.transactions then + -- transaction list (can be edited in the form, but than means nothing) + atm.read_transaction(n) + atm.showform_wtlist(player, atm.completed_transactions[n]) + elseif form == "atm.form.wtl" and pressed.transfer then + atm.showform_wt(player) + elseif form == "atm.form.wtl" and pressed.clr then + -- clear all transactions in the player's list + atm.read_transaction(n) + atm.completed_transactions[n] = nil + atm.write_transaction(n) + minetest.chat_send_player(n, S("Your transaction history has been cleared")) + atm.showform_wtlist(player, atm.completed_transactions[n]) + elseif form == "atm.form.wt" and pressed.pay then + + -- perform the checks of validity for wire transfer order + -- if passed, store the data in a temporary table and show confirmation window + atm.read_account(pressed.dstn) + if not atm.balance[pressed.dstn] then + minetest.chat_send_player(n, S("The Recipient <@1> is not registered in the banking system, aborting", + pressed.dstn)) + atm.showform_wt(player) + elseif not string.match(pressed.amnt, '^[0-9]+$') then + minetest.chat_send_player(n, S("Invalid amount <@1>: must be an integer number, aborting", pressed.amnt)) + atm.showform_wt(player) + elseif atm.balance[n] < tonumber(pressed.amnt) then + minetest.chat_send_player(n, S("Your account does not have enough funds to complete this transfer, aborting")) + atm.showform_wt(player) + else + atm.pending_transfers[n] = {to = pressed.dstn, sum = tonumber(pressed.amnt), desc = pressed.desc} + atm.showform_wtconf(player, pressed.dstn, pressed.amnt, pressed.desc) + end + + elseif form == "atm.form.wtc" then + -- transaction processing + local t = atm.pending_transfers[n] + if not t then + return + end + if not atm.completed_transactions[t.to] then + atm.completed_transactions[t.to] = {} + end + atm.read_transaction(t.to) + if atm.balance[n] < t.sum then + -- you can never be too paranoid about the funds availaible + minetest.chat_send_player(n, S("Your account does not have enough funds to complete this transfer, aborting")) + if not t.extern then + atm.showform_wt(player) + else + minetest.close_formspec(n, "atm.form.wtc") + end + return + end + + table.insert(atm.completed_transactions[t.to], {date=os.date("%Y-%m-%d"), from=n, sum=t.sum, desc=t.desc}) + atm.balance[n] = atm.balance[n] - t.sum + atm.balance[t.to] = atm.balance[t.to] + t.sum + atm.write_transaction(t.to) + atm.save_account(n) + atm.save_account(t.to) + minetest.chat_send_player(n, S("Payment of @1 to @2 completed", t.sum, t.to)) + minetest.chat_send_player(n, S("@1, thank you for choosing the Wire Transfer system", n)) + if t.callback then -- run callbacks from mods + t.callback(t) + end + if t.extern == true then -- Transfer was initiated by mod + atm.pending_transfers[n] = nil + minetest.close_formspec(n, "atm.form.wtc") + return + end + atm.pending_transfers[n] = nil + atm.showform_wt(player) + end + else + -- clear the pending transaction of the player, just in case + if atm.pending_transfers[n] then + atm.pending_transfers[n] = nil + end + end + + end + +end) diff --git a/shop.lua b/shop.lua index c8287df..8706342 100644 --- a/shop.lua +++ b/shop.lua @@ -234,8 +234,6 @@ core.register_node("emeraldbank:shop_empty", { core.register_on_player_receive_fields(function(sender, formname, fields) local prefix_len = string.len(formspec_prefix) if formname:sub(1,prefix_len) == formspec_prefix then - - atm.readaccounts() local pos_string = formname:sub(prefix_len+1) local pos = core.string_to_pos(pos_string) @@ -250,6 +248,7 @@ core.register_on_player_receive_fields(function(sender, formname, fields) local shop_item = meta:get_string("shop_item") local minv = meta:get_inventory() local pinv = sender:get_inventory() + atm.read_account(name) local bankemeralds = atm.balance[name] -- set or reset timer