UNPKG

@ginstone/nga-api

Version:

109 lines (108 loc) 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserContext = void 0; const UserInfoRead_1 = require("./UserInfoRead"); const AnonymousUser_1 = require("./AnonymousUser"); const ReputationUser_1 = require("../ReputationUser"); const Medal_1 = require("../Medal"); const UserGroup_1 = require("../UserGroup"); /** * 从上下文中取出徽章数据 * @param medalId * @param userContext */ const getMedals = (medalId, userContext) => { if (!medalId) { return undefined; } return medalId.map(id => userContext.medals.get(id)); }; class UserContext { constructor(raw) { this.userInfo = new Map; this.anonymousUser = new Map; this.medals = new Map; this.groups = new Map; Object.keys(raw).forEach(key => { const value = raw[key]; if ("__GROUPS" === key) { //用户组 Object.keys(value).forEach(k => { const v = value[k]; this.groups.set(Number(k), new UserGroup_1.UserGroup(v)); }); } else if ("__MEDALS" === key) { //徽章信息 Object.keys(value).forEach(k => { const v = value[k]; this.medals.set(Number(k), new Medal_1.Medal(v)); }); } else if ("__REPUTATIONS" === key) { //声望信息 this.reputationUser = new ReputationUser_1.ReputationUser(value); } else if (key.startsWith("-")) { //匿名用户信息 this.anonymousUser.set(Number(key), new AnonymousUser_1.AnonymousUser(value)); } else if (!isNaN(parseInt(key))) { //常规用户信息 this.userInfo.set(Number(key), new UserInfoRead_1.UserInfoRead(value)); } }); } static getReputation(customLevels, reputationValue) { if (!customLevels) { return undefined; } for (let i = customLevels.length - 1; i >= 0; i--) { const level = customLevels[i]; if (reputationValue >= level.rank) { return { label: level.name, value: reputationValue, }; } } } /** * 从数组中取一个随机头像返回 * @param avatars 头像列表 */ static getRandomAvatar(avatars) { if (!avatars) { return undefined; } return avatars[Math.floor(Math.random() * avatars.length)]; } /** * 获取用户信息 * @param userId 用户id */ getUserInfo(userId) { var _a; if (!userId) { return undefined; } // 是否为匿名用户 const anonymous = userId < 0; // 用户信息 const userInfo = anonymous ? this.anonymousUser.get(userId) : this.userInfo.get(userId); // 用户组 const group = (userInfo && userInfo.memberId) ? this.groups.get(userInfo.memberId) : undefined; // 声望值 const reputationValue = this.reputationUser ? this.reputationUser.data.get(userId) : 0; // 声望等级+声望值 const reputation = UserContext.getReputation(this.customLevels, reputationValue ? reputationValue : 0); // 头像buff const avatarBuff = userInfo instanceof UserInfoRead_1.UserInfoRead ? userInfo.getAvatarBuff() : undefined; // 随机头像 : 有头像buff按照头像buff,没有头像buff随机选择 const avatar = avatarBuff ? avatarBuff.getUrl() : (userInfo instanceof UserInfoRead_1.UserInfoRead ? UserContext.getRandomAvatar(userInfo.avatars) : undefined); // 徽章 const medals = userInfo instanceof UserInfoRead_1.UserInfoRead ? (_a = userInfo.medal) === null || _a === void 0 ? void 0 : _a.map(id => this.medals.get(id)) : undefined; return Object.assign(Object.assign({}, userInfo), { group, reputation, anonymous, avatar, medals, avatarBuff }); } } exports.UserContext = UserContext;