question

Larry T McBride avatar image
Larry T McBride asked

Best Practices for recording vitrual currency purchases and spending virtual currency

First, I am very new to network code. However, we have multiplayer working through Photon so I've learned quite a bit.

We will have a virtual currency, purchased through App Store or Google Play. So I am wondering about Best Practices when coding these purchases.

We do not have dedicated servers so we will have to use the client API. I am thinking that the purchase flow could go like this: the player makes a currency purchase through the appropriate store, this purchase is then saved to the players PlayFab account and to a local variable. The local variable would be for quick reference only, for displaying to the UI and such.

And then when a player spends their Currency the code makes a call to PlayFab to check if the player has enough of the Currency. If so the transaction is allowed and the local variable and the PlayFab account is updated. If not, then the local variable is updated if it is different.

Normally, players should not have to or even be allowed to spend their currency very often. However, we could add code that prevents players from spending their currency in rapid succession to deter to many PlayFab server calls.

So the question is...is this a good way to set this up? What are the alternatives?

And how could players misuse this system?

Player Data
10 |1200

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

1 Answer

·
brendan avatar image
brendan answered

Actually, it's far simpler than that. If you're using a Virtual Currency in our system, you would just use PurchaseItem from the client. It already checks that the player has the currency, subtracts it, and places the correct item in the player inventory.

But you are correct that if your design were to allow for rapid purchases, that would not be a good use of the system. The VC/PurchaseItem path is meant for the majority of games with VC, but if you have something like a "clicker" game, where players purchase things over and over in rapid succession for much of their play, that would be a case where you would aggregate information about those changes over time and only update the backend periodically (most clickers only update every couple of minutes, with the occasional "extra" update when a major milestone is passed). Also, most of those games use far larger values than would fit in a VC, so best to just write that into the player data (using a single Key to store a bunch of current status info for the player by serializing the JSON of the data).

9 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.

Larry T McBride avatar image Larry T McBride commented ·

Wow, that is amazing actually. I hadn't made it that far in the documentation. My producer and I both said the same thing...you all are legit.

OK, what is the best practice on purchasing VC itself... As mentioned we use the App stores for purchases, so after a VC purchase and you get a call back on a successful purchase, in that call back, is this where I should then place the PlayFabClientAPI.AddUserVirtualCurrency() method?

This is the way I set it up and it worked. Or would it be better if we add an VC item (like "100 Gold Coins") to the Catalog and purchase it that way?

0 Likes 0 ·
brendan avatar image brendan Larry T McBride commented ·

The latter - create a Bundle in the Catalog, put the currency in it, and set the Bundle to have a Usage Period of 3-5 seconds. In your Stores, set up the Bundle with the pricing you want. When purchased, the Bundle will drop the currency in the player account, then clean itself up.

0 Likes 0 ·
Larry T McBride avatar image Larry T McBride brendan commented ·

Brilliant. Ok, that is purchasing VC, but what about if the player earns VC by other means like in game achievements and such...how should the process go then?

0 Likes 0 ·
Show more comments
Larry T McBride avatar image Larry T McBride commented ·

Hmm, actually, since to get the VC you have to spend real money, it doesn't make sense to have VC on the VC store. We would have to set up a Real Money transaction for that.

0 Likes 0 ·
Larry T McBride avatar image Larry T McBride commented ·

Ok, so thinking it through I assume you go through the server because it is more secure? But if the client has to initiate the call either way, how is one way more secure than the other?

Also, back to your earlier reply of purchasing VC, in the bundle setup do we just leave the currency option off?

0 Likes 0 ·
brendan avatar image brendan Larry T McBride commented ·

You mean the prices? I'd recommend not putting them in the catalog, and use the Store to specify prices. That way you can use the overrides (https://api.playfab.com/docs/tutorials/landing-players/segmentation/segment-best). And yes, when granting VC as a reward, the point is that you do it server side so that you can have whatever checks make sense for your game. You can't trust what the client sends you, but you can evaluate it using questions like "is the score reasonable", "are the preconditions for the thing they said they did actually met", etc. A dedicated server is the most secure, since you'd be running the logic for managing the game state there, rather than on the client.

0 Likes 0 ·
appmaker avatar image appmaker commented ·

@Brendan

I am creating a game that allows the players to purchase and consume items rapidly. It will also update the players virtual currency afterwords. Item purchases can be made every 1-2 seconds. Based on all the documentation I see there is a limit to how frequently you can call the PlayFab API's.

You mentioned custom/dedicated server as a option, is there a no hassle way of implementing a custom server with PlayFab? Basically I want to create my game in the way described above without worrying about limitations.

Can I request for PlayFab to allow additional API requests automatically? If so where can I find details and pricing information about it?

0 Likes 0 ·
brendan avatar image brendan appmaker commented ·

The problem with high sustained rates of API calls is that this kind of behavior is expensive. So while we could work with you on an Enterprise contract that would include the costs for the extra resources we would need to use for this, it would be more cost effective for you to operate a server that maintains state information and only periodically updates the PlayFab service with the state of the player account.

The simplest way you could approach that would be by starting with our game server project: https://github.com/PlayFab/PlayFabGameServer

0 Likes 0 ·

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.