云代码执行
执行在服务器上注册的云代码函数。
URL 确认
此 API 使用 service-api.playnanoo.com 域名。
API 信息
- URL:
https://service-api.playnanoo.com/cloudcode/v20210901/run - Method:
PUT - 需要认证: 是
DeviceInfo 继承
此 API 的 Req 类继承自 DeviceInfo。DeviceInfo 的所有属性将自动包含。
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| table_code | string | 必填 | 云代码表代码 |
| function_name | string | 必填 | 要执行的函数名称 |
| function_arguments | string | 必填 | 函数参数 (JSON 字符串) |
响应数据
Res 类
| 字段 | 类型 | 说明 |
|---|---|---|
| Function | FunctionModel | 已执行的函数信息 |
| Logs | List<LogModel> | 函数执行日志 |
| Result | ResultModel | 函数执行结果 |
| Error | string | 错误消息 (发生错误时) |
| RunTimeMilliSeconds | int | 执行时间 (毫秒) |
FunctionModel 类
| 字段 | 类型 | 说明 |
|---|---|---|
| Name | string | 函数名称 |
| Version | string | 函数版本 |
LogModel 类
| 字段 | 类型 | 说明 |
|---|---|---|
| level | string | 日志级别 |
| message | string | 日志消息 |
ResultModel 类
| 字段 | 类型 | 说明 |
|---|---|---|
| message | string | 结果消息 |
Unity C# 实现
BaseResponse 类
所有 API 响应的基类。
public class BaseResponse
{
public string ErrorCode;
public string Message;
public string WithdrawalKey;
public string BlockKey;
}
字段说明:
ErrorCode: 错误代码Message: 错误消息WithdrawalKey: 处于注销等待状态时用于恢复的密钥 (仅在注销等待中的账户提供)BlockKey: 账户被封禁时提供的密钥 (仅在被封禁的账户提供)
云代码执行类
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;
}
}
使用示例
基本用法
using PlayNANOO;
public class PlayNANOOExample : MonoBehaviour
{
void ExecuteCloudCode()
{
CloudCodeExecution.Req req = new CloudCodeExecution.Req();
// 함수 인자 설정
CloudCodeExecution.FunctionArguments args = new CloudCodeExecution.FunctionArguments
{
InputValue1 = "value1",
InputValue2 = "value2",
InputValue3 = "value3"
};
// 파라미터 설정
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($"함수 실행 성공: {res.Function.Name} v{res.Function.Version}");
Debug.Log($"실행 시간: {res.RunTimeMilliSeconds}ms");
Debug.Log($"결과: {res.Result.message}");
// 로그 출력
foreach (var log in res.Logs)
{
Debug.Log($"[{log.level}] {log.message}");
}
},
onError: (error) =>
{
Debug.LogError($"클라우드코드 실행 실패: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}
服务器逻辑执行
using PlayNANOO;
using UnityEngine;
public class ServerLogicExample : MonoBehaviour
{
void CalculateReward(int level, int score)
{
CloudCodeExecution.Req req = new CloudCodeExecution.Req();
// 보상 계산을 위한 인자
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($"보상 계산 완료: {res.Result.message}");
Debug.Log($"실행 시간: {res.RunTimeMilliSeconds}ms");
}
else
{
Debug.LogError($"서버 로직 에러: {res.Error}");
}
},
onError: (error) =>
{
Debug.LogError($"API 호출 실패: {error.Message}");
}
));
}
}
数据验证
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("데이터 검증 통과");
// 로그 확인
foreach (var log in res.Logs)
{
if (log.level == "error")
{
Debug.LogWarning($"경고: {log.message}");
}
}
// 구매 처리 계속 진행
ProcessPurchase();
}
else
{
Debug.LogError($"데이터 검증 실패: {res.Error}");
ShowErrorMessage("구매할 수 없는 아이템입니다.");
}
},
onError: (error) =>
{
Debug.LogError($"검증 API 실패: {error.Message}");
}
));
}
void ProcessPurchase()
{
// 구매 처리 로직
}
void ShowErrorMessage(string message)
{
// UI 에러 메시지 표시
Debug.Log(message);
}
}
云代码活用
使用云代码可以在服务器端执行游戏逻辑,防止客户端篡改,并安全地处理复杂计算或数据验证。
函数参数
function_arguments 为 object 类型,可以传递匿名对象或可序列化的类。内部会转换为 JSON 字符串。
执行时间
如果云代码函数的执行时间过长,可能会发生超时。建议编写高效的代码。
日志活用
可以通过 Logs 字段查看函数执行过程中产生的日志。请用于调试和监控。