Make lurkcoin.change_bank pcall-safe.

This commit is contained in:
luk3yx 2019-09-19 09:04:59 +12:00
parent fd43ddfc35
commit 88bee36efe
1 changed files with 37 additions and 40 deletions

View File

@ -10,36 +10,32 @@ function lurkcoin.change_bank(bank)
assert(type(bank) == 'table') assert(type(bank) == 'table')
assert(bank.getbal and bank.setbal) assert(bank.getbal and bank.setbal)
-- Set lurkcoin.bank to the new bank
lurkcoin.bank = bank
bank = nil
-- Get the current mod name -- Get the current mod name
if not lurkcoin.bank.mod then if not bank.mod then
lurkcoin.bank.mod = minetest.get_current_modname() or '???' bank.mod = minetest.get_current_modname() or '???'
end end
-- Make sure "getbal" has a consistent return result and add user_exists if -- Make sure "getbal" has a consistent return result and add user_exists if
-- it doesn't. -- it doesn't.
if type(lurkcoin.bank.getbal('\194\164 Fake user')) ~= 'number' then if type(bank.getbal('\194\164 Fake user')) ~= 'number' then
local getbal = lurkcoin.bank.getbal local getbal = bank.getbal
if not lurkcoin.bank.user_exists then if not bank.user_exists then
function lurkcoin.bank.user_exists(name) function bank.user_exists(name)
return getbal(name) and true or false return getbal(name) and true or false
end end
end end
function lurkcoin.bank.getbal(name) function bank.getbal(name)
return getbal(name) or 0 return getbal(name) or 0
end end
end end
assert(lurkcoin.bank.user_exists) assert(bank.user_exists)
-- Make sure "setbal" has a consistent return value. -- Make sure "setbal" has a consistent return value.
do do
local setbal = lurkcoin.bank.setbal local setbal = bank.setbal
function lurkcoin.bank.setbal(name, amount, reason) function bank.setbal(name, amount, reason)
if type(amount) ~= 'number' or amount ~= amount then if type(amount) ~= 'number' or amount ~= amount then
return false return false
end end
@ -53,9 +49,9 @@ function lurkcoin.change_bank(bank)
end end
-- Make sure "changebal" exists -- Make sure "changebal" exists
if not lurkcoin.bank.changebal then if not bank.changebal then
if lurkcoin.bank.add and lurkcoin.bank.subtract then if bank.add and bank.subtract then
function lurkcoin.bank.changebal(name, amount, reason) function bank.changebal(name, amount, reason)
if amount == 0 then if amount == 0 then
return true return true
elseif type(amount) ~= 'number' or amount ~= amount then elseif type(amount) ~= 'number' or amount ~= amount then
@ -63,58 +59,56 @@ function lurkcoin.change_bank(bank)
end end
if amount > 0 then if amount > 0 then
return lurkcoin.bank.add(name, amount, reason) return bank.add(name, amount, reason)
else else
return lurkcoin.bank.subtract(name, amount, reason) return bank.subtract(name, amount, reason)
end end
end end
else else
function lurkcoin.bank.changebal(name, amount, reason) function bank.changebal(name, amount, reason)
if amount == 0 then if amount == 0 then
return true return true
elseif type(amount) ~= 'number' or amount ~= amount then elseif type(amount) ~= 'number' or amount ~= amount then
print('Not a number')
return false return false
end end
local resulting_bal = lurkcoin.bank.getbal(name) + amount local resulting_bal = bank.getbal(name) + amount
if resulting_bal < 0 then if resulting_bal < 0 then
print('Cannot afford to do that')
return false return false
end end
return lurkcoin.bank.setbal(name, resulting_bal, reason) return bank.setbal(name, resulting_bal, reason)
end end
end end
end end
-- Make sure "add" exists -- Make sure "add" exists
if not lurkcoin.bank.add then if not bank.add then
function lurkcoin.bank.add(name, amount, reason) function bank.add(name, amount, reason)
if amount < 0 then if amount < 0 then
return false return false
end end
return lurkcoin.bank.changebal(name, amount, reason) return bank.changebal(name, amount, reason)
end end
end end
-- Make sure "subtract" exists -- Make sure "subtract" exists
if not lurkcoin.bank.subtract then if not bank.subtract then
function lurkcoin.bank.subtract(name, amount, reason) function bank.subtract(name, amount, reason)
if amount < 0 then if amount < 0 then
return false return false
end end
return lurkcoin.bank.changebal(name, 0 - amount, reason) return bank.changebal(name, 0 - amount, reason)
end end
end end
-- Make sure "pay" exists, and if so, make sure it has consistent return -- Make sure "pay" exists, and if so, make sure it has consistent return
-- values. -- values.
if lurkcoin.bank.pay then if bank.pay then
local pay = lurkcoin.bank.pay local pay = bank.pay
function lurkcoin.bank.pay(from, to, amount) function bank.pay(from, to, amount)
if type(amount) ~= 'number' or amount ~= amount then if type(amount) ~= 'number' or amount ~= amount then
return false, 'Invalid number!' return false, 'Invalid number!'
end end
@ -127,30 +121,30 @@ function lurkcoin.change_bank(bank)
end end
end end
else else
function lurkcoin.bank.pay(from, to, amount) function bank.pay(from, to, amount)
if type(amount) == 'number' then if type(amount) == 'number' then
amount = math.floor(amount * 100) / 100 amount = math.floor(amount * 100) / 100
end end
if not lurkcoin.bank.user_exists(from) or not if not bank.user_exists(from) or not
lurkcoin.bank.user_exists(to) then bank.user_exists(to) then
return false, 'The specified user does not exist!' return false, 'The specified user does not exist!'
elseif type(amount) ~= 'number' or amount ~= amount or elseif type(amount) ~= 'number' or amount ~= amount or
amount <= 0 then amount <= 0 then
return false, 'Invalid number!' return false, 'Invalid number!'
elseif lurkcoin.bank.getbal(from) - amount < 0 then elseif bank.getbal(from) - amount < 0 then
return false, 'You cannot afford to do that!' return false, 'You cannot afford to do that!'
end end
local success = false local success = false
if lurkcoin.bank.subtract(from, amount, 'Transaction to ' .. if bank.subtract(from, amount, 'Transaction to ' ..
to) then to) then
success = lurkcoin.bank.add(to, amount, 'Transaction from ' .. success = bank.add(to, amount, 'Transaction from ' ..
from) from)
-- Revert failed transactions -- Revert failed transactions
if not success and success ~= nil then if not success and success ~= nil then
lurkcoin.bank.add(from, amount, bank.add(from, amount,
'Reverting failed transaction.') 'Reverting failed transaction.')
end end
end end
@ -163,6 +157,9 @@ function lurkcoin.change_bank(bank)
return false, 'Error processing transaction!' return false, 'Error processing transaction!'
end end
end end
-- Set lurkcoin.bank to the new bank.
lurkcoin.bank = bank
end end
-- Built-in mod integrations -- Built-in mod integrations