跳转到主要内容

写入日志

可以记录客户端产生的各种信息。

URL 确认

此 API 使用 service-api.playnanoo.com 域名。

API 信息

  • URL: https://service-api.playnanoo.com/gamelog/v20220901/save
  • Method: PUT
  • 需要认证: 是
DeviceInfo 继承

此 API 的 Req 类继承自 DeviceInfo。DeviceInfo 的所有属性将自动包含。

请求参数

参数类型必填说明
table_codestring必填表代码
log_valuestring必填日志数据 (JSON 字符串)
表代码

在保存游戏日志之前,需要在 PlayNANOO 管理控制台中创建表代码。

响应数据

Res 类

字段类型说明
Statusstring处理结果状态

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 SaveLog
{
static string path = "https://service-api.playnanoo.com/gamelog/v20220901/save";

[Serializable]
public class Req : DeviceInfo
{
//필수
public string table_code;
public string log_value;

public IEnumerator Send(string tableCode, List<LogParamsValueModel> logs, Action<Res> onSuccess, Action<BaseResponse> onError)
{
if (!string.IsNullOrEmpty(tableCode)) this.table_code = tableCode;

if(logs != null && logs.Count > 0)
{
LogParamsModel model = new LogParamsModel { items = logs };
this.log_value = JsonUtility.ToJson(model);
}

yield return HttpClient.Send<Req, Res>(
UnityWebRequest.kHttpVerbPUT,
path,
requireToken: true,
body: this,
onSuccess: onSuccess,
onError: onError
);
}
}

[Serializable]
public class Res : BaseResponse
{
public string Status;
}

[Serializable]
public class LogParamsModel
{
public List<LogParamsValueModel> items;
}

[Serializable]
public class LogParamsValueModel
{
public string key;

public string value;
}

}

使用示例

基本用法

using PlayNANOO;
using System.Collections.Generic;

public class PlayNANOOExample : MonoBehaviour
{
void SaveGameLog()
{
SaveLog.Req req = new SaveLog.Req();

// 로그 데이터 생성
List<SaveLog.LogParamsValueModel> logs = new List<SaveLog.LogParamsValueModel>
{
new SaveLog.LogParamsValueModel { key = "event_type", value = "level_complete" },
new SaveLog.LogParamsValueModel { key = "level", value = "5" },
new SaveLog.LogParamsValueModel { key = "score", value = "1000" },
new SaveLog.LogParamsValueModel { key = "time_spent", value = "120" }
};

StartCoroutine(req.Send(
tableCode: "your-gamelog-tablecode",
logs: logs,
onSuccess: res =>
{
Debug.Log($"게임 로그 저장 성공: {res.Status}");
},
onError: (error) =>
{
Debug.LogError($"게임 로그 저장 실패: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}

玩家行为日志记录

using PlayNANOO;
using System.Collections.Generic;

public class PlayerActionLogger : MonoBehaviour
{
void LogPlayerAction(string actionType, Dictionary<string, string> actionData)
{
SaveLog.Req req = new SaveLog.Req();

// Dictionary를 LogParamsValueModel 리스트로 변환
List<SaveLog.LogParamsValueModel> logs = new List<SaveLog.LogParamsValueModel>();
logs.Add(new SaveLog.LogParamsValueModel { key = "action_type", value = actionType });

foreach (var kvp in actionData)
{
logs.Add(new SaveLog.LogParamsValueModel { key = kvp.Key, value = kvp.Value });
}

StartCoroutine(req.Send(
tableCode: "player_actions",
logs: logs,
onSuccess: res =>
{
Debug.Log($"플레이어 행동 로그 저장 성공");
},
onError: (error) =>
{
Debug.LogError($"플레이어 행동 로그 저장 실패: [{error.ErrorCode}] [{error.Message}]");
}
));
}

// 사용 예시
void OnPlayerPurchase()
{
Dictionary<string, string> purchaseData = new Dictionary<string, string>
{
{ "item_id", "sword_001" },
{ "item_name", "Legendary Sword" },
{ "price", "1000" },
{ "currency", "gold" }
};

LogPlayerAction("purchase", purchaseData);
}
}

游戏进度日志

using PlayNANOO;
using System.Collections.Generic;

public class GameProgressLogger : MonoBehaviour
{
void LogStageComplete(int stageNumber, int score, float playTime)
{
SaveLog.Req req = new SaveLog.Req();

List<SaveLog.LogParamsValueModel> logs = new List<SaveLog.LogParamsValueModel>
{
new SaveLog.LogParamsValueModel { key = "event", value = "stage_complete" },
new SaveLog.LogParamsValueModel { key = "stage", value = stageNumber.ToString() },
new SaveLog.LogParamsValueModel { key = "score", value = score.ToString() },
new SaveLog.LogParamsValueModel { key = "play_time", value = playTime.ToString("F2") },
new SaveLog.LogParamsValueModel { key = "timestamp", value = System.DateTime.UtcNow.ToString("o") }
};

StartCoroutine(req.Send(
tableCode: "stage_progress",
logs: logs,
onSuccess: res =>
{
Debug.Log($"스테이지 완료 로그 저장 성공");
},
onError: (error) =>
{
Debug.LogError($"스테이지 완료 로그 저장 실패: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}
日志数据格式

日志数据以 LogParamsValueModel 列表形式传递,内部会自动转换为 JSON 字符串。可以通过 key-value 对记录各种数据。

自动 JSON 转换

Send 方法会自动将 LogParamsValueModel 列表转换为 JSON 字符串并设置到 log_value 中。

数据大小限制

日志数据应保持在适当的大小范围内。过大的数据可能会影响网络性能。