Documentation

UpdateCloudScript

Creates a new Cloud Script revision and uploads source code to it. Note that at this time, only one file should be submitted in the revision.

Request Details

UpdateCloudScriptRequest
Request Properties
DeveloperPlayFabId String

PlayFab user ID of the developer initiating the request.

Files (required) List<CloudScriptFile>

List of Cloud Script files to upload to create the new revision. Must have at least one file.

Publish (required) Boolean

Immediately publish the new revision

POST https://{{TitleID}}.playfabapi.com/Admin/UpdateCloudScript
    Content-Type: application/json
    X-SecretKey: <developer_secret_key>
{
  "Files": [
    {
      "Filename": "main.js",
      "FileContents": "var LevelRewards =\r\n[\r\n\t[\"TestItem1\"],\r\n\t[\"TestItem2\"],\r\n\t[\"TestItem3\"],\r\n\t[\"TestItem1\", \"TestItem2\"],\r\n\t[\"TestItem2\", \"TestItem2\"],\r\n\t[\"TestItem3\", \"TestItem3\"]\r\n]\r\n\r\nhandlers.onLevelComplete = function(args)\r\n{\r\n\tvar levelNum = args.level;\r\n\t\r\n\t// Do some basic input validation\r\n\tif(levelNum < 0 || levelNum >= LevelRewards.length)\r\n\t{\r\n\t\tlog.info(\"Invalid level \"+levelNum+\" completed by \"+currentPlayerId);\r\n\t\treturn {};\r\n\t}\r\n\t\r\n\tvar levelCompleteKey = \"LevelCompleted\"+levelNum;\r\n\t\r\n\t// Get the user's internal data\r\n\tvar playerInternalData = server.GetUserInternalData(\r\n\t{\r\n\t\tPlayFabId: currentPlayerId,\r\n\t\tKeys: [levelCompleteKey]\r\n\t});\r\n\t\r\n\t// Did they already complete this level?\r\n\tif(playerInternalData.Data[levelCompleteKey])\r\n\t{\r\n\t\tlog.info(\"Player \"+currentPlayerId+\" already completed level \"+levelNum);\r\n\t\treturn {};\r\n\t}\r\n\t\r\n\tvar rewards = LevelRewards[levelNum];\r\n\t\r\n\tvar resultItems = null;\r\n\tif(rewards)\r\n\t{\r\n\t\t// Grant reward items to player for completing the level\r\n\t\tvar itemGrantResult = server.GrantItemsToUser(\r\n\t\t{\r\n\t\t\tPlayFabId: currentPlayerId,\r\n\t\t\tAnnotation: \"Given by completing level \"+levelNum,\r\n\t\t\tItemIds: rewards\r\n\t\t});\r\n\t\t\r\n\t\tresultItems = itemGrantResult.ItemGrantResults;\r\n\t}\r\n\t\r\n\t// Mark the level as being completed so they can't get the reward again\r\n\tvar saveData = {};\r\n\tsaveData[levelCompleteKey] = \"true\";\r\n\tserver.UpdateUserInternalData(\r\n\t{\r\n\t\tPlayFabId: currentPlayerId,\r\n\t\tData: saveData\r\n\t});\r\n\r\n\t// Return the results of the item grant so the client can see what they got\r\n\treturn {\r\n\t\trewards: resultItems\r\n\t};\r\n}\r\n\r\nvar monsterRewards =\r\n{\r\n\t\"skrill\" : { \"ChumpCoins\" : 100},\r\n\t\"lumpur\" : { \"ChumpCoins\" : 200}\r\n}\r\n\r\nvar killCoolDown = 60;\r\n\r\nfunction currTimeSeconds()\r\n{\r\n\tvar now = new Date();\r\n\treturn now.getTime() / 1000;\r\n}\r\n\r\nhandlers.monsterKilled = function(args)\r\n{\r\n\tvar monsterType = args.type;\r\n\t\r\n\tvar now = currTimeSeconds();\r\n\t\r\n\t// Get the user's internal data\r\n\tvar playerInternalData = server.GetUserInternalData(\r\n\t{\r\n\t\tPlayFabId: currentPlayerId,\r\n\t\tKeys: [\"lastKill\"]\r\n\t});\r\n\t\r\n\t// Check when the last time they killed a monster was \r\n\tvar lastKill = playerInternalData.Data[\"lastKill\"];\r\n\tif(lastKill)\r\n\t{\r\n\t\t// We have a value, see when it is\r\n\t\tvar lastKillTime = parseInt(lastKill.Value);\r\n\t\tif(now - lastKillTime < killCoolDown)\r\n\t\t{\r\n\t\t\t// In this particular game, it should not be possible to kill a monster more often than once a minute, so they might be cheating\r\n\t\t\tlog.info(\"Player \"+currentPlayerId+\" killed \"+monsterType+\" again too quickly!\");\r\n\t\t\treturn {};\r\n\t\t}\r\n\t}\r\n\t\r\n\tvar killReward = monsterRewards[monsterType];\r\n\t\r\n\tif(killReward)\r\n\t{\r\n\t\tfor(var currency in killReward)\r\n\t\t{\r\n\t\t\tvar amount = killReward[currency];\r\n\t\t\tserver.AddUserVirtualCurrency({ PlayFabId: currentPlayerId, VirtualCurrency: currency, Amount: amount });\r\n\t\t}\r\n\t}\r\n\t\r\n\t// Reset the kill timer\r\n\tserver.UpdateUserInternalData(\r\n\t{\r\n\t\tPlayFabId: currentPlayerId,\r\n\t\tData: {\r\n\t\t\t\"lastKill\" : String(now)\r\n\t\t}\r\n\t});\r\n\t\r\n\treturn {\r\n\t\trewards: killReward\r\n\t};\r\n}"
    }
  ],
  "Publish": false
}
        

Response Details

UpdateCloudScriptResult
Result Properties
Revision (required) int32

New revision number created

Version (required) int32

Cloud Script version updated

Sample Response

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
  "code": 200,
  "status": "OK",
  "data": {
    "Version": 1,
    "Revision": 5
  }
}

Authentication

SecretKey

Possible Error Codes

Name Code
JavascriptException 1099
ServiceUnavailable 1123