Browse Source

Update to work with the new exchange rate system.

master
luk3yx 3 months ago
parent
commit
a8068f6d4a
3 changed files with 47 additions and 7 deletions
  1. 3
    1
      README.md
  2. 23
    5
      atm-core.lua
  3. 21
    1
      core.lua

+ 3
- 1
README.md View File

@@ -16,7 +16,9 @@ spaces.

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
`amount`cr, and calls `callback` on success/faliure. `callback` should have two
parameters, `success` and `msg`.

+ 23
- 5
atm-core.lua View File

@@ -52,7 +52,7 @@ end
local withdrawls = false

-- Payment screen
function formspecs.pay(name, fields)
function formspecs.pay(name, fields, guessed_amount)
fields = fields or {}
local formspec =
'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 ..
'label[0.5,7;Please confirm the above values.'
if fields.server ~= lurkcoin.server_name then
local r = (tonumber(fields.amount) or 0) / lurkcoin.exchange_rate
r = tostring(math.floor(r * 100) / 100)
formspec = formspec ..
'\n' .. fields.amount .. 'cr is equal to \xc2\xa4' .. r .. '.'
local exc = -1
if type(fields._exchange_rate) == 'number' then
exc = fields._exchange_rate
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
formspec = formspec .. ']' ..
'button[0.5,8;3.5,1;payuser;Cancel]' ..

+ 21
- 1
core.lua View File

@@ -130,6 +130,25 @@ end
-- Start syncing once the game is loaded.
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)
function lurkcoin.pay(from, to, server, amount, callback)
assert(type(amount) == 'number' and callback)
@@ -159,7 +178,8 @@ function lurkcoin.pay(from, to, server, amount, callback)
return get('pay', {
target = to,
server = server,
amount = tostring(amount / lurkcoin.exchange_rate)
amount = tostring(amount),
local_currency = 'true'
}, function(res)
if res.code ~= 200 then
lurkcoin.bank.add(from, amount, 'Reverting failed transaction.')

Loading…
Cancel
Save