..:D&G:.. Posted August 16, 2015 Share Posted August 16, 2015 Hey guys, I am trying to make a window that is showing the real life currency exchange rates. Something like "£1 -> $1.56" the thing is I don't know how to get the real life currency and how to make it that simple, because google's currency thing gives me a longer number... Link to comment
t3wz Posted August 16, 2015 Share Posted August 16, 2015 You can use a site that provides the currency exchange in an api, something like this or this. Link to comment
..:D&G:.. Posted August 16, 2015 Author Share Posted August 16, 2015 You can use a site that provides the currency exchange in an api, something like this or this. Those are API's and I have to make a website for that, which I don't really want to atm, I want something simple I found this https://www.google.com/finance/converter , But I don't know how to call it so I can select the currencies and amount in-game, and also display only the result of the conversion.. Link to comment
t3wz Posted August 16, 2015 Share Posted August 16, 2015 [quote name=..&G:..]Those are API's and I have to make a website for that, which I don't really want to atm, I want something simple I found this https://www.google.com/finance/converter , But I don't know how to call it so I can select the currencies and amount in-game, and also display only the result of the conversion.. you don't need to create a website... fetchRemote ( "http://api.fixer.io/latest", function ( response, error ) if error == 0 then response = fromJSON ( response ) -- the response is encoded in json, then we need to use fromJSON print ( "1 EUR == "..response["rates"]["BRL"].." BRL" ) else print ( "error "..error.." when getting the currency." ) end end ) Link to comment
..:D&G:.. Posted August 16, 2015 Author Share Posted August 16, 2015 [quote name=..&G:..]Those are API's and I have to make a website for that, which I don't really want to atm, I want something simple I found this https://www.google.com/finance/converter , But I don't know how to call it so I can select the currencies and amount in-game, and also display only the result of the conversion.. you don't need to create a website... fetchRemote ( "http://api.fixer.io/latest", function ( response, error ) if error == 0 then response = fromJSON ( response ) -- the response is encoded in json, then we need to use fromJSON print ( "1 EUR == "..response["rates"]["BRL"].." BRL" ) else print ( "error "..error.." when getting the currency." ) end end ) And how do I use that? I never used a fetchRemote function before... Like, how do I make a command like "/checkexchange 1 eur" ? Link to comment
t3wz Posted August 16, 2015 Share Posted August 16, 2015 (edited) [quote name=..&G:..]And how do I use that? I never used a fetchRemote function before... Like, how do I make a command like "/checkexchange 1 eur" ? fetchRemote gets the 'text' of a site (in this case http://api.fixer.io/latest) and send it as parameter of the function defined in the second parameter. the response is something like that: { "base": "EUR", "date": "2015-08-14", "rates": { "AUD": 1.5122, "BGN": 1.9558, "BRL": 3.91, "CAD": 1.4571, "CHF": 1.0875, "CNY": 7.1396, "CZK": 27.028, "DKK": 7.4633, "GBP": 0.7145, "HKD": 8.664, "HRK": 7.5435, "HUF": 310.69, "IDR": 15387.8, "ILS": 4.2187, "INR": 72.62, "JPY": 138.7, "KRW": 1314.78, "MXN": 18.326, "MYR": 4.5583, "NOK": 9.1375, "NZD": 1.7033, "PHP": 51.557, "PLN": 4.1862, "RON": 4.4257, "RUB": 72.452, "SEK": 9.4308, "SGD": 1.5667, "THB": 39.322, "TRY": 3.1652, "USD": 1.1171, "ZAR": 14.293 } } ... in JSON format, then we use fromJSON and convert it to Lua tables. table = { ["base"] = "EUR", ["date"] = "2015-08-14", ["rates"] = { ["AUD"] = = 1.5122, ["BGN"] = = 1.9558 ["BRL"] = 3.91, ["CAD"] = 1.4571, ["CHF"] = 1.0875, ["CNY"] = 7.1396, ["CZK"] = 27.028, ["DKK"] = 7.4633, ["GBP"] = 0.7145, ["HKD"] = 8.664, ["HRK"] = 7.5435, ["HUF"] = 310.69, ["IDR"] = 15387.8, ["ILS"] = 4.2187, ["INR"] = 72.62, ["JPY"] = 138.7, ["KRW"] = 1314.78, ["MXN"] = 18.326, ["MYR"] = 4.5583, ["NOK"] = 9.1375, ["NZD"] = 1.7033, ["PHP"] = 51.557, ["PLN"] = 4.1862, ["RON"] = 4.4257, ["RUB"] = 72.452, ["SEK"] = 9.4308, ["SGD"] = 1.5667, ["THB"] = 39.322, ["TRY"] = 3.1652, ["USD"] = 1.1171, ["ZAR"] = 14.293 } } with the table we can convert 1 EUR to 1 USD for example with table["rates"]["USD"] and make a command like this: currencies = {} -- table that will store all the currencies. function updateCurrencies ( ) -- function that will get the currencies from the site and update him at the table fetchRemote ( "http://api.fixer.io/latest", function ( response, error ) if error == 0 then local decode = fromJSON ( response ) currencies = decode["rates"] --set the currencies table equal to the response of the fetchRemote function else print ( "error "..error.." when getting the currency." ) end end ) return true end updateCurrencies() -- call the function for the first time setTimer ( updateCurrencies, 1 * 3600000, 0 ) -- update the values (call the function again) every one hour. function convertCommand ( thePlayer, cmd, currency, number ) -- command: /convert [currency] [number] (ex: /convert USD 2 will convert 2 EURs to 2 USDs) if currency and number and tonumber(number) and tonumber(number) > 0 then if currencies[currency] then -- if is a valid currency local number = tonumber ( number ) local result = currencies[currency] * number outputChatBox ( number.." EUR == "..result.." "..currency, thePlayer, 0, 255, 0 ) else outputChatBox ( "Invalid currency", thePlayer, 255, 0, 0 ) end else outputChatBox ( "Syntax: /convert [currency] [number]", thePlayer, 255, 0, 0 ) end end addCommandHandler ( "convert", convertCommand ) hope u understand. obs: the values (in the site) are updated daily (so there's no need to use fetchRemote every hour, that's just an example..) Edited August 16, 2015 by Guest Link to comment
..:D&G:.. Posted August 16, 2015 Author Share Posted August 16, 2015 The "updateCurrencies" function I didn't understand, but how do I convert USDs to Pounds for example? That function converts Euros to other thing. I see that the default currency is Euro, but how do I convert something different than Euros? Link to comment
t3wz Posted August 16, 2015 Share Posted August 16, 2015 as written in the page: Rates are quoted against the Euro by default. Quote against a different currency by setting the base parameter in your request. [url=http://api.fixer.io/latest?base=USD]http://api.fixer.io/latest?base=USD[/url] and what you don't understood in updateCurrencies function? Link to comment
..:D&G:.. Posted August 16, 2015 Author Share Posted August 16, 2015 You added the function in the fetchRemote and I don't get how that works... Also, I did this to change the currency but it doesn't seem to work, it gives me invalid currency after I try to convert something ---EDIT It shows me that the currency is invalid only when I try to convert the same currency... But it work. Any idea how to get the currency that I've set so I can change where it says "EUR =="? function updateCurrencies ( currency ) fetchRemote ( "http://api.fixer.io/latest?base="..currency, function ( response, error ) if error == 0 then local decode = fromJSON ( response ) currencies = decode["rates"] else print ( "error "..error.." when getting the currency." ) end end ) return true end --updateCurrencies() setTimer ( updateCurrencies, 1 * 3600000, 0 ) function updateCurrencyLink(thePlayer, cmd, currency) updateCurrencies(currency) outputChatBox(currency, thePlayer) end addCommandHandler("setcurrency", updateCurrencyLink) Link to comment
t3wz Posted August 16, 2015 Share Posted August 16, 2015 [...] It shows me that the currency is invalid only when I try to convert the same currency... But it work. Any idea how to get the currency that I've set so I can change where it says "EUR =="? updateCurrencies function only gets the currencies from the site, it does not return anything useful. you've to do something like this: currencies = {} function updateCurrencies ( ) fetchRemote ( "http://api.fixer.io/latest?base=USD", -- se, i'veh changed the url... function ( response, error ) if error == 0 then local decode = fromJSON ( response ) currencies = decode["rates"] else print ( "error "..error.." when getting the currency." ) end end ) return true end updateCurrencies() setTimer ( updateCurrencies, 1 * 3600000, 0 ) function convertCommand ( thePlayer, cmd, currency, number ) if currency and number and tonumber(number) and tonumber(number) > 0 then if currencies[currency] then local number = tonumber ( number ) local result = currencies[currency] * number outputChatBox ( number.." USD == "..result.." "..currency, thePlayer, 0, 255, 0 ) -- changing EUR to USD doesn't change anything on the script, it's just a string... else outputChatBox ( "Invalid currency", thePlayer, 255, 0, 0 ) end else outputChatBox ( "Syntax: /convert [currency] [number]", thePlayer, 255, 0, 0 ) end end addCommandHandler ( "convert", convertCommand ) about fetchRemote, if you go to the wiki you'll see the parameters of this function, the second parameter (ignoring the optional) has to be an function (which will receive the 'results' of the fetch) sorry if my explications is confusing you, my english does not help Link to comment
..:D&G:.. Posted August 16, 2015 Author Share Posted August 16, 2015 The thing is that I want to set the default currency from a window, so I want to make a command to set it. Link to comment
t3wz Posted August 17, 2015 Share Posted August 17, 2015 [quote name=..&G:..]The thing is that I want to set the default currency from a window, so I want to make a command to set it. Something like that? Server: local validCurrencies = { ["AUD"] = true, ["BGN"] = true, ["BRL"] = true, ["CAD"] = true, ["CHF"] = true, ["CNY"] = true, ["CZK"] = true, ["DKK"] = true, ["GBP"] = true, ["HKD"] = true, ["HRK"] = true, ["HUF"] = true, ["IDR"] = true, ["ILS"] = true, ["INR"] = true, ["JPY"] = true, ["KRW"] = true, ["MXN"] = true, ["MYR"] = true, ["NOK"] = true, ["NZD"] = true, ["PHP"] = true, ["PLN"] = true, ["RON"] = true, ["RUB"] = true, ["SEK"] = true, ["SGD"] = true, ["THB"] = true, ["TRY"] = true, ["USD"] = true, ["ZAR"] = true, ["EUR"] = true } function convert ( thePlayer, cmd, from, to, amount ) if from and to and amount and tonumber(amount) and tonumber(amount) > 0 then if validCurrencies[from] then if validCurrencies[to] then fetchRemote ( "http://api.fixer.io/latest?base="..from, function ( response, error ) if error == 0 then local results = fromJSON ( response ) triggerClientEvent ( thePlayer, "onResponse", thePlayer, from, to, tonumber ( amount ), results["rates"][to] ); else outputChatBox ( "Error "..error.." when getting the currency, contact an admin", thePlayer, 255, 0, 0 ) end end ) else outputChatBox ( "Invalid currency (to)", thePlayer, 255, 0, 0 ) end else outputChatBox ( "Invalid currency (from)", thePlayer, 255, 0, 0 ) end else outputChatBox ( "Syntax: /convert [FROM] [TO] [AMOUNT]", thePlayer, 255, 0, 0 ) end end addCommandHandler ( "convert", convert ) Client: function onResponse ( from, to, amount, result ) result = result * amount outputChatBox ( "results sucesfully passed to the client. ("..result..")" ) --[[ here you use guiSetText or something like that. parameters: @ from = A currency (string), ex: EUR @ to = A currency (string), ex: USD @ amount = How much the player want to calculate (int), ex: 2 @ result = How much 'to' is 'from' (int), ex: $1.2 (without the $) --]] end addEvent ( "onResponse", true ) addEventHandler ( "onResponse", root, onResponse ) of course you need to edit it (add the guis, remove the command if you want etc) Link to comment
..:D&G:.. Posted August 17, 2015 Author Share Posted August 17, 2015 May I ask why you did "result = result * amount"? Just curious Link to comment
t3wz Posted August 18, 2015 Share Posted August 18, 2015 [quote name=..&G:..]May I ask why you did "result = result * amount"? Just curious If the player wants to convert 20 EURs to x USDs for example, result return how much is 1 USD, then we need to do that math to convert 1 USD to 20 USDs Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now