UNPKG

kitten-cloud-function

Version:

用于编程猫源码云功能(云变量、云列表等)的客户端工具

111 lines (110 loc) 4.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KittenCloudPrivateVariable = void 0; const kitten_cloud_variable_1 = require("./kitten-cloud-variable"); const kitten_cloud_data_update_source_1 = require("./update/kitten-cloud-data-update-source"); const kitten_cloud_private_variable_set_command_1 = require("./update/command/kitten-cloud-private-variable-set-command"); const codemao_user_1 = require("../../codemao/user/codemao-user"); const other_1 = require("../../utils/other"); /** * 私有云变量。 */ class KittenCloudPrivateVariable extends kitten_cloud_variable_1.KittenCloudVariable { constructor(connection, group, cvid, name, value) { super(connection, group, cvid, name, value); this.group = group; this.getRankingListResolveArray = []; this.getRankingListRejectArray = []; } update(value) { this.updateManager.addUpdateCommand(new kitten_cloud_private_variable_set_command_1.KittenCloudPrivateVariableSetCommand(kitten_cloud_data_update_source_1.KittenCloudDataUpdateSource.CLOUD, this, value)); } /** * 设置私有云变量的值。 * * @param value 要设置的值 */ set(value) { value = this.singleValueProcess(value); const command = new kitten_cloud_private_variable_set_command_1.KittenCloudPrivateVariableSetCommand(kitten_cloud_data_update_source_1.KittenCloudDataUpdateSource.LOCAL, this, value); this.updateManager.addUpdateCommand(command); command.promise.catch((error) => { this.connection.errored.emit(error); }); return command.promise; } /** * 获取排名列表。 * * @param limit 限制数量,列表的长度不超过限制数量 * @param order 排名顺序,1 为顺序,-1 为逆序 * @returns 排名列表 */ getRankingList(limit, order) { if (!Number.isInteger(limit)) { throw new Error(`限制必须为整数,得到${limit}`); } if (limit <= 0) { throw new Error(`限制必须大于 0,得到${limit}`); } if (order != 1 && order != -1) { throw new Error("顺序只能为 1(顺序) 或 -1(逆序)"); } this.group.sendGetRankingList(this, { cvid: this.cvid, limit: limit, order_type: order }); return new Promise((resolve, reject) => { this.getRankingListResolveArray.push(resolve); this.getRankingListRejectArray.push(reject); }); } handleReceiveRankingList(data) { const resolve = this.getRankingListResolveArray.shift(), reject = this.getRankingListRejectArray.shift(); if (data == other_1.None) { reject(new Error("收到了空排名数据")); return; } if (typeof data != "object") { reject(new Error("无法识别的排名数据:" + data)); return; } if (!("items" in data && Array.isArray(data.items))) { reject(new Error("无法识别的排名数据:" + JSON.stringify(data))); return; } const list = data.items; const result = []; const errorArray = []; for (const item of list) { if (item == other_1.None) { errorArray.push(new Error("排名数据为空")); } else if (typeof item != "object") { errorArray.push(new Error("无法识别的排名数据:" + item)); } else if (!("value" in item && typeof item.value == "number" && "nickname" in item && typeof item.nickname == "string" && "avatar_url" in item && typeof item.avatar_url == "string" && "identifier" in item && typeof item.identifier == "string")) { errorArray.push(new Error("无法识别的排名数据:" + JSON.stringify(item))); } else { result.push({ value: item.value, user: new codemao_user_1.CodemaoUser({ id: parseInt(item.identifier), nickname: item.nickname, avatarURL: item.avatar_url, }) }); } } resolve(result); if (errorArray.length > 0) { reject(errorArray); } } } exports.KittenCloudPrivateVariable = KittenCloudPrivateVariable;