parent
848bb95693
commit
df17356000
@ -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)
|
@ -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
|
@ -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)
|
@ -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)
|
Loading…
Reference in new issue