question

Kim Strasser avatar image
Kim Strasser asked

CloudScript: JavascriptException while updating TitleDisplayName

I'm checking in VerifyText if the text is a valid display name. If the text is valid, then I want to call handlers.UpdateDisplayname = function (args, context) because I need to check if the player already has a display name and if the desired display name is still available. It's not possible to change the display name if you have already created a display name before.

But my CloudScript fails and I get a JavascriptException and I don't know what I'm doing wrong.

What am I doing wrong in my CloudScript? Is it not possible to call UpdateDisplayname(args.text) from VerifyText?

handlers.VerifyText = function (args, context)
{
    if (VerifyTextLength(args.text) && VerifyTextASCII(args.text))
    {
        UpdateDisplayname(args.text);
        log.info(args.text);
        return "true";
    }
    else
    {
        if (!VerifyTextLength(args.text) && !VerifyTextASCII(args.text))
        {
            log.info(args.text);
            return "too long and characters are not supported";
        }
        else
        {
            if (!VerifyTextLength(args.text))
            {
                log.info(args.text);
                return "too long";
            }
            if (!VerifyTextASCII(args.text))
            {
                log.info(args.text);
                return "characters are not supported";
            }
        }
    }
}

function VerifyTextLength(text)
{
    if (text.length <= 20)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
function VerifyTextASCII(text)
{
    var notSupportArray = new Array();
    for (var i = text.length - 1; i >= 0; i--)
    {
        var charCode = text.charCodeAt(i);
        if ((charCode >= 32 && charCode <= 512) || (charCode >= 8216 && charCode <= 8222) || (charCode >= 8352 && charCode <= 8378))
        {
 
        }
        else
        {
            notSupportArray.push(charCode);
        }
    }
    if (notSupportArray.length == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

handlers.UpdateDisplayname = function (args, context)
{
  var NewDisplayname = args.DesiredDisplayname;
  var CurrentDisplayName = "";
   
   var resultprofile = server.GetPlayerProfile(
       {
           PlayFabID: currentPlayerId, 
           ProfileConstraints : {
               ShowDisplayName: true
               
           }
       });
       
       if (resultprofile.Error == null)
       {
          if (resultprofile.PlayerProfile != null)
          {
            CurrentDisplayName = resultprofile.PlayerProfile.DisplayName;
           
          if ((CurrentDisplayName == null) || (CurrentDisplayName == ""))
           {
             var result =  UpdateUserTitleDisplayNameFromCloudScript(NewDisplayname, currentPlayerId);
             if (result.error == "NameNotAvailable")
             {
               log.info("This display name already exists: " + NewDisplayname.toString() + " Please choose another display name");
       	       return "display name already exists";
             }
             
             if (result.error == "InvalidParams")
             {
               log.info("The Display name must be a string with a minimum length of 3 and a maximum length of 20.");
       	       return "Invalid Parameters";
             }
       	       
       	     log.info("Display name changed");
       	     return NewDisplayname.toString();
           }
          else
            log.info("You already have a display name. It's not possible to change it:" + CurrentDisplayName.toString());
          }
       }
}

function UpdateUserTitleDisplayNameFromCloudScript(DesiredDisplayname, PlayFabId)
{
    var contentBodyTemp = {
        "DisplayName": DesiredDisplayname,
        "PlayFabId": PlayFabId
    };
    
    let url = "https://BFD0A.playfabapi.com/Admin/UpdateUserTitleDisplayName";
    let method = "POST";
    let contentBody = `{"PlayFabId": "${PlayFabId}", "DisplayName": "${DesiredDisplayname}"}`;
    let contentType = "application/json";
    let headers = { "X-SecretKey": "..." };
    let responseString = http.request(url, method, contentBody, contentType, headers);
    let responseJSONObj = JSON.parse(responseString);
    return (responseJSONObj);
}

JavascriptException:

Raw event JSON
{
    "EventName": "player_executed_cloudscript",
    "Source": "CloudScript",
    "FunctionName": "VerifyText",
    "CloudScriptExecutionResult": {
        "FunctionName": "VerifyText",
        "Revision": 61,
        "FunctionResult": null,
        "FunctionResultTooLarge": null,
        "Logs": [],
        "LogsTooLarge": null,
        "ExecutionTimeSeconds": 0.0005488,
        "ProcessorTimeSeconds": 0,
        "MemoryConsumedBytes": 41040,
        "APIRequestsIssued": 0,
        "HttpRequestsIssued": 0,
        "Error": {
            "Error": "JavascriptException",
            "Message": "JavascriptException",
            "StackTrace": "ReferenceError: UpdateDisplayname is not defined\n    at handlers.VerifyText (BFD0A-main.js:175:9)"
        }
    },
    "EventNamespace": "com.playfab",
    "EntityType": "player",
    "TitleId": "BFD0A",
    "EntityId": "1066CF1F71C39EC1",
    "EventId": "75c1201751fd41eeaa0fdb02977cd0f9",
    "SourceType": "BackEnd",
    "Timestamp": "2019-10-09T08:43:51.8591905Z",
    "History": null,
    "CustomTags": null,
    "Reserved": null,
    "PlayFabEnvironment": {
        "Vertical": "master",
        "Cloud": "main",
        "Application": "logicserver",
        "Commit": "a881f0a"
    }
}

In the client code:

CloudScript
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

·
Seth Du avatar image
Seth Du answered

If you have defined a handler function, you need to use it via “handers.xxxx()”, for example:

handlers.UpdateDisplayname(args.text,context);

functions that defined by handlers can be find by the ExecuteCloudScript API, which means if UpdateDisplayname will only be used internally by a handler function, you should not exposed it publicly. You may just define it in the normal way:

function UpdateDisplayname(a)
{ 
    return a;

}

So that your code won’t raise a not-defined error:

UpdateDisplayname(args.text);
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.

Kim Strasser avatar image Kim Strasser commented ·

It works now. I changed it to function UpdateDisplayname(a).

1 Like 1 ·

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.