ch_pvp.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import ch_util from "../ch_util";
  2. /**
  3. * pvp扩展玩法 基于 chsdk
  4. */
  5. class ch_pvp {
  6. private readonly _set_url: string = '/user/setPvpSceneData';
  7. private readonly _match_url: string = '/user/startPvpMatch';
  8. private readonly _finish_url: string = '/user/pvpFinishAction';
  9. private readonly _record_url: string = '/user/pvpChallengeRecord';
  10. private static _instance: ch_pvp;
  11. public static getInstance(): ch_pvp {
  12. if (!this._instance) this._instance = new ch_pvp();
  13. return this._instance;
  14. }
  15. /**
  16. * 设置自己的 PVP 擂台数据
  17. * 该方法用于更新当前用户在 PVP 擂台上的数据。数据将根据传入的 `ext` 参数进行更新,
  18. * 并返回一个包含操作状态和可能错误信息的 Promise。
  19. * @param extend - 一个pvp擂台数据对象,包含要更新的具体数据。
  20. * @returns 一个 Promise,解析为一个包含以下属性的对象:
  21. * - code: 操作的结果状态。0 表示成功,其他值表示不同的错误类型。
  22. * - err: 可选字符串,指示错误信息(如果有)。
  23. */
  24. public async setSceneData<T extends { [key: string]: any }>(extend: T): Promise<{ code: number, err?: string }> {
  25. const check = chsdk.check_req_time('setSceneData');
  26. if (check) return check;
  27. const body = { extends: ch_util.stringify(extend) };
  28. const ret = await chsdk.makePostTokenRequest(chsdk.getUrl(this._set_url), body);
  29. if (ret.code != chsdk.code.success) {
  30. chsdk.log.warn(ret);
  31. } else {
  32. chsdk.log.log(`擂台数据上报成功`);
  33. }
  34. return ret;
  35. }
  36. /**
  37. * 开始匹配一个pvp擂台数据
  38. * @returns null匹配失败
  39. * extends PVP 擂台数据
  40. * other 台主信息
  41. * own 自己的信息
  42. */
  43. public async startMatch<T extends { [key: string]: any }>(): Promise<{
  44. extends: T,
  45. other: { head: string, nickName: string, uid: number, hid: number, province: string, pvp: number },
  46. own: { head: string, nickName: string, uid: number, hid: number, province: string, pvp: number }
  47. } | null> {
  48. const check = chsdk.check_req_time('startMatch');
  49. if (check) return null;
  50. const ret = await chsdk.makePostTokenRequest(chsdk.getUrl(this._match_url));
  51. if (ret.code != chsdk.code.success) {
  52. chsdk.log.warn(ret);
  53. return null;
  54. } else {
  55. const data = ret.data.data;
  56. data.other.uid = Number.parseInt(data.other.uid);
  57. data.other.hid = Number.parseInt(data.other.hid);
  58. data.other.province = chsdk.provinceCode2Name(data.other.hid);
  59. data.own.uid = Number.parseInt(data.own.uid);
  60. data.own.hid = Number.parseInt(data.own.hid);
  61. data.own.province = chsdk.provinceCode2Name(data.own.hid);
  62. return { extends: ch_util.parse(data.extends) as T, other: data.other, own: data.own }
  63. }
  64. }
  65. /**
  66. * 完成pvp上报
  67. * @param otherUid 对方uid
  68. * @param status 0:挑战失败 1:挑战胜利
  69. * @param ext PVP 擂台数据
  70. * @param serverData 服务器处理排行数据,订阅消息
  71. * ..ownValue 增减自己积分值
  72. * ..otherValue 增减对方积分值
  73. * ..msg 自定义的订阅消息
  74. * @param pvpId 复仇id
  75. * @returns
  76. */
  77. public async finish<T extends { [key: string]: any }>(otherUid: number, status: 0 | 1, ext: T,
  78. serverData: { ownValue?: number, otherValue?: number, msg?: string }
  79. , pvpId?: string
  80. ): Promise<{ code: number, err?: string, msg?: string }> {
  81. const check = chsdk.check_req_time('finish');
  82. if (check) return null;
  83. const body = { otherUid: otherUid.toString(), status: status, extends: ch_util.stringify(ext), serverData: ch_util.stringify(serverData), pvpId: pvpId };
  84. const ret = await chsdk.makePostTokenRequest(chsdk.getUrl(this._finish_url), body);
  85. if (ret.code != chsdk.code.success) {
  86. chsdk.log.warn(ret);
  87. } else {
  88. chsdk.log.log(`pvp结果数据上报成功`);
  89. }
  90. return ret;
  91. }
  92. //
  93. private _cache_records: any | null = null;
  94. private _cache_time: number;
  95. private get_cache_records(): any | null {
  96. if (chsdk.date.now() - this._cache_time > 6e4) return null;
  97. return this._cache_records;
  98. }
  99. /**
  100. * 获取pvp对战记录
  101. * @param cache 默认为true,优先缓存拿数据,缓存存在一分钟
  102. * @returns extends PVP 擂台数据
  103. * reward 奖励惩罚值,由于serverData中得来
  104. * type 0:自己擂台记录信息 1:被挑战的记录信息 2:挑战别人的记录 3:复仇成功
  105. * status 0:挑战失败 1:挑战胜利
  106. * time 时间
  107. */
  108. public async getRecords<T extends { [key: string]: any }>(cache: boolean = true): Promise<{
  109. extends: T,
  110. type: 0 | 1 | 2 | 3,
  111. status: 0 | 1,
  112. time: number,
  113. reward: number,
  114. other: { head: string, nickName: string, uid: number, hid: number, province: string },
  115. own: { head: string, nickName: string, uid: number, hid: number, province: string },
  116. pvpId: string
  117. }[]> {
  118. if (cache) {
  119. const data = this.get_cache_records();
  120. if (data) return data;
  121. }
  122. const check = chsdk.check_req_time('getRecords');
  123. if (check) return null;
  124. const ret = await chsdk.makePostTokenRequest(chsdk.getUrl(this._record_url));
  125. if (ret.code != chsdk.code.success) {
  126. chsdk.log.warn(ret);
  127. return null;
  128. } else {
  129. let data = ret.data.data;
  130. data ||= [];
  131. for (let i = 0; i < data.length; i++) {
  132. const d = data[i];
  133. d.extends = ch_util.parse(d.extends) as T;
  134. d.own.uid = Number.parseInt(d.own.uid);
  135. d.own.hid = Number.parseInt(d.own.hid);
  136. d.own.province = chsdk.provinceCode2Name(d.own.hid);
  137. if (d.other) {
  138. d.other.uid = Number.parseInt(d.other.uid);
  139. d.other.hid = Number.parseInt(d.other.hid);
  140. d.other.province = chsdk.provinceCode2Name(d.other.hid);
  141. }
  142. }
  143. this._cache_records = data;
  144. this._cache_time = chsdk.date.now();
  145. return data;
  146. }
  147. }
  148. /**
  149. * 获取pvp值榜单
  150. * @returns
  151. */
  152. public async getRank(): Promise<{ list: any[], ower: any }> {
  153. const d = await chsdk.loadRankData('pvp', 1, 100, true);
  154. return { list: d.data.list, ower: d.data.own };
  155. }
  156. }
  157. export default ch_pvp.getInstance();