UserMgr.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import {
  2. Sprite,
  3. Label,
  4. resources,
  5. ImageAsset,
  6. Texture2D,
  7. SpriteFrame,
  8. assetManager,
  9. } from "cc";
  10. import { UserInfo } from "../shared/types/UserInfo";
  11. import { lobbyNet } from "./NetGameServer";
  12. import { UserLocalCache } from "./UserLocalCache";
  13. import { SpriteUtils } from "./SpriteUtils";
  14. const NAMES_PREFIX = [
  15. "神秘的",
  16. "勇敢的",
  17. "活泼的",
  18. "机智的",
  19. "冷静的",
  20. "优雅的",
  21. "狂野的",
  22. "聪明的",
  23. "忠诚的",
  24. "奔放的",
  25. "威严的",
  26. "敏捷的",
  27. "热情的",
  28. "坚定的",
  29. "宁静的",
  30. "强大的",
  31. "好奇的",
  32. "温和的",
  33. "活泼的",
  34. "无畏的",
  35. ];
  36. const NAMES = [
  37. "晨风",
  38. "星辰",
  39. "云起",
  40. "月影",
  41. "风行",
  42. "寒霜",
  43. "炎舞",
  44. "秋叶",
  45. "碧落",
  46. "雷鸣",
  47. "流光",
  48. "雪见",
  49. "岩松",
  50. "梦露",
  51. "灵溪",
  52. "林风语",
  53. "寒江雪",
  54. "碧水寒",
  55. "烈焰行",
  56. "星辰变",
  57. "风中翼",
  58. "月下影",
  59. "雷霆怒",
  60. "梦无痕",
  61. "秋水寒",
  62. "流云飞",
  63. "雪中舞",
  64. "岩谷风",
  65. "梦境花",
  66. "灵山月",
  67. ];
  68. export class UserMgr {
  69. private static _inst: UserMgr;
  70. public static get inst(): UserMgr {
  71. if (!this._inst) {
  72. this._inst = new UserMgr();
  73. }
  74. return this._inst;
  75. }
  76. constructor() {}
  77. private _userInfo: UserInfo = {
  78. uid: "",
  79. name: "",
  80. visualId: 0,
  81. gender: 0,
  82. introduction: "",
  83. };
  84. private _roomId: string;
  85. private _userInfoMap: { [key: string]: UserInfo } = {};
  86. public getRandomName(randomNew: boolean = false) {
  87. if (!randomNew) {
  88. return (
  89. UserLocalCache.inst.getRoleName(this.uid) || this.getRandomName(true)
  90. );
  91. }
  92. let pre = NAMES_PREFIX[Math.floor(Math.random() * NAMES_PREFIX.length)];
  93. let name = pre + NAMES[Math.floor(Math.random() * NAMES.length)];
  94. UserLocalCache.inst.storeRoleName(this.uid, name);
  95. return name;
  96. }
  97. public setUserInfo(info: UserInfo) {
  98. for (let key in info) {
  99. this._userInfo[key] = info[key];
  100. }
  101. this._userInfoMap[this._userInfo.uid] = this._userInfo;
  102. }
  103. public get uid(): string {
  104. return this._userInfo.uid;
  105. }
  106. public get name(): string {
  107. return this._userInfo.name;
  108. }
  109. public get visualId(): number {
  110. return this._userInfo.visualId;
  111. }
  112. public get roomId(): string {
  113. return this._roomId;
  114. }
  115. public get coin(): number {
  116. return this._userInfo.coin || 0;
  117. }
  118. async setUserIconAndName(
  119. userId: string,
  120. sprIcon: Sprite,
  121. lblName?: Label,
  122. bundleName?: string
  123. ) {
  124. if (!sprIcon && !lblName) {
  125. return;
  126. }
  127. let info = await this.rpc_GetUserInfo(userId);
  128. if (!info) {
  129. return;
  130. }
  131. if (lblName && lblName.isValid) {
  132. lblName.string = info.name || "User_" + userId;
  133. }
  134. if (sprIcon && sprIcon.isValid) {
  135. SpriteUtils.setUserIcon(sprIcon, info.visualId, bundleName);
  136. }
  137. }
  138. async rpc_GetUserInfo(userId: string) {
  139. let info = this._userInfoMap[userId];
  140. if (info) {
  141. return info as UserInfo;
  142. }
  143. if (!userId) {
  144. return null;
  145. }
  146. let ret = await lobbyNet.callApi("lobby/GetUserInfo", { uid: userId });
  147. if (!ret.isSucc || !ret.res.infos.length) {
  148. return null;
  149. }
  150. info = ret.res.infos[0];
  151. this._userInfoMap[info.uid] = info;
  152. return info;
  153. }
  154. async rpc_GetUserInfos(userIds: Array<string>) {
  155. let uncachedIds = [];
  156. for (let i = 0; i < userIds.length; ++i) {
  157. let userId = userIds[i];
  158. if (!this._userInfoMap[userId]) {
  159. uncachedIds.push(userId);
  160. }
  161. }
  162. if (uncachedIds.length == 0) {
  163. return this._userInfoMap;
  164. }
  165. let ret = await lobbyNet.callApi("lobby/GetUserInfo", {
  166. uids: uncachedIds,
  167. });
  168. if (!ret.isSucc || !ret.res.infos.length) {
  169. return null;
  170. }
  171. for (let i = 0; i < ret.res.infos.length; ++i) {
  172. let info = ret.res.infos[i];
  173. this._userInfoMap[info.uid] = info;
  174. }
  175. return this._userInfoMap;
  176. }
  177. async rpc_ModifyUserInfo(
  178. gender: number | undefined,
  179. introduction: string | undefined
  180. ) {
  181. let ret = await lobbyNet.callApi("lobby/ModifyUserInfo", {
  182. gender: gender,
  183. introduction: introduction,
  184. });
  185. if (ret.isSucc) {
  186. if (ret.res.gender != undefined) {
  187. this._userInfo.gender = ret.res.gender;
  188. }
  189. if (ret.res.introduction != undefined) {
  190. this._userInfo.introduction = ret.res.introduction;
  191. }
  192. }
  193. return ret;
  194. }
  195. }