Code style changes and typo fixes.

This commit is contained in:
luk3yx 2019-09-19 18:48:39 +12:00
parent a5314b5005
commit d5224e7124
4 changed files with 127 additions and 112 deletions

View File

@ -39,6 +39,9 @@ The following lurkcoin-specific functions and variables exist:
parameters, `success` and `msg`. parameters, `success` and `msg`.
- `lurkcoin.server_name`: The account name used to log into lurkcoin. - `lurkcoin.server_name`: The account name used to log into lurkcoin.
*Callbacks may or may not be pcall()ed, this is liable to change and you should
not rely on errors bringing down the server or just being logged.*
### Bank API ### Bank API
This mod creates a "universal"™ bank API. This mod creates a "universal"™ bank API.

View File

@ -170,8 +170,9 @@ if minetest.get_modpath('currency') then
if not lurkcoin.bank.add(pname, m, 'Deposit') then if not lurkcoin.bank.add(pname, m, 'Deposit') then
player:get_inventory():add_item('main', stack) player:get_inventory():add_item('main', stack)
end end
core.log('action', 'Player ' .. pname .. ' deposts ' .. core.log('action', 'Player ' .. pname .. ' deposits ' ..
stack:to_string() .. ' into an ATM.') tostring(m) .. 'Mg (' .. stack:to_string() ..
') into a lurkcoin ATM.')
end end
end end
inv:set_list(listname, {}) inv:set_list(listname, {})
@ -239,124 +240,131 @@ function lurkcoin.show_atm(name, page, params)
end end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == 'lurkcoin:atm' then if formname ~= 'lurkcoin:atm' then return end
local name = player:get_player_name()
if withdrawls then local name = player:get_player_name()
if fields.deposit then
return lurkcoin.show_atm(name, 'deposit')
elseif fields.wd then
local amount = tonumber(fields.withdraw)
if not amount or amount ~= amount or amount <= 0 then
return lurkcoin.show_atm(name, 'error',
'ERROR: Invalid number!')
end
local bal = lurkcoin.bank.getbal(name)
if amount > bal then
return lurkcoin.show_atm(name, 'error',
'ERROR: You cannot afford to do that!')
end
local note = false -- These probably don't need to be deleted.
for id, item in pairs(withdrawls) do fields._err = nil
if (not note or id > note) and fields._exchange_rate = nil
math.floor(amount / id) * id == amount then
local def = minetest.registered_items[item] if withdrawls then
if def and amount / id <= (def.stack_max or 99) then if fields.deposit then
note = id return lurkcoin.show_atm(name, 'deposit')
end elseif fields.wd then
local amount = tonumber(fields.withdraw)
if not amount or amount ~= amount or amount <= 0 then
return lurkcoin.show_atm(name, 'error',
'ERROR: Invalid number!')
end
local bal = lurkcoin.bank.getbal(name)
if amount > bal then
return lurkcoin.show_atm(name, 'error',
'ERROR: You cannot afford to do that!')
end
local note = false
for id, item in pairs(withdrawls) do
if (not note or id > note) and
math.floor(amount / id) * id == amount then
local def = minetest.registered_items[item]
if def and amount / id <= (def.stack_max or 99) then
note = id
end end
end end
end
if not note then if not note then
return lurkcoin.show_atm(name, 'error', return lurkcoin.show_atm(name, 'error',
'ERROR: I cannot store that amount of\nmoney in a ' .. 'ERROR: I cannot store that amount of\nmoney in a ' ..
'single stack of notes/coins!') 'single stack of notes/coins!')
end
local stack = ItemStack({
name = withdrawls[note],
count = amount / note,
})
local inv = player:get_inventory()
if not inv:room_for_item('main', stack) then
return lurkcoin.show_atm(name, 'error',
'ERROR: You do not have enough inventory space!')
end
minetest.log('action', 'Player ' .. name .. ' withdraws ' ..
tostring(amount) .. 'Mg (' .. stack:to_string() ..
') from a lurkcoin ATM.')
lurkcoin.bank.subtract(name, amount)
inv:add_item('main', stack)
return lurkcoin.show_atm(name, 'success')
end
end
if fields.payconfirm and open_atms[name] and open_atms[name].pay then
local data = open_atms[name]
for k, v in pairs(data) do
if type(v) == 'number' then fields[k] = tonumber(fields[k]) end
if k ~= 'pay' and fields[k] ~= v then
if k ~= 'amount' then
k = 'target ' .. k
end end
fields._err = 'The ' .. k ..
local stack = ItemStack({ ' was modified before pressing confirm!'
name = withdrawls[note], lurkcoin.show_atm(name, 'pay', fields)
count = amount / note, return
})
local inv = player:get_inventory()
if not inv:room_for_item('main', stack) then
return lurkcoin.show_atm(name, 'error',
'ERROR: You do not have enough inventory space!')
end
lurkcoin.bank.subtract(name, amount)
inv:add_item('main', stack)
return lurkcoin.show_atm(name, 'success')
end end
end end
lurkcoin.show_atm(name, 'processing')
if fields.payconfirm and open_atms[name] and open_atms[name].pay then lurkcoin.pay(name, data.user, data.server, data.amount,
local data = open_atms[name] function(success, msg)
for k, v in pairs(data) do local page = success and 'success' or 'error'
if type(v) == 'number' then fields[k] = tonumber(fields[k]) end return lurkcoin.show_atm(name, page, msg)
if k ~= 'pay' and fields[k] ~= v then
if k ~= 'amount' then
k = 'target ' .. k
end
fields._err = 'The ' .. k ..
' was modified before pressing confirm!'
lurkcoin.show_atm(name, 'pay', fields)
return
end
end end
lurkcoin.show_atm(name, 'processing') )
lurkcoin.pay(name, data.user, data.server, data.amount, elseif fields.payuser or fields.paysubmit or fields.payconfirm then
function(success, msg) if fields.paysubmit then
local page = success and 'success' or 'error' local amount = tonumber(fields.amount)
return lurkcoin.show_atm(name, page, msg) if not amount or amount ~= amount or amount <= 0 then
end fields._err = 'Invalid number!'
) lurkcoin.show_atm(name, 'pay', fields)
elseif fields.payuser or fields.paysubmit or fields.payconfirm then return
if fields.paysubmit then elseif lurkcoin.bank.getbal(name) - amount < 0 then
local amount = tonumber(fields.amount) fields._err = 'You cannot afford to do that!'
if not amount or amount ~= amount or amount <= 0 then lurkcoin.show_atm(name, 'pay', fields)
fields._err = 'Invalid number!' return
lurkcoin.show_atm(name, 'pay', fields) elseif fields.server then
return fields.server = fields.server:gsub('^ *(.-) *$', '%1')
elseif lurkcoin.bank.getbal(name) - amount < 0 then end
fields._err = 'You cannot afford to do that!'
lurkcoin.show_atm(name, 'pay', fields) if fields.user then
return fields.user = fields.user:gsub('^ *(.-) *$', '%1')
elseif fields.server then end
fields.server = fields.server:gsub('^ *(.-) *$', '%1')
end if not fields.server or fields.server == '' then
fields.server = lurkcoin.server_name
if fields.user then end
fields.user = fields.user:gsub('^ *(.-) *$', '%1')
end open_atms[name] = {
pay = true,
if not fields.server or fields.server == '' then user = fields.user or '',
fields.server = lurkcoin.server_name server = fields.server,
end amount = amount,
}
open_atms[name] = { if open_atms[name].user == '' or (
pay = true, fields.server == lurkcoin.server_name and
user = fields.user or '', not lurkcoin.bank.user_exists(open_atms[name].user)
server = fields.server, ) then
amount = amount, fields._err = 'That user does not exist!'
} lurkcoin.show_atm(name, 'pay', fields)
if open_atms[name].user == '' or ( return
fields.server == lurkcoin.server_name and
not lurkcoin.bank.user_exists(open_atms[name].user)
) then
fields._err = 'That user does not exist!'
lurkcoin.show_atm(name, 'pay', fields)
return
end
end end
fields._err = nil
lurkcoin.show_atm(name, 'pay', fields)
elseif fields.home then
lurkcoin.show_atm(name, 'main')
elseif fields.quit then
open_atms[name] = nil
end end
fields._err = nil
lurkcoin.show_atm(name, 'pay', fields)
elseif fields.home then
lurkcoin.show_atm(name, 'main')
elseif fields.quit then
open_atms[name] = nil
end end
end) end)

View File

@ -29,9 +29,12 @@ if not http then
'Please add lurkcoin to secure.http_mods in minetest.conf.') 'Please add lurkcoin to secure.http_mods in minetest.conf.')
end end
-- Download functions -- This user agent is not strictly required, however I might do something
-- with the lurkcoin mod version later.
lurkcoin.user_agent = 'Minetest ' .. minetest.get_version().string .. lurkcoin.user_agent = 'Minetest ' .. minetest.get_version().string ..
' (with lurkcoin mod v' .. tostring(lurkcoin.version) .. ')' ' (with lurkcoin mod v' .. tostring(lurkcoin.version) .. ')'
-- Download functions
local function get(url, data, callback) local function get(url, data, callback)
if not data then if not data then
data = {} data = {}
@ -155,7 +158,7 @@ function lurkcoin.pay(from, to, server, amount, callback)
-- Run lurkcoin.bank.pay() if this is not a cross-server transaction. -- Run lurkcoin.bank.pay() if this is not a cross-server transaction.
if not server or server == '' or server:lower() == if not server or server == '' or server:lower() ==
lurkcoin.server_name:lower() then lurkcoin.server_name:lower() then
return callback(lurkcoin.bank.pay(from, to, amount)) return callback(lurkcoin.bank.pay(from, to, amount))
end end

View File

@ -31,11 +31,12 @@ if not minetest.get_modpath('cloaking') or not cloaking.hide_player then
table.insert(minetest.registered_on_chat_messages, 1, function(name, msg) table.insert(minetest.registered_on_chat_messages, 1, function(name, msg)
if msg:find('[\r\n]') then if msg:find('[\r\n]') then
minetest.chat_send_player(name, minetest.chat_send_player(name,
'Error sending message: https://xkcd.com/327') 'New lines are not permitted in chat messages')
return true return true
end end
end) end)
-- Also tweak minetest.log because of paranoia.
local log = minetest.log local log = minetest.log
function minetest.log(level, text) function minetest.log(level, text)
level = level:gsub('[\r\n]', ' ') level = level:gsub('[\r\n]', ' ')