2016-02-25 23:38:19 +01:00
|
|
|
|
|
|
|
-- A telling machine. Call this file with the exchange argument.
|
|
|
|
local exchange = ...
|
|
|
|
|
|
|
|
local atm_form = "global_exchange:atm_form"
|
|
|
|
|
2016-03-05 15:05:00 +01:00
|
|
|
local function main_menu(p_name)
|
|
|
|
local balance = exchange:get_balance(p_name)
|
|
|
|
local formspec = 'size[6,2]' ..
|
|
|
|
'label[0,0;' .. p_name .. '\'s account]' ..
|
|
|
|
'label[0,0.5;Balance: ' .. balance .. ']' ..
|
|
|
|
'button[0,1;2,1;cash;Cash In/Out]' ..
|
|
|
|
'button[2,1;2,1;wire;Transfer]' ..
|
|
|
|
'button[4,1;2,1;transaction_log;History]'
|
|
|
|
return formspec
|
|
|
|
end
|
2016-02-25 23:38:19 +01:00
|
|
|
|
2016-03-05 15:04:00 +01:00
|
|
|
local function back(x,y)
|
|
|
|
return "button[" .. x .. "," .. y .. ";2,1;main;Back]"
|
2016-02-25 23:38:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function label(x,y,text)
|
|
|
|
return "label[" .. x .. "," .. y .. ";"
|
|
|
|
.. minetest.formspec_escape(text) .. "]"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function field(x,y, w,h, name, label, default)
|
|
|
|
return "field[" .. x .. "," .. y .. ";" .. w .. "," .. h .. ";"
|
|
|
|
.. name .. ";" .. minetest.formspec_escape(label) .. ";"
|
|
|
|
.. minetest.formspec_escape(default) .. "]"
|
|
|
|
end
|
|
|
|
|
|
|
|
local unique_num = 1
|
|
|
|
|
|
|
|
local function unique()
|
|
|
|
local ret = unique_num
|
|
|
|
unique_num = unique_num + 1
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2016-03-05 15:02:00 +01:00
|
|
|
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
|
2016-03-05 15:04:00 +01:00
|
|
|
'list[current_player;main;0,4;8,4;]'..
|
|
|
|
--back button
|
|
|
|
back(0,8)
|
2016-03-05 15:02:00 +01:00
|
|
|
--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
|
|
|
|
end
|
2016-02-25 23:38:19 +01:00
|
|
|
|
|
|
|
local function wire_fs(p_name)
|
|
|
|
local balance = exchange:get_balance(p_name)
|
|
|
|
|
2016-03-05 15:04:00 +01:00
|
|
|
local fs = "size[4,5]" .. back(0,4)
|
2016-02-25 23:38:19 +01:00
|
|
|
|
|
|
|
-- To prevent duplicates
|
2016-03-05 11:47:35 +01:00
|
|
|
return fs .. field(-100, -100, 0,0, "trans_id", "", unique()) ..
|
|
|
|
label(0.5,0.5, "Balance: " .. balance) ..
|
|
|
|
field(0.5,1.5, 2,1, "recipient", "Send to:", "") ..
|
|
|
|
field(0.5,2.5, 2,1, "amount", "Amount", "") ..
|
|
|
|
"button[2,4;2,1;send;Send]"
|
2016-02-25 23:38:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function send_fs(p_name, receiver, amt_str)
|
|
|
|
local fs = "size[7,3]"
|
|
|
|
|
|
|
|
local amt = tonumber(amt_str)
|
|
|
|
|
2016-03-06 12:21:27 +01:00
|
|
|
if not amt or amt <= 0 then
|
2016-03-05 11:47:35 +01:00
|
|
|
return fs .. label(0.5,0.5, "Invalid transfer amount.") ..
|
|
|
|
"button[0.5,2;2,1;wire;Back]"
|
2016-02-25 23:38:19 +01:00
|
|
|
end
|
2016-03-05 11:47:35 +01:00
|
|
|
|
2016-02-25 23:38:19 +01:00
|
|
|
local succ, err = exchange:transfer_credits(p_name, receiver, amt)
|
|
|
|
|
|
|
|
if not succ then
|
2016-03-05 11:47:35 +01:00
|
|
|
return fs .. label(0.5,0.5, "Error: " .. err) ..
|
|
|
|
"button[0.5,2;2,1;wire;Back]"
|
2016-02-25 23:38:19 +01:00
|
|
|
end
|
2016-03-05 11:47:35 +01:00
|
|
|
return fs.. label(0.5,0.5, "Successfully sent " ..
|
|
|
|
amt .. " credits to " .. receiver) ..
|
|
|
|
"button[0.5,2;2,1;wire;Back]"
|
2016-02-25 23:38:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-02-27 04:45:32 +01:00
|
|
|
local function log_fs(p_name)
|
2016-03-05 11:47:35 +01:00
|
|
|
local res = {
|
2016-03-05 15:04:00 +01:00
|
|
|
"size[8,8]label[0,0;Transaction Log]button[0,7;2,1;main;Back]",
|
2016-03-05 11:47:35 +01:00
|
|
|
"tablecolumns[text;text]",
|
|
|
|
"table[0,1;8,6;log_table;Time,Message",
|
2016-02-27 04:45:32 +01:00
|
|
|
}
|
|
|
|
|
2016-03-05 11:47:35 +01:00
|
|
|
for i, entry in ipairs(exchange:player_log(p_name)) do
|
|
|
|
i = i*4
|
|
|
|
res[i] = ","
|
|
|
|
res[i+1] = tostring(entry.Time)
|
|
|
|
res[i+2] = ","
|
|
|
|
res[i+3] = entry.Message
|
2016-02-27 04:45:32 +01:00
|
|
|
end
|
2016-03-05 11:47:35 +01:00
|
|
|
res[#res+1] ="]"
|
2016-02-27 04:45:32 +01:00
|
|
|
|
|
|
|
return table.concat(res)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-02-25 23:38:19 +01:00
|
|
|
local trans_ids = {}
|
|
|
|
|
|
|
|
|
2016-03-05 11:47:35 +01:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
2016-02-25 23:38:19 +01:00
|
|
|
if formname ~= atm_form then return end
|
2016-03-05 11:47:35 +01:00
|
|
|
if fields.quit then return true end
|
2016-02-25 23:38:19 +01:00
|
|
|
|
|
|
|
local p_name = player:get_player_name()
|
|
|
|
|
|
|
|
local this_id = fields.trans_id
|
|
|
|
|
|
|
|
if this_id and this_id == trans_ids[p_name] then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
trans_ids[p_name] = this_id
|
|
|
|
|
2016-03-05 15:04:00 +01:00
|
|
|
if fields.main then
|
2016-03-05 15:05:00 +01:00
|
|
|
minetest.show_formspec(p_name, atm_form, main_menu(p_name))
|
2016-02-25 23:38:19 +01:00
|
|
|
end
|
|
|
|
|
2016-03-05 15:02:00 +01:00
|
|
|
if fields.cash then
|
|
|
|
minetest.show_formspec(p_name, atm_form, cash_fs(p_name))
|
|
|
|
local balance = exchange:get_balance(p_name)
|
|
|
|
local inv = minetest.get_inventory({type="detached", name="global_exchange"})
|
|
|
|
inv:set_size('money', 3)
|
|
|
|
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
|
|
|
|
|
2016-03-05 11:47:35 +01:00
|
|
|
if fields.wire then
|
2016-02-25 23:38:19 +01:00
|
|
|
minetest.show_formspec(p_name, atm_form, wire_fs(p_name))
|
|
|
|
end
|
|
|
|
|
2016-03-05 11:47:35 +01:00
|
|
|
if fields.send then
|
2016-02-25 23:38:19 +01:00
|
|
|
minetest.show_formspec(p_name, atm_form,
|
|
|
|
send_fs(p_name, fields.recipient, fields.amount))
|
|
|
|
end
|
|
|
|
|
2016-03-05 11:47:35 +01:00
|
|
|
if fields.transaction_log then
|
2016-02-27 04:45:32 +01:00
|
|
|
minetest.show_formspec(p_name, atm_form, log_fs(p_name))
|
|
|
|
end
|
|
|
|
|
2016-02-25 23:38:19 +01:00
|
|
|
return true
|
2016-03-05 11:47:35 +01:00
|
|
|
end)
|
2016-02-25 23:38:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
minetest.register_node("global_exchange:atm", {
|
|
|
|
description = "ATM",
|
|
|
|
tiles = {"global_exchange_atm_top.png",
|
|
|
|
"global_exchange_atm_top.png",
|
|
|
|
"global_exchange_atm_side.png",
|
|
|
|
},
|
|
|
|
groups = {cracky=2},
|
2016-03-05 11:47:35 +01:00
|
|
|
on_rightclick = function(pos, _, clicker)
|
2016-03-05 15:05:00 +01:00
|
|
|
minetest.show_formspec(clicker:get_player_name(), atm_form, main_menu(clicker:get_player_name()))
|
2016-02-25 23:38:19 +01:00
|
|
|
end,
|
|
|
|
})
|
2016-02-27 05:55:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
minetest.register_craft( {
|
|
|
|
output = "global_exchange:atm",
|
|
|
|
recipe = {
|
|
|
|
{ "default:stone", "default:stone", "default:stone" },
|
|
|
|
{ "default:stone", "default:gold_ingot", "default:stone" },
|
|
|
|
{ "default:stone", "default:stone", "default:stone" },
|
|
|
|
}
|
|
|
|
})
|