In my game the player can choose his country in a list. After the player has chosen his country, I call my CloudScript UpdateCountryReadOnlyData to create the country data in the player's profile.
Is it possible to find out in my CloudScript code if the country code "PlayerDataCountryCode" is a valid country code?
For example:
If PlayerDataCountryCode = US, then create the country data in the player's profile because "US" is a valid country code.
case US: NewCountry = "United States";
If PlayerDataCountryCode = XX, then don't create the country data in the player's profile because "XX" is not a valid country code of this list: https://api.playfab.com/playstream/events/datatype/CountryCode
How can I find out if "PlayerDataCountryCode" is in this list?
Client code:
private async Task UpdateCountry() { var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest() { FunctionName = "UpdateCountryReadOnlyData", FunctionParameter = new { PlayerDataCountryCode = "US" }, GeneratePlayStreamEvent = true }); if (result.Error != null) Console.WriteLine(result.Error.Error.ToString()); else { if (result.Result.Logs.Count() > 0) { Console.WriteLine(result.Result.Logs[0].Message); Console.WriteLine("Country is " + result.Result.FunctionResult); } else Console.WriteLine("Country is " + result.Result.FunctionResult); } }
CloudScript:
handlers.UpdateCountryReadOnlyData = function (args, context) { var resultdata = server.GetUserReadOnlyData({PlayFabId: currentPlayerId, Keys: "Country"}); // first search for the current country info var CurrentCountry = ""; var NewCountryCodeIsvalid = false; var countrycode = args.PlayerDataCountry; var NewCountry = ChooseaCountry(args.PlayerDataCountry); if(resultdata.Data.hasOwnProperty("Country")) { // if country info already exists in readonly data if ((resultdata.Data.Country.Value != null) && (resultdata.Data.Country.Value != "")) { // make sure the value is not null or "" CurrentCountry = resultdata.Data.Country.Value; log.info("Already has country info, current country is:" + CurrentCountry.toString()); // if country value already exists, notify the client that Country info already been set } else { // if country value is null or "", set the Country value server.UpdateUserReadOnlyData({ PlayFabId: currentPlayerId, Data: { "Country": NewCountry } }); if (NewCountryCodeIsvalid == false) log.info(NewCountry.toString() + " " + "is not a valid country name. Please enter a valid country name."); } } else {// if country hasn't been set yet, set it for the user server.UpdateUserReadOnlyData({ PlayFabId: currentPlayerId, Data: { "Country": NewCountry } }); if (NewCountryCodeIsvalid == false) log.info(NewCountry.toString() + " " + "is not a valid country name. Please enter a valid country name."); } return server.GetUserReadOnlyData({PlayFabID: currentPlayerId, Keys: "Country"}).Data.Country.Value; // return the current country info to the clients. } function ChooseaCountry(countrycode) { switch (countrycode) { case "US": NewCountryCodeIsvalid = true; return("United States"); break; case "DE": NewCountryCodeIsvalid = true; return("Germany"); break; case "FR": NewCountryCodeIsvalid = true; return("France"); break; default: // if the countrycode is not valid, for example if the countrycode is "XX" NewCountryCodeIsvalid = false; return(""); break; } }