UserM.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import EV, { EV_TYPE } from "../mgr/EV";
  2. import Utils from "../utils/Utils";
  3. import { WalletManager } from "../web3/WalletManager";
  4. import { GoodsId } from "./GoodsId";
  5. import WsM from "./WsM";
  6. import { EventTarget } from "cc";
  7. export class GoodInfo {
  8. id: number;
  9. count: number;
  10. }
  11. export class SeasonInfo {
  12. id: number;
  13. startTimeAt: number;
  14. endTimeAt: number;
  15. gamePassNum: number;
  16. }
  17. export class UserInfo {
  18. userId: number; // 用户id
  19. userName: string; // 用户名
  20. walletAddress: string; // 用户钱包
  21. systemWalletAddress: string; // 系统钱包
  22. season: SeasonInfo;
  23. currentTime: number; // 当前时间
  24. goodList: GoodInfo[];
  25. }
  26. export class BindWalletResult {
  27. walletAddress: string;
  28. systemWalletAddress: string;
  29. }
  30. export class BadgeInfo {
  31. id: number; // 徽章id
  32. type: number; // 徽章类型
  33. num: number; // 数量
  34. addition: number; // 加成
  35. }
  36. export class MyBadgeInfo {
  37. badgeList: BadgeInfo[];
  38. }
  39. export default class UserM {
  40. isBindWallet(): boolean {
  41. return (
  42. this.data?.walletAddress != null && this.data?.walletAddress.length > 0
  43. );
  44. }
  45. async getBadgeList() {
  46. let result = await WsM.ins.syncRequest(20002, {});
  47. return result;
  48. }
  49. getGamePassCount() {
  50. return this.data?.season?.gamePassNum ?? 0;
  51. }
  52. addGoods(id: number, count: number) {
  53. let good = this.data.goodList.find((item) => item.id == id);
  54. if (good) {
  55. good.count += count;
  56. } else {
  57. this.data.goodList.push({ id: id, count: count });
  58. }
  59. this.refreshGoods(this.data.goodList);
  60. }
  61. subtractGoods(id: number, count: number) {
  62. let good = this.data.goodList.find((item) => item.id == id);
  63. if (good) {
  64. good.count -= count;
  65. if (good.count < 0) {
  66. good.count = 0;
  67. }
  68. }
  69. this.refreshGoods(this.data.goodList);
  70. }
  71. getGradeAddValue(): number {
  72. return 30;
  73. }
  74. getGradeName() {
  75. return "BRONZE";
  76. }
  77. getGrade(): number {
  78. return 1;
  79. }
  80. getDisPlayGoods(): GoodInfo[] {
  81. let DisplayIds = [
  82. GoodsId.FREE_ITEM_BOX,
  83. GoodsId.ITEM_BOX,
  84. GoodsId.FREE_POG_BOX,
  85. GoodsId.POG_BOX,
  86. GoodsId.GAME_PASS,
  87. GoodsId.GAME_PASS_SHARD,
  88. GoodsId.POG_CRITICAL_CARD,
  89. ];
  90. //不改變目標庫,換方式實現
  91. let list = [];
  92. for (let i = 0; i < this.data.goodList.length; i++) {
  93. let item = this.data.goodList[i];
  94. if (DisplayIds.indexOf(item.id) != -1) {
  95. if (item.count > 0) {
  96. list.push(item);
  97. }
  98. }
  99. }
  100. // 排序, 按照 DisplayIds的顺序倒序
  101. list = list.sort((a, b) => {
  102. return DisplayIds.indexOf(a.id) - DisplayIds.indexOf(b.id);
  103. });
  104. return list;
  105. }
  106. getEsTogDisplay(): string {
  107. return Utils.formatNumber(this.getGoodsCount(GoodsId.ES_TOG), 0);
  108. }
  109. getTogDisplay(): string {
  110. return Utils.formatNumber(this.getGoodsCount(GoodsId.TOG), 0);
  111. }
  112. getPogDisplay(): string {
  113. return Utils.formatNumber(this.getGoodsCount(GoodsId.POG), 0);
  114. }
  115. refreshGoods(goodList: GoodInfo[]) {
  116. this.data.goodList = goodList;
  117. EV.ins.emit(EV_TYPE.USER_GOOD_REFRESH, this.data.goodList);
  118. }
  119. getGoodsCount(id: number): number {
  120. return this.data.goodList.find((item) => item.id == id)?.count ?? 0;
  121. }
  122. getSeasonEndTimeText(): string {
  123. return Utils.formateDateRemaining(this.data.season.endTimeAt);
  124. }
  125. getUserName(): string {
  126. return this.data ? this.data.userName : "UnLogin";
  127. }
  128. private static _ins: UserM;
  129. public static get ins(): UserM {
  130. return (UserM._ins ??= new UserM());
  131. }
  132. public data: UserInfo;
  133. public async userInfo(): Promise<UserInfo> {
  134. if (this.data != null) {
  135. return this.data;
  136. }
  137. let result = await this.refreshInfo();
  138. return result;
  139. }
  140. async refreshInfo() {
  141. let result = await WsM.ins.syncRequest(20001, {});
  142. console.log("userInfo", result);
  143. Utils.setServerTime(result.currentTime);
  144. this.data = result;
  145. EV.ins.emit(EV_TYPE.USER_GOOD_REFRESH, this.data.goodList);
  146. return result;
  147. }
  148. refreshData() {
  149. this.data = null;
  150. this.userInfo();
  151. }
  152. public systemWalletAddress: string = "";
  153. async bindWallet(address: string): Promise<BindWalletResult> {
  154. return new Promise((resolve, reject) => {
  155. WsM.ins
  156. .syncRequest(23002, { wallet: address })
  157. .then((result) => {
  158. if (result) {
  159. this.systemWalletAddress = result.systemWalletAddress;
  160. resolve(result);
  161. } else {
  162. WalletManager.ins.disconnect();
  163. }
  164. })
  165. .catch((err) => {
  166. WalletManager.ins.disconnect();
  167. reject(err);
  168. });
  169. });
  170. }
  171. }