From a8068f6d4abe4257dcc7cfbd28ac958f7f1f927b Mon Sep 17 00:00:00 2001 From: luk3yx Date: Sat, 4 May 2019 10:49:46 +1200 Subject: [PATCH] Update to work with the new exchange rate system. --- README.md | 4 +++- atm-core.lua | 28 +++++++++++++++++++++++----- core.lua | 22 +++++++++++++++++++++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7ee8647..f5d634e 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/atm-core.lua b/atm-core.lua index 95ad26d..74d1889 100644 --- a/atm-core.lua +++ b/atm-core.lua @@ -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]' .. diff --git a/core.lua b/core.lua index 2d75da6..64d0507 100644 --- a/core.lua +++ b/core.lua @@ -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.')