| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import ch_util from "../ch_util";
- /**
- * pvp扩展玩法 基于 chsdk
- */
- class ch_pvp {
- private readonly _set_url: string = '/user/setPvpSceneData';
- private readonly _match_url: string = '/user/startPvpMatch';
- private readonly _finish_url: string = '/user/pvpFinishAction';
- private readonly _record_url: string = '/user/pvpChallengeRecord';
- private static _instance: ch_pvp;
- public static getInstance(): ch_pvp {
- if (!this._instance) this._instance = new ch_pvp();
- return this._instance;
- }
- /**
- * 设置自己的 PVP 擂台数据
- * 该方法用于更新当前用户在 PVP 擂台上的数据。数据将根据传入的 `ext` 参数进行更新,
- * 并返回一个包含操作状态和可能错误信息的 Promise。
- * @param extend - 一个pvp擂台数据对象,包含要更新的具体数据。
- * @returns 一个 Promise,解析为一个包含以下属性的对象:
- * - code: 操作的结果状态。0 表示成功,其他值表示不同的错误类型。
- * - err: 可选字符串,指示错误信息(如果有)。
- */
- public async setSceneData<T extends { [key: string]: any }>(extend: T): Promise<{ code: number, err?: string }> {
- const check = chsdk.check_req_time('setSceneData');
- if (check) return check;
- const body = { extends: ch_util.stringify(extend) };
- const ret = await chsdk.makePostTokenRequest(chsdk.getUrl(this._set_url), body);
- if (ret.code != chsdk.code.success) {
- chsdk.log.warn(ret);
- } else {
- chsdk.log.log(`擂台数据上报成功`);
- }
- return ret;
- }
- /**
- * 开始匹配一个pvp擂台数据
- * @returns null匹配失败
- * extends PVP 擂台数据
- * other 台主信息
- * own 自己的信息
- */
- public async startMatch<T extends { [key: string]: any }>(): Promise<{
- extends: T,
- other: { head: string, nickName: string, uid: number, hid: number, province: string, pvp: number },
- own: { head: string, nickName: string, uid: number, hid: number, province: string, pvp: number }
- } | null> {
- const check = chsdk.check_req_time('startMatch');
- if (check) return null;
- const ret = await chsdk.makePostTokenRequest(chsdk.getUrl(this._match_url));
- if (ret.code != chsdk.code.success) {
- chsdk.log.warn(ret);
- return null;
- } else {
- const data = ret.data.data;
- data.other.uid = Number.parseInt(data.other.uid);
- data.other.hid = Number.parseInt(data.other.hid);
- data.other.province = chsdk.provinceCode2Name(data.other.hid);
- data.own.uid = Number.parseInt(data.own.uid);
- data.own.hid = Number.parseInt(data.own.hid);
- data.own.province = chsdk.provinceCode2Name(data.own.hid);
- return { extends: ch_util.parse(data.extends) as T, other: data.other, own: data.own }
- }
- }
- /**
- * 完成pvp上报
- * @param otherUid 对方uid
- * @param status 0:挑战失败 1:挑战胜利
- * @param ext PVP 擂台数据
- * @param serverData 服务器处理排行数据,订阅消息
- * ..ownValue 增减自己积分值
- * ..otherValue 增减对方积分值
- * ..msg 自定义的订阅消息
- * @param pvpId 复仇id
- * @returns
- */
- public async finish<T extends { [key: string]: any }>(otherUid: number, status: 0 | 1, ext: T,
- serverData: { ownValue?: number, otherValue?: number, msg?: string }
- , pvpId?: string
- ): Promise<{ code: number, err?: string, msg?: string }> {
- const check = chsdk.check_req_time('finish');
- if (check) return null;
- const body = { otherUid: otherUid.toString(), status: status, extends: ch_util.stringify(ext), serverData: ch_util.stringify(serverData), pvpId: pvpId };
- const ret = await chsdk.makePostTokenRequest(chsdk.getUrl(this._finish_url), body);
- if (ret.code != chsdk.code.success) {
- chsdk.log.warn(ret);
- } else {
- chsdk.log.log(`pvp结果数据上报成功`);
- }
- return ret;
- }
- //
- private _cache_records: any | null = null;
- private _cache_time: number;
- private get_cache_records(): any | null {
- if (chsdk.date.now() - this._cache_time > 6e4) return null;
- return this._cache_records;
- }
- /**
- * 获取pvp对战记录
- * @param cache 默认为true,优先缓存拿数据,缓存存在一分钟
- * @returns extends PVP 擂台数据
- * reward 奖励惩罚值,由于serverData中得来
- * type 0:自己擂台记录信息 1:被挑战的记录信息 2:挑战别人的记录 3:复仇成功
- * status 0:挑战失败 1:挑战胜利
- * time 时间
- */
- public async getRecords<T extends { [key: string]: any }>(cache: boolean = true): Promise<{
- extends: T,
- type: 0 | 1 | 2 | 3,
- status: 0 | 1,
- time: number,
- reward: number,
- other: { head: string, nickName: string, uid: number, hid: number, province: string },
- own: { head: string, nickName: string, uid: number, hid: number, province: string },
- pvpId: string
- }[]> {
- if (cache) {
- const data = this.get_cache_records();
- if (data) return data;
- }
- const check = chsdk.check_req_time('getRecords');
- if (check) return null;
- const ret = await chsdk.makePostTokenRequest(chsdk.getUrl(this._record_url));
- if (ret.code != chsdk.code.success) {
- chsdk.log.warn(ret);
- return null;
- } else {
- let data = ret.data.data;
- data ||= [];
- for (let i = 0; i < data.length; i++) {
- const d = data[i];
- d.extends = ch_util.parse(d.extends) as T;
- d.own.uid = Number.parseInt(d.own.uid);
- d.own.hid = Number.parseInt(d.own.hid);
- d.own.province = chsdk.provinceCode2Name(d.own.hid);
- if (d.other) {
- d.other.uid = Number.parseInt(d.other.uid);
- d.other.hid = Number.parseInt(d.other.hid);
- d.other.province = chsdk.provinceCode2Name(d.other.hid);
- }
- }
- this._cache_records = data;
- this._cache_time = chsdk.date.now();
- return data;
- }
- }
- /**
- * 获取pvp值榜单
- * @returns
- */
- public async getRank(): Promise<{ list: any[], ower: any }> {
- const d = await chsdk.loadRankData('pvp', 1, 100, true);
- return { list: d.data.list, ower: d.data.own };
- }
- }
- export default ch_pvp.getInstance();
|