Update to work with the new exchange rate system.
This commit is contained in:
parent
411417661c
commit
a8068f6d4a
|
@ -16,7 +16,9 @@ spaces.
|
||||||
|
|
||||||
The following lurkcoin-specific functions and variables exist:
|
The following lurkcoin-specific functions and variables exist:
|
||||||
|
|
||||||
- `lurkcoin.exchange_rate`: The exchange rate, *do not modify this!*
|
- `lurkcoin.exchange_rate`: The raw exchange rate, *do not modify this!*
|
||||||
|
- `lurkcoin.get_exchange_rate(amount, to, callback)`: Get an exchange rate
|
||||||
|
from this server.
|
||||||
- `lurkcoin.pay(from, to, server, amount, callback)`: Makes `from` pay someone
|
- `lurkcoin.pay(from, to, server, amount, callback)`: Makes `from` pay someone
|
||||||
`amount`cr, and calls `callback` on success/faliure. `callback` should have two
|
`amount`cr, and calls `callback` on success/faliure. `callback` should have two
|
||||||
parameters, `success` and `msg`.
|
parameters, `success` and `msg`.
|
||||||
|
|
28
atm-core.lua
28
atm-core.lua
|
@ -52,7 +52,7 @@ end
|
||||||
local withdrawls = false
|
local withdrawls = false
|
||||||
|
|
||||||
-- Payment screen
|
-- Payment screen
|
||||||
function formspecs.pay(name, fields)
|
function formspecs.pay(name, fields, guessed_amount)
|
||||||
fields = fields or {}
|
fields = fields or {}
|
||||||
local formspec =
|
local formspec =
|
||||||
'field[0.8,3.5;7,1;user;User to pay;' .. e(fields.user or '') .. ']' ..
|
'field[0.8,3.5;7,1;user;User to pay;' .. e(fields.user or '') .. ']' ..
|
||||||
|
@ -73,10 +73,28 @@ function formspecs.pay(name, fields)
|
||||||
formspec = formspec ..
|
formspec = formspec ..
|
||||||
'label[0.5,7;Please confirm the above values.'
|
'label[0.5,7;Please confirm the above values.'
|
||||||
if fields.server ~= lurkcoin.server_name then
|
if fields.server ~= lurkcoin.server_name then
|
||||||
local r = (tonumber(fields.amount) or 0) / lurkcoin.exchange_rate
|
local exc = -1
|
||||||
r = tostring(math.floor(r * 100) / 100)
|
if type(fields._exchange_rate) == 'number' then
|
||||||
formspec = formspec ..
|
exc = fields._exchange_rate
|
||||||
'\n' .. fields.amount .. 'cr is equal to \xc2\xa4' .. r .. '.'
|
end
|
||||||
|
|
||||||
|
if exc < 0 then
|
||||||
|
lurkcoin.get_exchange_rate(fields.amount, fields.server or
|
||||||
|
lurkcoin.server_name, function(data)
|
||||||
|
print('Exchange rate callback with data ' .. tostring(data))
|
||||||
|
if not data then
|
||||||
|
fields._err = 'That server does not exist!'
|
||||||
|
end
|
||||||
|
fields._exchange_rate = data
|
||||||
|
return lurkcoin.show_atm(name, 'pay', fields)
|
||||||
|
end)
|
||||||
|
return formspec .. '\n' .. fields.amount .. 'cr is equal to' ..
|
||||||
|
' ...\n\n(Calculating...)]'
|
||||||
|
end
|
||||||
|
|
||||||
|
exc = tostring(math.floor(exc * 100) / 100)
|
||||||
|
formspec = formspec .. '\n' .. fields.amount .. 'cr is ' ..
|
||||||
|
'equal to \xc2\xa4' .. exc .. '.'
|
||||||
end
|
end
|
||||||
formspec = formspec .. ']' ..
|
formspec = formspec .. ']' ..
|
||||||
'button[0.5,8;3.5,1;payuser;Cancel]' ..
|
'button[0.5,8;3.5,1;payuser;Cancel]' ..
|
||||||
|
|
22
core.lua
22
core.lua
|
@ -130,6 +130,25 @@ end
|
||||||
-- Start syncing once the game is loaded.
|
-- Start syncing once the game is loaded.
|
||||||
minetest.after(0, sync)
|
minetest.after(0, sync)
|
||||||
|
|
||||||
|
-- Get an exchange rate
|
||||||
|
function lurkcoin.get_exchange_rate(amount, to, callback)
|
||||||
|
assert(callback)
|
||||||
|
amount = amount and tonumber(amount)
|
||||||
|
if not amount or amount ~= amount then return callback(nil) end
|
||||||
|
|
||||||
|
get('exchange_rates', {
|
||||||
|
from = lurkcoin.server_name,
|
||||||
|
to = to or 'lurkcoin',
|
||||||
|
amount = tostring(amount),
|
||||||
|
}, function(res)
|
||||||
|
if res.code == 200 then
|
||||||
|
local amount = tonumber(res.data)
|
||||||
|
if amount == amount then return callback(amount) end
|
||||||
|
end
|
||||||
|
return callback(nil)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
-- Pay a user (cross-server)
|
-- Pay a user (cross-server)
|
||||||
function lurkcoin.pay(from, to, server, amount, callback)
|
function lurkcoin.pay(from, to, server, amount, callback)
|
||||||
assert(type(amount) == 'number' and callback)
|
assert(type(amount) == 'number' and callback)
|
||||||
|
@ -159,7 +178,8 @@ function lurkcoin.pay(from, to, server, amount, callback)
|
||||||
return get('pay', {
|
return get('pay', {
|
||||||
target = to,
|
target = to,
|
||||||
server = server,
|
server = server,
|
||||||
amount = tostring(amount / lurkcoin.exchange_rate)
|
amount = tostring(amount),
|
||||||
|
local_currency = 'true'
|
||||||
}, function(res)
|
}, function(res)
|
||||||
if res.code ~= 200 then
|
if res.code ~= 200 then
|
||||||
lurkcoin.bank.add(from, amount, 'Reverting failed transaction.')
|
lurkcoin.bank.add(from, amount, 'Reverting failed transaction.')
|
||||||
|
|
Loading…
Reference in New Issue