记录分数
用于在排行榜中记录分数的 API。
URL 确认
此 API 使用 service-api.playnanoo.com 域名。
API 信息
- URL:
https://service-api.playnanoo.com/leaderboard/v20240301/record - Method:
PUT - 需要认证: 是
DeviceInfo 继承
此 API 的 Req 类继承自 DeviceInfo。DeviceInfo 的所有属性会自动包含。
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| uid | string | 必填 | 表代码 |
| record_id | string | 必填 | 记录 ID(玩家标识符) |
| score | double | 必填 | 分数 |
| extra_data | string | 必填 | 附加数据(JSON 字符串) |
响应数据
Res 类
| 字段 | 类型 | 说明 |
|---|---|---|
| Status | 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 UnityEngine.Networking;
public class Record
{
static string path = "https://service-api.playnanoo.com/leaderboard/v20240301/record";
[Serializable]
public class Req : DeviceInfo
{
public string uid;
public string record_id;
public double score;
public string extra_data;
public IEnumerator Send(string table_code, string recordId, double score, string extraData, Action<Res> onSuccess, Action<BaseResponse> onError)
{
if (!string.IsNullOrEmpty(table_code)) this.uid = table_code;
if (!string.IsNullOrEmpty(recordId)) this.record_id = recordId;
if (!string.IsNullOrEmpty(extraData)) this.extra_data = extraData;
this.score = score;
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;
}
}
使用示例
public void RecordScore()
{
Record.Req req = new Record.Req();
// 추가 데이터를 JSON으로 직렬화
var extraData = new
{
level = 10,
playtime = 3600
};
string jsonExtra = JsonUtility.ToJson(extraData);
StartCoroutine(req.Send(
table_code: "my_leaderboard_table",
recordId: "player_001",
score: 1500.5,
extraData: jsonExtra,
onSuccess: res =>
{
Debug.Log("점수 기록 성공");
Debug.Log($"상태: {res.Status}");
},
onError: error =>
{
Debug.LogError($"점수 기록 실패: [{error.ErrorCode}] {error.Message}");
}
));
}
记录分数
记录的分数将根据表设置(record_type)进行处理:
- highscore:仅保留最高分
- lowscore:仅保留最低分
- sum:累加所有分数
- last:更新为最近的分数
附加数据
extra_data 中可以以 JSON 格式包含玩家的等级、游戏时间、道具信息等与分数一起保存的附加信息。