question

Matt avatar image
Matt asked

Getting and Returning Virtual Currency in CloudScript

I've got a cloud script function that handles a few different things when the user completes a level. One of the things it does is add the relevant virtual currencies based on the player's performance in that level. I'd like this cloud script function to return the current VC values so the client can make sure its values are fresh.

What I'd like to do is:

1. Get current VC values

2. Apply any earned changes based on performance

3. < other things with stats, save progress, etc >

4. Return the current VC values

The key here is that it's all one call from client to server, but I don't see a way for me to do step 1 and Get the current values (which means I can't do step 4 either). I see an Add/Subtract call I can use to adjust the values (which are now atomic?), but not a way for me to get them.

Any info would be greatly appreciated. Thanks!

-Matt

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Currently, you can approach this one of two ways:

1. Query the VC balance, add to that balance via the Server API, then return a value of the balance you got from the query, plus the amount you added.

1. Query the VC balance, add to that balance via the Server API, then let the client query for the updated VC balance after the script is complete.

We do have a backlog item to add the updated VC balance to the returned data from the Add/Subtract call, but we don't have a specific date for when that will be added to a sprint.

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt avatar image Matt commented ·

That sounds perfect, I guess my problem is...I don't see a way to query for the VC balance from the pre-defined server object API.

0 Likes 0 ·
brendan avatar image brendan Matt commented ·

Ah, I see the confusion. You can query the VC via Server/GetUserInventory.

0 Likes 0 ·
Matt avatar image Matt brendan commented ·

There it is! I read the description saying "virtual goods" and didn't look further.

0 Likes 0 ·
Matt avatar image
Matt answered

Sorry, one more cloud related question and I think I'll be good to finish this off. I save a comma-separated array of integer values in TitleData. To do that I take the values and convert them to Base64 strings. Everything is fine in game, but within the Cloud Script, I don't see how to convert from the Base64 string to a number, for instance.

It appears atob and btoa is the way to go but those both pop up errors saying they are undefined. Again, this is probably just a new-to-javascript problem.

Either way I appreciate the help!

-Matt

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image brendan commented ·

I'm not sure what you're storing in Title Data - is it a string of CSV data that you then Base64 encode? Either way, the issue is likely with the Base64 decode, as btoa expects each character of the string passed in to represent an 8-bit value. You may want to use something like the code snippet here for your decode operation: http://www.webtoolkit.info/javascript_base64.html#.WGIg1PkrJaQ.

0 Likes 0 ·
Matt avatar image
Matt answered

Wow ok, I've probably over-complicated this as well. I was always under the impression that any of the KVPs stored on the server had to be stored as base64 strings. I may have lead myself down that path because of how I encode one of the pieces.

Just to clarify, any one of my 25 (my game ID is AC50 btw) TitleData keys that represent a group of 5 levels has an integer for the version (converted to base64 string), a bit-by-bit compilation of what an entire level of my game is (converted to base64 string), and a bunch of other integers for points and such so I can easily access them from the cloud script (also converted to base64 strings). There's 1 version but 5 times the code and integers (since 5 levels are stored per KVP).

Anyway, I thought I had to save it all as base64 string, but perhaps I only had to do that for my bit-wise levelCode and the other integers I could just use .ToString().

An example of the code I used to create the integers in my c# code is:

value += Convert.ToBase64String( BitConverter.GetBytes( m_PointThresholds[ i ][ j ] ) ) + ",";

Since window.btoa/atob can't be used in the CloudScript on your servers, it'd be ideal if I could store the levelCode as base64 (I'll never need that in cloud script anyway), but everything else as just strings. I'll go ahead and try doing that and see if I can get around this entire base64 issue in the first place.

Oh, for the record, I grabbed similar code from (http://stackoverflow.com/questions/11524268/atob-not-working-in-ie) and I still got NaN with my test:

log.info( levelData[levelStartIndex + 1 + StarsPerLevel + ( i * DataPerBonus ) + 1] );
var test = base64.decode(levelData[levelStartIndex + 1 + StarsPerLevel + ( i * DataPerBonus ) + 1] )
log.info( test ); // "" - empty string
var test = Number( test );
log.info( test ); // NaN

If my using normal strings doesn't work, I'll try the code you linked as well.

Thanks Brendan! Hopefully this information can benefit others as well.

-Matt

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.