Cloud Code Execution
Executes a cloud code function registered on the server.
This API uses the service-api.playnanoo.com domain.
API Information
- URL:
https://service-api.playnanoo.com/cloudcode/v20210901/run - Method:
PUT - Authentication Required: Yes
The Req class of this API inherits from DeviceInfo. All properties of DeviceInfo are automatically included.
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| table_code | string | Required | Cloud code table code |
| function_name | string | Required | Function name to execute |
| function_arguments | string | Required | Function arguments (JSON string) |
Response Data
Res Class
| Field | Type | Description |
|---|---|---|
| Function | FunctionModel | Executed function information |
| Logs | List<LogModel> | Function execution logs |
| Result | ResultModel | Function execution result |
| Error | string | Error message (when error occurs) |
| RunTimeMilliSeconds | int | Execution time (milliseconds) |
FunctionModel Class
| Field | Type | Description |
|---|---|---|
| Name | string | Function name |
| Version | string | Function version |
LogModel Class
| Field | Type | Description |
|---|---|---|
| level | string | Log level |
| message | string | Log message |
ResultModel Class
| Field | Type | Description |
|---|---|---|
| message | string | Result message |
Unity C# Implementation
BaseResponse Class
The base class for all API responses.
public class BaseResponse
{
public string ErrorCode;
public string Message;
public string WithdrawalKey;
public string BlockKey;
}
Field descriptions:
ErrorCode: Error codeMessage: Error messageWithdrawalKey: Key required for recovery when in withdrawal grace period (provided only for accounts in withdrawal grace period)BlockKey: Key provided when account is blocked (provided only for blocked accounts)
Cloud Code Execution Class
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class CloudCodeExecution
{
static string path = "https://service-api.playnanoo.com/cloudcode/v20210901/run";
[Serializable]
public class Req : DeviceInfo
{
public string table_code;
public string function_name;
public string function_arguments;
public IEnumerator Send(CloudCodeParameters param, Action<Res> onSuccess, Action<BaseResponse> onError)
{
this.table_code = param.table_code;
this.function_name = param.function_name;
this.function_arguments = JsonUtility.ToJson(param.function_arguments);
yield return HttpClient.Send<Req, Res>(
UnityWebRequest.kHttpVerbPUT,
path,
requireToken: true,
body: this,
onSuccess: onSuccess,
onError: onError
);
}
}
[Serializable]
public class Res : BaseResponse
{
public FunctionModel Function;
public List<LogModel> Logs = new List<LogModel>();
public ResultModel Result;
public string Error;
public int RunTimeMilliSeconds;
}
[Serializable]
public class FunctionModel
{
public string Name;
public string Version;
}
[Serializable]
public class LogModel
{
public string level;
public string message;
}
[Serializable]
public class ResultModel
{
public string message;
}
[Serializable]
public class CloudCodeParameters
{
public string table_code;
public string function_name;
public object function_arguments;
}
[Serializable]
public class FunctionArguments
{
public string InputValue1;
public string InputValue2;
public string InputValue3;
}
}
Usage Examples
Basic Usage
using PlayNANOO;
public class PlayNANOOExample : MonoBehaviour
{
void ExecuteCloudCode()
{
CloudCodeExecution.Req req = new CloudCodeExecution.Req();
// Set function arguments
CloudCodeExecution.FunctionArguments args = new CloudCodeExecution.FunctionArguments
{
InputValue1 = "value1",
InputValue2 = "value2",
InputValue3 = "value3"
};
// Set parameters
CloudCodeExecution.CloudCodeParameters param = new CloudCodeExecution.CloudCodeParameters
{
table_code = "cloudcode_table",
function_name = "MyFunction",
function_arguments = args
};
StartCoroutine(req.Send(
param: param,
onSuccess: res =>
{
Debug.Log($"Function execution successful: {res.Function.Name} v{res.Function.Version}");
Debug.Log($"Execution time: {res.RunTimeMilliSeconds}ms");
Debug.Log($"Result: {res.Result.message}");
// Print logs
foreach (var log in res.Logs)
{
Debug.Log($"[{log.level}] {log.message}");
}
},
onError: (error) =>
{
Debug.LogError($"Cloud code execution failed: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}
Server Logic Execution
using PlayNANOO;
using UnityEngine;
public class ServerLogicExample : MonoBehaviour
{
void CalculateReward(int level, int score)
{
CloudCodeExecution.Req req = new CloudCodeExecution.Req();
// Arguments for reward calculation
var args = new
{
PlayerLevel = level,
GameScore = score,
BonusMultiplier = 1.5f
};
CloudCodeExecution.CloudCodeParameters param = new CloudCodeExecution.CloudCodeParameters
{
table_code = "game_logic",
function_name = "CalculateReward",
function_arguments = args
};
StartCoroutine(req.Send(
param: param,
onSuccess: res =>
{
if (string.IsNullOrEmpty(res.Error))
{
Debug.Log($"Reward calculation completed: {res.Result.message}");
Debug.Log($"Execution time: {res.RunTimeMilliSeconds}ms");
}
else
{
Debug.LogError($"Server logic error: {res.Error}");
}
},
onError: (error) =>
{
Debug.LogError($"API call failed: {error.Message}");
}
));
}
}
Data Validation
using PlayNANOO;
using UnityEngine;
public class DataValidationExample : MonoBehaviour
{
void ValidateGameData(string userId, int itemId, int quantity)
{
CloudCodeExecution.Req req = new CloudCodeExecution.Req();
var args = new
{
UserId = userId,
ItemId = itemId,
Quantity = quantity
};
CloudCodeExecution.CloudCodeParameters param = new CloudCodeExecution.CloudCodeParameters
{
table_code = "validation",
function_name = "ValidateItemPurchase",
function_arguments = args
};
StartCoroutine(req.Send(
param: param,
onSuccess: res =>
{
if (string.IsNullOrEmpty(res.Error))
{
Debug.Log("Data validation passed");
// Check logs
foreach (var log in res.Logs)
{
if (log.level == "error")
{
Debug.LogWarning($"Warning: {log.message}");
}
}
// Continue with purchase processing
ProcessPurchase();
}
else
{
Debug.LogError($"Data validation failed: {res.Error}");
ShowErrorMessage("This item cannot be purchased.");
}
},
onError: (error) =>
{
Debug.LogError($"Validation API failed: {error.Message}");
}
));
}
void ProcessPurchase()
{
// Purchase processing logic
}
void ShowErrorMessage(string message)
{
// Display UI error message
Debug.Log(message);
}
}
By using cloud code, you can execute game logic on the server to prevent client manipulation and safely handle complex calculations or data validation.
function_arguments is of type object, allowing you to pass anonymous objects or serializable classes. It is internally converted to a JSON string.
If the cloud code function execution time is too long, a timeout may occur. Efficient code writing is recommended.
You can check logs generated during function execution through the Logs field. Use it for debugging and monitoring.