For example, I want to convert 2000 JPY to USD. I was told that this is possible in GameSparks. How do I do it in cloud code?
Best Answer
C
Customer Support
said
about 7 years ago
Hey Nipitpon,
I might have a workaround for you, but it would require you to find some site that has a REST api that will get the exchange rates you need. Sites that offer this data for free might not give you a full list of currencies though, so you might be limited. The way this works is to keep a collection of exchange rates that you get from an external server or website and update the rate each day. In this example i am using the calls from http://fixer.io/ because it was a really easy solution.
First you need to call the REST api and get the exchange rate. You will get a response like this...
So you can stick this code into the every-day script to make sure it gets the new rates everyday.
var response = Spark.getHttp("http://api.fixer.io/latest").get().getResponseJson(); // get the response from the rate provider
Spark.runtimeCollection('exchangeRates').drop(); // clear the collection because we want completly new rates for today
var rates = response.rates; // you can skip this step, ive just got it to make the code a bit neater
var noOfRates = Object.keys(rates).length; // get the number of rates in the response so we can loop though them
for(var i = 0; i < noOfRates; i++){
var newRate = {};
newRate.id = Object.keys(rates)[i].toString();
newRate.rate = rates[Object.keys(rates)[i]];
Spark.runtimeCollection('exchangeRates').insert(newRate); // insert a new doc into the colleciton
}
This will give you a collection you can query any rate. For example if you need the EURO-> AUD rate you can query Spark.runtimeCollection.findOne({ "id" : "AUD" })
Now, this gets you all rates for euro conversion, but if you want you can use that rate to find out the relationship between each of them.
Currently there are no built-in modules that GameSparks provides for currency exchange functionality, the logic for handling conversions would have to be implemented by yourself. Additionally may I suggest raising a feature request, if it gets approved, the functionality would be added in a future release of the platform.
Regards,
Mantas
Customer Support
said
about 7 years ago
Answer
Hey Nipitpon,
I might have a workaround for you, but it would require you to find some site that has a REST api that will get the exchange rates you need. Sites that offer this data for free might not give you a full list of currencies though, so you might be limited. The way this works is to keep a collection of exchange rates that you get from an external server or website and update the rate each day. In this example i am using the calls from http://fixer.io/ because it was a really easy solution.
First you need to call the REST api and get the exchange rate. You will get a response like this...
So you can stick this code into the every-day script to make sure it gets the new rates everyday.
var response = Spark.getHttp("http://api.fixer.io/latest").get().getResponseJson(); // get the response from the rate provider
Spark.runtimeCollection('exchangeRates').drop(); // clear the collection because we want completly new rates for today
var rates = response.rates; // you can skip this step, ive just got it to make the code a bit neater
var noOfRates = Object.keys(rates).length; // get the number of rates in the response so we can loop though them
for(var i = 0; i < noOfRates; i++){
var newRate = {};
newRate.id = Object.keys(rates)[i].toString();
newRate.rate = rates[Object.keys(rates)[i]];
Spark.runtimeCollection('exchangeRates').insert(newRate); // insert a new doc into the colleciton
}
This will give you a collection you can query any rate. For example if you need the EURO-> AUD rate you can query Spark.runtimeCollection.findOne({ "id" : "AUD" })
Now, this gets you all rates for euro conversion, but if you want you can use that rate to find out the relationship between each of them.
Hope that helps,
Sean
Customer Support
said
almost 7 years ago
Hey Nipitpon,
Does that work for you?
Sean
N
Nipitpon Wongsuparatkul
said
almost 7 years ago
It's not as clean as I'd expected. I thought there would be built-in module for it. But this will have to do.
Nipitpon Wongsuparatkul
For example, I want to convert 2000 JPY to USD. I was told that this is possible in GameSparks. How do I do it in cloud code?
I might have a workaround for you, but it would require you to find some site that has a REST api that will get the exchange rates you need. Sites that offer this data for free might not give you a full list of currencies though, so you might be limited.
The way this works is to keep a collection of exchange rates that you get from an external server or website and update the rate each day.
In this example i am using the calls from http://fixer.io/ because it was a really easy solution.
First you need to call the REST api and get the exchange rate. You will get a response like this...
So you can stick this code into the every-day script to make sure it gets the new rates everyday.
This will give you a collection you can query any rate.
For example if you need the EURO-> AUD rate you can query Spark.runtimeCollection.findOne({ "id" : "AUD" })
Now, this gets you all rates for euro conversion, but if you want you can use that rate to find out the relationship between each of them.
Hope that helps,
Sean
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstTech Support
Hello Nipitpon Wongsuparatkul,
Currently there are no built-in modules that GameSparks provides for currency exchange functionality, the logic for handling conversions would have to be implemented by yourself. Additionally may I suggest raising a feature request, if it gets approved, the functionality would be added in a future release of the platform.
Regards,
Mantas
Customer Support
I might have a workaround for you, but it would require you to find some site that has a REST api that will get the exchange rates you need. Sites that offer this data for free might not give you a full list of currencies though, so you might be limited.
The way this works is to keep a collection of exchange rates that you get from an external server or website and update the rate each day.
In this example i am using the calls from http://fixer.io/ because it was a really easy solution.
First you need to call the REST api and get the exchange rate. You will get a response like this...
So you can stick this code into the every-day script to make sure it gets the new rates everyday.
This will give you a collection you can query any rate.
For example if you need the EURO-> AUD rate you can query Spark.runtimeCollection.findOne({ "id" : "AUD" })
Now, this gets you all rates for euro conversion, but if you want you can use that rate to find out the relationship between each of them.
Hope that helps,
Sean
Customer Support
Does that work for you?
Sean
Nipitpon Wongsuparatkul
It's not as clean as I'd expected. I thought there would be built-in module for it. But this will have to do.
Thanks for the suggestions.
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics